1 17 package org.alfresco.repo.audit; 18 19 import java.util.Date ; 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 42 public class AuditableAspect 43 { 44 private static final Log logger = LogFactory.getLog(AuditableAspect.class); 46 47 private static final String USERNAME_UNKNOWN = "unknown"; 49 50 private NodeService nodeService; 52 private AuthenticationService authenticationService; 53 private PolicyComponent policyComponent; 54 55 private Behaviour onCreateAudit; 57 private Behaviour onAddAudit; 58 private Behaviour onUpdateAudit; 59 60 61 64 public void setNodeService(NodeService nodeService) 65 { 66 this.nodeService = nodeService; 67 } 68 69 72 public void setPolicyComponent(PolicyComponent policyComponent) 73 { 74 this.policyComponent = policyComponent; 75 } 76 77 80 public void setAuthenticationService(AuthenticationService authenticationService) 81 { 82 this.authenticationService = authenticationService; 83 } 84 85 88 public void init() 89 { 90 onCreateAudit = new JavaBehaviour(this, "onCreateAudit"); 92 onAddAudit = new JavaBehaviour(this, "onAddAudit"); 93 onUpdateAudit = new JavaBehaviour(this, "onUpdateAudit"); 94 95 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 policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"), ContentModel.ASPECT_AUDITABLE, new JavaBehaviour(this, "onCopy")); 102 } 103 104 109 public void onCreateAudit(ChildAssociationRef childAssocRef) 110 { 111 NodeRef nodeRef = childAssocRef.getChildRef(); 112 onAddAudit(nodeRef, null); 113 } 114 115 121 public void onAddAudit(NodeRef nodeRef, QName aspect) 122 { 123 try 124 { 125 onUpdateAudit.disable(); 126 127 Date now = new Date (System.currentTimeMillis()); 129 nodeService.setProperty(nodeRef, ContentModel.PROP_CREATED, now); 130 nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIED, now); 131 132 String creator = (String )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 155 public void onUpdateAudit(NodeRef nodeRef) 156 { 157 Date now = new Date (System.currentTimeMillis()); 159 nodeService.setProperty(nodeRef, ContentModel.PROP_MODIFIED, now); 160 161 String 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 172 private String getUsername() 173 { 174 String currentUserName = authenticationService.getCurrentUserName(); 175 if (currentUserName != null) 176 { 177 return currentUserName; 178 } 179 return USERNAME_UNKNOWN; 180 } 181 182 190 public void onCopy( 191 QName sourceClassRef, 192 NodeRef sourceNodeRef, 193 StoreRef destinationStoreRef, 194 boolean copyToNewNode, 195 PolicyScope copyDetails) 196 { 197 } 199 } 200 | Popular Tags |