KickJava   Java API By Example, From Geeks To Geeks.

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


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.DictionaryService;
23 import org.alfresco.service.cmr.repository.AssociationRef;
24 import org.alfresco.service.cmr.repository.ChildAssociationRef;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.repository.NodeService;
27 import org.alfresco.service.namespace.QName;
28 import org.alfresco.service.namespace.RegexQNamePattern;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Event raised to check the target multiplicity for an association type
34  * from the given node.
35  * <p>
36  * Checks are ignored is the target node doesn't exist.
37  *
38  * @author Derek Hulley
39  */

40 public class AssocTargetMultiplicityIntegrityEvent extends AbstractIntegrityEvent
41 {
42     private static Log logger = LogFactory.getLog(AssocTargetMultiplicityIntegrityEvent.class);
43     
44     /** true if the assoc type may not be valid, e.g. during association deletions */
45     private boolean isDelete;
46     
47     public AssocTargetMultiplicityIntegrityEvent(
48             NodeService nodeService,
49             DictionaryService dictionaryService,
50             NodeRef sourceNodeRef,
51             QName assocTypeQName,
52             boolean isDelete)
53     {
54         super(nodeService, dictionaryService, sourceNodeRef, assocTypeQName, null);
55         this.isDelete = isDelete;
56     }
57     
58     @Override JavaDoc
59     public boolean equals(Object JavaDoc obj)
60     {
61         if (!super.equals(obj))
62         {
63             return false;
64         }
65         // so far, so good
66
AssocTargetMultiplicityIntegrityEvent that = (AssocTargetMultiplicityIntegrityEvent) obj;
67         return this.isDelete == that.isDelete;
68     }
69     
70     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
71     {
72         QName assocTypeQName = getTypeQName();
73         NodeRef sourceNodeRef = getNodeRef();
74         // event is irrelevant if the node is gone
75
QName sourceNodeTypeQName = getNodeType(sourceNodeRef);
76         if (sourceNodeTypeQName == null)
77         {
78             // target or target is missing
79
if (logger.isDebugEnabled())
80             {
81                 logger.debug("Ignoring integrity check - node gone: \n" +
82                         " event: " + this);
83             }
84             return;
85         }
86         
87         // get the association def
88
AssociationDefinition assocDef = getAssocDef(eventResults, assocTypeQName);
89         // the association definition must exist
90
if (assocDef == null)
91         {
92             if (!isDelete) // strict about the type
93
{
94                 IntegrityRecord result = new IntegrityRecord(
95                         "Association type does not exist: \n" +
96                         " Source Node Type: " + sourceNodeTypeQName + "\n" +
97                         " Association Type: " + assocTypeQName);
98                 eventResults.add(result);
99                 return;
100             }
101             else // not strict about the type
102
{
103                 return;
104             }
105         }
106         
107         // perform required checks
108
checkTargetMultiplicity(eventResults, assocDef, assocTypeQName, sourceNodeRef);
109     }
110
111     /**
112      * Checks that the target multiplicity has not been violated for the
113      * source of the association.
114      */

115     protected void checkTargetMultiplicity(
116             List JavaDoc<IntegrityRecord> eventResults,
117             AssociationDefinition assocDef,
118             QName assocTypeQName,
119             NodeRef sourceNodeRef)
120     {
121         // get the source multiplicity
122
boolean mandatory = assocDef.isTargetMandatory();
123         boolean allowMany = assocDef.isTargetMany();
124         // do we need to check
125
if (!mandatory && allowMany)
126         {
127             // it is not mandatory and it allows many on both sides of the assoc
128
return;
129         }
130         int actualSize = 0;
131         if (assocDef.isChild())
132         {
133             // check the child assocs present
134
List JavaDoc<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(
135                     sourceNodeRef,
136                     assocTypeQName,
137                     RegexQNamePattern.MATCH_ALL);
138             actualSize = childAssocRefs.size();
139         }
140         else
141         {
142             // check the target assocs present
143
List JavaDoc<AssociationRef> targetAssocRefs = nodeService.getTargetAssocs(sourceNodeRef, assocTypeQName);
144             actualSize = targetAssocRefs.size();
145         }
146         if ((mandatory && actualSize == 0) || (!allowMany && actualSize > 1))
147         {
148             String JavaDoc childOrTargetStr = (assocDef.isChild() ? "child" : "target");
149             IntegrityRecord result = new IntegrityRecord(
150                     "The association " + childOrTargetStr + " multiplicity has been violated: \n" +
151                     " Association: " + assocDef + "\n" +
152                     " Required " + childOrTargetStr + " Multiplicity: " + getMultiplicityString(mandatory, allowMany) + "\n" +
153                     " Actual " + childOrTargetStr + " Multiplicity: " + actualSize);
154             eventResults.add(result);
155         }
156     }
157 }
158
Popular Tags