Some cleanup 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 +
86          * ((relationship == 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         /* (non-Javadoc)
159          * @see org.glom.web.shared.libglom.layout.UsesRelationship#get_table_used(java.lang.String)
160          */
161         @Override
162         public String get_table_used(String parentTableName) {
163                 String result = "";
164         
165                 if(relatedRelationship != null) {
166                         result = relatedRelationship.get_to_table();
167                 }
168                         
169                 if(StringUtils.isEmpty(result) && (relationship != null)) {
170                         result = relationship.get_to_table();
171                 }
172                 
173                 if(StringUtils.isEmpty(result)) {
174                         result = parentTableName;
175                 }
176                 
177                 return result;
178 }
179
180         /* (non-Javadoc)
181          * @see org.glom.web.shared.libglom.layout.UsesRelationship#get_sql_table_or_join_alias_name(java.lang.String)
182          */
183         @Override
184         public String get_sql_table_or_join_alias_name(String parent_table) {
185                 if(getHasRelationshipName() || getHasRelatedRelationshipName())
186                 {
187                         final String result = get_sql_join_alias_name();
188                         if(StringUtils.isEmpty(result)) {
189                                 //Non-linked-fields relationship:
190                                 return get_table_used(parent_table);
191                         }
192                         else
193                                 return result;
194                 }
195                 else
196                         return parent_table;
197         }
198         
199         @Override
200         public UsesRelationshipImpl clone() {
201                 UsesRelationshipImpl result;
202                 try {
203                         result = (UsesRelationshipImpl)super.clone();
204                 } catch (CloneNotSupportedException e) {
205                          System.err.println("UsesRelationshipImpl.clone() failed: " + e.getMessage());
206                          return null;
207                 }
208
209                 result.relationship = this.relationship.clone();
210                 result.relatedRelationship = this.relatedRelationship.clone();
211
212                 return result;
213         }
214
215         /* (non-Javadoc)
216          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getRelationshipNameUsed()
217          */
218         @Override
219         public String getRelationshipNameUsed() {
220                 if(relatedRelationship != null) {
221                         return relatedRelationship.get_name();
222                 } else if(relationship != null) {
223                         return relationship.get_name();
224                 } else {
225                         return "";
226                 }
227         }
228 }