KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
22 import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
23 import org.alfresco.service.cmr.dictionary.DictionaryService;
24 import org.alfresco.service.cmr.repository.ChildAssociationRef;
25 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.namespace.QName;
29 import org.alfresco.service.namespace.RegexQNamePattern;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Event to check the association target role name
35  *
36  * @author Derek Hulley
37  */

38 public class AssocTargetRoleIntegrityEvent extends AbstractIntegrityEvent
39 {
40     private static Log logger = LogFactory.getLog(AssocTargetRoleIntegrityEvent.class);
41     
42     public AssocTargetRoleIntegrityEvent(
43             NodeService nodeService,
44             DictionaryService dictionaryService,
45             NodeRef sourceNodeRef,
46             QName assocTypeQName,
47             QName assocName)
48     {
49         super(nodeService, dictionaryService, sourceNodeRef, assocTypeQName, assocName);
50     }
51     
52     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
53     {
54         NodeRef sourceNodeRef = getNodeRef();
55         QName assocTypeQName = getTypeQName();
56         QName assocQName = getQName();
57         
58         // get the association def
59
AssociationDefinition assocDef = getAssocDef(eventResults, assocTypeQName);
60         // the association definition must exist
61
if (assocDef == null)
62         {
63             IntegrityRecord result = new IntegrityRecord(
64                     "Association type does not exist: \n" +
65                     " Association Type: " + assocTypeQName);
66             eventResults.add(result);
67             return;
68         }
69         
70         // check that we are dealing with child associations
71
if (assocQName == null)
72         {
73             throw new IllegalArgumentException JavaDoc("The association qualified name must be supplied");
74         }
75         if (!assocDef.isChild())
76         {
77             throw new UnsupportedOperationException JavaDoc("This operation is only relevant to child associations");
78         }
79         ChildAssociationDefinition childAssocDef = (ChildAssociationDefinition) assocDef;
80         
81         // perform required checks
82
checkAssocQNameRegex(eventResults, childAssocDef, assocQName);
83         checkAssocQNameDuplicate(eventResults, childAssocDef, sourceNodeRef, assocQName);
84     }
85
86     /**
87      * Checks that the association name matches the constraints imposed by the model.
88      */

89     protected void checkAssocQNameRegex(
90             List JavaDoc<IntegrityRecord> eventResults,
91             ChildAssociationDefinition assocDef,
92             QName assocQName)
93     {
94         // check the association name
95
QName assocRoleQName = assocDef.getTargetRoleName();
96         if (assocRoleQName != null)
97         {
98             // the assoc defines a role name - check it
99
RegexQNamePattern rolePattern = new RegexQNamePattern(assocRoleQName.toString());
100             if (!rolePattern.isMatch(assocQName))
101             {
102                 IntegrityRecord result = new IntegrityRecord(
103                         "The association name does not match the allowed role names: \n" +
104                         " Association: " + assocDef + "\n" +
105                         " Allowed roles: " + rolePattern + "\n" +
106                         " Name assigned: " + assocRoleQName);
107                 eventResults.add(result);
108             }
109         }
110     }
111
112     /**
113      * Checks that the association name matches the constraints imposed by the model.
114      */

115     protected void checkAssocQNameDuplicate(
116             List JavaDoc<IntegrityRecord> eventResults,
117             ChildAssociationDefinition assocDef,
118             NodeRef sourceNodeRef,
119             QName assocQName)
120     {
121         if (assocDef.getDuplicateChildNamesAllowed())
122         {
123             // nothing to do
124
return;
125         }
126         QName assocTypeQName = assocDef.getName();
127         // see if there is another association with the same name
128
try
129         {
130             List JavaDoc<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(sourceNodeRef, assocTypeQName, assocQName);
131             // duplicates not allowed
132
if (childAssocs.size() > 1)
133             {
134                 IntegrityRecord result = new IntegrityRecord(
135                         "Duplicate child associations are not allowed: \n" +
136                         " Association: " + assocDef + "\n" +
137                         " Name: " + assocQName);
138                 eventResults.add(result);
139             }
140         }
141         catch (InvalidNodeRefException e)
142         {
143             // node has gone
144
}
145     }
146 }
147
Popular Tags