1 17 package org.alfresco.repo.dictionary; 18 19 import java.io.Serializable ; 20 import java.util.HashSet ; 21 import java.util.Map ; 22 import java.util.Set ; 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 45 public class DictionaryModelType implements ContentServicePolicies.OnContentUpdatePolicy, 46 NodeServicePolicies.OnUpdatePropertiesPolicy, 47 NodeServicePolicies.BeforeDeleteNodePolicy 48 { 49 50 private static final String KEY_PENDING_MODELS = "dictionaryModelType.pendingModels"; 51 52 53 private DictionaryDAO dictionaryDAO; 54 55 56 private NamespaceDAO namespaceDAO; 57 58 59 private NodeService nodeService; 60 61 62 private ContentService contentService; 63 64 65 private PolicyComponent policyComponent; 66 67 68 private DictionaryModelTypeTransactionListener transactionListener; 69 70 75 public void setDictionaryDAO(DictionaryDAO dictionaryDAO) 76 { 77 this.dictionaryDAO = dictionaryDAO; 78 } 79 80 85 public void setNamespaceDAO(NamespaceDAO namespaceDAO) 86 { 87 this.namespaceDAO = namespaceDAO; 88 } 89 90 95 public void setNodeService(NodeService nodeService) 96 { 97 this.nodeService = nodeService; 98 } 99 100 105 public void setContentService(ContentService contentService) 106 { 107 this.contentService = contentService; 108 } 109 110 115 public void setPolicyComponent(PolicyComponent policyComponent) 116 { 117 this.policyComponent = policyComponent; 118 } 119 120 123 public void init() 124 { 125 policyComponent.bindClassBehaviour( 127 ContentServicePolicies.ON_CONTENT_UPDATE, 128 ContentModel.TYPE_DICTIONARY_MODEL, 129 new JavaBehaviour(this, "onContentUpdate")); 130 131 policyComponent.bindClassBehaviour( 133 QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateProperties"), 134 ContentModel.TYPE_DICTIONARY_MODEL, 135 new JavaBehaviour(this, "onUpdateProperties")); 136 137 policyComponent.bindClassBehaviour( 139 QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"), 140 ContentModel.TYPE_DICTIONARY_MODEL, 141 new JavaBehaviour(this, "beforeDeleteNode")); 142 143 this.transactionListener = new DictionaryModelTypeTransactionListener(this.nodeService, this.contentService); 145 } 146 147 152 public void onContentUpdate(NodeRef nodeRef, boolean newContent) 153 { 154 queueModel(nodeRef); 155 } 156 157 @SuppressWarnings ("unchecked") 158 private void queueModel(NodeRef nodeRef) 159 { 160 Set <NodeRef> pendingModelUpdates = (Set <NodeRef>)AlfrescoTransactionSupport.getResource(KEY_PENDING_MODELS); 161 if (pendingModelUpdates == null) 162 { 163 pendingModelUpdates = new HashSet <NodeRef>(); 164 AlfrescoTransactionSupport.bindResource(KEY_PENDING_MODELS, pendingModelUpdates); 165 } 166 pendingModelUpdates.add(nodeRef); 167 168 AlfrescoTransactionSupport.bindListener(this.transactionListener); 169 } 170 171 178 public void onUpdateProperties( 179 NodeRef nodeRef, 180 Map <QName, Serializable > before, 181 Map <QName, Serializable > after) 182 { 183 Boolean beforeValue = (Boolean )before.get(ContentModel.PROP_MODEL_ACTIVE); 184 Boolean afterValue = (Boolean )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 queueModel(nodeRef); 194 } 195 else if (beforeValue != null && afterValue != null && beforeValue.equals(afterValue) == false) 196 { 197 queueModel(nodeRef); 198 } 199 } 200 201 @SuppressWarnings ("unchecked") 202 public void beforeDeleteNode(NodeRef nodeRef) 203 { 204 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 dictionaryDAO.removeModel(modelName); 212 213 } 215 } 216 } 217 218 221 public class DictionaryModelTypeTransactionListener implements TransactionListener 222 { 223 226 private String 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 240 public void flush() 241 { 242 } 243 244 247 @SuppressWarnings ("unchecked") 248 public void beforeCommit(boolean readOnly) 249 { 250 Set <NodeRef> pendingModels = (Set <NodeRef>)AlfrescoTransactionSupport.getResource(KEY_PENDING_MODELS); 251 252 if (pendingModels != null) 253 { 254 for (NodeRef nodeRef : pendingModels) 255 { 256 boolean isActive = false; 258 Boolean value = (Boolean )nodeService.getProperty(nodeRef, ContentModel.PROP_MODEL_ACTIVE); 259 if (value != null) 260 { 261 isActive = value.booleanValue(); 262 } 263 264 if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY) == false) 266 { 267 if (isActive == true) 268 { 269 272 ContentReader contentReader = this.contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); 273 if (contentReader != null) 274 { 275 M2Model m2Model = M2Model.createModel(contentReader.getContentInputStream()); 277 279 ModelDefinition modelDefintion = m2Model.compile(dictionaryDAO, namespaceDAO).getModelDefinition(); 281 283 Map <QName, Serializable > 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 294 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 dictionaryDAO.removeModel(modelName); 305 } 306 } 307 } 308 } 309 } 310 } 311 312 315 public void beforeCompletion() 316 { 317 } 318 319 322 public void afterCommit() 323 { 324 } 325 326 329 @SuppressWarnings ("unchecked") 330 public void afterRollback() 331 { 332 } 333 334 337 @Override 338 public boolean equals(Object 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 |