KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > domain > hibernate > ChildAssocImpl


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.domain.hibernate;
18
19 import org.alfresco.repo.domain.ChildAssoc;
20 import org.alfresco.repo.domain.Node;
21 import org.alfresco.service.cmr.repository.ChildAssociationRef;
22 import org.alfresco.service.namespace.QName;
23 import org.alfresco.util.EqualsHelper;
24
25 /**
26  * @author Derek Hulley
27  */

28 public class ChildAssocImpl implements ChildAssoc
29 {
30     private Long JavaDoc id;
31     private Node parent;
32     private Node child;
33     private QName typeQName;
34     private QName qName;
35     private boolean isPrimary;
36     private int index;
37     private transient ChildAssociationRef childAssocRef;
38     
39     public ChildAssocImpl()
40     {
41         setIndex(Integer.MAX_VALUE); // comes last
42
}
43
44     public void buildAssociation(Node parentNode, Node childNode)
45     {
46         // add the forward associations
47
this.setParent(parentNode);
48         this.setChild(childNode);
49         // add the inverse associations
50
parentNode.getChildAssocs().add(this);
51         childNode.getParentAssocs().add(this);
52     }
53     
54     public void removeAssociation()
55     {
56         // maintain inverse assoc from parent node to this instance
57
this.getParent().getChildAssocs().remove(this);
58         // maintain inverse assoc from child node to this instance
59
this.getChild().getParentAssocs().remove(this);
60     }
61     
62     public synchronized ChildAssociationRef getChildAssocRef()
63     {
64         if (childAssocRef == null)
65         {
66             childAssocRef = new ChildAssociationRef(
67                     this.typeQName,
68                     getParent().getNodeRef(),
69                     this.qName,
70                     getChild().getNodeRef(),
71                     this.isPrimary,
72                     -1);
73         }
74         return childAssocRef;
75     }
76
77     public String JavaDoc toString()
78     {
79         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32);
80         sb.append("ChildAssoc")
81           .append("[ parent=").append(parent)
82           .append(", child=").append(child)
83           .append(", name=").append(getQname())
84           .append(", isPrimary=").append(isPrimary)
85           .append("]");
86         return sb.toString();
87     }
88
89     public boolean equals(Object JavaDoc obj)
90     {
91         if (obj == null)
92         {
93             return false;
94         }
95         else if (obj == this)
96         {
97             return true;
98         }
99         else if (!(obj instanceof ChildAssoc))
100         {
101             return false;
102         }
103         ChildAssoc that = (ChildAssoc) obj;
104         return (this.getIsPrimary() == that.getIsPrimary()
105                 && EqualsHelper.nullSafeEquals(this.getTypeQName(), that.getTypeQName())
106                 && EqualsHelper.nullSafeEquals(this.getQname(), that.getQname())
107                 && EqualsHelper.nullSafeEquals(this.getParent(), that.getParent())
108                 && EqualsHelper.nullSafeEquals(this.getChild(), that.getChild()));
109     }
110     
111     public int hashCode()
112     {
113         return (qName == null ? 0 : qName.hashCode());
114     }
115
116     /**
117      * Orders the child associations by ID. A smaller ID has a higher priority.
118      * This may change once we introduce a changeable index against which to order.
119      */

120     public int compareTo(ChildAssoc another)
121     {
122         if (this == another)
123         {
124             return 0;
125         }
126         
127         int thisIndex = this.getIndex();
128         int anotherIndex = another.getIndex();
129         
130         Long JavaDoc thisId = this.getId();
131         Long JavaDoc anotherId = another.getId();
132
133         if (thisId == null) // this ID has not been set, make this instance greater
134
{
135             return -1;
136         }
137         else if (anotherId == null) // other ID has not been set, make this instance lesser
138
{
139             return 1;
140         }
141         else if (thisIndex == anotherIndex) // use the explicit index
142
{
143             return thisId.compareTo(anotherId);
144         }
145         else // fallback on order of creation
146
{
147             return (thisIndex > anotherIndex) ? 1 : -1; // a lower index, make this instance lesser
148
}
149     }
150
151     public Long JavaDoc getId()
152     {
153         return id;
154     }
155
156     /**
157      * For Hibernate use
158      */

159     private void setId(Long JavaDoc id)
160     {
161         this.id = id;
162     }
163
164     public Node getParent()
165     {
166         return parent;
167     }
168
169     /**
170      * For Hibernate use
171      */

172     private void setParent(Node parentNode)
173     {
174         this.parent = parentNode;
175     }
176
177     public Node getChild()
178     {
179         return child;
180     }
181
182     /**
183      * For Hibernate use
184      */

185     private void setChild(Node node)
186     {
187         child = node;
188     }
189     
190     public QName getTypeQName()
191     {
192         return typeQName;
193     }
194     
195     public void setTypeQName(QName typeQName)
196     {
197         this.typeQName = typeQName;
198     }
199     
200     public QName getQname()
201     {
202         return qName;
203     }
204
205     public void setQname(QName qname)
206     {
207         this.qName = qname;
208     }
209
210     public boolean getIsPrimary()
211     {
212         return isPrimary;
213     }
214
215     public void setIsPrimary(boolean isPrimary)
216     {
217         this.isPrimary = isPrimary;
218     }
219
220     public int getIndex()
221     {
222         return index;
223     }
224
225     public void setIndex(int index)
226     {
227         this.index = index;
228     }
229 }
230
Popular Tags