KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > audit > AuditableAspect


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.audit;
18
19 import java.util.Date JavaDoc;
20
21 import org.alfresco.model.ContentModel;
22 import org.alfresco.repo.policy.Behaviour;
23 import org.alfresco.repo.policy.JavaBehaviour;
24 import org.alfresco.repo.policy.PolicyComponent;
25 import org.alfresco.repo.policy.PolicyScope;
26 import org.alfresco.service.cmr.repository.ChildAssociationRef;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.repository.StoreRef;
30 import org.alfresco.service.cmr.security.AuthenticationService;
31 import org.alfresco.service.namespace.NamespaceService;
32 import org.alfresco.service.namespace.QName;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37 /**
38  * This aspect maintains the audit properties of the Auditable aspect.
39  *
40  * @author David Caruana
41  */

42 public class AuditableAspect
43 {
44     // Logger
45
private static final Log logger = LogFactory.getLog(AuditableAspect.class);
46
47     // Unknown user, for when authentication has not occured
48
private static final String JavaDoc USERNAME_UNKNOWN = "unknown";
49     
50     // Dependencies
51
private NodeService nodeService;
52     private AuthenticationService authenticationService;
53     private PolicyComponent policyComponent;
54
55     // Behaviours
56
private Behaviour onCreateAudit;
57     private Behaviour onAddAudit;
58     private Behaviour onUpdateAudit;
59     
60
61     /**
62      * @param nodeService the node service to use for audit property maintenance
63      */

64     public void setNodeService(NodeService nodeService)
65     {
66         this.nodeService = nodeService;
67     }
68
69     /**
70      * @param policyComponent the policy component
71      */

72     public void setPolicyComponent(PolicyComponent policyComponent)
73     {
74         this.policyComponent = policyComponent;
75     }
76     
77     /**
78      * @param authenticationService the authentication service
79      */

80     public void setAuthenticationService(AuthenticationService authenticationService)
81     {
82         this.authenticationService = authenticationService;
83     }
84     
85     /**
86      * Initialise the Auditable Aspect
87      */

88     public void init()
89     {
90         // Create behaviours
91
onCreateAudit = new JavaBehaviour(this, "onCreateAudit");
92         onAddAudit = new JavaBehaviour(this, "onAddAudit");
93         onUpdateAudit = new JavaBehaviour(this, "onUpdateAudit");
94         
95         // Bind behaviours to node policies
96
policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateNode"), ContentModel.ASPECT_AUDITABLE, onCreateAudit);
97         policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"), ContentModel.ASPECT_AUDITABLE, onAddAudit);
98         policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateNode"), ContentModel.ASPECT_AUDITABLE, onUpdateAudit);
99         
100         // Register onCopy class behaviour
101
policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"), ContentModel.ASPECT_AUDITABLE, new JavaBehaviour(this, "onCopy"));
102     }
103
104     /**
105      * Maintain audit properties on creation of Node
106      *
107      * @param childAssocRef the association to the child created
108      */

109     public void onCreateAudit(ChildAssociationRef childAssocRef)
110     {
111         NodeRef nodeRef = childAssocRef.getChildRef();
112         onAddAudit(nodeRef, null);
113     }
114
115     /**
116      * Maintain audit properties on addition of audit aspect to a node
117      *
118      * @param nodeRef the node to which auditing has been added
119      * @param aspect the aspect added
120      */

121     public void onAddAudit(NodeRef nodeRef, QName aspect)
122     {
123         try
124         {
125             onUpdateAudit.disable();
126             
127             // Set created / updated date
128
Date JavaDoc now = new Date JavaDoc(System.currentTimeMillis());
129             nodeService.setProperty(nodeRef, ContentModel.PROP_CREATED, now);
130             nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIED, now);
131
132             // Set creator (but do not override, if explicitly set)
133
String JavaDoc creator = (String JavaDoc)nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR);
134             if (creator == null || creator.length() == 0)
135             {
136                 creator = getUsername();
137                 nodeService.setProperty(nodeRef, ContentModel.PROP_CREATOR, creator);
138             }
139             nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIER, creator);
140             
141             if (logger.isDebugEnabled())
142                 logger.debug("Auditable node " + nodeRef + " created [created,modified=" + now + ";creator,modifier=" + creator + "]");
143         }
144         finally
145         {
146             onUpdateAudit.enable();
147         }
148     }
149
150     /**
151      * Maintain audit properties on update of node
152      *
153      * @param nodeRef the updated node
154      */

155     public void onUpdateAudit(NodeRef nodeRef)
156     {
157         // Set updated date
158
Date JavaDoc now = new Date JavaDoc(System.currentTimeMillis());
159         nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIED, now);
160
161         // Set modifier
162
String JavaDoc modifier = getUsername();
163         nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIER, modifier);
164         
165         if (logger.isDebugEnabled())
166             logger.debug("Auditable node " + nodeRef + " updated [modified=" + now + ";modifier=" + modifier + "]");
167     }
168
169     /**
170      * @return the current username (or unknown, if unknown)
171      */

172     private String JavaDoc getUsername()
173     {
174         String JavaDoc currentUserName = authenticationService.getCurrentUserName();
175         if (currentUserName != null)
176         {
177            return currentUserName;
178         }
179         return USERNAME_UNKNOWN;
180     }
181     
182     /**
183      * OnCopy behaviour implementation for the lock aspect.
184      * <p>
185      * Ensures that the propety values of the lock aspect are not copied onto
186      * the destination node.
187      *
188      * @see org.alfresco.repo.copy.CopyServicePolicies.OnCopyNodePolicy#onCopyNode(QName, NodeRef, StoreRef, boolean, PolicyScope)
189      */

190     public void onCopy(
191             QName sourceClassRef,
192             NodeRef sourceNodeRef,
193             StoreRef destinationStoreRef,
194             boolean copyToNewNode,
195             PolicyScope copyDetails)
196     {
197         // The auditable aspect should not be copied
198
}
199 }
200
Popular Tags