KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > listdata > data > DataCondition


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DataCondition.java,v 1.13 2007/01/07 06:14:50 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.patterns.listdata.data;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Class representing logical condition which data should match.
31  *
32  * @version $Id: DataCondition.java,v 1.13 2007/01/07 06:14:50 bastafidli Exp $
33  * @author Peter Satury
34  * @code.reviewer Miro Halas
35  * @code.reviewed 1.10 2006/05/26 07:30:24 jlegeny
36  */

37 public class DataCondition implements Serializable JavaDoc
38 {
39    // Constants ////////////////////////////////////////////////////////////////
40

41    /**
42     * Constant for no attribute set in the data condition.
43     */

44    public static final int NO_ATTRIBUTE = 0;
45    
46    /**
47     * Code for no operand set in the data condition.
48     */

49    public static final int NO_OPERAND = 0;
50    
51    /**
52     * = (equals) operand code
53     */

54    public static final int OPERAND_EQUALS = 1;
55    
56    /**
57     * IN operand code
58     * Operand can be array of objects (Object[]) or String or Collection of Objects.
59     * This allows to for example do things like this ID: in (1,2,3,4) when
60     * Integer[] = [1,2,3,4,5] is passed in or when String "1,2,3,4,5" is passed in.
61     * But it is also possible to do ID in (SELECT ID FROM xxx WHERE yyy) if
62     * String SELECT ID FROM xxx WHERE yyy is passed in.
63     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
64     */

65    public static final int OPERAND_IN = 2;
66    
67    /**
68     * NOT IN operand code
69     * Operand can be array of objects (Object[]) or String or Collection of Objects.
70     * This allows to for example do things like this: ID not in (1,2,3,4) when
71     * Integer[] = [1,2,3,4,5] is passed in or when String "1,2,3,4,5" is passed in.
72     * But it is also possible to do: ID not in (SELECT ID FROM xxx WHERE yyy)
73     * if String SELECT ID FROM xxx WHERE yyy is passed in.
74     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
75     */

76    public static final int OPERAND_NOT_IN = 3;
77
78    /**
79     * <> (not equals) operand code
80     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
81     */

82    public static final int OPERAND_NOT_EQUALS = 4;
83    
84    /**
85     * > (Greater than) operand code
86     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
87     */

88    public static final int OPERAND_GREATER = 5;
89
90    /**
91     * >= (Greater or equals than) operand code
92     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
93     */

94    public static final int OPERAND_GREATER_EQUALS = 6;
95
96    /**
97     * < (Less than) operand code
98     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
99     */

100    public static final int OPERAND_LESS = 7;
101
102    /**
103     * <= (Less or equals than) operand code
104     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
105     */

106    public static final int OPERAND_LESS_EQUALS = 8;
107
108    /**
109     * Case unsensitive like %str%
110     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
111     */

112    public static final int OPERAND_LIKE_CASEUNSENSITIVE = 9;
113
114    /**
115     * Case sensitive like %str%
116     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
117     */

118    public static final int OPERAND_LIKE_CASESENSITIVE = 10;
119
120    /**
121     * Sql query operand
122     * This operand is used if special query part have to be in the final query
123     * In this case just create data condition of this type and set what ever
124     * attribute you want except NO_ATTRIBUTE, value will be String representing
125     * query value type will be VALUE_TYPE_STRING.
126     * In that case this query will be added to the final query as
127     * ... AND MY_QUERY AND ...
128     * This way you can do very special queries.
129     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
130     */

131    public static final int OPERAND_SQL_QUERY = 11;
132
133    /**
134     * Sql query operand
135     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
136     */

137    public static final int OPERAND_EQUALS_OR_NULL = 12;
138    
139    /**
140     * case unsensitive not like %str%
141     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
142     */

143    public static final int OPERAND_NOT_LIKE_CASEUNSENSITIVE = 13;
144    
145    /**
146     * Case sensitive not like %str%
147     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
148     */

149    public static final int OPERAND_NOT_LIKE_CASESENSITIVE = 14;
150    
151    /**
152     * Case unsensitive like str%
153     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
154     */

155    public static final int OPERAND_STARTS_CASEUNSENSITIVE = 15;
156
157    /**
158     * Case sensitive like str%
159     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
160     */

161    public static final int OPERAND_STARTS_CASESENSITIVE = 16;
162    
163    /**
164     * Case unsensitive not like str%
165     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
166     */

167    public static final int OPERAND_NOT_STARTS_CASEUNSENSITIVE = 17;
168    
169    /**
170     * Case sensitive not like str%
171     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
172     */

173    public static final int OPERAND_NOT_STARTS_CASESENSITIVE = 18;
174    
175    /**
176     * Case unsensitive like %str
177     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
178     */

179    public static final int OPERAND_ENDS_CASEUNSENSITIVE = 19;
180    
181    /**
182     * Case sensitive like %str
183     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
184     */

185    public static final int OPERAND_ENDS_CASESENSITIVE = 20;
186    
187    /**
188     * Case unsensitive not like %str
189     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
190     */

191    public static final int OPERAND_NOT_ENDS_CASEUNSENSITIVE = 21;
192    
193    /**
194     * Case sensitive not like %str
195     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
196     */

197    public static final int OPERAND_NOT_ENDS_CASESENSITIVE = 22;
198    
199    /**
200     * Case unsensitive like str
201     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
202     */

203    public static final int OPERAND_EQUALS_CASEUNSENSITIVE = 23;
204    
205    /**
206     * Case sensitive like str
207     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
208     */

209    public static final int OPERAND_EQUALS_CASESENSITIVE = 24;
210
211    /**
212     * Case unsensitive not like str
213     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
214     */

215    public static final int OPERAND_NOT_EQUALS_CASEUNSENSITIVE = 25;
216    
217    /**
218     * Case sensitive not like str
219     * The value has to match index of value if ListDatabaseSchema.OPERANDS.
220     */

221    public static final int OPERAND_NOT_EQUALS_CASESENSITIVE = 26;
222
223    /**
224     * code for YES flag
225     */

226    public static final int DATA_CODE_FLAG_YES = 1;
227    
228    /**
229     * Object code for YES flag
230     */

231    public static final Integer JavaDoc DATA_CODE_FLAG_YES_OBJ = new Integer JavaDoc(DATA_CODE_FLAG_YES);
232       
233    /**
234     * code for NO flag
235     */

236    public static final int DATA_CODE_FLAG_NO = 0;
237    
238    /**
239     * Object code for NO flag
240     */

241    public static final Integer JavaDoc DATA_CODE_FLAG_NO_OBJ = new Integer JavaDoc(DATA_CODE_FLAG_NO);
242
243    // TODO: Everyone: In the future if we will decide to add next datatypes, we
244
// have to add these new datatypes also into the FILTER related code.
245

246    /**
247     * Unknown value type constant.
248     */

249    public static final int VALUE_TYPE_UNKNOWN = 0;
250    
251    /**
252     * Value type constant for ID.
253     */

254    public static final int VALUE_TYPE_ID = 1;
255
256    /**
257     * Value type constant for Boolean.
258     */

259    public static final int VALUE_TYPE_BOOLEAN = 2;
260
261    /**
262     * Value type constant for Integer.
263     */

264    public static final int VALUE_TYPE_INTEGER = 3;
265    
266    /**
267     * Value type constant for Double.
268     */

269    public static final int VALUE_TYPE_DOUBLE = 4;
270    
271    /**
272     * Value type constant for String.
273     */

274    public static final int VALUE_TYPE_STRING = 5;
275    
276    /**
277     * Value type constant for Timestamp.
278     */

279    public static final int VALUE_TYPE_TIMESTAMP = 6;
280
281    // Cached values ////////////////////////////////////////////////////////////
282

283    /**
284     * hash table with yes - no mapping
285     */

286    private static Map JavaDoc s_mapYesNo = new HashMap JavaDoc(4);
287
288    // Attributes ///////////////////////////////////////////////////////////////
289

290    /**
291     * Generated serial version uid
292     */

293    private static final long serialVersionUID = 8522583231285548064L;
294
295    /**
296     * Constant for logical attribute of a data object, which has relation to
297     * some value.
298     */

299    protected int m_iAttribute;
300    
301    /**
302     * Operation, constant for the operation between the data object attribute
303     * the value of the specified attribute.
304     */

305    protected int m_iOperation;
306    
307    /**
308     * Value which applies to operation for given attribute.
309     */

310    protected Object JavaDoc m_objValue;
311    
312    /**
313     * Type of the value. One of VALUE_TYPE_XXX constants
314     */

315    protected int m_iValueType;
316    
317    /**
318     * Original value which applies to operation for given attribute.
319     * This attribute has to be set up because some operations can modify value
320     * and original value will be lost. Therefore we need to store this original
321     * value within the extra parameter.
322     */

323    protected Object JavaDoc m_objOriginalValue;
324
325    // Constructors /////////////////////////////////////////////////////////////
326

327    static
328    {
329       s_mapYesNo.put("YES", new Integer JavaDoc(1));
330       s_mapYesNo.put("TRUE", new Integer JavaDoc(1));
331       s_mapYesNo.put("NO", new Integer JavaDoc(0));
332       s_mapYesNo.put("FALSE", new Integer JavaDoc(0));
333    }
334
335    /**
336     * Default empty constructor
337     */

338    public DataCondition()
339    {
340       this(DataCondition.NO_ATTRIBUTE, DataCondition.NO_OPERAND,
341            null, VALUE_TYPE_STRING);
342    }
343    
344    /**
345     * Full constructor
346     *
347     * @param attribute - attribute - the code for the attribute on which the
348     * operation should be performed, see the
349     * OPERAND_XXX constants
350     * @param operation - operation - what operation should be performed
351     * between the attribute and the value
352     * @param value - value - any data object which is compared or which operate
353     * on the attribute in some fashion (e.g. attribute
354     * "count", operation "greater than", value "5"
355     * @param valueType - type of the value (see constants VALUE_TYPE_XXX),
356     * this is used when inserting value to the SQL query
357     * to put it there in correct format
358     */

359    public DataCondition(
360       int attribute,
361       int operation,
362       Object JavaDoc value,
363       int valueType
364    )
365    {
366       this(attribute, operation, value, valueType, value);
367    }
368
369    /**
370     * Copy constructor
371     *
372     * @param attribute - attribute - the code for the attribute on which the
373     * operation should be performed, see the
374     * OPERAND_XXX constants
375     * @param operation - operation - what operation should be performed
376     * between the attribute and the value
377     * @param value - value - any data object which is compared or which operate
378     * on the attribute in some fashion (e.g. attribute
379     * "count", operation "greater than", value "5"
380     * @param valueType - type of the value (see constants VALUE_TYPE_XXX),
381     * this is used when inserting value to the SQL query
382     * to put it there in correct format
383     * @param originalValue - original value
384     */

385    public DataCondition(
386       int attribute,
387       int operation,
388       Object JavaDoc value,
389       int valueType,
390       Object JavaDoc originalValue
391    )
392    {
393       m_iAttribute = attribute;
394       m_iOperation = operation;
395       m_objValue = value;
396       m_iValueType = valueType;
397       m_objOriginalValue = originalValue;
398    }
399
400    /**
401     * @return int - attribute
402     */

403    public int getAttribute()
404    {
405       return m_iAttribute;
406    }
407
408    /**
409     * @return int - operation
410     */

411    public int getOperation()
412    {
413       return m_iOperation;
414    }
415
416    /**
417     * @return Object - value
418     */

419    public Object JavaDoc getValue()
420    {
421       return m_objValue;
422    }
423    
424    /**
425     * @param objValue - new object value
426     */

427    public void setValue(
428       Object JavaDoc objValue
429    )
430    {
431       m_objValue = objValue;
432    }
433
434    /**
435     * @return int - code of value type
436     */

437    public int getValueType()
438    {
439       return m_iValueType;
440    }
441
442    /**
443     * @return Object - originalValue
444     */

445    public Object JavaDoc getOriginalValue()
446    {
447       return m_objOriginalValue;
448    }
449    
450    /**
451     * @param objOriginalValue - new object original value
452     */

453    public void setOriginalValue(
454       Object JavaDoc objOriginalValue
455    )
456    {
457       m_objOriginalValue = objOriginalValue;
458    }
459    
460    /**
461     * @return String - representation of value
462     */

463    public String JavaDoc getValueString()
464    {
465       StringBuffer JavaDoc sbReturn = new StringBuffer JavaDoc();
466       if (m_objValue != null)
467       {
468          
469          if (m_iOperation != DataCondition.OPERAND_IN
470             && m_iOperation != DataCondition.OPERAND_NOT_IN)
471          {
472             // parse data condition simple value
473
switch (m_iValueType)
474             {
475                case (DataCondition.VALUE_TYPE_ID) :
476                {
477                   sbReturn.append(((Integer JavaDoc) m_objValue).intValue());
478                   break;
479                }
480                case (DataCondition.VALUE_TYPE_BOOLEAN) :
481                {
482                   sbReturn.append(((Boolean JavaDoc) m_objValue).booleanValue());
483                   break;
484                }
485                case (DataCondition.VALUE_TYPE_INTEGER) :
486                {
487                   sbReturn.append(((Integer JavaDoc) m_objValue).intValue());
488                   break;
489                }
490                case (DataCondition.VALUE_TYPE_DOUBLE) :
491                {
492                   sbReturn.append(((Double JavaDoc) m_objValue).doubleValue());
493                   break;
494                }
495                case (DataCondition.VALUE_TYPE_STRING) :
496                {
497                   sbReturn.append(m_objValue);
498                   break;
499                }
500                case (DataCondition.VALUE_TYPE_TIMESTAMP) :
501                {
502                   sbReturn.append(((Timestamp JavaDoc) m_objValue).getTime());
503                   break;
504                }
505                default :
506                {
507                   assert false : "Not supported data condition value type.";
508                }
509             }
510          }
511          else
512          {
513             // parse data condition multiple value (IN or NOT IN operands)
514
Object JavaDoc[] arrObject = (Object JavaDoc[]) m_objValue;
515             Object JavaDoc oHelp;
516             for (int iCounter = 0; iCounter < arrObject.length; iCounter++)
517             {
518                oHelp = arrObject[iCounter];
519                if (sbReturn.length() > 0)
520                {
521                   sbReturn.append(",");
522                }
523                switch (m_iValueType)
524                {
525                   case (DataCondition.VALUE_TYPE_ID) :
526                   {
527                      sbReturn.append(((Integer JavaDoc) oHelp).intValue());
528                      break;
529                   }
530                   case (DataCondition.VALUE_TYPE_BOOLEAN) :
531                   {
532                      sbReturn.append(((Boolean JavaDoc) oHelp).booleanValue());
533                      break;
534                   }
535                   case (DataCondition.VALUE_TYPE_INTEGER) :
536                   {
537                      sbReturn.append(((Integer JavaDoc) oHelp).intValue());
538                      break;
539                   }
540                   case (DataCondition.VALUE_TYPE_DOUBLE) :
541                   {
542                      sbReturn.append(((Double JavaDoc) oHelp).doubleValue());
543                      break;
544                   }
545                   case (DataCondition.VALUE_TYPE_STRING) :
546                   {
547                      sbReturn.append(oHelp);
548                      break;
549                   }
550                   case (DataCondition.VALUE_TYPE_TIMESTAMP) :
551                   {
552                      sbReturn.append(((Timestamp JavaDoc) oHelp).getTime());
553                      break;
554                   }
555                   default :
556                   {
557                      assert false : "Not supported data condition value type.";
558                   }
559                }
560             }
561          }
562       }
563       return sbReturn.toString();
564    }
565    
566    /**
567     * @return String - representation of original value
568     */

569    public String JavaDoc getOriginalValueString()
570    {
571       StringBuffer JavaDoc sbReturn = new StringBuffer JavaDoc();
572       if (m_objOriginalValue != null)
573       {
574          
575          if (m_iOperation != DataCondition.OPERAND_IN
576             && m_iOperation != DataCondition.OPERAND_NOT_IN)
577          {
578             // parse data condition simple value
579
switch (m_iValueType)
580             {
581                case (DataCondition.VALUE_TYPE_ID) :
582                {
583                   sbReturn.append(((Integer JavaDoc) m_objOriginalValue).intValue());
584                   break;
585                }
586                case (DataCondition.VALUE_TYPE_BOOLEAN) :
587                {
588                   sbReturn.append(((Boolean JavaDoc) m_objOriginalValue).booleanValue());
589                   break;
590                }
591                case (DataCondition.VALUE_TYPE_INTEGER) :
592                {
593                   sbReturn.append(((Integer JavaDoc) m_objOriginalValue).intValue());
594                   break;
595                }
596                case (DataCondition.VALUE_TYPE_DOUBLE) :
597                {
598                   sbReturn.append(((Double JavaDoc) m_objOriginalValue).doubleValue());
599                   break;
600                }
601                case (DataCondition.VALUE_TYPE_STRING) :
602                {
603                   sbReturn.append(m_objOriginalValue);
604                   break;
605                }
606                case (DataCondition.VALUE_TYPE_TIMESTAMP) :
607                {
608                   sbReturn.append(((Timestamp JavaDoc) m_objOriginalValue).getTime());
609                   break;
610                }
611                default :
612                {
613                   assert false : "Not supported data condition value type.";
614                }
615             }
616          }
617          else
618          {
619             // parse data condition multiple value (IN or NOT IN operands)
620
Object JavaDoc[] arrObject = (Object JavaDoc[]) m_objOriginalValue;
621             Object JavaDoc oHelp;
622             for (int iCounter = 0; iCounter < arrObject.length; iCounter++)
623             {
624                oHelp = arrObject[iCounter];
625                if (sbReturn.length() > 0)
626                {
627                   sbReturn.append(",");
628                }
629                switch (m_iValueType)
630                {
631                   case (DataCondition.VALUE_TYPE_ID) :
632                   {
633                      sbReturn.append(((Integer JavaDoc) oHelp).intValue());
634                      break;
635                   }
636                   case (DataCondition.VALUE_TYPE_BOOLEAN) :
637                   {
638                      sbReturn.append(((Boolean JavaDoc) oHelp).booleanValue());
639                      break;
640                   }
641                   case (DataCondition.VALUE_TYPE_INTEGER) :
642                   {
643                      sbReturn.append(((Integer JavaDoc) oHelp).intValue());
644                      break;
645                   }
646                   case (DataCondition.VALUE_TYPE_DOUBLE) :
647                   {
648                      sbReturn.append(((Double JavaDoc) oHelp).doubleValue());
649                      break;
650                   }
651                   case (DataCondition.VALUE_TYPE_STRING) :
652                   {
653                      sbReturn.append(oHelp);
654                      break;
655                   }
656                   case (DataCondition.VALUE_TYPE_TIMESTAMP) :
657                   {
658                      sbReturn.append(((Timestamp JavaDoc) oHelp).getTime());
659                      break;
660                   }
661                   default :
662                   {
663                      assert false : "Not supported data condition value type.";
664                   }
665                }
666             }
667          }
668       }
669       return sbReturn.toString();
670    }
671
672    /**
673     * @return Map - map yes, no, true, false strings into int values
674     */

675    public static Map JavaDoc getYesNoMap(
676    )
677    {
678       return s_mapYesNo;
679    }
680 }
681
Popular Tags