KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > integrity > PropertiesIntegrityEvent


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.node.integrity;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.alfresco.service.cmr.dictionary.AspectDefinition;
26 import org.alfresco.service.cmr.dictionary.DictionaryService;
27 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
28 import org.alfresco.service.cmr.dictionary.TypeDefinition;
29 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
30 import org.alfresco.service.cmr.repository.NodeRef;
31 import org.alfresco.service.cmr.repository.NodeService;
32 import org.alfresco.service.namespace.QName;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Event raised to check nodes
38  *
39  * @author Derek Hulley
40  */

41 public class PropertiesIntegrityEvent extends AbstractIntegrityEvent
42 {
43     private static Log logger = LogFactory.getLog(PropertiesIntegrityEvent.class);
44     
45     protected PropertiesIntegrityEvent(
46             NodeService nodeService,
47             DictionaryService dictionaryService,
48             NodeRef nodeRef)
49     {
50         super(nodeService, dictionaryService, nodeRef, null, null);
51     }
52     
53     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
54     {
55         try
56         {
57             checkAllProperties(getNodeRef(), eventResults);
58         }
59         catch (InvalidNodeRefException e)
60         {
61             // node has gone
62
if (logger.isDebugEnabled())
63             {
64                 logger.debug("Event ignored - node gone: " + this);
65             }
66             eventResults.clear();
67             return;
68         }
69     }
70
71     /**
72      * Checks the properties for the type and aspects of the given node.
73      *
74      * @param nodeRef
75      * @param eventResults
76      */

77     private void checkAllProperties(NodeRef nodeRef, List JavaDoc<IntegrityRecord> eventResults)
78     {
79         // get all properties for the node
80
Map JavaDoc<QName, Serializable JavaDoc> nodeProperties = nodeService.getProperties(nodeRef);
81         
82         // get the node type
83
QName nodeTypeQName = nodeService.getType(nodeRef);
84         // get property definitions for the node type
85
TypeDefinition typeDef = dictionaryService.getType(nodeTypeQName);
86         Collection JavaDoc<PropertyDefinition> propertyDefs = typeDef.getProperties().values();
87         // check them
88
checkAllProperties(nodeRef, nodeTypeQName, propertyDefs, nodeProperties, eventResults);
89         
90         // get the node aspects
91
Set JavaDoc<QName> aspectTypeQNames = nodeService.getAspects(nodeRef);
92         for (QName aspectTypeQName : aspectTypeQNames)
93         {
94             // get property definitions for the aspect
95
AspectDefinition aspectDef = dictionaryService.getAspect(aspectTypeQName);
96             propertyDefs = aspectDef.getProperties().values();
97             // check them
98
checkAllProperties(nodeRef, aspectTypeQName, propertyDefs, nodeProperties, eventResults);
99         }
100         // done
101
}
102
103     /**
104      * Checks the specific map of properties against the required property definitions
105      *
106      * @param nodeRef the node to which this applies
107      * @param typeQName the qualified name of the aspect or type to which the properties belong
108      * @param propertyDefs the definitions to check against - may be null or empty
109      * @param nodeProperties the properties to check
110      */

111     private void checkAllProperties(
112             NodeRef nodeRef,
113             QName typeQName,
114             Collection JavaDoc<PropertyDefinition> propertyDefs,
115             Map JavaDoc<QName, Serializable JavaDoc> nodeProperties,
116             Collection JavaDoc<IntegrityRecord> eventResults)
117     {
118         // check for null or empty definitions
119
if (propertyDefs == null || propertyDefs.isEmpty())
120         {
121             return;
122         }
123         for (PropertyDefinition propertyDef : propertyDefs)
124         {
125             QName propertyQName = propertyDef.getName();
126             Serializable JavaDoc propertyValue = nodeProperties.get(propertyQName);
127             // check that mandatory properties are set
128
if (propertyDef.isMandatory() && !nodeProperties.containsKey(propertyQName))
129             {
130                 IntegrityRecord result = new IntegrityRecord(
131                         "Mandatory property not set: \n" +
132                         " Node: " + nodeRef + "\n" +
133                         " Type: " + typeQName + "\n" +
134                         " Property: " + propertyQName);
135                 eventResults.add(result);
136                 // next one
137
continue;
138             }
139             // TODO: Incorporate value constraint checks - JIRA AR166
140
}
141     }
142 }
143
Popular Tags