KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.alfresco.service.cmr.dictionary.AspectDefinition;
23 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
24 import org.alfresco.service.cmr.dictionary.ClassDefinition;
25 import org.alfresco.service.cmr.dictionary.DictionaryService;
26 import org.alfresco.service.cmr.dictionary.TypeDefinition;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.namespace.QName;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Event to check the source type of an association
35  * <p>
36  * Checks are ignored if the source node has been deleted.
37  *
38  * @author Derek Hulley
39  */

40 public class AssocSourceTypeIntegrityEvent extends AbstractIntegrityEvent
41 {
42     private static Log logger = LogFactory.getLog(AssocSourceTypeIntegrityEvent.class);
43     
44     public AssocSourceTypeIntegrityEvent(
45             NodeService nodeService,
46             DictionaryService dictionaryService,
47             NodeRef sourceNodeRef,
48             QName assocTypeQName)
49     {
50         super(nodeService, dictionaryService, sourceNodeRef, assocTypeQName, null);
51     }
52     
53     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
54     {
55         QName assocTypeQName = getTypeQName();
56         NodeRef sourceNodeRef = getNodeRef();
57         // if the node is gone then the check is irrelevant
58
QName sourceNodeTypeQName = getNodeType(sourceNodeRef);
59         if (sourceNodeTypeQName == null)
60         {
61             // target or source is missing
62
if (logger.isDebugEnabled())
63             {
64                 logger.debug("Ignoring integrity check - node gone: \n" +
65                         " event: " + this);
66             }
67             return;
68         }
69         
70         // get the association def
71
AssociationDefinition assocDef = getAssocDef(eventResults, assocTypeQName);
72         // the association definition must exist
73
if (assocDef == null)
74         {
75             IntegrityRecord result = new IntegrityRecord(
76                     "Association type does not exist: \n" +
77                     " Source Node Type: " + sourceNodeTypeQName + "\n" +
78                     " Association Type: " + assocTypeQName);
79             eventResults.add(result);
80             return;
81         }
82         
83         // perform required checks
84
checkSourceType(eventResults, assocDef, sourceNodeRef, sourceNodeTypeQName);
85     }
86     
87     /**
88      * Checks that the source node type is valid for the association.
89      */

90     protected void checkSourceType(
91             List JavaDoc<IntegrityRecord> eventResults,
92             AssociationDefinition assocDef,
93             NodeRef sourceNodeRef,
94             QName sourceNodeTypeQName)
95     {
96         // check the association source type
97
ClassDefinition sourceDef = assocDef.getSourceClass();
98         if (sourceDef instanceof TypeDefinition)
99         {
100             // the node type must be a match
101
if (!dictionaryService.isSubClass(sourceNodeTypeQName, sourceDef.getName()))
102             {
103                 IntegrityRecord result = new IntegrityRecord(
104                         "The association source type is incorrect: \n" +
105                         " Association: " + assocDef + "\n" +
106                         " Required Source Type: " + sourceDef.getName() + "\n" +
107                         " Actual Source Type: " + sourceNodeTypeQName);
108                 eventResults.add(result);
109             }
110         }
111         else if (sourceDef instanceof AspectDefinition)
112         {
113             // the source must have a relevant aspect
114
Set JavaDoc<QName> sourceAspects = nodeService.getAspects(sourceNodeRef);
115             boolean found = false;
116             for (QName sourceAspectTypeQName : sourceAspects)
117             {
118                 if (dictionaryService.isSubClass(sourceAspectTypeQName, sourceDef.getName()))
119                 {
120                     found = true;
121                     break;
122                 }
123             }
124             if (!found)
125             {
126                 IntegrityRecord result = new IntegrityRecord(
127                         "The association source is missing the aspect required for this association: \n" +
128                         " Association: " + assocDef + "\n" +
129                         " Required Source Aspect: " + sourceDef.getName() + "\n" +
130                         " Actual Source Aspects: " + sourceAspects);
131                 eventResults.add(result);
132             }
133         }
134         else
135         {
136             IntegrityRecord result = new IntegrityRecord(
137                     "Unknown ClassDefinition subclass on the source definition: \n" +
138                     " Association: " + assocDef + "\n" +
139                     " Source Definition: " + sourceDef.getName());
140             eventResults.add(result);
141         }
142     }
143 }
144
Popular Tags