KickJava   Java API By Example, From Geeks To Geeks.

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


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: MoveDocumentTask.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import org.apache.lenya.cms.publication.Document;
23 import org.apache.lenya.cms.publication.DocumentBuildException;
24 import org.apache.lenya.cms.publication.DocumentBuilder;
25 import org.apache.lenya.cms.publication.Label;
26 import org.apache.lenya.cms.publication.Publication;
27 import org.apache.lenya.cms.publication.SiteTree;
28 import org.apache.lenya.cms.publication.SiteTreeNode;
29 import org.apache.lenya.cms.publication.SiteTreeNodeVisitor;
30 import org.apache.lenya.cms.workflow.WorkflowFactory;
31 import org.apache.lenya.workflow.WorkflowException;
32 import org.apache.tools.ant.BuildException;
33
34 /**
35  * Ant task, which implements the SiteTreeNodeVisitor for the operation move a document. (Visitor
36  * pattern)
37  */

38 public class MoveDocumentTask extends PublicationTask implements SiteTreeNodeVisitor {
39
40     private String JavaDoc firstarea;
41     private String JavaDoc firstdocumentid;
42     private String JavaDoc secarea;
43     private String JavaDoc secdocumentid;
44
45     /**
46      *
47      */

48     public MoveDocumentTask() {
49         super();
50     }
51
52     /**
53      * @return String The area of the source.
54      */

55     public String JavaDoc getFirstarea() {
56         return firstarea;
57     }
58
59     /**
60      * @return String The document-id corresponding to the source.
61      */

62     public String JavaDoc getFirstdocumentid() {
63         return firstdocumentid;
64     }
65
66     /**
67      * @return String The area of the destination.
68      */

69     public String JavaDoc getSecarea() {
70         return secarea;
71     }
72
73     /**
74      * @return String The document-id corresponding to the destination.
75      */

76     public String JavaDoc getSecdocumentid() {
77         return secdocumentid;
78     }
79
80     /**
81      * @param string
82      * The area of the source.
83      */

84     public void setFirstarea(String JavaDoc string) {
85         firstarea = string;
86     }
87
88     /**
89      * @param string
90      * The document-id corresponding to the source.
91      */

92     public void setFirstdocumentid(String JavaDoc string) {
93         firstdocumentid = string;
94     }
95
96     /**
97      * @param string
98      * The area of the destination.
99      */

100     public void setSecarea(String JavaDoc string) {
101         secarea = string;
102     }
103
104     /**
105      * @param string
106      * The document-id corresponding to the destination.
107      */

108     public void setSecdocumentid(String JavaDoc string) {
109         secdocumentid = string;
110     }
111
112     /**
113      * move the workflow files
114      *
115      * @see org.apache.lenya.cms.publication.SiteTreeNodeVisitor#visitSiteTreeNode(org.apache.lenya.cms.publication.SiteTreeNode)
116      */

117     public void visitSiteTreeNode(SiteTreeNode node) {
118         Publication publication = getPublication();
119
120         Label[] labels = node.getLabels();
121         for (int i = 0; i < labels.length; i++) {
122             String JavaDoc language = labels[i].getLanguage();
123
124             String JavaDoc srcDocumentid = node.getAbsoluteId();
125             String JavaDoc destDocumentid = srcDocumentid.replaceFirst(firstdocumentid, secdocumentid);
126
127             // TODO: content(fix the build file)
128
// TODO: resources (fix the build file)
129
// TODO: rcml (fix the build file)
130
// TODO: rcbak (fix the build file)
131

132             //move workflow
133
DocumentBuilder builder = publication.getDocumentBuilder();
134             String JavaDoc url = builder.buildCanonicalUrl(publication, firstarea, srcDocumentid, language);
135             String JavaDoc newurl =
136                 builder.buildCanonicalUrl(publication, secarea, destDocumentid, language);
137
138             Document document;
139             Document newDocument;
140             WorkflowFactory factory = WorkflowFactory.newInstance();
141
142             log("move workflow history");
143             try {
144                 document = builder.buildDocument(publication, url);
145                 newDocument = builder.buildDocument(publication, newurl);
146             } catch (DocumentBuildException e) {
147                 throw new BuildException(e);
148             }
149             try {
150                 if (factory.hasWorkflow(document)) {
151                     WorkflowFactory.moveHistory(document, newDocument);
152                 }
153             } catch (WorkflowException e) {
154                 throw new BuildException(e);
155             }
156
157         }
158     }
159
160     /**
161      * @see org.apache.tools.ant.Task#execute()
162      */

163     public void execute() throws BuildException {
164         try {
165             log("document id for the source" + this.getFirstdocumentid());
166             log("area for the source" + this.getFirstarea());
167             log("document id for the destination" + this.getSecdocumentid());
168             log("area for the destination" + this.getSecarea());
169
170             Publication publication = getPublication();
171             SiteTree tree = publication.getTree(getFirstarea());
172             SiteTreeNode node = tree.getNode(getFirstdocumentid());
173
174             node.acceptSubtree(this);
175         } catch (Exception JavaDoc e) {
176             throw new BuildException(e);
177         }
178     }
179
180 }
181
Popular Tags