1 16 package org.apache.cocoon.portal.coplet; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 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 37 public class CopletData 38 extends AbstractProducible 39 implements DeltaApplicable { 40 41 protected String title; 42 43 protected CopletBaseData copletBaseData; 44 45 protected Map attributes = new HashMap (); 46 47 protected String allowedRoles; 48 49 protected transient List allowedRolesList; 50 51 54 private boolean deltaApplied = false; 55 56 59 public CopletData() { 60 } 62 63 67 public String getTitle() { 68 return title; 69 } 70 71 75 public void setTitle(String title) { 76 this.title = title; 77 } 78 79 83 public CopletBaseData getCopletBaseData() { 84 return copletBaseData; 85 } 86 87 91 public void setCopletBaseData(CopletBaseData copletBaseData) { 92 this.copletBaseData = copletBaseData; 93 } 94 95 public Object removeAttribute(String key) { 96 return this.attributes.remove(key); 97 } 98 99 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 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 this.setAttribute(key, delta); 143 } else if (attribute instanceof DeltaApplicable) { 144 boolean success = ((DeltaApplicable)attribute).applyDelta(delta); 146 if (!success) { 147 this.setAttribute(key, delta); 149 } 150 } else { 151 this.setAttribute(key, delta); 153 } 154 } 155 156 return true; 157 } 158 159 162 public boolean deltaApplied() { 163 return this.deltaApplied; 164 } 165 166 169 public String getAllowedRoles() { 170 return this.allowedRoles; 171 } 172 175 public void setAllowedRoles(String roles) { 176 this.allowedRoles = roles; 177 this.allowedRolesList = null; 178 } 179 180 184 public List getAllowedRolesList() { 185 if ( StringUtils.isBlank(this.allowedRoles) ) { 186 return null; 187 } 188 if ( this.allowedRolesList == null ) { 189 this.allowedRolesList = new ArrayList (); 190 final StringTokenizer tokenizer = new StringTokenizer (this.allowedRoles, ","); 191 while ( tokenizer.hasMoreElements() ) { 192 String token = (String )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 role) { 204 List l = this.getAllowedRolesList(); 205 if ( l == null ) { 206 l = new ArrayList (); 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 role) { 217 List 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 fromList) { 230 this.allowedRolesList = fromList; 231 StringBuffer buffer = new StringBuffer (); 232 boolean first = true; 233 Iterator i = fromList.iterator(); 234 while ( i.hasNext() ) { 235 String role = (String )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 |