KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
23 import org.alfresco.service.cmr.dictionary.DictionaryService;
24 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
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.util.EqualsHelper;
29
30 /**
31  * Base class for integrity events. It provides basic support for checking
32  * model integrity.
33  *
34  * @author Derek Hulley
35  */

36 public abstract class AbstractIntegrityEvent implements IntegrityEvent
37 {
38     protected final NodeService nodeService;
39     protected final DictionaryService dictionaryService;
40
41     /** the potential problem traces */
42     private List JavaDoc<StackTraceElement JavaDoc[]> traces;
43     /** support for derived classes */
44     private final NodeRef nodeRef;
45     /** support for derived classes */
46     private final QName typeQName;
47     /** support for derived classes */
48     private final QName qname;
49     
50     /** cached hashcode as the members are all final */
51     private int hashCode = 0;
52
53     /**
54      * Constructor with helper values for storage
55      */

56     protected AbstractIntegrityEvent(
57             NodeService nodeService,
58             DictionaryService dictionaryService,
59             NodeRef nodeRef,
60             QName typeQName,
61             QName qname)
62     {
63         this.nodeService = nodeService;
64         this.dictionaryService = dictionaryService;
65         this.traces = new ArrayList JavaDoc<StackTraceElement JavaDoc[]>(0);
66         
67         this.nodeRef = nodeRef;
68         this.typeQName = typeQName;
69         this.qname = qname;
70     }
71
72     @Override JavaDoc
73     public int hashCode()
74     {
75         if (hashCode == 0)
76         {
77             hashCode =
78                 0
79                 + 1 * (nodeRef == null ? 0 : nodeRef.hashCode())
80                 - 17* (typeQName == null ? 0 : typeQName.hashCode())
81                 + 17* (qname == null ? 0 : qname.hashCode());
82         }
83         return hashCode;
84     }
85     
86     /**
87      * Compares based on the class of this instance and the incoming instance, before
88      * comparing based on all the internal data. If derived classes store additional
89      * data for their functionality, then they should override this.
90      */

91     @Override JavaDoc
92     public boolean equals(Object JavaDoc obj)
93     {
94         if (obj == null)
95             return false;
96         else if (this == obj)
97             return true;
98         else if (this.getClass() != obj.getClass())
99             return false;
100         // we can safely cast
101
AbstractIntegrityEvent that = (AbstractIntegrityEvent) obj;
102         return
103                 EqualsHelper.nullSafeEquals(this.nodeRef, that.nodeRef) &&
104                 EqualsHelper.nullSafeEquals(this.typeQName, that.typeQName) &&
105                 EqualsHelper.nullSafeEquals(this.qname, that.qname);
106     }
107     
108     @Override JavaDoc
109     public String JavaDoc toString()
110     {
111         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(56);
112         sb.append("IntegrityEvent")
113           .append("[ name=").append(getClass().getName());
114         if (nodeRef != null)
115             sb.append(", nodeRef=").append(nodeRef);
116         if (typeQName != null)
117             sb.append(", typeQName=").append(typeQName);
118         if (qname != null)
119             sb.append(", qname=").append(qname);
120         sb.append("]");
121         // done
122
return sb.toString();
123     }
124     
125     /**
126      * Gets the node type if the node exists
127      *
128      * @param nodeRef
129      * @return Returns the node's type or null if the node no longer exists
130      */

131     protected QName getNodeType(NodeRef nodeRef)
132     {
133         try
134         {
135             return nodeService.getType(nodeRef);
136         }
137         catch (InvalidNodeRefException e)
138         {
139             // node has disappeared
140
return null;
141         }
142     }
143     
144     /**
145      * @return Returns the traces (if present) that caused the creation of this event
146      */

147     public List JavaDoc<StackTraceElement JavaDoc[]> getTraces()
148     {
149         return traces;
150     }
151
152     public void addTrace(StackTraceElement JavaDoc[] trace)
153     {
154         traces.add(trace);
155     }
156
157     protected NodeRef getNodeRef()
158     {
159         return nodeRef;
160     }
161
162     protected QName getTypeQName()
163     {
164         return typeQName;
165     }
166
167     protected QName getQName()
168     {
169         return qname;
170     }
171     
172     /**
173      * Gets the association definition from the dictionary. If the source node type is
174      * provided then the association particular to the subtype is attempted.
175      *
176      * @param eventResults results to add a violation message to
177      * @param assocTypeQName the type of the association
178      * @return Returns the association definition, or null if not found
179      */

180     protected AssociationDefinition getAssocDef(List JavaDoc<IntegrityRecord> eventResults, QName assocTypeQName)
181     {
182         return dictionaryService.getAssociation(assocTypeQName);
183     }
184     
185     protected String JavaDoc getMultiplicityString(boolean mandatory, boolean allowMany)
186     {
187         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(4);
188         sb.append(mandatory ? "1" : "0");
189         sb.append("..");
190         sb.append(allowMany ? "*" : "1");
191         return sb.toString();
192     }
193 }
194
Popular Tags