KickJava   Java API By Example, From Geeks To Geeks.

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


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 source 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 AssocSourceMultiplicityIntegrityEvent extends AbstractIntegrityEvent
41 {
42     private static Log logger = LogFactory.getLog(AssocSourceMultiplicityIntegrityEvent.class);
43     
44     /** true if the assoc type may not be valid, e.g. during association deletions */
45     private boolean isDelete;
46     
47     public AssocSourceMultiplicityIntegrityEvent(
48             NodeService nodeService,
49             DictionaryService dictionaryService,
50             NodeRef targetNodeRef,
51             QName assocTypeQName,
52             boolean isDelete)
53     {
54         super(nodeService, dictionaryService, targetNodeRef, 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
AssocSourceMultiplicityIntegrityEvent that = (AssocSourceMultiplicityIntegrityEvent) obj;
67         return this.isDelete == that.isDelete;
68     }
69     
70     public void checkIntegrity(List JavaDoc<IntegrityRecord> eventResults)
71     {
72         QName assocTypeQName = getTypeQName();
73         NodeRef targetNodeRef = getNodeRef();
74         // event is irrelevant if the node is gone
75
QName targetNodeTypeQName = getNodeType(targetNodeRef);
76         if (targetNodeTypeQName == null)
77         {
78             // target or source 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                         " Target Node Type: " + targetNodeTypeQName + "\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
checkSourceMultiplicity(eventResults, assocDef, assocTypeQName, targetNodeRef);
109     }
110     
111     /**
112      * Checks that the source multiplicity has not been violated for the
113      * target of the association.
114      */

115     protected void checkSourceMultiplicity(
116             List JavaDoc<IntegrityRecord> eventResults,
117             AssociationDefinition assocDef,
118             QName assocTypeQName,
119             NodeRef targetNodeRef)
120     {
121         // get the source multiplicity
122
boolean mandatory = assocDef.isSourceMandatory();
123         boolean allowMany = assocDef.isSourceMany();
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 parent assocs present
134
List JavaDoc<ChildAssociationRef> parentAssocRefs = nodeService.getParentAssocs(
135                     targetNodeRef,
136                     assocTypeQName,
137                     RegexQNamePattern.MATCH_ALL);
138             actualSize = parentAssocRefs.size();
139         }
140         else
141         {
142             // check the source assocs present
143
List JavaDoc<AssociationRef> sourceAssocRefs = nodeService.getSourceAssocs(targetNodeRef, assocTypeQName);
144             actualSize = sourceAssocRefs.size();
145         }
146         if ((mandatory && actualSize == 0) || (!allowMany && actualSize > 1))
147         {
148             String JavaDoc parentOrSourceStr = (assocDef.isChild() ? "child" : "target");
149             IntegrityRecord result = new IntegrityRecord(
150                     "The association " + parentOrSourceStr + " multiplicity has been violated: \n" +
151                     " Association: " + assocDef + "\n" +
152                     " Required " + parentOrSourceStr + " Multiplicity: " + getMultiplicityString(mandatory, allowMany) + "\n" +
153                     " Actual " + parentOrSourceStr + " Multiplicity: " + actualSize);
154             eventResults.add(result);
155         }
156     }
157 }
158
Popular Tags