KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > DictionaryModelType


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.dictionary;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.content.ContentServicePolicies;
26 import org.alfresco.repo.node.NodeServicePolicies;
27 import org.alfresco.repo.policy.JavaBehaviour;
28 import org.alfresco.repo.policy.PolicyComponent;
29 import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
30 import org.alfresco.repo.transaction.TransactionListener;
31 import org.alfresco.service.cmr.dictionary.ModelDefinition;
32 import org.alfresco.service.cmr.repository.ContentReader;
33 import org.alfresco.service.cmr.repository.ContentService;
34 import org.alfresco.service.cmr.repository.NodeRef;
35 import org.alfresco.service.cmr.repository.NodeService;
36 import org.alfresco.service.namespace.NamespaceService;
37 import org.alfresco.service.namespace.QName;
38 import org.alfresco.util.GUID;
39
40 /**
41  * Dictionary model type behaviour.
42  *
43  * @author Roy Wetherall
44  */

45 public class DictionaryModelType implements ContentServicePolicies.OnContentUpdatePolicy,
46                                             NodeServicePolicies.OnUpdatePropertiesPolicy,
47                                             NodeServicePolicies.BeforeDeleteNodePolicy
48 {
49     /** Key to the pending models */
50     private static final String JavaDoc KEY_PENDING_MODELS = "dictionaryModelType.pendingModels";
51     
52     /** The dictionary DAO */
53     private DictionaryDAO dictionaryDAO;
54     
55     /** The namespace DAO */
56     private NamespaceDAO namespaceDAO;
57     
58     /** The node service */
59     private NodeService nodeService;
60     
61     /** The content service */
62     private ContentService contentService;
63     
64     /** The policy component */
65     private PolicyComponent policyComponent;
66     
67     /** Transaction listener */
68     private DictionaryModelTypeTransactionListener transactionListener;
69         
70     /**
71      * Set the dictionary DAO
72      *
73      * @param dictionaryDAO the dictionary DAO
74      */

75     public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
76     {
77         this.dictionaryDAO = dictionaryDAO;
78     }
79     
80     /**
81      * Set the namespace DOA
82      *
83      * @param namespaceDAO the namespace DAO
84      */

85     public void setNamespaceDAO(NamespaceDAO namespaceDAO)
86     {
87         this.namespaceDAO = namespaceDAO;
88     }
89     
90     /**
91      * Set the node service
92      *
93      * @param nodeService the node service
94      */

95     public void setNodeService(NodeService nodeService)
96     {
97         this.nodeService = nodeService;
98     }
99     
100     /**
101      * Set the content service
102      *
103      * @param contentService the content service
104      */

105     public void setContentService(ContentService contentService)
106     {
107         this.contentService = contentService;
108     }
109     
110     /**
111      * Set the policy component
112      *
113      * @param policyComponent the policy component
114      */

115     public void setPolicyComponent(PolicyComponent policyComponent)
116     {
117         this.policyComponent = policyComponent;
118     }
119     
120     /**
121      * The initialise method
122      */

123     public void init()
124     {
125         // Register interest in the onContentUpdate policy for the dictionary model type
126
policyComponent.bindClassBehaviour(
127                 ContentServicePolicies.ON_CONTENT_UPDATE,
128                 ContentModel.TYPE_DICTIONARY_MODEL,
129                 new JavaBehaviour(this, "onContentUpdate"));
130         
131         // Register interest in the onPropertyUpdate policy for the dictionary model type
132
policyComponent.bindClassBehaviour(
133                 QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateProperties"),
134                 ContentModel.TYPE_DICTIONARY_MODEL,
135                 new JavaBehaviour(this, "onUpdateProperties"));
136         
137         // Register interest in the node delete policy
138
policyComponent.bindClassBehaviour(
139                 QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"),
140                 ContentModel.TYPE_DICTIONARY_MODEL,
141                 new JavaBehaviour(this, "beforeDeleteNode"));
142         
143         // Create the transaction listener
144
this.transactionListener = new DictionaryModelTypeTransactionListener(this.nodeService, this.contentService);
145     }
146     
147     /**
148      * On content update behaviour implementation
149      *
150      * @param nodeRef the node reference whose content has been updated
151      */

152     public void onContentUpdate(NodeRef nodeRef, boolean newContent)
153     {
154         queueModel(nodeRef);
155     }
156     
157     @SuppressWarnings JavaDoc("unchecked")
158     private void queueModel(NodeRef nodeRef)
159     {
160         Set JavaDoc<NodeRef> pendingModelUpdates = (Set JavaDoc<NodeRef>)AlfrescoTransactionSupport.getResource(KEY_PENDING_MODELS);
161         if (pendingModelUpdates == null)
162         {
163             pendingModelUpdates = new HashSet JavaDoc<NodeRef>();
164             AlfrescoTransactionSupport.bindResource(KEY_PENDING_MODELS, pendingModelUpdates);
165         }
166         pendingModelUpdates.add(nodeRef);
167         
168         AlfrescoTransactionSupport.bindListener(this.transactionListener);
169     }
170     
171     /**
172      * On update properties behaviour implementation
173      *
174      * @param nodeRef the node reference
175      * @param before the values of the properties before update
176      * @param after the values of the properties after the update
177      */

178     public void onUpdateProperties(
179             NodeRef nodeRef,
180             Map JavaDoc<QName, Serializable JavaDoc> before,
181             Map JavaDoc<QName, Serializable JavaDoc> after)
182     {
183         Boolean JavaDoc beforeValue = (Boolean JavaDoc)before.get(ContentModel.PROP_MODEL_ACTIVE);
184         Boolean JavaDoc afterValue = (Boolean JavaDoc)after.get(ContentModel.PROP_MODEL_ACTIVE);
185         
186         if (beforeValue == null && afterValue != null)
187         {
188             queueModel(nodeRef);
189         }
190         else if (afterValue == null && beforeValue != null)
191         {
192             // Remove the model since the value has been cleared
193
queueModel(nodeRef);
194         }
195         else if (beforeValue != null && afterValue != null && beforeValue.equals(afterValue) == false)
196         {
197             queueModel(nodeRef);
198         }
199     }
200     
201     @SuppressWarnings JavaDoc("unchecked")
202     public void beforeDeleteNode(NodeRef nodeRef)
203     {
204         // Ignore if the node is a working copy
205
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY) == false)
206         {
207             QName modelName = (QName)this.nodeService.getProperty(nodeRef, ContentModel.PROP_MODEL_NAME);
208             if (modelName != null)
209             {
210                 // Remove the model from the dictionary
211
dictionaryDAO.removeModel(modelName);
212                 
213                 // TODO how can we make this transactional ??
214
}
215         }
216     }
217     
218     /**
219      * Dictionary model type transaction listener class.
220      */

