KickJava   Java API By Example, From Geeks To Geeks.

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


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.Node;
20 import org.alfresco.repo.domain.NodeAssoc;
21 import org.alfresco.service.cmr.repository.AssociationRef;
22 import org.alfresco.service.namespace.QName;
23 import org.alfresco.util.EqualsHelper;
24
25 /**
26  * Hibernate-specific implementation of the generic node association
27  *
28  * @author Derek Hulley
29  */

30 public class NodeAssocImpl implements NodeAssoc
31 {
32     private long id;
33     private Node source;
34     private Node target;
35     private QName typeQName;
36     private transient AssociationRef nodeAssocRef;
37
38     public NodeAssocImpl()
39     {
40     }
41
42     public void buildAssociation(Node sourceNode, Node targetNode)
43     {
44         // add the forward associations
45
this.setTarget(targetNode);
46         this.setSource(sourceNode);
47         // add the inverse associations
48
sourceNode.getTargetNodeAssocs().add(this);
49         targetNode.getSourceNodeAssocs().add(this);
50     }
51     
52     public void removeAssociation()
53     {
54         // maintain inverse assoc from source node to this instance
55
this.getSource().getTargetNodeAssocs().remove(this);
56         // maintain inverse assoc from target node to this instance
57
this.getTarget().getSourceNodeAssocs().remove(this);
58     }
59     
60     public synchronized AssociationRef getNodeAssocRef()
61     {
62         if (nodeAssocRef == null)
63         {
64             nodeAssocRef = new AssociationRef(getSource().getNodeRef(),
65                     this.typeQName,
66                     getTarget().getNodeRef());
67         }
68         return nodeAssocRef;
69     }
70
71     public String JavaDoc toString()
72     {
73         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32);
74         sb.append("NodeAssoc")
75           .append("[ source=").append(source)
76           .append(", target=").append(target)
77           .append(", name=").append(getTypeQName())
78           .append("]");
79         return sb.toString();
80     }
81
82     public boolean equals(Object JavaDoc obj)
83     {
84         if (obj == null)
85         {
86             return false;
87         }
88         else if (obj == this)
89         {
90             return true;
91         }
92         else if (!(obj instanceof NodeAssoc))
93         {
94             return false;
95         }
96         NodeAssoc that = (NodeAssoc) obj;
97         return (EqualsHelper.nullSafeEquals(this.getTypeQName(), that.getTypeQName())
98                 && EqualsHelper.nullSafeEquals(this.getTarget(), that.getTarget())
99                 && EqualsHelper.nullSafeEquals(this.getSource(), that.getSource()));
100     }
101     
102     public int hashCode()
103     {
104         return (typeQName == null ? 0 : typeQName.hashCode());
105     }
106
107     public long getId()
108     {
109         return id;
110     }
111
112     /**
113      * For Hibernate use
114      */

115     private void setId(long id)
116     {
117         this.id = id;
118     }
119
120     public Node getSource()
121     {
122         return source;
123     }
124
125     /**
126      * For internal use
127      */

128     private void setSource(Node source)
129     {
130         this.source = source;
131     }
132
133     public Node getTarget()
134     {
135         return target;
136     }
137
138     /**
139      * For internal use
140      */

141     private void setTarget(Node target)
142     {
143         this.target = target;
144     }
145
146     public QName getTypeQName()
147     {
148         return typeQName;
149     }
150
151     public void setTypeQName(QName typeQName)
152     {
153         this.typeQName = typeQName;
154     }
155 }
156
Popular Tags