1 17 package org.alfresco.web.bean.repository; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.faces.context.FacesContext; 27 28 import org.alfresco.service.ServiceRegistry; 29 import org.alfresco.service.cmr.repository.AssociationRef; 30 import org.alfresco.service.cmr.repository.ChildAssociationRef; 31 import org.alfresco.service.cmr.repository.NodeRef; 32 import org.alfresco.service.cmr.security.AccessStatus; 33 import org.alfresco.service.cmr.security.PermissionService; 34 import org.alfresco.service.namespace.QName; 35 import org.alfresco.service.namespace.RegexQNamePattern; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 44 public class Node implements Serializable 45 { 46 private static final long serialVersionUID = 3544390322739034169L; 47 48 protected static Log logger = LogFactory.getLog(Node.class); 49 50 protected NodeRef nodeRef; 51 private String name; 52 private QName type; 53 private String path; 54 private String id; 55 private Set <QName> aspects = null; 56 private Map <String , Boolean > permissions; 57 protected QNameNodeMap<String , Object > properties; 58 protected boolean propsRetrieved = false; 59 protected ServiceRegistry services = null; 60 61 private boolean childAssocsRetrieved = false; 62 private QNameNodeMap childAssociations; 63 private Map <String , Map <String , ChildAssociationRef>> childAssociationsAdded; 64 private Map <String , Map <String , ChildAssociationRef>> childAssociationsRemoved; 65 66 private boolean assocsRetrieved = false; 67 private QNameNodeMap associations; 68 private Map <String , Map <String , AssociationRef>> associationsAdded; 69 private Map <String , Map <String , AssociationRef>> associationsRemoved; 70 71 76 public Node(NodeRef nodeRef) 77 { 78 if (nodeRef == null) 79 { 80 throw new IllegalArgumentException ("NodeRef must be supplied for creation of a Node."); 81 } 82 83 this.nodeRef = nodeRef; 84 this.id = nodeRef.getId(); 85 86 this.properties = new QNameNodeMap<String , Object >(getServiceRegistry().getNamespaceService(), this); 87 } 88 89 92 public Map <String , Object > getProperties() 93 { 94 if (this.propsRetrieved == false) 95 { 96 Map <QName, Serializable > props = getServiceRegistry().getNodeService().getProperties(this.nodeRef); 97 98 for (QName qname: props.keySet()) 99 { 100 Serializable propValue = props.get(qname); 101 this.properties.put(qname.toString(), propValue); 102 } 103 104 this.propsRetrieved = true; 105 } 106 107 return this.properties; 108 } 109 110 114 public final Map getAssociations() 115 { 116 if (this.assocsRetrieved == false) 117 { 118 associations = new QNameNodeMap(getServiceRegistry().getNamespaceService(), this); 119 120 List <AssociationRef> assocs = getServiceRegistry().getNodeService().getTargetAssocs(this.nodeRef, RegexQNamePattern.MATCH_ALL); 121 122 for (AssociationRef assocRef: assocs) 123 { 124 String assocName = assocRef.getTypeQName().toString(); 125 126 List list = (List )this.associations.get(assocName); 127 if (list == null) 129 { 130 list = new ArrayList <AssociationRef>(); 131 this.associations.put(assocName, list); 132 } 133 134 list.add(assocRef); 136 } 137 138 this.assocsRetrieved = true; 139 } 140 141 return this.associations; 142 } 143 144 149 public final Map <String , Map <String , AssociationRef>> getAddedAssociations() 150 { 151 if (this.associationsAdded == null) 152 { 153 this.associationsAdded = new HashMap <String , Map <String , AssociationRef>>(); 154 } 155 return this.associationsAdded; 156 } 157 158 163 public final Map <String , Map <String , AssociationRef>> getRemovedAssociations() 164 { 165 if (this.associationsRemoved == null) 166 { 167 this.associationsRemoved = new HashMap <String , Map <String , AssociationRef>>(); 168 } 169 return this.associationsRemoved; 170 } 171 172 176 public final Map getChildAssociations() 177 { 178 if (this.childAssocsRetrieved == false) 179 { 180 this.childAssociations = new QNameNodeMap(getServiceRegistry().getNamespaceService(), this); 181 182 List <ChildAssociationRef> assocs = getServiceRegistry().getNodeService().getChildAssocs(this.nodeRef); 183 184 for (ChildAssociationRef assocRef: assocs) 185 { 186 String assocName = assocRef.getTypeQName().toString(); 187 188 List list = (List )this.childAssociations.get(assocName); 189 if (list == null) 191 { 192 list = new ArrayList <ChildAssociationRef>(); 193 this.childAssociations.put(assocName, list); 194 } 195 196 list.add(assocRef); 198 } 199 200 this.childAssocsRetrieved = true; 201 } 202 203 return this.childAssociations; 204 } 205 206 211 public final Map <String , Map <String , ChildAssociationRef>> getAddedChildAssociations() 212 { 213 if (this.childAssociationsAdded == null) 214 { 215 this.childAssociationsAdded = new HashMap <String , Map <String , ChildAssociationRef>>(); 216 } 217 return this.childAssociationsAdded; 218 } 219 220 225 public final Map <String , Map <String , ChildAssociationRef>> getRemovedChildAssociations() 226 { 227 if (this.childAssociationsRemoved == null) 228 { 229 this.childAssociationsRemoved = new HashMap <String , Map <String , ChildAssociationRef>>(); 230 } 231 return this.childAssociationsRemoved; 232 } 233 234 240 public final void addPropertyResolver(String name, NodePropertyResolver resolver) 241 { 242 this.properties.addPropertyResolver(name, resolver); 243 } 244 245 252 public final boolean containsPropertyResolver(String name) 253 { 254 return this.properties.containsPropertyResolver(name); 255 } 256 257 263 public final boolean hasProperty(String propertyName) 264 { 265 return getProperties().containsKey(propertyName); 266 } 267 268 271 public final NodeRef getNodeRef() 272 { 273 return this.nodeRef; 274 } 275 276 279 public final QName getType() 280 { 281 if (this.type == null) 282 { 283 this.type = getServiceRegistry().getNodeService().getType(this.nodeRef); 284 } 285 286 return type; 287 } 288 289 292 public final String getName() 293 { 294 if (this.name == null) 295 { 296 this.name = (String )getProperties().get("cm:name"); 298 299 if (this.name == null) 301 { 302 this.name = getServiceRegistry().getNodeService().getPrimaryParent(this.nodeRef).getQName().getLocalName(); 303 } 304 } 305 306 return this.name; 307 } 308 309 312 public final Set <QName> getAspects() 313 { 314 if (this.aspects == null) 315 { 316 this.aspects = getServiceRegistry().getNodeService().getAspects(this.nodeRef); 317 } 318 319 return this.aspects; 320 } 321 322 326 public final boolean hasAspect(QName aspect) 327 { 328 Set aspects = getAspects(); 329 return aspects.contains(aspect); 330 } 331 332 339 public final boolean hasPermission(String permission) 340 { 341 Boolean valid = null; 342 if (permissions != null) 343 { 344 valid = permissions.get(permission); 345 } 346 else 347 { 348 permissions = new HashMap <String , Boolean >(5, 1.0f); 349 } 350 351 if (valid == null) 352 { 353 PermissionService service = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getPermissionService(); 354 valid = Boolean.valueOf(service.hasPermission(this.nodeRef, permission) == AccessStatus.ALLOWED); 355 permissions.put(permission, valid); 356 } 357 358 return valid.booleanValue(); 359 } 360 361 364 public final String getId() 365 { 366 return this.id; 367 } 368 369 372 public final String getPath() 373 { 374 if (this.path == null) 375 { 376 this.path = getServiceRegistry().getNodeService().getPath(this.nodeRef).toString(); 377 } 378 379 return this.path; 380 } 381 382 385 public void reset() 386 { 387 this.name = null; 388 this.type = null; 389 this.path = null; 390 this.properties.clear(); 391 this.propsRetrieved = false; 392 this.aspects = null; 393 this.permissions = null; 394 395 this.associations = null; 396 this.associationsAdded = null; 397 this.associationsRemoved = null; 398 this.assocsRetrieved = false; 399 400 this.childAssociations = null; 401 this.childAssociationsAdded = null; 402 this.childAssociationsRemoved = null; 403 this.childAssocsRetrieved = false; 404 } 405 406 409 public String toString() 410 { 411 if (getServiceRegistry().getNodeService() != null) 412 { 413 if (getServiceRegistry().getNodeService().exists(nodeRef)) 414 { 415 return "Node Type: " + getType() + 416 "\nNode Properties: " + this.getProperties().toString() + 417 "\nNode Aspects: " + this.getAspects().toString(); 418 } 419 else 420 { 421 return "Node no longer exists: " + nodeRef; 422 } 423 } 424 else 425 { 426 return super.toString(); 427 } 428 } 429 430 protected ServiceRegistry getServiceRegistry() 431 { 432 if (this.services == null) 433 { 434 this.services = Repository.getServiceRegistry(FacesContext.getCurrentInstance()); 435 } 436 return this.services; 437 } 438 } 439 | Popular Tags |