KickJava   Java API By Example, From Geeks To Geeks.

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


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 Creation
17

18
19 package org.jahia.data.containers;
20
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
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.services.fields.ContentField;
32 import org.jahia.services.version.EntryLoadRequest;
33 import org.jahia.utils.JahiaConsole;
34 import org.jahia.utils.JahiaTools;
35
36
37
38 /**
39  * A filter used to returns all containers of a given definition.
40  * Same container definition name
41  *
42  * @see FilterClause
43  * @see ContainerFilters
44  * @see JahiaContainerSet
45  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
46  */

47 public class ContainerFilterByContainerDefinition implements Serializable JavaDoc, ContainerFilterInterface{
48
49     private static org.apache.log4j.Logger logger =
50         org.apache.log4j.Logger.getLogger(ContainerFilterByContainerDefinition.class);
51
52     public static final String JavaDoc VERSION_ID = "version_id";
53     public static final String JavaDoc WORKFLOW_STATE = "workflow_state";
54
55     private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
56
57     private String JavaDoc containerDefinitionName;
58
59     private ContainerFilters containerFilters = null;
60
61     //--------------------------------------------------------------------------
62
/**
63      * Constructor
64      *
65      * @param containerDefinitionName
66      * @param entryLoadRequest
67      */

68     public ContainerFilterByContainerDefinition(String JavaDoc containerDefinitionName,
69             EntryLoadRequest entryLoadRequest){
70
71         this.containerDefinitionName = containerDefinitionName;
72         if ( entryLoadRequest != null ){
73             this.entryLoadRequest = entryLoadRequest;
74         }
75
76     }
77
78     //--------------------------------------------------------------------------
79
/**
80      * Return the container definition name
81      *
82      * @return
83      */

84     public String JavaDoc getContainerDefinitionName()
85     {
86         return this.containerDefinitionName;
87     }
88
89     //--------------------------------------------------------------------------
90
/**
91      * Perform filtering.
92      * The expected result is a bit set of matching container ids.
93      *
94      * @param int ctnListID, the container list id
95      * @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.
96      */

97     public BitSet JavaDoc doFilter(int ctnListID)
98     throws JahiaException
99     {
100         BitSet JavaDoc result = null;
101
102         result = doFiltering(ctnListID);
103
104         return result;
105     }
106
107     //--------------------------------------------------------------------------
108
/**
109      * The expected result is a bit set of matching container ids.
110      *
111      * @param int ctnListID, the container list id
112      * @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.
113      */

114     private BitSet JavaDoc doFiltering(int ctnListID)
115     throws JahiaException
116     {
117         String JavaDoc fieldFilterQuery = getSelect(ctnListID,true);
118         if ( fieldFilterQuery == null && !fieldFilterQuery.trim().equals("") ){
119             return null;
120         }
121
122         BitSet JavaDoc bits = new BitSet JavaDoc();
123
124         Connection JavaDoc dbConn = null;
125         Statement JavaDoc stmt = null;
126         ResultSet JavaDoc rs = null;
127
128         ArrayList JavaDoc deletedCtns = getDeletedContainers(ctnListID);
129
130         try
131         {
132             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
133             stmt = dbConn.createStatement();
134             rs = stmt.executeQuery( fieldFilterQuery );
135             while (rs.next())
136             {
137                 int ctnID = rs.getInt(1);
138                 int workflowState = rs.getInt(2);
139                 if ( this.entryLoadRequest.isCurrent()
140                      || !deletedCtns.contains(new Integer JavaDoc(ctnID))){
141                     if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
142                         workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE;
143                     }
144                     if ( this.entryLoadRequest.isCurrent() &&
145                          workflowState == EntryLoadRequest.ACTIVE_WORKFLOW_STATE){
146                         bits.set(ctnID);
147                     } else if ( this.entryLoadRequest.isStaging()
148                                && workflowState >= EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
149                         bits.set(ctnID);
150                     }
151                 }
152             }
153         }
154         catch (SQLException JavaDoc se)
155         {
156             String JavaDoc errorMsg = "Error in doFiltering : " + se.getMessage();
157             JahiaConsole.println ("ContainerFilterByContainerDefinition.doFiltering", errorMsg);
158         } finally {
159
160             closeStatement (stmt);
161         }
162
163         return bits;
164     }
165
166     //--------------------------------------------------------------------------
167
/**
168      * Return the select statement, build with the clauses for all container list of the site.
169      *
170      * Return the select statement, build with the clauses for all container list of the site.
171      * Do not execute this query !! It's only used for comparison between query.
172      * It's not a valable sql query.
173      *
174      * @param int ctnListID, the container list id
175      * @return String , the sql statement. Null on error
176      */

