KickJava   Java API By Example, From Geeks To Geeks.

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


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: DeactivateResourcesTask.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
24 import org.apache.lenya.cms.publication.SiteTree;
25 import org.apache.lenya.cms.publication.Document;
26 import org.apache.lenya.cms.publication.DocumentBuildException;
27 import org.apache.lenya.cms.publication.DocumentBuilder;
28 import org.apache.lenya.cms.publication.Label;
29 import org.apache.lenya.cms.publication.Publication;
30 import org.apache.lenya.cms.publication.ResourcesManager;
31 import org.apache.lenya.cms.publication.SiteTreeException;
32 import org.apache.lenya.cms.publication.SiteTreeNode;
33 import org.apache.tools.ant.BuildException;
34
35 /**
36  * Ant task to remove the resources belonging to a document with document id <documentid>, area
37  * <area>and language <language>. The resources are removed when no more version of this document
38  * is available.
39  */

40 public class DeactivateResourcesTask extends PublicationTask {
41     private String JavaDoc area;
42     private String JavaDoc documentid;
43     private String JavaDoc language;
44
45     /**
46      * Creates a new instance of DeactivateResourcesTask
47      */

48     public DeactivateResourcesTask() {
49         super();
50     }
51
52     /**
53      * Remove the resources belonging to the document with document id <documentid>, area <area>
54      * and language <language>, when no more version of this document is available.
55      *
56      * @param language
57      * The language
58      * @param documentid
59      * The document id
60      * @param area
61      * The area
62      *
63      * @throws SiteTreeException
64      * if an error occurs
65      */

66     public void deactivateResources(String JavaDoc language, String JavaDoc documentid, String JavaDoc area)
67         throws SiteTreeException {
68         Publication publication = getPublication();
69         SiteTree tree = null;
70
71         try {
72             tree = publication.getTree(area);
73             SiteTreeNode node = tree.getNode(documentid);
74             Label[] labels = null;
75             if (node != null) {
76                 labels = node.getLabels();
77             }
78             if (node == null || (labels != null && labels.length < 1)) {
79
80                 DocumentBuilder builder = publication.getDocumentBuilder();
81                 String JavaDoc url = builder.buildCanonicalUrl(publication, area, documentid, language);
82                 Document doc;
83                 try {
84                     doc = builder.buildDocument(publication, url);
85                 } catch (DocumentBuildException e) {
86                     throw new BuildException(e);
87                 }
88                 ResourcesManager resourcesMgr = new ResourcesManager(doc);
89                 File JavaDoc[] resources = resourcesMgr.getResources();
90                 for (int i = 0; i < resources.length; i++) {
91                     resources[i].delete();
92                 }
93                 File JavaDoc directory = resourcesMgr.getPath();
94                 directory.delete();
95             }
96         } catch (Exception JavaDoc e) {
97             throw new SiteTreeException(e);
98         }
99     }
100
101     /**
102      * (non-Javadoc)
103      *
104      * @see org.apache.tools.ant.Task#execute()
105      */

106     public void execute() throws BuildException {
107         try {
108             log("document-id : " + getDocumentid());
109             log("area: " + getArea());
110             log("language : " + getArea());
111             deactivateResources(getLanguage(), getDocumentid(), getArea());
112         } catch (Exception JavaDoc e) {
113             throw new BuildException(e);
114         }
115     }
116
117     /**
118      * Get the value of the area.
119      *
120      * @return The area.
121      */

122     public String JavaDoc getArea() {
123         return area;
124     }
125
126     /**
127      * Get the value of the document id.
128      *
129      * @return The document id.
130      */

131     public String JavaDoc getDocumentid() {
132         return documentid;
133     }
134
135     /**
136      * Get the value of the language.
137      *
138      * @return The language.
139      */

140     public String JavaDoc getLanguage() {
141         return language;
142     }
143
144     /**
145      * Set the value of the area.
146      *
147      * @param string
148      * The area.
149      */

150     public void setArea(String JavaDoc string) {
151         area = string;
152     }
153
154     /**
155      * Set the value of the document id.
156      *
157      * @param string
158      * The document id.
159      */

160     public void setDocumentid(String JavaDoc string) {
161         documentid = string;
162     }
163
164     /**
165      * Set the value of the language.
166      *
167      * @param string
168      * The language.
169      */

170     public void setLanguage(String JavaDoc string) {
171         language = string;
172     }
173
174 }
175
Popular Tags