KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > ownable > impl > OwnableServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.ownable.impl;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
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 /**
33  * Ownership service support. Use in permissions framework as dynamic authority.
34  *
35  * @author Andy Hind
36  */

37 public class OwnableServiceImpl implements OwnableService, InitializingBean
38 {
39     private NodeService nodeService;
40     
41     private AuthenticationService authenticationService;
42     
43     private SimpleCache<NodeRef, String JavaDoc> nodeOwnerCache;
44
45     public OwnableServiceImpl()
46     {
47         super();
48     }
49
50     // IOC
51

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     /**
63      * @param ownerCache a transactionally-safe cache of node owners
64      */

65     public void setNodeOwnerCache(SimpleCache<NodeRef, String JavaDoc> ownerCache)
66     {
67         this.nodeOwnerCache = ownerCache;
68     }
69
70     public void afterPropertiesSet() throws Exception JavaDoc
71     {
72         if (nodeService == null)
73         {
74             throw new IllegalArgumentException JavaDoc("Property 'nodeService' has not been set");
75         }
76         if (authenticationService == null)
77         {
78             throw new IllegalArgumentException JavaDoc("Property 'authenticationService' has not been set");
79         }
80         if (nodeOwnerCache == null)
81         {
82             throw new IllegalArgumentException JavaDoc("Property 'nodeOwnerCache' has not been set");
83         }
84     }
85     
86     // OwnableService implmentation
87

88     public String JavaDoc getOwner(NodeRef nodeRef)
89     {
90         String JavaDoc userName = nodeOwnerCache.get(nodeRef);
91         
92         if (userName == null)
93         {
94             // If ownership is not explicitly set then we fall back to the creator
95
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
96             {
97                 userName = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef, ContentModel.PROP_OWNER));
98             }
99             else if(nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUDITABLE))
100             {
101                 userName = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.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 JavaDoc userName)
110     {
111         if (!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
112         {
113             HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(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