KickJava   Java API By Example, From Geeks To Geeks.

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


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: RemoveLabelTask.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.SiteTree;
23 import org.apache.lenya.cms.publication.Label;
24 import org.apache.lenya.cms.publication.SiteTreeException;
25 import org.apache.lenya.cms.publication.SiteTreeNode;
26 import org.apache.tools.ant.BuildException;
27
28 /**
29  * Ant task to insert a label into an existing node in a tree.
30  */

31 public class RemoveLabelTask extends PublicationTask {
32     private String JavaDoc documentid;
33     private String JavaDoc labelName;
34     private String JavaDoc area;
35     private String JavaDoc language;
36
37     /**
38      * Creates a new instance of InsertLabelTask
39      */

40     public RemoveLabelTask() {
41         super();
42     }
43
44     /**
45      * Get the area of the site tree.
46      *
47      * @return the area of the tree.
48      */

49     protected String JavaDoc getArea() {
50         return area;
51     }
52
53     /**
54      * Set the area.
55      *
56      * @param area the area.
57      */

58     public void setArea(String JavaDoc area) {
59         this.area = area;
60     }
61
62     /**
63      * Set the value of the area of the tree.
64      *
65      * @param area the area of the tree.
66      */

67     public void setAbsolutetreepath(String JavaDoc area) {
68         this.area = area;
69     }
70
71     /**
72      * Return the document-id corresponding to the node to delete.
73      *
74      * @return string The document-id.
75      */

76     protected String JavaDoc getDocumentid() {
77         return documentid;
78     }
79
80     /**
81      * Set the value of the document-id corresponding to the node to delete.
82      *
83      * @param string The document-id.
84      */

85     public void setDocumentid(String JavaDoc string) {
86         documentid = string;
87     }
88
89     /**
90      * Get the name of the label.
91      *
92      * @return the labelName
93      */

94     public String JavaDoc getLabelName() {
95         return labelName;
96     }
97
98     /**
99      * Set the labelName.
100      *
101      * @param labelName the name of the label
102      */

103     public void setLabelName(String JavaDoc labelName) {
104         this.labelName = labelName;
105     }
106
107     /**
108      * Get the language.
109      *
110      * @return the language
111      */

112     public String JavaDoc getLanguage() {
113         return language;
114     }
115
116     /**
117      * Set the language.
118      *
119      * @param language the language
120      */

121     public void setLanguage(String JavaDoc language) {
122         this.language = language;
123     }
124
125     /**
126      * Remove a label in an existing node in the tree.
127      * If this happens to be the last label it is not removed.
128      *
129      * @param documentid the document-id of the document.
130      * @param labelName the name of the label that is to be inserted.
131      * @param language the language of the label that is to be inserted.
132      * @param area determines in which sitetree the label is to be inserted
133      *
134      * @throws SiteTreeException if an error occurs.
135      */

136     public void removeLabel(
137         String JavaDoc documentid,
138         String JavaDoc labelName,
139         String JavaDoc language,
140         String JavaDoc area)
141         throws SiteTreeException {
142
143         SiteTree tree = null;
144         Label label = null;
145         try {
146             tree = getPublication().getTree(area);
147             SiteTreeNode node = tree.getNode(documentid);
148             // if there is only one label left do not delete it.
149
if (node.getLabels().length > 1) {
150                 // if there are more than one labels in this node
151
// try to delete the label specified by the labelName.
152
label = new Label(labelName, language);
153                 tree.removeLabel(documentid, label);
154                 tree.save();
155             }
156
157         } catch (Exception JavaDoc e) {
158             throw new SiteTreeException(
159                 "Cannot remove label " + label + " from tree " + area,
160                 e);
161         }
162
163     }
164
165     /** (non-Javadoc)
166      * @see org.apache.tools.ant.Task#execute()
167      */

168     public void execute() throws BuildException {
169         try {
170             log("document-id corresponding to the node: " + getDocumentid());
171             log("label name: " + getLabelName());
172             log("language: " + getLanguage());
173             log("area: " + getArea());
174             removeLabel(
175                 getDocumentid(),
176                 getLabelName(),
177                 getLanguage(),
178                 getArea());
179         } catch (Exception JavaDoc e) {
180             throw new BuildException(e);
181         }
182     }
183 }
184
Popular Tags