177     public String JavaDoc getSelect(int ctnListID)
178     {
179         return getSelect (ctnListID, false);
180     }
181
182     /**
183      * Return the select statement, build with the clauses for all container list of the site.
184      *
185      * @param int ctnListID, the container list id
186      * @param ignoreLang boolean, add language in query or not ( should not when performing sql query, because containre table do not have lang column
187      * @return String , the sql statement. Null on error
188      */

189     protected String JavaDoc getSelect (int ctnListID, boolean ignoreLang) {
190         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries WHERE listid_jahia_ctn_entries=");
191         buff.append(ctnListID);
192         buff.append(" AND ");
193         buff.append(buildMultilangAndWorlflowQuery(this.entryLoadRequest,ignoreLang));
194         buff.append(" ORDER BY id_jahia_ctn_entries, workflow_state");
195
196         return buff.toString();
197     }
198     //--------------------------------------------------------------------------
199
/**
200      * Set reference to a containerFilters
201      *
202      * @return
203      * @throws JahiaException
204      */

205     public void setContainerFilters(ContainerFilters containerFilters){
206         this.containerFilters = containerFilters;
207     }
208
209     //--------------------------------------------------------------------------
210
/**
211      *
212      * @return
213      */

214     static public String JavaDoc buildMultilangAndWorlflowQuery(EntryLoadRequest entryLoadRequest){
215
216         return buildMultilangAndWorlflowQuery(entryLoadRequest,
217                                                false);
218     }
219
220     //--------------------------------------------------------------------------
221
/**
222      *
223      * @return
224      */

