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