KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > sample > contentHits > ContentHitsAspect


1 package org.alfresco.sample.contentHits;
2
3 import java.util.Date JavaDoc;
4
5 import org.alfresco.repo.content.ContentServicePolicies;
6 import org.alfresco.repo.node.NodeServicePolicies;
7 import org.alfresco.repo.policy.JavaBehaviour;
8 import org.alfresco.repo.policy.PolicyComponent;
9 import org.alfresco.service.cmr.repository.NodeRef;
10 import org.alfresco.service.cmr.repository.NodeService;
11 import org.alfresco.service.namespace.NamespaceService;
12 import org.alfresco.service.namespace.QName;
13
14 /**
15  * This class contains the behaviour behind the 'my:contentHits' aspect.
16  *
17  * @author Roy Wetherall
18  */

19 public class ContentHitsAspect implements ContentServicePolicies.OnContentReadPolicy,
20                                           ContentServicePolicies.OnContentUpdatePolicy,
21                                           NodeServicePolicies.OnAddAspectPolicy
22 {
23     /** Aspect name */
24     public static final QName ASPECT_CONTENT_HITS = QName.createQName("extension.contenthits", "contentHits");
25     
26     /** Property names */
27     public static final QName PROP_COUNT_STARTED_DATE = QName.createQName("extension.contenthits", "countStartedDate");
28     public static final QName PROP_UPDATE_COUNT = QName.createQName("extension.contenthits", "updateCount");
29     public static final QName PROP_READ_COUNT = QName.createQName("extension.contenthits", "readCount");
30     
31     /** The policy component */
32     private PolicyComponent policyComponent;
33     
34     /** The node service */
35     private NodeService nodeService;
36     
37     /**
38      * Sets the policy component
39      *
40      * @param policyComponent the policy component
41      */

42     public void setPolicyComponent(PolicyComponent policyComponent)
43     {
44         this.policyComponent = policyComponent;
45     }
46     
47     /**
48      * Sets the node service
49      *
50      * @param nodeService the node service
51      */

52     public void setNodeService(NodeService nodeService)
53     {
54         this.nodeService = nodeService;
55     }
56     
57     /**
58      * Spring initilaise method used to register the policy behaviours
59      */

60     public void initialise()
61     {
62         // Register the policy behaviours
63
this.policyComponent.bindClassBehaviour(
64                                  QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"),
65                                  ASPECT_CONTENT_HITS,
66                                  new JavaBehaviour(this, "onAddAspect"));
67         this.policyComponent.bindClassBehaviour(
68                                  ContentServicePolicies.ON_CONTENT_READ,
69                                  ASPECT_CONTENT_HITS,
70                                  new JavaBehaviour(this, "onContentRead"));
71         this.policyComponent.bindClassBehaviour(
72                                  ContentServicePolicies.ON_CONTENT_UPDATE,
73                                  ASPECT_CONTENT_HITS,
74                                  new JavaBehaviour(this, "onContentUpdate"));
75     }
76
77     /**
78      * onAddAspect policy behaviour.
79      *
80      * Sets the count started date to the date/time at which the contentHits aspect was
81      * first applied.
82      *
83      * @param nodeRef the node reference
84      * @param aspectTypeQName the qname of the aspect being applied
85      */

86     public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName)
87     {
88         if (aspectTypeQName.equals(ASPECT_CONTENT_HITS) == true)
89         {
90             // Set the count started date
91
this.nodeService.setProperty(nodeRef, PROP_COUNT_STARTED_DATE, new Date JavaDoc());
92         }
93     }
94     
95     /**
96      * onContentRead policy behaviour.
97      *
98      * Increments the aspect's read count property by one.
99      *
100      * @see org.alfresco.repo.content.ContentServicePolicies.OnContentReadPolicy#onContentRead(org.alfresco.service.cmr.repository.NodeRef)
101      */

102     public void onContentRead(NodeRef nodeRef)
103     {
104         // Increment the read count property value
105
Integer JavaDoc currentValue = (Integer JavaDoc)this.nodeService.getProperty(nodeRef, PROP_READ_COUNT);
106         int newValue = currentValue.intValue() + 1;
107         this.nodeService.setProperty(nodeRef, PROP_READ_COUNT, newValue);
108     }
109
110     /**
111      * onContentUpdate policy behaviour.
112      *
113      * Increments the aspect's update count property by one.
114      *
115      * @see org.alfresco.repo.content.ContentServicePolicies.OnContentUpdatePolicy#onContentUpdate(org.alfresco.service.cmr.repository.NodeRef, boolean)
116      */

117     public void onContentUpdate(NodeRef nodeRef, boolean newContent)
118     {
119         // Increment the update count property value
120
Integer JavaDoc currentValue = (Integer JavaDoc)this.nodeService.getProperty(nodeRef, PROP_UPDATE_COUNT);
121         int newValue = currentValue.intValue() + 1;
122         this.nodeService.setProperty(nodeRef, PROP_UPDATE_COUNT, newValue);
123     }
124 }
125
Popular Tags