KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplet > CopletData


1 /*
2  * Copyright 1999-2002,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 package org.apache.cocoon.portal.coplet;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.cocoon.portal.factory.impl.AbstractProducible;
26 import org.apache.cocoon.portal.util.DeltaApplicable;
27 import org.apache.commons.lang.StringUtils;
28
29 /**
30  *
31  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
32  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
33  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
34  *
35  * @version CVS $Id: CopletData.java 290668 2005-09-21 09:43:26Z cziegeler $
36  */

37 public class CopletData
38 extends AbstractProducible
39 implements DeltaApplicable {
40
41     protected String JavaDoc title;
42
43     protected CopletBaseData copletBaseData;
44
45     protected Map JavaDoc attributes = new HashMap JavaDoc();
46
47     protected String JavaDoc allowedRoles;
48     
49     protected transient List JavaDoc allowedRolesList;
50     
51     /**
52      * Signals whether a delta has been applied.
53      */

54     private boolean deltaApplied = false;
55
56     /**
57      * Constructor
58      */

59     public CopletData() {
60         // Nothing to do
61
}
62
63     /**
64      * Returns the title.
65      * @return String
66      */

67     public String JavaDoc getTitle() {
68         return title;
69     }
70
71     /**
72      * Sets the title.
73      * @param title The title to set
74      */

75     public void setTitle(String JavaDoc title) {
76         this.title = title;
77     }
78
79     /**
80      * Returns the copletBaseData.
81      * @return CopletBaseData
82      */

83     public CopletBaseData getCopletBaseData() {
84         return copletBaseData;
85     }
86
87     /**
88      * Sets the copletBaseData.
89      * @param copletBaseData The copletBaseData to set
90      */

91     public void setCopletBaseData(CopletBaseData copletBaseData) {
92         this.copletBaseData = copletBaseData;
93     }
94
95     public Object JavaDoc removeAttribute(String JavaDoc key) {
96         return this.attributes.remove(key);
97     }
98     
99     public Object JavaDoc getAttribute(String JavaDoc key) {
100         return this.attributes.get(key);
101     }
102
103     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
104         this.attributes.put(key, value);
105     }
106     
107     public Map JavaDoc getAttributes() {
108         return this.attributes;
109     }
110     
111     /**
112      * Applies the specified delta.
113      * @throws ClassCastException If the object is not of the expected type.
114      */

115     public boolean applyDelta(Object JavaDoc object) {
116         CopletData data = (CopletData)object;
117         
118         this.deltaApplied = true;
119         
120         String JavaDoc title = data.getTitle();
121         if (title != null) {
122             this.setTitle(title);
123         }
124             
125         CopletBaseData copletBaseData = data.getCopletBaseData();
126         if (copletBaseData != null) {
127             this.setCopletBaseData(copletBaseData);
128         }
129             
130         Iterator JavaDoc iterator = data.getAttributes().entrySet().iterator();
131         Object JavaDoc attribute, delta;
132         String JavaDoc key;
133         Map.Entry JavaDoc entry;
134         while (iterator.hasNext()) {
135             entry = (Map.Entry JavaDoc)iterator.next();
136             key = (String JavaDoc)entry.getKey();
137             delta = entry.getValue();
138
139             attribute = this.getAttribute(key);
140             if (attribute == null) {
141                 // add new attribute
142
this.setAttribute(key, delta);
143             } else if (attribute instanceof DeltaApplicable) {
144                 // apply delta
145
boolean success = ((DeltaApplicable)attribute).applyDelta(delta);
146                 if (!success) {
147                     // replace attribute
148
this.setAttribute(key, delta);
149                 }
150             } else {
151                 // replace attribute
152
this.setAttribute(key, delta);
153             }
154         }
155         
156         return true;
157     }
158     
159     /**
160      * Checks if a delta has been applied.
161      */

162     public boolean deltaApplied() {
163         return this.deltaApplied;
164     }
165     
166     /**
167      * @return Returns the allowed roles.
168      */

169     public String JavaDoc getAllowedRoles() {
170         return this.allowedRoles;
171     }
172     /**
173      * @param roles The allowed roles to set.
174      */

175     public void setAllowedRoles(String JavaDoc roles) {
176         this.allowedRoles = roles;
177         this.allowedRolesList = null;
178     }
179     
180     /**
181      * Return the list of roles that are allowed to access this coplet
182      * @return A list of roles or null if everyone is allowed.
183      */

184     public List JavaDoc getAllowedRolesList() {
185         if ( StringUtils.isBlank(this.allowedRoles) ) {
186             return null;
187         }
188         if ( this.allowedRolesList == null ) {
189             this.allowedRolesList = new ArrayList JavaDoc();
190             final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(this.allowedRoles, ",");
191             while ( tokenizer.hasMoreElements() ) {
192                 String JavaDoc token = (String JavaDoc)tokenizer.nextElement();
193                 this.allowedRolesList.add(token);
194             }
195             if ( this.allowedRolesList.size() == 0 ) {
196                 this.allowedRoles = null;
197                 this.allowedRolesList = null;
198             }
199         }
200         return this.allowedRolesList;
201     }
202     
203     public void addToAllowedRoles(String JavaDoc role) {
204         List JavaDoc l = this.getAllowedRolesList();
205         if ( l == null ) {
206             l = new ArrayList JavaDoc();
207             l.add(role);
208         } else {
209             if ( !l.contains(role) ) {
210                 l.add(role);
211             }
212         }
213         this.buildRolesString(l);
214     }
215     
216     public void removeFromAllowedRoles(String JavaDoc role) {
217         List JavaDoc l = this.getAllowedRolesList();
218         if ( l != null && l.contains(role) ) {
219             l.remove(role);
220             if ( l.size() == 0 ) {
221                 this.allowedRoles = null;
222                 this.allowedRolesList = null;
223             } else {
224                 this.buildRolesString(l);
225             }
226         }
227     }
228     
229     protected void buildRolesString(List JavaDoc fromList) {
230         this.allowedRolesList = fromList;
231         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
232         boolean first = true;
233         Iterator JavaDoc i = fromList.iterator();
234         while ( i.hasNext() ) {
235             String JavaDoc role = (String JavaDoc)i.next();
236             if ( !first ) {
237                 buffer.append(',');
238             }
239             first = false;
240             buffer.append(role);
241         }
242         this.allowedRoles = buffer.toString();
243     }
244 }
245
Popular Tags