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