KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > webapps > fichotheque > Query


1 package org.jahia.webapps.fichotheque;
2
3 import java.util.*;
4 import java.sql.*;
5
6
7 import org.jahia.tools.*;
8 import org.jahia.tools.db.*;
9
10
11 /**
12  * Represents the part of query that correspond to the search params
13  *
14  * @author Yann Gentil <a HREF="mailto:gentil@xo3.com">gentil@xo3.com</a>
15  * @version %I%, %G%
16
17  */

18 public class Query
19 {
20   private static org.apache.log4j.Logger logger =
21       org.apache.log4j.Logger.getLogger(Query.class);
22     /** True if the query is active,
23         then only the cards corresponding to the query are displayed **/

24     private boolean queryActive = false;
25     /** part of the "where" clause for the first search param **/
26     private String JavaDoc param1 = "";
27     /** part of the "where" clause for the second search param **/
28     private String JavaDoc param2 = "";
29
30
31
32     public Query()
33     {
34     }
35
36
37     /**
38      * Get if the query is active
39      *
40      * @return boolean
41      */

42      public boolean isQueryActive()
43      {
44         return this.queryActive;
45      }
46
47
48     /**
49      * Get first search param
50      *
51      * @return String param1
52      */

53     public String JavaDoc getParam1()
54     {
55         return this.param1;
56     }
57
58
59     /**
60      * Get second search param
61      *
62      * @return String param2
63      */

64     public String JavaDoc getParam2()
65     {
66         return this.param2;
67     }
68
69
70     /**
71      * set the queryActive flag.
72      *
73      * @param b boolean true or false
74      */

75      public void setqueryActive(boolean b)
76      {
77         this.queryActive = b;
78      }
79
80
81     /**
82      * set the query params
83      *
84      * @param param1Name name of the attribute in which the search must be done
85      * @param param1 the value that must be search
86      * @param param2Name name of the attribute in which the search must be done
87      * @param param2 the value that must be search
88      * @param card card that contains the names of the fields
89      */

90     public void setQuery(String JavaDoc param1Name, String JavaDoc param1, String JavaDoc param2Name,
91                          String JavaDoc param2, Card card)
92     {
93         String JavaDoc s = "";
94         Vector fieldsNames = card.getFieldsName();
95         int nb = fieldsNames.size();
96
97         // set the query Active
98
this.queryActive = true;
99
100         // set param1 and param2 of the query
101
// for exemple: Title LIKE '%Java%' OR Description LIKE '%Java%'
102
if (param1 == null || param1.equals(""))
103         {
104             this.param1 = "";
105         }
106         else
107         {
108             param1 = param1.toUpperCase();
109             if (param1Name.equals("All Attributes"))
110             {
111                 s = s + "Upper(" + fieldsNames.get(1) + ") LIKE '%" + param1 + "%'";
112
113                 for(int i = 2; i < nb; i++)
114                 {
115                     s = s + " OR UPPER(" + fieldsNames.get(i) + ") LIKE '%"
116                       + param1 + "%'";
117                 }
118                 this.param1 = s;
119             }
120             else
121             {
122                 this.param1 = "UPPER(" + param1Name + ")" + " LIKE '%" + param1 + "%'";
123             }
124         }
125
126         if (param2 == null || param2.equals(""))
127         {
128             this.param2 = "";
129         }
130         else
131         {
132             param2 = param2.toUpperCase();
133             if (param2Name.equals("All Attributes"))
134             {
135                 s = s + "UPPER(" + fieldsNames.get(1) + ") LIKE '%" + param2 + "%'";
136
137                 for(int i=2;i<nb;i++)
138                 {
139                     s = s + " OR UPPER(" + fieldsNames.get(i) + ") LIKE '%" + param2 + "%'";
140                 }
141                 this.param2 = s;
142             }
143             else
144             {
145                 this.param2 = "UPPER("+param2Name + ") LIKE '%" + param2 + "%'";
146             }
147         }
148         logger.debug("Query: setQuery param1: " + this.param1 + ", param2: " + this.param2);
149
150     }
151
152
153 }
154
Popular Tags