KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > policy > BehaviourFilterImpl


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.policy;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.namespace.QName;
27
28 /**
29  * Implementation of Behaviour Filter.
30  *
31  * @author David Caruana
32  */

33 public class BehaviourFilterImpl implements BehaviourFilter
34 {
35     // Thread local storage of filters
36
ThreadLocal JavaDoc<List JavaDoc<QName>> classFilter = new ThreadLocal JavaDoc<List JavaDoc<QName>>();
37     ThreadLocal JavaDoc<Map JavaDoc<NodeRef,List JavaDoc<QName>>> nodeRefFilter = new ThreadLocal JavaDoc<Map JavaDoc<NodeRef,List JavaDoc<QName>>>();
38     
39     // Dictionary Service
40
private DictionaryService dictionaryService;
41     
42     /**
43      * @param dictionaryService dictionary service
44      */

45     public void setDictionaryService(DictionaryService dictionaryService)
46     {
47         this.dictionaryService = dictionaryService;
48     }
49         
50     /* (non-Javadoc)
51      * @see org.alfresco.repo.policy.BehaviourFilter#disableBehaviour(org.alfresco.service.namespace.QName)
52      */

53     public boolean disableBehaviour(QName className)
54     {
55         List JavaDoc<QName> classNames = classFilter.get();
56         if (classNames == null)
57         {
58             classNames = new ArrayList JavaDoc<QName>();
59             classFilter.set(classNames);
60         }
61         boolean alreadyDisabled = classNames.contains(className);
62         if (!alreadyDisabled)
63         {
64             classNames.add(className);
65         }
66         return alreadyDisabled;
67     }
68
69     /* (non-Javadoc)
70      * @see org.alfresco.repo.policy.BehaviourFilter#disableBehaviour(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
71      */

72     public boolean disableBehaviour(NodeRef nodeRef, QName className)
73     {
74         Map JavaDoc<NodeRef,List JavaDoc<QName>> filters = nodeRefFilter.get();
75         if (filters == null)
76         {
77             filters = new HashMap JavaDoc<NodeRef,List JavaDoc<QName>>();
78             nodeRefFilter.set(filters);
79         }
80         List JavaDoc<QName> classNames = filters.get(nodeRef);
81         if (classNames == null)
82         {
83             classNames = new ArrayList JavaDoc<QName>();
84             filters.put(nodeRef, classNames);
85         }
86         boolean alreadyDisabled = classNames.contains(className);
87         if (!alreadyDisabled)
88         {
89             classNames.add(className);
90         }
91         return alreadyDisabled;
92     }
93
94     /* (non-Javadoc)
95      * @see org.alfresco.repo.policy.BehaviourFilter#enableBehaviour(org.alfresco.service.namespace.QName)
96      */

97     public void enableBehaviour(QName className)
98     {
99         List JavaDoc<QName> classNames = classFilter.get();
100         if (classNames != null)
101         {
102             classNames.remove(className);
103         }
104     }
105     
106     /* (non-Javadoc)
107      * @see org.alfresco.repo.policy.BehaviourFilter#enableBehaviour(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
108      */

109     public void enableBehaviour(NodeRef nodeRef, QName className)
110     {
111         Map JavaDoc<NodeRef,List JavaDoc<QName>> filters = nodeRefFilter.get();
112         if (filters != null)
113         {
114             List JavaDoc<QName> classNames = filters.get(nodeRef);
115             if (classNames != null)
116             {
117                 classNames.remove(className);
118             }
119             if (classNames.size() == 0)
120             {
121                 filters.remove(nodeRef);
122             }
123         }
124     }
125
126     /* (non-Javadoc)
127      * @see org.alfresco.repo.policy.BehaviourFilter#enableBehaviours(org.alfresco.service.cmr.repository.NodeRef)
128      */

129     public void enableBehaviours(NodeRef nodeRef)
130     {
131         Map JavaDoc<NodeRef,List JavaDoc<QName>> filters = nodeRefFilter.get();
132         if (filters != null)
133         {
134             filters.remove(nodeRef);
135         }
136     }
137     
138     /* (non-Javadoc)
139      * @see org.alfresco.repo.policy.BehaviourFilter#enableAllBehaviours()
140      */

141     public void enableAllBehaviours()
142     {
143         Map JavaDoc<NodeRef,List JavaDoc<QName>> filters = nodeRefFilter.get();
144         if (filters != null)
145         {
146             filters.clear();
147         }
148     }
149
150     /* (non-Javadoc)
151      * @see org.alfresco.repo.policy.BehaviourFilter#isEnabled(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
152      */

153     public boolean isEnabled(NodeRef nodeRef, QName className)
154     {
155         // check global filters
156
if (!isEnabled(className))
157         {
158             return false;
159         }
160         
161         // check node level filters
162
Map JavaDoc<NodeRef,List JavaDoc<QName>> nodeFilters = nodeRefFilter.get();
163         if (nodeFilters != null)
164         {
165             List JavaDoc<QName> nodeClassFilters = nodeFilters.get(nodeRef);
166             if (nodeClassFilters != null)
167             {
168                 boolean filtered = nodeClassFilters.contains(className);
169                 if (filtered)
170                 {
171                     return false;
172                 }
173                 for (QName filterName : nodeClassFilters)
174                 {
175                     filtered = dictionaryService.isSubClass(className, filterName);
176                     if (filtered)
177                     {
178                         return false;
179                     }
180                 }
181             }
182         }
183         
184         return true;
185     }
186
187     /* (non-Javadoc)
188      * @see org.alfresco.repo.policy.BehaviourFilter#isEnabled(org.alfresco.service.namespace.QName)
189      */

190     public boolean isEnabled(QName className)
191     {
192         // check global class filters
193
List JavaDoc<QName> classFilters = classFilter.get();
194         if (classFilters != null)
195         {
196             boolean filtered = classFilters.contains(className);
197             if (filtered)
198             {
199                 return false;
200             }
201             for (QName filterName : classFilters)
202             {
203                 filtered = dictionaryService.isSubClass(className, filterName);
204                 if (filtered)
205                 {
206                     return false;
207                 }
208             }
209         }
210
211         return true;
212     }
213
214     /* (non-Javadoc)
215      * @see org.alfresco.repo.policy.BehaviourFilter#isActivated()
216      */

217     public boolean isActivated()
218     {
219         List JavaDoc<QName> classFilters = classFilter.get();
220         Map JavaDoc<NodeRef,List JavaDoc<QName>> nodeFilters = nodeRefFilter.get();
221         return (classFilters != null && !classFilters.isEmpty()) || (nodeFilters != null && !nodeFilters.isEmpty());
222     }
223
224 }
225
Popular Tags