KickJava   Java API By Example, From Geeks To Geeks.

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


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: MoveNode.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 moves a node in a tree.
31  */

32 public class MoveNode extends TwoNodesTask {
33     private String JavaDoc refdocumentid;
34     /**
35      *
36      */

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

50     public void manipulateTree(
51         String JavaDoc firstdocumentid,
52         String JavaDoc secdocumentid,
53         String JavaDoc firstarea,
54         String JavaDoc secarea)
55         throws SiteTreeException {
56
57         Publication publication = getPublication();
58         SiteTree firsttree = publication.getTree(firstarea);
59         SiteTree sectree = publication.getTree(secarea);
60
61         String JavaDoc parentid = "";
62         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(secdocumentid, "/");
63         int length = st.countTokens();
64
65         for (int i = 0; i < (length - 1); i++) {
66             parentid = parentid + "/" + st.nextToken();
67         }
68         String JavaDoc newid = st.nextToken();
69
70         SiteTreeNode node = firsttree.getNode(firstdocumentid);
71         if (node != null) {
72             SiteTreeNode parentNode = sectree.getNode(parentid);
73             if (parentNode != null) {
74                 sectree.move(node, parentNode, newid, this.getRefdocumentid());
75             } else {
76                 throw new SiteTreeException(
77                     "The parent node "
78                         + parentNode
79                         + " where the removed node shall be inserted not found");
80             }
81         } else {
82             throw new SiteTreeException(
83                 "Node " + node + " couldn't be removed");
84         }
85
86         if (firstarea.equals(secarea)) {
87             firsttree.save();
88         } else {
89             firsttree.save();
90             sectree.save();
91         }
92     }
93     /**
94      * @return string The document-id corresponding to the reference node, before which
95      * the moved node shoul be inserted. If null, the node is inserted at the end.
96      */

97     public String JavaDoc getRefdocumentid() {
98         return refdocumentid;
99     }
100
101     /**
102      * @param string The document-id corresponding to the reference node, before which
103      * the moved node shoul be inserted. If null, the node is inserted at the end.
104      */

105     public void setRefdocumentid(String JavaDoc string) {
106         refdocumentid = string;
107     }
108
109 }
110
Popular Tags