libglom classes: Use serialization instead of deep cloning.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / shared / libglom / layout / UsesRelationshipImpl.java
1 package org.glom.web.shared.libglom.layout;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.glom.web.shared.libglom.Relationship;
5
6 public class UsesRelationshipImpl implements UsesRelationship {
7         private static final long serialVersionUID = -3778108396526473307L;
8         private Relationship relationship;
9         private Relationship relatedRelationship;
10
11         @Override
12         public void setRelationship(final Relationship relationship) {
13                 this.relationship = relationship;
14         }
15
16         /**
17          * @param get_related_relationship
18          */
19         @Override
20         public void setRelatedRelationship(final Relationship relationship) {
21                 this.relatedRelationship = relationship;
22         }
23
24         @Override
25         public Relationship getRelationship() {
26                 return relationship;
27         }
28
29         @Override
30         public Relationship getRelatedRelationship() {
31                 return relatedRelationship;
32         }
33
34         @Override
35         public boolean getHasRelationshipName() {
36                 if (relationship == null) {
37                         return false;
38                 }
39
40                 if (StringUtils.isEmpty(relationship.getName())) {
41                         return false;
42                 }
43
44                 return true;
45         }
46
47         @Override
48         public boolean getHasRelatedRelationshipName() {
49                 if (relatedRelationship == null) {
50                         return false;
51                 }
52
53                 if (StringUtils.isEmpty(relatedRelationship.getName())) {
54                         return false;
55                 }
56
57                 return true;
58         }
59
60         @Override
61         public String getSqlJoinAliasName() {
62                 String result = "";
63
64                 if (getHasRelationshipName() && relationship.getHasFields()) // relationships that link to tables together
65                                                                                                                                                 // via a field
66                 {
67                         // We use relationship_name.field_name instead of related_tableName.field_name,
68                         // because, in the JOIN below, will specify the relationship_name as an alias for the related table name
69                         result += ("relationship_" + relationship.getName());
70
71                         if (getHasRelatedRelationshipName() && relatedRelationship.getHasFields()) {
72                                 result += ('_' + relatedRelationship.getName());
73                         }
74                 }
75
76                 return result;
77         }
78
79         /*
80          * (non-Javadoc)
81          * 
82          * @see java.lang.Object#hashCode()
83          */
84         /*
85          * @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result +
86          * ((relatedRelationship == null) ? 0 : relatedRelationship.hashCode()); result = prime * result + ((relationship ==
87          * null) ? 0 : relationship.hashCode()); return result; }
88          */
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see java.lang.Object#equals(java.lang.Object)
94          * 
95          * 
96          * TODO: This causes NullPointerExceptions when used from contains().
97          */
98         @Override
99         public boolean equals(final Object obj) {
100                 if (this == obj) {
101                         return true;
102                 }
103
104                 if (obj == null) {
105                         return false;
106                 }
107
108                 if (!(obj instanceof UsesRelationshipImpl)) {
109                         return false;
110                 }
111
112                 final UsesRelationshipImpl other = (UsesRelationshipImpl) obj;
113                 if (relationship == null) {
114                         if (other.relationship != null) {
115                                 return false;
116                         }
117                 } else if (!relationshipEquals(relationship, other.relationship)) {
118                         return false;
119                 }
120
121                 if (relatedRelationship == null) {
122                         if (other.relatedRelationship != null) {
123                                 return false;
124                         }
125                 } else if (!relationshipEquals(relatedRelationship, other.relatedRelationship)) {
126                         return false;
127                 }
128
129                 return true;
130         }
131
132         /**
133          * We use this utility function because Relationship.equals() fails in the the generated SWIG C++ code with a
134          * NullPointerException.
135          */
136         public static boolean relationshipEquals(final Relationship a, final Relationship b) {
137                 if (a == null) {
138                         if (b == null) {
139                                 return true;
140                         } else {
141                                 return false;
142                         }
143                 }
144
145                 if (b == null) {
146                         return false;
147                 }
148
149                 final String aName = a.getName();
150                 final String bName = b.getName();
151
152                 if (!StringUtils.equals(aName, bName)) { // TODO: And the rest.
153                         return false;
154                 }
155
156                 return true;
157         }
158
159         /*
160          * (non-Javadoc)
161          * 
162          * @see org.glom.web.shared.libglom.layout.UsesRelationship#get_table_used(java.lang.String)
163          */
164         @Override
165         public String getTableUsed(final String parentTableName) {
166                 String result = "";
167
168                 if (relatedRelationship != null) {
169                         result = relatedRelationship.getToTable();
170                 }
171
172                 if (StringUtils.isEmpty(result) && (relationship != null)) {
173                         result = relationship.getToTable();
174                 }
175
176                 if (StringUtils.isEmpty(result)) {
177                         result = parentTableName;
178                 }
179
180                 return result;
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see org.glom.web.shared.libglom.layout.UsesRelationship#get_sql_table_or_join_alias_name(java.lang.String)
187          */
188         @Override
189         public String getSqlTableOrJoinAliasName(final String parent_table) {
190                 if (getHasRelationshipName() || getHasRelatedRelationshipName()) {
191                         final String result = getSqlJoinAliasName();
192                         if (StringUtils.isEmpty(result)) {
193                                 // Non-linked-fields relationship:
194                                 return getTableUsed(parent_table);
195                         } else
196                                 return result;
197                 } else
198                         return parent_table;
199         }
200
201         /*
202          * (non-Javadoc)
203          * 
204          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getRelationshipNameUsed()
205          */
206         @Override
207         public String getRelationshipNameUsed() {
208                 if (relatedRelationship != null) {
209                         return relatedRelationship.getName();
210                 } else if (relationship != null) {
211                         return relationship.getName();
212                 } else {
213                         return "";
214                 }
215         }
216 }