1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.core;
20
21 /*** The RelationSelector represents a binary relation between two data values.
22 *
23 * @see RelationType
24 *
25 * @author Dennis Zikovic
26 * @version 1.00
27 *
28 *//*
29 *
30 * WHEN WHO WHY & WHAT
31 * ------------------------------------------------------------------------------
32 * 2001-07-04 Dennis Zikovic Creation
33 */
34 public class RelationQualifier extends Qualifier
35 {
36
37 private IFieldDescriptor mFieldDescriptor;
38 private RelationType mRelationType;
39 private Object mRelationValue;
40
41 private String mEntityIdentity = null;
42 private String mFieldValueEntityIdentity = null;
43
44
45
46 /*** Creates a new RelationQualifier with realtion type set to EQUAL.
47 * Note that the relationValue can be a IFieldDescriptor.
48 *
49 * @param fieldDescriptor The field descriptor to be qualified.
50 * @param relationValue The value to compare the field with.
51 */
52 public RelationQualifier(IFieldDescriptor fieldDescriptor, Object relationValue)
53 {
54 this(fieldDescriptor, RelationType.EQUAL, relationValue);
55 }
56
57 /*** Creates a new RelationQualifier. Note that the relationValue
58 * can be a IFieldDescriptor.
59 *
60 * @param fieldDescriptor The field descriptor to be qualified.
61 * @param relationType The relation type to be used.
62 * @param relationValue The value to compare the field with.
63 */
64 public RelationQualifier(IFieldDescriptor fieldDescriptor, RelationType relationType, Object relationValue)
65 {
66 mFieldDescriptor = fieldDescriptor;
67 mRelationType = relationType;
68 mRelationValue = relationValue;
69 }
70
71
72
73 /*** This method returns true if the qualifier does select the
74 * provided entity object.
75 */
76 public boolean doesQualify(IEntity entity)
77 {
78 if(!this.canDirectlyQualify(entity.getEntityDescriptor()))
79 return false;
80 try
81 {
82 Object value = mRelationValue;
83 if(value instanceof IFieldDescriptor)
84 value = entity.getData((IFieldDescriptor)mRelationValue);
85 return mRelationType.compare(entity.getData(mFieldDescriptor), value);
86 }
87 catch(org.caleigo.core.exception.InvalidFieldException e)
88 {
89 return false;
90 }
91 }
92
93 /*** This method returns true if the qualifier can select entities
94 * of the type defined by the provided entity descriptor.
95 */
96 public boolean canQualify(IEntityDescriptor entityDescriptor)
97 {
98
99 boolean canSelect = entityDescriptor.contains(mFieldDescriptor);
100
101
102 if(!canSelect && mRelationValue instanceof IFieldDescriptor)
103 canSelect = entityDescriptor.contains((IFieldDescriptor)mRelationValue);
104
105 return canSelect;
106 }
107
108 /*** This abstract method must return true if the qualifier can select
109 * entities of the type defined by the provided entity descriptor without
110 * the nead of any complementary data. No other information than can be
111 * accessed through the provided entity descriptor should be neaded if
112 * this method returns true. This validates that the doesQualify method
113 * can be called for the called qualifier.
114 */
115 public boolean canDirectlyQualify(IEntityDescriptor entityDescriptor)
116 {
117
118 boolean canSelect = entityDescriptor.contains(mFieldDescriptor);
119
120
121 for(int j=0; !canSelect && j<entityDescriptor.getFieldCount(); j++)
122 if(entityDescriptor.getFieldDescriptor(j).isReferenceField())
123 canSelect = mFieldDescriptor==entityDescriptor.getFieldDescriptor(j).getReferenceFieldRelation().getTargetField();
124
125
126 if(canSelect && mRelationValue instanceof IFieldDescriptor)
127 canSelect = entityDescriptor.contains((IFieldDescriptor)mRelationValue);
128
129 return canSelect;
130 }
131
132 /*** This method returns true if the qualifier can uniquely select
133 * entities of the type defined by the provided entity descriptor.
134 * If that is the case then the qualifier is an identity qualifier and can
135 * never qualify more then a single entity instance of the specified type.
136 */
137 public boolean canUniquelyQualify(IEntityDescriptor entityDescriptor)
138 {
139 if(mRelationType!=RelationType.EQUAL)
140 return false;
141
142 IFieldDescriptor[] fields = Relations.getIdentityFields(entityDescriptor);
143
144 if(fields==null || fields.length>1)
145 return false;
146 else if(fields[0]==mFieldDescriptor)
147 return true;
148 else if(mRelationValue instanceof IFieldDescriptor)
149 return entityDescriptor.contains((IFieldDescriptor)mRelationValue);
150 else
151 return false;
152 }
153
154 /*** The toString method returns an "abstract" expression of what the
155 * qualifier selects. Note that this expresion is only intended for visual
156 * aid and is not a valid SQL expresion.
157 */
158 public String toString()
159 {
160 return mFieldDescriptor+" "+mRelationType+" "+mFieldDescriptor.getDataType().makeLogString(mRelationValue);
161 }
162
163
164
165 /*** Returns true if the qualifier defines a field to field relation.
166 */
167 public boolean isFieldToFieldRelation()
168 {
169 return (mRelationValue instanceof IFieldDescriptor);
170 }
171
172 /*** Access method that returns the IFieldDescriptor that defines the data
173 * that are to be matched/related by the contained data in the qualifier.
174 */
175 public IFieldDescriptor getFieldDescriptor()
176 {
177 return mFieldDescriptor;
178 }
179
180 /*** Mutation method that updates the IFieldDescriptor that defines the data
181 * that are to be matched/related by the contained data in the qualifier.
182 */
183 public void setFieldDescriptor(IFieldDescriptor fieldDescriptor)
184 {
185 mFieldDescriptor = fieldDescriptor;
186 this.fireStructureChangedEvent();
187 }
188
189 /*** Access method that returns the contained RelationType object that
190 * defines how the mapped and contained data in the Qualifier should
191 * be compared.
192 */
193 public RelationType getRelationType()
194 {
195 return mRelationType;
196 }
197
198 /*** Mutation method that updates the contained RelationType object that
199 * defines how the mapped and contained data in the Qualifier should
200 * be compared.
201 */
202 public void setRelationType(RelationType relationType)
203 {
204 mRelationType = relationType;
205 this.fireStructureChangedEvent();
206 }
207
208 /*** Access method that returns the contained value that are used in relation
209 * with the field value which is adressed by the conained IFieldDescriptor.
210 * Note that this value may in turn be IFieldDescriptor that defines another
211 * field value in a targeted entity type.
212 */
213 public Object getRelationValue()
214 {
215 return mRelationValue;
216 }
217
218 /*** Mutation method that sets the contained value that are used in relation
219 * with the field value which is adressed by the conained IFieldDescriptor.
220 * Note that this value may in turn be IFieldDescriptor that defines another
221 * field value in a targeted entity type.
222 */
223 public void setRelationValue(Object relationValue)
224 {
225 mRelationValue = relationValue;
226 this.fireContentChangedEvent();
227 }
228
229 /*** Access method that returns the entity identity object that defines
230 * what entity instance the field in the Qualifier addresses and should
231 * be applied on. If no identity is set the default identity is returned.
232 */
233 public String getEntityIdentity()
234 {
235 if(mEntityIdentity==null)
236 return mFieldDescriptor.getEntityDescriptor().getCodeName();
237 else
238 return mEntityIdentity;
239 }
240
241 /*** Mutation method that sets the entity identity object that defines
242 * what entity instance the field in the Qualifier adresses and should
243 * be applied on. If no identity is set the default identity will be used.
244 */
245 public void setEntityIdentity(String entityIdentity)
246 {
247 mEntityIdentity = entityIdentity;
248 this.fireStructureChangedEvent();
249 }
250
251 /*** Access method that returns the entity identity object that defines
252 * what entity instance the field in the Qualifier addresses and should
253 * be applied on. If no identity is set the default identity is returned.
254 */
255 public String getFieldValueEntityIdentity()
256 {
257 if(mFieldValueEntityIdentity==null && this.isFieldToFieldRelation())
258 return ((IFieldDescriptor)mRelationValue).getEntityDescriptor().getCodeName();
259 else
260 return mFieldValueEntityIdentity;
261 }
262
263 /*** Mutation method that sets the entity identity object that defines
264 * what entity instance the field in the Qualifier adresses and should
265 * be applied on. If no identity is set the default identity will be used.
266 */
267 public void setFieldValueEntityIdentity(String entityIdentity)
268 {
269 mFieldValueEntityIdentity = entityIdentity;
270 this.fireStructureChangedEvent();
271 }
272 }