KickJava   Java API By Example, From Geeks To Geeks.

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


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
//
17
// 27.05.2002 NK added in Jahia
18

19
20 package org.jahia.data.containers;
21
22 import java.io.Serializable JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.BitSet JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.jahia.exceptions.JahiaException;
33 import org.jahia.params.ParamBean;
34 import org.jahia.registries.ServicesRegistry;
35 import org.jahia.services.version.EntryLoadRequest;
36 import org.jahia.utils.JahiaConsole;
37 import org.jahia.utils.JahiaTools;
38
39
40 /**
41  * Jahia Standard Containers Filtering Handler.
42  *
43  * There are two main filtering modes:
44  * 1) Filtering on a containers of a unique container list
45  * 2) Filtering on all containers of a site or of all sites
46  *
47  * @see ContainerFilterInterface
48  * @see JahiaContainerSet
49  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
50  */

51
52 public class ContainerFilters implements Serializable JavaDoc {
53
54     private static org.apache.log4j.Logger logger =
55         org.apache.log4j.Logger.getLogger(ContainerFilters.class);
56
57     private static final String JavaDoc CLASS_NAME = ContainerFilters.class.getName();
58
59     private int ctnListID = -1;
60
61     private int siteId = -1;
62
63     private String JavaDoc containerDefinitionName;
64
65     private boolean siteModeFiltering = false;
66
67     private BitSet JavaDoc bits;
68
69     private Vector JavaDoc containerFilters;
70
71     private String JavaDoc query;
72
73     private boolean isValid = false;
74
75     private boolean updated = false;
76
77     private long lastFilteringTime = -1;
78
79     private ArrayList JavaDoc stagingFields = null;
80
81     //--------------------------------------------------------------------------
82
/**
83      * Constructor
84      *
85      * Filtering on one unique container list
86      *
87      * @param int ctnListID, the container list id.
88      * @param Vector containerFilters, a vector of ContainerFilterInterface objects.
89      */

90     public ContainerFilters(int ctnListID, Vector JavaDoc containerFilters)
91     throws JahiaException
92     {
93         this.ctnListID = ctnListID;
94         this.containerFilters = containerFilters;
95         initContainerFilters();
96         buildQuery();
97     }
98
99     //--------------------------------------------------------------------------
100
/**
101      * Constructor
102      *
103      * Filtering on one unique container list
104      *
105      * @param String containerListName, the container list name.
106      * @param ParamBean, the param bean.
107      * @param Vector containerFilters, a vector of ContainerFilterInterface objects.
108      */

109     public ContainerFilters(String JavaDoc containerListName, ParamBean params, Vector JavaDoc containerFilters)
110     throws JahiaException
111     {
112         //JahiaConsole.println("ContainerFilters","Created container filter : " + containerListName);
113
this.containerFilters = containerFilters;
114         initContainerFilters();
115
116         if ( containerListName != null ){
117             int clistID = ServicesRegistry.getInstance().getJahiaContainersService().
118                getContainerListID( containerListName, params.getPage().getID() );
119             if ( clistID != -1 ){
120                 this.ctnListID = clistID;
121                 buildQuery();
122             }
123         }
124     }
125
126     //--------------------------------------------------------------------------
127
/**
128      * Constructor
129      *
130      * Filtering on all containers of a given site or of all sites
131      *
132      * If siteId = -1 , returns containers from all sites
133      *
134      * If the containerDefinitionName is null, return result from all containers
135      * no regards to it definition !
136      *
137      * @param containerFilters
138      * @param siteId
139      * @param containerDefinitionName
140      * @throws JahiaException
141      */

142     public ContainerFilters(Vector JavaDoc containerFilters, int siteId,
143                             String JavaDoc containerDefinitionName)
144     throws JahiaException
145     {
146         this.siteId = siteId;
147         this.siteModeFiltering = true;
148         this.containerDefinitionName = containerDefinitionName;
149
150         this.containerFilters = containerFilters;
151         initContainerFilters();
152         buildQuery();
153     }
154
155     //--------------------------------------------------------------------------
156
/**
157      * Perform filtering.
158      * The expected result is a bit set of matching container ids.
159      *
160      * @param loadRequest
161      * @return BitSet bits, the expected result as a bit set of matching ctn ids,each bit position set to true correspond to matching ctn ids.
162      * @throws JahiaException
163      */

164     public BitSet JavaDoc doFilter()
165     throws JahiaException
166     {
167         if ( !isQueryValid() )
168             return null;
169
170         //this.getStagingFields(true);
171

172         BitSet JavaDoc result = new BitSet JavaDoc();
173
174         BitSet JavaDoc bits = null;
175         ContainerFilterInterface containerFilter = null;
176         Vector JavaDoc v = getContainerFilters();
177         int size = v.size();
178         for ( int i=0 ; i<size ; i++ )
179         {
180             bits = new BitSet JavaDoc();
181             containerFilter = (ContainerFilterInterface)v.get(i);
182             if ( this.isSiteModeFiltering() ){
183                 bits = containerFilter.doFilterBySite(getSiteId(),
184                         this.getContainerDefinitionName(),getCtnListID());
185             } else {
186                 bits = containerFilter.doFilter(getCtnListID());
187             }
188             if ( bits != null )
189             {
190                 if ( i == 0 ){
191                     result.or(bits);
192                 } else {
193                     result.and(bits);
194                 }
195             }
196         }
197         this.bits = result;
198         this.updated = true;
199
200         // Set search time
201
this.lastFilteringTime = System.currentTimeMillis();
202         this.stagingFields = null;
203         return result;
204     }
205
206     //--------------------------------------------------------------------------
207
/**
208      * Return the bit set of matching ctn ids.
209      * YOu must call doFilter first.
210      *
211      * @return BitSet the bit set of matching ctn ids.
212      */

213     public BitSet JavaDoc bits()
214     {
215         return this.bits;
216     }
217
218     //--------------------------------------------------------------------------
219
/**
220      * Return true if the filtering is done on an entire site ( or all sites )
221      * false, if the filtering is done on one container list ( using ctnListId )
222      *
223      * @return
224      */

225     public boolean isSiteModeFiltering()
226     {
227         return this.siteModeFiltering;
228     }
229
230     //--------------------------------------------------------------------------
231
/**
232      * Return the container list id.
233      *
234      * @return int ctnListID, the container list id.
235      */

236     public int getCtnListID()
237     {
238         return this.ctnListID;
239     }
240
241     public void setCtnListID(int id){
242         this.ctnListID = id;
243     }
244
245     //--------------------------------------------------------------------------
246
/**
247      * Return the site Id.
248      *
249      */

250     public int getSiteId()
251     {
252         return this.siteId;
253     }
254
255     //--------------------------------------------------------------------------
256
/**
257      * Return the container definition name.
258      *
259      */

260     public String JavaDoc getContainerDefinitionName()
261     {
262         return this.containerDefinitionName;
263     }
264
265     //--------------------------------------------------------------------------
266
/**
267      * Return the query as String.
268      * This query is only a SQL query representation of the combination of all filter select statement,
269      * but not the one performed agains the DB ! "Intersect" Set operator is not available with
270      * all DB ( i.e : ACCESS ).
271      *
272      * @return String, the query.
273      */

274     public String JavaDoc getQuery()
275     {
276         return this.query;
277     }
278
279     //--------------------------------------------------------------------------
280
/**
281      * Return the vector of container filter bean.
282      *
283      * @return Vector, the vector ofcontainer filter bean.
284      */

285     public Vector JavaDoc getContainerFilters()
286     {
287         return this.containerFilters;
288     }
289
290     //--------------------------------------------------------------------------
291
/**
292      * Return true if the query is valid.
293      *
294      * @return boolean , true if the query is valid.
295      */

296     public boolean isQueryValid()
297     {
298         return this.isValid;
299     }
300
301     //--------------------------------------------------------------------------
302
/**
303      * Return the last filtering running time.
304      *
305      * @return long , the last filtering time
306      */

307     public long getLastFilteringTime()
308     {
309         return this.lastFilteringTime;
310     }
311
312     //--------------------------------------------------------------------------
313
/**
314      * Return the update status. Each time the doFilter method is called, this update status is set to true.
315      *
316      * @return boolean, the internal updated status value.
317      */

318     public boolean getUpdateStatus()
319     {
320         return this.updated;
321     }
322
323     //--------------------------------------------------------------------------
324
/**
325      * Set the update status to true.
326      *
327      */

328     public void setUpdateStatus()
329     {
330         this.updated = true;
331     }
332
333     //--------------------------------------------------------------------------
334
/**
335      * You can reset the internal update status by setting it to false
336      *
337      */

338     public void resetUpdateStatus()
339     {
340         this.updated = false;
341     }
342
343     //--------------------------------------------------------------------------
344
/**
345      * Returns an array of fields that are in staging.
346      *
347      * @return
348      * @todo should be cached !
349      * @throws JahiaException
350      */

351     public ArrayList JavaDoc getStagingFields(boolean loadFromDB,
352                                       String JavaDoc containerDefinitionName,
353                                       EntryLoadRequest entryLoadRequest)
354     throws JahiaException
355     {
356
357         if ( !loadFromDB && this.stagingFields != null ){
358             return this.stagingFields;
359         }
360
361         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b, jahia_ctn_def c WHERE ");
362         if ( !this.isSiteModeFiltering() ){
363            buff.append(" a.listid_jahia_ctn_entries=");
364            buff.append(this.ctnListID);
365            buff.append(" AND ");
366         } else {
367             if ( this.getSiteId() != -1 ){
368                 buff.append("a.jahiaid_jahia_ctn_entries=");
369                 buff.append(this.siteId);
370                 buff.append(" AND ");
371             }
372
373             if ( containerDefinitionName != null &&
374                  !"".equals(containerDefinitionName.trim()) ){
375                 buff.append(" a.ctndefid_jahia_ctn_entries = c.id_jahia_ctn_def ");
376                 buff.append(" AND c.name_jahia_ctn_def='");
377                 buff.append(JahiaTools.quote(containerDefinitionName));
378                 buff.append("' AND ");
379             }
380
381         }
382         buff.append(" a.id_jahia_ctn_entries=b.ctnid_jahia_fields_data AND b.workflow_state>1 ");
383         buff.append(" AND ( ");
384         buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(entryLoadRequest,false,true));
385         buff.append(" )");
386
387         Connection JavaDoc dbConn = null;
388         Statement JavaDoc stmt = null;
389         ResultSet JavaDoc rs = null;
390
391         ArrayList JavaDoc datas = new ArrayList JavaDoc();
392
393         try
394         {
395             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
396             stmt = dbConn.createStatement();
397             rs = stmt.executeQuery( buff.toString() );
398
399             while (rs.next()) {
400                 datas.add(new Integer JavaDoc(rs.getInt(1)));
401             }
402         } catch (SQLException JavaDoc se) {
403             String JavaDoc errorMsg = "Error in getStagingFields() : " + se.getMessage();
404             JahiaConsole.println ("ContainerFilters.getStagingFields()", errorMsg);
405         } finally {
406
407             closeStatement (stmt);
408         }
409         this.stagingFields = datas;
410         return this.stagingFields;
411     }
412
413     //--------------------------------------------------------------------------
414
/**
415      * Compare two queries.
416      *
417      * @return boolean , true if the given query is different with the internal query.
418      */

