KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > rule > RulesAspect


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.rule;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.policy.Behaviour;
24 import org.alfresco.repo.policy.JavaBehaviour;
25 import org.alfresco.repo.policy.PolicyComponent;
26 import org.alfresco.repo.policy.PolicyScope;
27 import org.alfresco.service.cmr.repository.ChildAssociationRef;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.NodeService;
30 import org.alfresco.service.cmr.repository.StoreRef;
31 import org.alfresco.service.cmr.rule.RuleService;
32 import org.alfresco.service.namespace.NamespaceService;
33 import org.alfresco.service.namespace.QName;
34 import org.alfresco.service.namespace.RegexQNamePattern;
35
36 /**
37  * Class containing behaviour for the rules aspect
38  *
39  * @author Roy Wetherall
40  */

41 public class RulesAspect
42 {
43     private Behaviour onAddAspectBehaviour;
44     
45     private PolicyComponent policyComponent;
46     
47     private RuleService ruleService;
48     
49     private NodeService nodeService;
50     
51     public void setPolicyComponent(PolicyComponent policyComponent)
52     {
53         this.policyComponent = policyComponent;
54     }
55     
56     public void setNodeService(NodeService nodeService)
57     {
58         this.nodeService = nodeService;
59     }
60    
61     public void setRuleService(RuleService ruleService)
62     {
63         this.ruleService = ruleService;
64     }
65     
66     public void init()
67     {
68         this.policyComponent.bindClassBehaviour(
69                 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"),
70                 RuleModel.ASPECT_RULES,
71                 new JavaBehaviour(this, "onCopyNode"));
72         this.policyComponent.bindClassBehaviour(
73                 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyComplete"),
74                 RuleModel.ASPECT_RULES,
75                 new JavaBehaviour(this, "onCopyComplete"));
76         
77         this.onAddAspectBehaviour = new JavaBehaviour(this, "onAddAspect");
78         this.policyComponent.bindClassBehaviour(
79                 QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"),
80                 RuleModel.ASPECT_RULES,
81                 onAddAspectBehaviour);
82     }
83     
84     /**
85      * Helper to diable the on add aspect policy behaviour. Helpful when importing,
86      * copying and other bulk respstorative operations.
87      *
88      * TODO will eventually be redundant when policies can be enabled/diabled in the
89      * policy componenet
90      */

91     public void disbleOnAddAspect()
92     {
93         this.onAddAspectBehaviour.disable();
94     }
95     
96     /**
97      * Helper to enable the on add aspect policy behaviour. Helpful when importing,
98      * copying and other bulk respstorative operations.
99      *
100      * TODO will eventually be redundant when policies can be enabled/diabled in the
101      * policy componenet
102      */

103     public void enableOnAddAspect()
104     {
105         this.onAddAspectBehaviour.enable();
106     }
107     
108     /**
109      * On add aspect policy behaviour
110      * @param nodeRef
111      * @param aspectTypeQName
112      */

113     public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName)
114     {
115         this.ruleService.disableRules(nodeRef);
116         try
117         {
118             int count = this.nodeService.getChildAssocs(nodeRef, RuleModel.ASSOC_RULE_FOLDER, RuleModel.ASSOC_RULE_FOLDER).size();
119             if (count == 0)
120             {
121                 this.nodeService.createNode(
122                         nodeRef,
123                         RuleModel.ASSOC_RULE_FOLDER,
124                         RuleModel.ASSOC_RULE_FOLDER,
125                         ContentModel.TYPE_SYSTEM_FOLDER);
126             }
127         }
128         finally
129         {
130             this.ruleService.enableRules(nodeRef);
131         }
132     }
133     
134     public void onCopyNode(
135             QName classRef,
136             NodeRef sourceNodeRef,
137             StoreRef destinationStoreRef,
138             boolean copyToNewNode,
139             PolicyScope copyDetails)
140     {
141         copyDetails.addAspect(RuleModel.ASPECT_RULES);
142         
143         List JavaDoc<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(
144                 sourceNodeRef,
145                 RegexQNamePattern.MATCH_ALL,
146                 RuleModel.ASSOC_RULE_FOLDER);
147         for (ChildAssociationRef assoc : assocs)
148         {
149             copyDetails.addChildAssociation(classRef, assoc, true);
150         }
151         
152         this.onAddAspectBehaviour.disable();
153     }
154     
155     public void onCopyComplete(
156             QName classRef,
157             NodeRef sourceNodeRef,
158             NodeRef destinationRef,
159             boolean copyToNew,
160             Map JavaDoc<NodeRef, NodeRef> copyMap)
161     {
162         this.onAddAspectBehaviour.enable();
163     }
164 }
165
Popular Tags