KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > FilterClause


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
//
15
//
16
// 27.05.2002 NK added in Jahia
17

18
19 package org.jahia.data.containers;
20
21 import java.io.Serializable JavaDoc;
22
23
24
25 /**
26  * Hold information about a filtering clause.
27  *
28  * @see ContainerFilterBean
29  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
30  */

31 class FilterClause implements Serializable JavaDoc {
32
33     private static final String JavaDoc CLASS_NAME = FilterClause.class.getName();
34
35     /** The lower comparator or simple comparator **/
36     private String JavaDoc lowerComp;
37     /** The upper comparator **/
38     private String JavaDoc upperComp;
39     /** The array of values **/
40     private String JavaDoc[] values;
41     /** The valid state of this clause instance **/
42     private boolean isValid = false;
43     /** If this clause a range clause ( with two comparators ) **/
44     private boolean isRangeClause = false;
45
46     //--------------------------------------------------------------------------
47
/**
48      * Constructor for a simple comparison clause with a single value.
49      *
50      * @param String comparator, the comparator
51      * @param String value, the value
52      */

53     public FilterClause(String JavaDoc comp, String JavaDoc value){
54         if ( value != null && comp != null )
55         {
56             String JavaDoc[] values = {value};
57             this.values = values;
58             this.lowerComp = comp;
59             this.isValid = true;
60         }
61     }
62
63     //--------------------------------------------------------------------------
64
/**
65      * Constructor for a simple comparison clause with a multiple values.
66      *
67      * @param String comparator, the comparator
68      * @param String[] values, the values
69      */

70     public FilterClause(String JavaDoc comp, String JavaDoc[] values){
71         if ( (values != null) && (values.length>0)&& (comp != null) )
72         {
73             this.values = values;
74             this.lowerComp = comp;
75             this.isValid = true;
76         }
77     }
78
79     //--------------------------------------------------------------------------
80
/**
81      * Constructor for a range comparison clause.
82      *
83      * @param String lowerComp, the lower comparator
84      * @param String upperComp, the upper comparator
85      * @param String lowerValue, the lower value
86      * @param String upperValue, the upper value
87      */

88     public FilterClause( String JavaDoc lowerComp,
89                          String JavaDoc upperComp,
90                          String JavaDoc lowerVal,
91                          String JavaDoc upperVal ){
92
93         if ( (lowerVal != null)
94              && (upperVal != null)
95              && (lowerComp != null)
96              && (upperComp != null) )
97         {
98             String JavaDoc[] values = {lowerVal,upperVal};
99             this.values = values;
100             this.lowerComp = lowerComp;
101             this.upperComp = upperComp;
102             this.isValid = true;
103             this.isRangeClause = true;
104         }
105     }
106
107     /**
108      * Return true if this clause is valid.
109      */

110     public boolean isValid(){
111         return this.isValid;
112     }
113
114     /**
115      * Return true if this clause is a range clause ( with two comparators )
116      */

117     public boolean isRangeClause(){
118         return this.isRangeClause;
119     }
120
121     /**
122      * Return the values.
123      */

124     public String JavaDoc[] getValues(){
125         return this.values;
126     }
127
128     /**
129      * Return the single value (= the first value [0] in the values array).
130      */

131     public String JavaDoc getValue(){
132         return this.values[0];
133     }
134
135     /**
136      * return the lower value (= the first value [0] in the values array).
137      */

138     public String JavaDoc getLowerValue(){
139         return this.values[0];
140     }
141
142     /**
143      * return the upper value (= the second value [1] in the values array).
144      */

145     public String JavaDoc getUpperValue(){
146         return this.values[1];
147     }
148
149     /**
150      * return the comparator for a simple clause.
151      */

152     public String JavaDoc getComp(){
153         return this.lowerComp;
154     }
155
156     /**
157      * return the lower comparator for a range clause.
158      */

159     public String JavaDoc getLowerComp(){
160         return this.lowerComp;
161     }
162
163     /**
164      * return the upper comparator for a range clause.
165      */

166     public String JavaDoc getUpperComp(){
167         return this.upperComp;
168     }
169
170     //--------------------------------------------------------------------------
171
/**
172      * Return true if the given Long value match this clause
173      * Here, the clause's values are converted to long and a number comparison is performed
174      *
175      * @param value
176      * @param numberFormat , @see NumberFormats
177      * @return boolean , the comparison result.
178      */

179     public boolean compareNumber(String JavaDoc value, String JavaDoc numberFormat){
180         if ( !isValid ){
181             return false;
182         }
183
184         boolean result = false;
185
186         try {
187             String JavaDoc comp = getComp();
188             String JavaDoc v = null;
189             
190             // SCSE: check for IS_NULL and NOT_NULL clauses
191
if ( isNullOrNotNullClause() ){
192               result = compareNullOrNotNull(value);
193             } else if ( isRangeClause() ) {
194                 v = getValue();
195                 result = compare(value,v,comp,numberFormat);
196                 if ( result )
197                 {
198                     v = getUpperValue();
199                     result = compare(value,v,getUpperComp(),numberFormat);
200                 }
201             } else {
202                 String JavaDoc[] vals = this.getValues();
203                 int size = vals.length;
204                 for ( int i=0; i<size; i++ ){
205                     v = vals[i];
206                     result = compare(value,v,comp,numberFormat);
207                     if ( result ){
208                         return true;
209                     }
210                 }
211             }
212         } catch ( Throwable JavaDoc t ){
213             t.printStackTrace();
214         }
215         return result;
216     }
217
218     //--------------------------------------------------------------------------
219
/**
220      *
221      * @param value
222      * @return boolean , the comparison result.
223      */

224     public boolean compare(String JavaDoc value){
225         if ( !isValid || value == null ){
226             return false;
227         }
228
229         boolean result = false;
230
231         String JavaDoc[] vals = this.getValues();
232         int size = vals.length;
233         for ( int i=0; i<size; i++ ){
234             String JavaDoc val = vals[i];
235             if ( ContainerFilterBean.COMP_EQUAL.equals(this.getComp()) ){
236                 if ( value.equals(val) ){
237                     return true;
238                 }
239             } else if ( ContainerFilterBean.COMP_NOT_EQUAL.equals(this.getComp())) {
240                 if ( !value.equals(val) ){
241                     return true;
242                 }
243             } else if ( ContainerFilterBean.COMP_ISNULL.equals(this.getComp())) {
244                 if ( value == null || "".equals(value.trim()) ){
245                     return true;
246                 }
247             } else if ( ContainerFilterBean.COMP_NOTNULL.equals(this.getComp())) {
248                 if ( value != null && !"".equals(value.trim()) ){
249                     return true;
250                 }
251             } else if ( ContainerFilterBean.COMP_STARTS_WITH.equals(this.getComp())) {
252               if ( val.toLowerCase().startsWith(value.toLowerCase())){
253                   return true;
254               }
255             }
256         }
257         return result;
258     }
259
260     private boolean compareNullOrNotNull(String JavaDoc value)
261     {
262       if (!isValid)
263       {
264         return false;
265       }
266   
267       if (ContainerFilterBean.COMP_ISNULL.equals(this.getComp()))
268       {
269         return value == null || value.trim().length() == 0;
270       }
271       else if (ContainerFilterBean.COMP_NOTNULL.equals(this.getComp()))
272       {
273         return value != null && value.trim().length() > 0;
274       }
275       
276       return false;
277     }
278
279     // --------------------------------------------------------------------------
280
/**
281      * Return true if the given long valueA (left) match the given valueB (right)
282      *
283      * @param valueA
284      * @param valueB
285      * @param comp the comparator
286      * @return boolean the comparison result.
287      */

288     private boolean compare(String JavaDoc valueA, String JavaDoc valueB, String JavaDoc comp, String JavaDoc format){
289         boolean result = false;
290         int compResult = 0;
291         if ( comp.equals(ContainerFilterBean.COMP_EQUAL) ){
292             result = ( NumberFormats.compareNumber(valueA,valueB, format) == 0 );
293         } else if ( comp.equals(ContainerFilterBean.COMP_SMALLER) ){
294             result = ( NumberFormats.compareNumber(valueA,valueB, format) == -1 );
295         } else if ( comp.equals(ContainerFilterBean.COMP_SMALLER_OR_EQUAL) ){
296             compResult = NumberFormats.compareNumber(valueA,valueB, format);
297             result = ( compResult == 0 || compResult == -1 );
298         } else if ( comp.equals(ContainerFilterBean.COMP_BIGGER_OR_EQUAL) ){
299             compResult = NumberFormats.compareNumber(valueA,valueB, format);
300             result = ( compResult == 0 || compResult == 1 );
301         } else if ( comp.equals(ContainerFilterBean.COMP_BIGGER) ){
302             compResult = NumberFormats.compareNumber(valueA,valueB, format);
303             result = ( compResult == 1 );
304         } else if ( comp.equals(ContainerFilterBean.COMP_NOT_EQUAL) ){
305             if ( valueA == null && valueB == null ){
306                 return true;
307             } else {
308                 try {
309                     result = valueA.equals(valueB) ;
310                 } catch ( Throwable JavaDoc t ){
311                 }
312             }
313         }
314         //JahiaConsole.println(CLASS_NAME+".compareNumber","valueA["+ valueA +"] " + comp + " valueB[" + valueB + "] result=" + result);
315

316         return result;
317     }
318
319     /**
320      * Return <code>true</code> if this clause is COMP_NOTNULL or COMP_ISNULL clause.
321      * @return <code>true</code> if this clause is COMP_NOTNULL or COMP_ISNULL clause
322      */

323     public boolean isNullOrNotNullClause()
324     {
325       return ContainerFilterBean.COMP_ISNULL.equals(getComp())
326         || ContainerFilterBean.COMP_NOTNULL.equals(getComp());
327     }
328
329 }
330
Popular Tags