001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.coplet;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.StringTokenizer;
025:
026: import org.apache.cocoon.portal.factory.impl.AbstractProducible;
027: import org.apache.cocoon.portal.util.DeltaApplicable;
028: import org.apache.commons.lang.StringUtils;
029:
030: /**
031: *
032: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
033: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
034: * @author <a href="mailto:bluetkemeier@s-und-n.de">Björn Lütkemeier</a>
035: *
036: * @version CVS $Id: CopletData.java 603311 2007-12-11 17:24:31Z cziegeler $
037: */
038: public class CopletData extends AbstractProducible implements
039: DeltaApplicable {
040:
041: protected String title;
042:
043: protected CopletBaseData copletBaseData;
044:
045: protected Map attributes = new HashMap(3);
046:
047: protected String allowedRoles;
048:
049: protected transient List allowedRolesList;
050:
051: /**
052: * Signals whether a delta has been applied.
053: */
054: private boolean deltaApplied = false;
055:
056: /**
057: * Constructor
058: */
059: public CopletData() {
060: // Nothing to do
061: }
062:
063: /**
064: * Returns the title.
065: * @return String
066: */
067: public String getTitle() {
068: return title;
069: }
070:
071: /**
072: * Sets the title.
073: * @param title The title to set
074: */
075: public void setTitle(String title) {
076: this .title = title;
077: }
078:
079: /**
080: * Returns the copletBaseData.
081: * @return CopletBaseData
082: */
083: public CopletBaseData getCopletBaseData() {
084: return copletBaseData;
085: }
086:
087: /**
088: * Sets the copletBaseData.
089: * @param copletBaseData The copletBaseData to set
090: */
091: public void setCopletBaseData(CopletBaseData copletBaseData) {
092: this .copletBaseData = copletBaseData;
093: }
094:
095: public Object removeAttribute(String key) {
096: return this .attributes.remove(key);
097: }
098:
099: public Object getAttribute(String key) {
100: return this .attributes.get(key);
101: }
102:
103: public void setAttribute(String key, Object value) {
104: this .attributes.put(key, value);
105: }
106:
107: public Map 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 object) {
116: CopletData data = (CopletData) object;
117:
118: this .deltaApplied = true;
119:
120: String 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 iterator = data.getAttributes().entrySet().iterator();
131: Object attribute, delta;
132: String key;
133: Map.Entry entry;
134: while (iterator.hasNext()) {
135: entry = (Map.Entry) iterator.next();
136: key = (String) 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)
146: .applyDelta(delta);
147: if (!success) {
148: // replace attribute
149: this .setAttribute(key, delta);
150: }
151: } else {
152: // replace attribute
153: this .setAttribute(key, delta);
154: }
155: }
156:
157: return true;
158: }
159:
160: /**
161: * Checks if a delta has been applied.
162: */
163: public boolean deltaApplied() {
164: return this .deltaApplied;
165: }
166:
167: /**
168: * @return Returns the allowed roles.
169: */
170: public String getAllowedRoles() {
171: return this .allowedRoles;
172: }
173:
174: /**
175: * @param roles The allowed roles to set.
176: */
177: public void setAllowedRoles(String roles) {
178: this .allowedRoles = roles;
179: this .allowedRolesList = null;
180: }
181:
182: /**
183: * Return the list of roles that are allowed to access this coplet
184: * @return A list of roles or null if everyone is allowed.
185: */
186: public List getAllowedRolesList() {
187: if (StringUtils.isBlank(this .allowedRoles)) {
188: return null;
189: }
190: if (this .allowedRolesList == null) {
191: this .allowedRolesList = new ArrayList();
192: final StringTokenizer tokenizer = new StringTokenizer(
193: this .allowedRoles, ",");
194: while (tokenizer.hasMoreElements()) {
195: String token = (String) tokenizer.nextElement();
196: this .allowedRolesList.add(token);
197: }
198: if (this .allowedRolesList.size() == 0) {
199: this .allowedRoles = null;
200: this .allowedRolesList = null;
201: }
202: }
203: return this .allowedRolesList;
204: }
205:
206: public void addToAllowedRoles(String role) {
207: List l = this .getAllowedRolesList();
208: if (l == null) {
209: l = new ArrayList();
210: l.add(role);
211: } else {
212: if (!l.contains(role)) {
213: l.add(role);
214: }
215: }
216: this .buildRolesString(l);
217: }
218:
219: public void removeFromAllowedRoles(String role) {
220: List l = this .getAllowedRolesList();
221: if (l != null && l.contains(role)) {
222: l.remove(role);
223: if (l.size() == 0) {
224: this .allowedRoles = null;
225: this .allowedRolesList = null;
226: } else {
227: this .buildRolesString(l);
228: }
229: }
230: }
231:
232: protected void buildRolesString(List fromList) {
233: this .allowedRolesList = fromList;
234: StringBuffer buffer = new StringBuffer();
235: boolean first = true;
236: Iterator i = fromList.iterator();
237: while (i.hasNext()) {
238: String role = (String) i.next();
239: if (!first) {
240: buffer.append(',');
241: }
242: first = false;
243: buffer.append(role);
244: }
245: this.allowedRoles = buffer.toString();
246: }
247: }
|