KickJava   Java API By Example, From Geeks To Geeks.

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


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 target type of an association
35  * <p>
36  * Checks are ignored if the target node has been deleted.
37  *
38  * @author Derek Hulley
39  */

40 public class AssocTargetTypeIntegrityEvent extends AbstractIntegrityEvent
41 {
42     private static Log logger = LogFactory.getLog(AssocTargetTypeIntegrityEvent.class);
43     
44     public AssocTargetTypeIntegrityEvent(
45             NodeService nodeService,
46             DictionaryService dictionaryService,
47             NodeRef targetNodeRef,
48             QName assocTypeQName)
49     {
50         super(nodeService, dictionaryService, targetNodeRef, assocTypeQName, null);
51     }
52     
53     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
54     {
55         QName assocTypeQName = getTypeQName();
56         NodeRef targetNodeRef = getNodeRef();
57         // if the node is gone then the check is irrelevant
58
QName targetNodeTypeQName = getNodeType(targetNodeRef);
59         if (targetNodeTypeQName == 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                     " Target Node Type: " + targetNodeTypeQName + "\n" +
78                     " Association Type: " + assocTypeQName);
79             eventResults.add(result);
80             return;
81         }
82         
83         // perform required checks
84
checkTargetType(eventResults, assocDef, targetNodeRef, targetNodeTypeQName);
85     }
86     
87     /**
88      * Checks that the target node type is valid for the association.
89      */

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