419     public boolean compareQuery(String JavaDoc query)
420     {
421         if ( !isQueryValid() || query == null ){
422             return false;
423         }
424         return (this.query.equals(query));
425     }
426
427     //--------------------------------------------------------------------------
428
/**
429      *
430      */

431     private void initContainerFilters(){
432         if ( this.containerFilters == null
433              || this.containerFilters.size() == 0 ){
434             return;
435         }
436         int size = this.containerFilters.size();
437         for ( int i=0 ; i<size ; i++ ){
438             ContainerFilterInterface filterBean =
439                     (ContainerFilterInterface)this.containerFilters.get(i);
440             if ( filterBean != null ){
441                 filterBean.setContainerFilters(this);
442             }
443         }
444     }
445
446     //--------------------------------------------------------------------------
447
/**
448      * Build the internal DB query.
449      *
450      */

451     private void buildQuery()
452     {
453
454         //JahiaConsole.println("ContainerFilters.buildQuery","Started");
455

456         if ( containerFilters == null || containerFilters.size()==0 )
457         {
458             return;
459         }
460
461         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
462         String JavaDoc fieldFilterQuery;
463         ContainerFilterInterface containerFilter;
464         int size = containerFilters.size();
465         for ( int i=0 ; i<size ; i++ )
466         {
467             containerFilter = (ContainerFilterInterface)containerFilters.get(i);
468             if ( containerFilter == null )
469                 return;
470             if ( this.isSiteModeFiltering() ){
471                 fieldFilterQuery = containerFilter.getSelectBySiteID(this.getSiteId(), this.getContainerDefinitionName());
472             } else {
473                 fieldFilterQuery = containerFilter.getSelect(this.getCtnListID());
474             }
475
476             if ( fieldFilterQuery != null && !fieldFilterQuery.trim().equals("") )
477             {
478                 buff.append(fieldFilterQuery);
479             }
480             if ( i<size-1 )
481             {
482                 buff.append(" INTERSECT ");
483             }
484         }
485
486         this.query = buff.toString();
487         //JahiaConsole.println("ContainerFilters.buildQuery","Builded query : " + this.query);
488

489         this.isValid = true;
490     }
491
492     //-------------------------------------------------------------------------
493
private void closeStatement (Statement JavaDoc statement)
494     {
495         // Close the opened statement
496
try {
497             if (statement!=null) {
498                 statement.close();
499             }
500         }
501         catch (SQLException JavaDoc sqlEx) {
502             // just create an exception without raising it, just to notify it
503
// in the logs.
504
JahiaException je = new JahiaException ("Cannot close a statement",
505                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
506                     JahiaException.WARNING_SEVERITY, sqlEx);
507             logger.error("Error:", je);
508         }
509     }
510
511     public String JavaDoc toString() {
512         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
513         if (containerFilters != null) {
514             Enumeration JavaDoc filterEnum = containerFilters.elements();
515             result.append("Container filters:");
516             result.append(containerFilters);
517             result.append("\n");
518         }
519         if (bits != null) {
520             result.append("Bitset: ");
521             result.append(bits);
522             result.append("\n");
523         }
524         if (stagingFields != null) {
525             result.append("Staging fields: ");
526             result.append(stagingFields);
527             result.append("\n");
528         }
529         return result.toString();
530     }
531 }
532
Popular Tags