225     static public String JavaDoc buildMultilangAndWorlflowQuery(EntryLoadRequest entryLoadRequest,
226                                                         boolean ignoreLang){
227
228         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(" ");
229         if ( entryLoadRequest.isCurrent()){
230             strBuf.append(WORKFLOW_STATE);
231             strBuf.append("=");
232             strBuf.append(EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
233         } else if ( entryLoadRequest.isStaging() ) {
234             strBuf.append(WORKFLOW_STATE);
235             strBuf.append(">");
236             strBuf.append(EntryLoadRequest.VERSIONED_WORKFLOW_STATE);
237             strBuf.append(" AND ");
238             strBuf.append("version_id");
239             strBuf.append(" <> -1 ");
240         } else {
241             strBuf.append("version_id");
242             strBuf.append("=");
243             strBuf.append(entryLoadRequest.getVersionID());
244         }
245         if ( !ignoreLang ) {
246             String JavaDoc languageCode = entryLoadRequest.getFirstLocale(true).
247                 toString();
248             strBuf.append(" AND (");
249             strBuf.append("language_code");
250             strBuf.append("='");
251             strBuf.append(JahiaTools.quote(languageCode));
252             strBuf.append("' OR ");
253             strBuf.append("language_code");
254             strBuf.append("='");
255             strBuf.append(ContentField.SHARED_LANGUAGE);
256             strBuf.append("') ");
257         }
258         /*
259         strBuf.append("') AND ");
260         strBuf.append(FIELD_VERSION_ID);
261         strBuf.append(">=0 ");*/

262
263         return strBuf.toString();
264
265     }
266
267     //--------------------------------------------------------------------------
268
/**
269      * Returns an array of cntids that are workflow=staging and versionid=-1 (deleted)
270      *
271      * @param ctnListID
272      * @return
273      * @throws JahiaException
274      */

275     static public ArrayList JavaDoc getDeletedContainers(int ctnListID)
276     throws JahiaException
277     {
278
279         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_ctn_entries FROM jahia_ctn_entries WHERE workflow_state>1 AND version_id=-1 AND listid_jahia_ctn_entries=");
280         buff.append(ctnListID);
281
282         Connection JavaDoc dbConn = null;
283         Statement JavaDoc stmt = null;
284         ResultSet JavaDoc rs = null;
285
286         ArrayList JavaDoc datas = new ArrayList JavaDoc();
287
288         try
289         {
290             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
291             stmt = dbConn.createStatement();
292             rs = stmt.executeQuery( buff.toString() );
293
294             while (rs.next()) {
295                 datas.add(new Integer JavaDoc(rs.getInt(1)));
296             }
297         }
298         catch (SQLException JavaDoc se)
299         {
300             String JavaDoc errorMsg = "Error in getDeletedContainers() : " + se.getMessage();
301             JahiaConsole.println ("ContainerFilterBean.getDeletedContainers()", errorMsg);
302         } finally {
303
304             closeStatement (stmt);
305         }
306         return datas;
307     }
308
309     //--------------------------------------------------------------------------
310
/**
311      * Perform filtering on a given site or all sites
312      *
313      * The expected result is a bit set of matching container ids.
314      *
315      * If siteId = -1 , returns results from all sites
316      *
317      * If the containerDefinitionName is null, return result from all containers
318      * no regards to it definition !
319      *
320      * @param siteId
321      * @param containerDefinitionName
322      * @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.
323      * @throws JahiaException
324      */

325     public BitSet JavaDoc doFilterBySite(int siteId, String JavaDoc containerDefinitionName, int listId)
326     throws JahiaException
327     {
328
329         BitSet JavaDoc result = null;
330         result = doFilteringBySite(siteId, containerDefinitionName,listId);
331         return result;
332     }
333
334     //--------------------------------------------------------------------------
335
/**
336      *
337      * The expected result is a bit set of matching container ids for a given siteId.
338      * if siteId = -1 , return result from all sites
339      *
340      * If the containerDefinitionName is null, return result from all containers
341      * no regards to it definition !
342      *
343      * @param siteId
344      * @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.
345      * @throws JahiaException
346      */

347     private BitSet JavaDoc doFilteringBySite(int siteId,
348             String JavaDoc containerDefinitionName, int listId)
349     throws JahiaException
350     {
351         String JavaDoc fieldFilterQuery = this.getSelectBySiteID(siteId, containerDefinitionName,true);
352         if ( fieldFilterQuery == null && !fieldFilterQuery.trim().equals("") ){
353             return null;
354         }
355
356         BitSet JavaDoc bits = new BitSet JavaDoc();
357
358         Connection JavaDoc dbConn = null;
359         Statement JavaDoc stmt = null;
360         ResultSet JavaDoc rs = null;
361
362         ArrayList JavaDoc deletedCtns = getDeletedContainersBySite(siteId, containerDefinitionName);
363
364         try
365         {
366             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
367             stmt = dbConn.createStatement();
368             rs = stmt.executeQuery( fieldFilterQuery );
369             while (rs.next())
370             {
371                 int ctnID = rs.getInt(1);
372                 int workflowState = rs.getInt(2);
373                 if ( this.entryLoadRequest.isCurrent()
374                      || !deletedCtns.contains(new Integer JavaDoc(ctnID))){
375                     if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
376                         workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE;
377                     }
378                     if ( this.entryLoadRequest.isCurrent()
379                          && workflowState==EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
380                         bits.set(ctnID);
381                     } else if ( this.entryLoadRequest.isStaging()
382                                && workflowState >= EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
383                         bits.set(ctnID);
384                     }
385                 }
386             }
387         }
388         catch (SQLException JavaDoc se)
389         {
390             String JavaDoc errorMsg = "Error in doStringValueFiltering : " + se.getMessage();
391             JahiaConsole.println ("ContainerFilterBean.doStringValueFiltering", errorMsg);
392         } finally {
393
394             closeStatement (stmt);
395         }
396
397         return bits;
398     }
399
400     //--------------------------------------------------------------------------
401
/**
402      * Return the select statement, build with the clauses for a given site.
403      * If siteId = -1 -> build query for all sites
404      *
405      * If the containerDefinitionName is null, return result from all containers
406      * no regards to it definition !
407      *
408      * Do not execute this query !! It's only used for comparison between query.
409      * It's not a valable sql query.
410      *
411      * @param siteId
412      * @param containerDefinitionName
413      * @param ignoreLang boolean, add language in query or not ( should not when performing sql query, because containre table do not have lang column
414      * @return
415      */

