1 17 package org.alfresco.repo.ownable.impl; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 22 import org.alfresco.model.ContentModel; 23 import org.alfresco.repo.cache.SimpleCache; 24 import org.alfresco.service.cmr.repository.NodeRef; 25 import org.alfresco.service.cmr.repository.NodeService; 26 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 27 import org.alfresco.service.cmr.security.AuthenticationService; 28 import org.alfresco.service.cmr.security.OwnableService; 29 import org.alfresco.service.namespace.QName; 30 import org.springframework.beans.factory.InitializingBean; 31 32 37 public class OwnableServiceImpl implements OwnableService, InitializingBean 38 { 39 private NodeService nodeService; 40 41 private AuthenticationService authenticationService; 42 43 private SimpleCache<NodeRef, String > nodeOwnerCache; 44 45 public OwnableServiceImpl() 46 { 47 super(); 48 } 49 50 52 public void setNodeService(NodeService nodeService) 53 { 54 this.nodeService = nodeService; 55 } 56 57 public void setAuthenticationService(AuthenticationService authenticationService) 58 { 59 this.authenticationService = authenticationService; 60 } 61 62 65 public void setNodeOwnerCache(SimpleCache<NodeRef, String > ownerCache) 66 { 67 this.nodeOwnerCache = ownerCache; 68 } 69 70 public void afterPropertiesSet() throws Exception 71 { 72 if (nodeService == null) 73 { 74 throw new IllegalArgumentException ("Property 'nodeService' has not been set"); 75 } 76 if (authenticationService == null) 77 { 78 throw new IllegalArgumentException ("Property 'authenticationService' has not been set"); 79 } 80 if (nodeOwnerCache == null) 81 { 82 throw new IllegalArgumentException ("Property 'nodeOwnerCache' has not been set"); 83 } 84 } 85 86 88 public String getOwner(NodeRef nodeRef) 89 { 90 String userName = nodeOwnerCache.get(nodeRef); 91 92 if (userName == null) 93 { 94 if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE)) 96 { 97 userName = DefaultTypeConverter.INSTANCE.convert(String .class, nodeService.getProperty(nodeRef, ContentModel.PROP_OWNER)); 98 } 99 else if(nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUDITABLE)) 100 { 101 userName = DefaultTypeConverter.INSTANCE.convert(String .class, nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR)); 102 } 103 nodeOwnerCache.put(nodeRef, userName); 104 } 105 106 return userName; 107 } 108 109 public void setOwner(NodeRef nodeRef, String userName) 110 { 111 if (!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE)) 112 { 113 HashMap <QName, Serializable > properties = new HashMap <QName, Serializable >(1, 1.0f); 114 properties.put(ContentModel.PROP_OWNER, userName); 115 nodeService.addAspect(nodeRef, ContentModel.ASPECT_OWNABLE, properties); 116 } 117 else 118 { 119 nodeService.setProperty(nodeRef, ContentModel.PROP_OWNER, userName); 120 } 121 nodeOwnerCache.put(nodeRef, userName); 122 } 123 124 public void takeOwnership(NodeRef nodeRef) 125 { 126 setOwner(nodeRef, authenticationService.getCurrentUserName()); 127 } 128 129 public boolean hasOwner(NodeRef nodeRef) 130 { 131 return getOwner(nodeRef) != null; 132 } 133 } 134 | Popular Tags |