221     public class DictionaryModelTypeTransactionListener implements TransactionListener
222     {
223         /**
224          * Id used in equals and hash
225          */

226         private String JavaDoc id = GUID.generate();
227         
228         private NodeService nodeService;
229         private ContentService contentService;
230         
231         public DictionaryModelTypeTransactionListener(NodeService nodeService, ContentService contentService)
232         {
233             this.nodeService = nodeService;
234             this.contentService = contentService;
235         }
236         
237         /**
238          * @see org.alfresco.repo.transaction.TransactionListener#flush()
239          */

240         public void flush()
241         {
242         }
243         
244         /**
245          * @see org.alfresco.repo.transaction.TransactionListener#beforeCommit(boolean)
246          */

247         @SuppressWarnings JavaDoc("unchecked")
248         public void beforeCommit(boolean readOnly)
249         {
250             Set JavaDoc<NodeRef> pendingModels = (Set JavaDoc<NodeRef>)AlfrescoTransactionSupport.getResource(KEY_PENDING_MODELS);
251             
252             if (pendingModels != null)
253             {
254                 for (NodeRef nodeRef : pendingModels)
255                 {
256                     // Find out whether the model is active (by default it is)
257
boolean isActive = false;
258                     Boolean JavaDoc value = (Boolean JavaDoc)nodeService.getProperty(nodeRef, ContentModel.PROP_MODEL_ACTIVE);
259                     if (value != null)
260                     {
261                         isActive = value.booleanValue();
262                     }
263                     
264                     // Ignore if the node is a working copy or if its inactive
265
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY) == false)
266                     {
267                         if (isActive == true)
268                         {
269                             // 1. Compile the model and update the details on the node
270
// 2. Re-put the model
271

272                             ContentReader contentReader = this.contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
273                             if (contentReader != null)
274                             {
275                                 // Create a model from the current content
276
M2Model m2Model = M2Model.createModel(contentReader.getContentInputStream());
277                                 // TODO what do we do if we don't have a model??
278

279                                 // Try and compile the model
280
ModelDefinition modelDefintion = m2Model.compile(dictionaryDAO, namespaceDAO).getModelDefinition();
281                                 // TODO what do we do if the model does not compile
282

283                                 // Update the meta data for the model
284
Map JavaDoc<QName, Serializable JavaDoc> props = this.nodeService.getProperties(nodeRef);
285                                 props.put(ContentModel.PROP_MODEL_NAME, modelDefintion.getName());
286                                 props.put(ContentModel.PROP_MODEL_DESCRIPTION, modelDefintion.getDescription());
287                                 props.put(ContentModel.PROP_MODEL_AUTHOR, modelDefintion.getAuthor());
288                                 props.put(ContentModel.PROP_MODEL_PUBLISHED_DATE, modelDefintion.getPublishedDate());
289                                 props.put(ContentModel.PROP_MODEL_VERSION, modelDefintion.getVersion());
290                                 this.nodeService.setProperties(nodeRef, props);
291                                 
292                                 // TODO how do we get the dependancies for this model ??
293

294                                 // Put the model
295
dictionaryDAO.putModel(m2Model);
296                             }
297                         }
298                         else
299                         {
300                             QName modelName = (QName)this.nodeService.getProperty(nodeRef, ContentModel.PROP_MODEL_NAME);
301                             if (modelName != null)
302                             {
303                                 // Remove the model from the dictionary
304
dictionaryDAO.removeModel(modelName);
305                             }
306                         }
307                     }
308                 }
309             }
310         }
311
312         /**
313          * @see org.alfresco.repo.transaction.TransactionListener#beforeCompletion()
314          */

315         public void beforeCompletion()
316         {
317         }
318
319         /**
320          * @see org.alfresco.repo.transaction.TransactionListener#afterCommit()
321          */

322         public void afterCommit()
323         {
324         }
325         
326         /**
327          * @see org.alfresco.repo.transaction.TransactionListener#afterRollback()
328          */

329         @SuppressWarnings JavaDoc("unchecked")
330         public void afterRollback()
331         {
332         }
333         
334         /**
335          * @see java.lang.Object#equals(java.lang.Object)
336          */

337         @Override JavaDoc
338         public boolean equals(Object JavaDoc obj)
339         {
340             if (this == obj)
341             {
342                 return true;
343             }
344             if (obj instanceof DictionaryModelTypeTransactionListener)
345             {
346                 DictionaryModelTypeTransactionListener that = (DictionaryModelTypeTransactionListener) obj;
347                 return (this.id.equals(that.id));
348             }
349             else
350             {
351                 return false;
352             }
353         }
354     }
355 }
356
Popular Tags