KickJava   Java API By Example, From Geeks To Geeks.

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


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: DeletePoliciesTask.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.avalon.excalibur.io.FileUtil;
27 import org.apache.lenya.cms.publication.Publication;
28 import org.apache.lenya.cms.publication.SiteTree;
29 import org.apache.lenya.cms.publication.SiteTreeNode;
30 import org.apache.tools.ant.BuildException;
31
32 /**
33  * Ant task to delete the policies of documents corresponding to a defined subtree
34  * Visitor of the defined subtree (visitor pattern). The subtree is reverse visited.
35  */

36 public class DeletePoliciesTask extends TwoDocumentsOperationTask {
37     private String JavaDoc policiesDir;
38
39     /**
40      *
41      */

42     public DeletePoliciesTask() {
43         super();
44     }
45
46     /**
47      * @return string The policies directory.
48      */

49     public String JavaDoc getPoliciesDir() {
50         return policiesDir;
51     }
52     /**
53      * @param string The policies directory
54      */

55     public void setPoliciesDir(String JavaDoc string) {
56         policiesDir = string;
57     }
58
59     /**
60      * Get all files in a given directory, that are not directories.
61      * If the given directory doesn't exist, return null.
62      * @param directory The directory
63      * @return List of files
64      */

65     public File JavaDoc[] getFiles(File JavaDoc directory) {
66         FileFilter JavaDoc filter = new FileFilter JavaDoc() {
67
68             public boolean accept(File JavaDoc file) {
69                 return file.isFile();
70             }
71         };
72         if (directory.exists() && directory.isDirectory()) {
73             return directory.listFiles(filter);
74         }
75         return null;
76     }
77
78     /**
79      * Delte the policies file
80      * @param srcDir The directory of the policies files.
81      */

82     public void deletePolicies(File JavaDoc srcDir) {
83         File JavaDoc[] authoringPolicies = this.getFiles(srcDir);
84         if (authoringPolicies == null) {
85             log("no policies file to delete");
86             return;
87         }
88         for (int i = 0; i < authoringPolicies.length; i++) {
89             try {
90             FileUtil.forceDelete(authoringPolicies[i]);
91             } catch (IOException JavaDoc e) {
92                 //FIXME: catch Exception because of window's delete problem
93
log("exception " +e);
94             }
95
96         }
97         if (srcDir.exists() && srcDir.isDirectory() && srcDir.listFiles().length == 0) {
98             try {
99           FileUtil.forceDelete(srcDir);
100             } catch (IOException JavaDoc e) {
101                 //FIXME: catch Exception because of window's delete problem
102
log("exception " +e);
103             }
104         }
105     }
106
107     /** (non-Javadoc)
108      * @see org.apache.lenya.cms.ant.DocumentOperationTask#visitSiteTreeNode(org.apache.lenya.cms.publication.SiteTreeNode)
109      */

110     public void visitSiteTreeNode(SiteTreeNode node) {
111         String JavaDoc srcArea = this.getFirstarea();
112         String JavaDoc destArea = this.getSecarea();
113
114         String JavaDoc destDocumentid = node.getAbsoluteId();
115         String JavaDoc srcDocumentid =
116             destDocumentid.replaceFirst(
117                 getSecdocumentid(),
118                 getFirstdocumentid());
119
120         try {
121             if (srcArea.equals(Publication.AUTHORING_AREA)) {
122                 if (destArea.equals(Publication.AUTHORING_AREA)) {
123                     File JavaDoc srcDir =
124                         new File JavaDoc(
125                             policiesDir,
126                             this.getFirstarea()
127                                 + File.separator
128                                 + srcDocumentid);
129                     log("delete :" + srcDir.getCanonicalPath());
130                     deletePolicies(srcDir);
131                     File JavaDoc srcLiveDir =
132                         new File JavaDoc(
133                             policiesDir,
134                             Publication.LIVE_AREA
135                                 + File.separator
136                                 + srcDocumentid);
137                     log("delete :" + srcLiveDir.getCanonicalPath());
138                     deletePolicies(srcLiveDir);
139
140                 } else if (
141                     destArea.equals(Publication.ARCHIVE_AREA)
142                         | destArea.equals(Publication.TRASH_AREA)) {
143                     File JavaDoc srcDir =
144                         new File JavaDoc(
145                             policiesDir,
146                             this.getFirstarea()
147                                 + File.separator
148                                 + srcDocumentid);
149                     log("delete :" + srcDir.getCanonicalPath());
150                     deletePolicies(srcDir);
151
152                     File JavaDoc srcLiveDir =
153                         new File JavaDoc(
154                             policiesDir,
155                             Publication.LIVE_AREA
156                                 + File.separator
157                                 + srcDocumentid);
158                     log("delete :" + srcLiveDir.getCanonicalPath());
159                     deletePolicies(srcLiveDir);
160                 }
161             } else if (
162                 srcArea.equals(Publication.ARCHIVE_AREA)
163                     | srcArea.equals(Publication.TRASH_AREA)) {
164                 if (destArea.equals(Publication.AUTHORING_AREA)) {
165                     File JavaDoc srcDir =
166                         new File JavaDoc(
167                             policiesDir,
168                             this.getFirstarea()
169                                 + File.separator
170                                 + this.getSecarea()
171                                 + File.separator
172                                 + srcDocumentid);
173                     log("delete :" + srcDir.getCanonicalPath());
174                     deletePolicies(srcDir);
175
176                     File JavaDoc srcLiveDir =
177                         new File JavaDoc(
178                             policiesDir,
179                             this.getFirstarea()
180                                 + File.separator
181                                 + Publication.LIVE_AREA
182                                 + File.separator
183                                 + srcDocumentid);
184                     log("delete :" + srcLiveDir.getCanonicalPath());
185                     deletePolicies(srcLiveDir);
186                 }
187             }
188         } catch (IOException JavaDoc e) {
189             throw new BuildException(e);
190         }
191     }
192
193     /**
194      * @see org.apache.tools.ant.Task#execute()
195      **/

196     public void execute() throws BuildException {
197         try {
198             log("document-id for the source :" + this.getFirstdocumentid());
199             log("area for the source :" + this.getFirstarea());
200             log("document-id for the destination :" + this.getSecdocumentid());
201             log("area for the destination :" + this.getSecarea());
202
203             Publication publication = getPublication();
204             SiteTree tree = publication.getTree(this.getSecarea());
205             SiteTreeNode node = tree.getNode(this.getSecdocumentid());
206             node.acceptReverseSubtree(this);
207         } catch (Exception JavaDoc e) {
208             throw new BuildException(e);
209         }
210     }
211 }
212
Popular Tags