KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > InsertCopyNode


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: InsertCopyNode.java 160543 2005-04-08 11:30:01Z andreas $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.lenya.cms.publication.Publication;
25 import org.apache.lenya.cms.publication.SiteTree;
26 import org.apache.lenya.cms.publication.SiteTreeException;
27 import org.apache.lenya.cms.publication.SiteTreeNode;
28
29 /**
30  * Ant task that copies a node of a tree and inserts it in tree
31  */

32 public class InsertCopyNode extends TwoNodesTask {
33     /**
34      * Creates a new instance of InsertCopyNode
35      */

36     public InsertCopyNode() {
37         super();
38     }
39
40     /**
41      * copies a node corresponding to a document with id firstdocumentid and area firstarea and
42      * inserts it like a node corresponding to a document with id secdocumentid and area secarea.
43      * @param firstdocumentid The document-id of the document corresponding to the source node.
44      * @param secdocumentid The document-id of the document corresponding to the destination node.
45      * @param firstarea The area of the document corresponding to the source node.
46      * @param secarea The area of the document corresponding to the destination node.
47      * @throws SiteTreeException if there are problems with creating or saving the site tree.
48      */

49     public void manipulateTree(String JavaDoc firstdocumentid, String JavaDoc secdocumentid, String JavaDoc firstarea,
50             String JavaDoc secarea) throws SiteTreeException {
51
52         Publication publication = getPublication();
53         SiteTree firsttree = publication.getTree(firstarea);
54         SiteTree sectree = publication.getTree(secarea);
55
56         String JavaDoc parentid = "";
57         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(secdocumentid, "/");
58         int length = st.countTokens();
59
60         for (int i = 0; i < (length - 1); i++) {
61             parentid = parentid + "/" + st.nextToken();
62         }
63         String JavaDoc newid = st.nextToken();
64
65         SiteTreeNode node = firsttree.getNode(firstdocumentid);
66
67         if (node != null) {
68             SiteTreeNode parentNode = sectree.getNode(parentid);
69             if (parentNode != null) {
70                 sectree.copy(node, parentNode, newid, null);
71             } else {
72                 throw new SiteTreeException("The parent node " + parentNode
73                         + " where the copied node shall be inserted not found");
74             }
75         } else {
76             throw new SiteTreeException("Node " + node + " couldn't be found");
77         }
78         if (firstarea.equals(secarea)) {
79             firsttree.save();
80         } else {
81             firsttree.save();
82             sectree.save();
83         }
84     }
85 }
Popular Tags