416     public String JavaDoc getSelectBySiteID (int siteId, String JavaDoc containerDefinitionName) {
417         return getSelectBySiteID (siteId, containerDefinitionName, false);
418     }
419
420     /**
421      * Return the select statement, build with the clauses for a given site.
422      * If siteId = -1 -> build query for all sites
423      *
424      * If the containerDefinitionName is null, return result from all containers
425      * no regards to it definition !
426      *
427      * @param siteId
428      * @param containerDefinitionName
429      * @param ignoreLang boolean, add language in query or not ( should not when performing sql query, because containre table do not have lang column
430      * @return
431      */

432     public String JavaDoc getSelectBySiteID (int siteId, String JavaDoc containerDefinitionName,
433                                      boolean ignoreLang) {
434
435         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries a, jahia_ctn_def b WHERE ");
436         buff.append(buildMultilangAndWorlflowQuery(this.entryLoadRequest,ignoreLang));
437
438         if ( siteId != -1 ){
439             buff.append(" AND ");
440             buff.append(" jahiaid_jahia_ctn_entries=");
441             buff.append(siteId);
442         }
443
444         if ( containerDefinitionName != null &&
445              !"".equals(containerDefinitionName.trim()) ){
446             buff.append(" AND ");
447             buff.append(" a.ctndefid_jahia_ctn_entries = b.id_jahia_ctn_def ");
448             buff.append(" AND b.name_jahia_ctn_def='");
449             buff.append(JahiaTools.quote(containerDefinitionName));
450             buff.append("'");
451         }
452
453         buff.append(" ORDER BY id_jahia_ctn_entries,");
454         buff.append(WORKFLOW_STATE);
455
456         return buff.toString();
457     }
458
459     //--------------------------------------------------------------------------
460
/**
461      * Returns an array of cntids that are workflow=staging and versionid=-1 (deleted)
462      * for a given site
463      * If siteId = -1 , return deleted containers from all sites
464      *
465      * @param ctnListID
466      * @return
467      * @throws JahiaException
468      */

469     static public ArrayList JavaDoc getDeletedContainersBySite(int siteId,
470             String JavaDoc containerDefinitionName)
471     throws JahiaException
472     {
473
474         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_ctn_entries FROM jahia_ctn_entries, jahia_ctn_def WHERE workflow_state>1 AND version_id=-1 ");
475         if ( siteId != -1 ){
476             buff.append(" AND jahiaid_jahia_ctn_entries=");
477             buff.append(siteId);
478         }
479
480         if ( containerDefinitionName != null &&
481              !"".equals(containerDefinitionName.trim()) ){
482             buff.append(" AND ");
483             buff.append(" ctndefid_jahia_ctn_entries = id_jahia_ctn_def ");
484             buff.append(" AND name_jahia_ctn_def='");
485             buff.append(JahiaTools.quote(containerDefinitionName));
486             buff.append("'");
487         }
488
489         Connection JavaDoc dbConn = null;
490         Statement JavaDoc stmt = null;
491         ResultSet JavaDoc rs = null;
492
493         ArrayList JavaDoc datas = new ArrayList JavaDoc();
494
495         try
496         {
497             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
498             stmt = dbConn.createStatement();
499             rs = stmt.executeQuery( buff.toString() );
500
501             while (rs.next()) {
502                 datas.add(new Integer JavaDoc(rs.getInt(1)));
503             }
504         }
505         catch (SQLException JavaDoc se)
506         {
507             String JavaDoc errorMsg = "Error in getDeletedContainers() : " + se.getMessage();
508             JahiaConsole.println ("ContainerFilterBean.getDeletedContainers()", errorMsg);
509         } finally {
510
511             closeStatement (stmt);
512         }
513         return datas;
514     }
515
516     //-------------------------------------------------------------------------
517
static private void closeStatement (Statement JavaDoc statement)
518     {
519         // Close the opened statement
520
try {
521             if (statement!=null) {
522                 statement.close();
523             }
524         }
525         catch (SQLException JavaDoc sqlEx) {
526             // just create an exception without raising it, just to notify it
527
// in the logs.
528
logger.error("Error closing statement", sqlEx);
529         }
530     }
531
532 }
533
Popular Tags