KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.jahia.data.containers;
19
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.BitSet JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.jahia.content.ObjectKey;
30 import org.jahia.content.ContentDefinition;
31 import org.jahia.exceptions.JahiaException;
32 import org.jahia.registries.ServicesRegistry;
33 import org.jahia.services.categories.Category;
34 import org.jahia.services.containers.ContentContainer;
35 import org.jahia.services.search.JahiaSearchConstant;
36 import org.jahia.services.version.EntryLoadRequest;
37
38
39
40 /**
41  * A filter used to returns all containers in given categories.
42  * Same container definition name
43  *
44  * @see FilterClause
45  * @see ContainerFilters
46  * @see JahiaContainerSet
47  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
48  */

49 public class ContainerFilterByCategories implements Serializable JavaDoc, ContainerFilterInterface{
50
51     private static org.apache.log4j.Logger logger =
52         org.apache.log4j.Logger.getLogger(ContainerFilterByCategories.class);
53
54     private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
55
56     private String JavaDoc containerDefinitionName;
57
58     private ContainerFilters containerFilters = null;
59
60     private Set JavaDoc catetories;
61
62     private boolean withAllContainersOfCompoundContainerList = false;
63
64     //--------------------------------------------------------------------------
65
/**
66      * Constructor
67      *
68      * if withAllContainersOfCompoundContainerList = true,
69      * return all containers of the compound container list ( allowing
70      * to return the containers without any category
71      *
72      * @param categories
73      * @param jParams
74      * @param entryLoadRequest
75      * @param withAllContainersOfCompoundContainerList
76      */

77     public ContainerFilterByCategories(Set JavaDoc categories,
78                                        EntryLoadRequest entryLoadRequest,
79                                        boolean withAllContainersOfCompoundContainerList){
80
81         if ( entryLoadRequest != null ){
82             this.entryLoadRequest = entryLoadRequest;
83         }
84         this.catetories = categories;
85         if ( categories == null ){
86             categories = new HashSet JavaDoc();
87         }
88         this.withAllContainersOfCompoundContainerList =
89                 withAllContainersOfCompoundContainerList;
90     }
91
92     //--------------------------------------------------------------------------
93
/**
94      * Perform filtering.
95      * The expected result is a bit set of matching container ids.
96      *
97      * @param int ctnListID, the container list id
98      * @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.
99      */

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

117     private BitSet JavaDoc doFiltering(int ctnListID)
118     throws JahiaException
119     {
120         BitSet JavaDoc result = new BitSet JavaDoc();
121         try {
122             ArrayList JavaDoc containers = getContainers(this.catetories);
123             int size = containers.size();
124             for ( int i=0 ; i<size; i++ ){
125                 ContentContainer contentContainer =
126                         (ContentContainer)containers.get(i);
127                 if ( contentContainer.getParentContainerListID() == ctnListID ){
128                     result.set(contentContainer.getID());
129                 }
130             }
131         }catch ( Throwable JavaDoc t ){
132             logger.debug("Exception :",t);
133         }
134
135         if ( this.withAllContainersOfCompoundContainerList ){
136             int ctnListId = this.containerFilters.getCtnListID();
137             Vector JavaDoc ids = ServicesRegistry.getInstance().getJahiaContainersService()
138                     .getctnidsInList(ctnListId,this.entryLoadRequest);
139             int size = ids.size();
140             for ( int i=0 ; i<size; i++ ){
141                 Integer JavaDoc I = (Integer JavaDoc)ids.get(i);
142                 result.set(I.intValue());
143             }
144         }
145         ContainerFilterByLoadRequest cfblr =
146                 new ContainerFilterByLoadRequest(this.entryLoadRequest);
147         BitSet JavaDoc result2 = cfblr.doFilter(ctnListID);
148
149         result.and(result2);
150         return result;
151
152     }
153
154     //--------------------------------------------------------------------------
155
/**
156      * Return the select statement, build with the clauses for all container list of the site.
157      *
158      * @param int ctnListID, the container list id
159      * @return String , the sql statement. Null on error
160      */

161     public String JavaDoc getSelect(int ctnListID)
162     {
163         String JavaDoc loadRequest = "";
164         if ( this.entryLoadRequest != null ){
165           loadRequest = this.entryLoadRequest.toString();
166         }
167         return (getCategoriesSearchQuery() + "_" + loadRequest);
168     }
169
170     //--------------------------------------------------------------------------
171
/**
172      * Set reference to a containerFilters
173      *
174      * @return
175      * @throws JahiaException
176      */

177     public void setContainerFilters(ContainerFilters containerFilters){
178         this.containerFilters = containerFilters;
179     }
180
181     //--------------------------------------------------------------------------
182
/**
183      * Perform filtering on a given site or all sites
184      *
185      * The expected result is a bit set of matching container ids.
186      *
187      * If siteId = -1 , returns results from all sites
188      *
189      * If the containerDefinitionName is null, return result from all containers
190      * no regards to it definition !
191      *
192      * @todo : actually, return all containers no distinction of type
193      *
194      * @param siteId
195      * @param containerDefinitionName
196      * @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.
197      * @throws JahiaException
198      */

199     public BitSet JavaDoc doFilterBySite(int siteId, String JavaDoc containerDefinitionName, int listId)
200     throws JahiaException
201     {
202
203         BitSet JavaDoc result = null;
204         result = doFilteringBySite(siteId, containerDefinitionName,listId);
205         return result;
206     }
207
208     //--------------------------------------------------------------------------
209
/**
210      *
211      * The expected result is a bit set of matching container ids for a given siteId.
212      * if siteId = -1 , return result from all sites
213      *
214      * If the containerDefinitionName is null, return result from all containers
215      * no regards to it definition !
216      *
217      *
218      * @param siteId
219      * @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.
220      * @throws JahiaException
221      */

222     private BitSet JavaDoc doFilteringBySite(int siteId,
223             String JavaDoc containerDefinitionName,int listId)
224     throws JahiaException
225     {
226         BitSet JavaDoc result = new BitSet JavaDoc();
227         try {
228             ArrayList JavaDoc containers = getContainers(this.catetories);
229             int size = containers.size();
230             for ( int i=0 ; i<size; i++ ){
231                 ContentContainer contentContainer =
232                         (ContentContainer)containers.get(i);
233
234                 result.set(contentContainer.getID());
235             }
236         }catch ( Throwable JavaDoc t ){
237             logger.debug("Exception :",t);
238         }
239
240         if ( this.withAllContainersOfCompoundContainerList ){
241             Vector JavaDoc ids = ServicesRegistry.getInstance().getJahiaContainersService()
242                     .getctnidsInList(listId,this.entryLoadRequest);
243             int size = ids.size();
244             for ( int i=0 ; i<size; i++ ){
245                 Integer JavaDoc I = (Integer JavaDoc)ids.get(i);
246                 result.set(I.intValue());
247             }
248         }
249         ContainerFilterByLoadRequest cfblr =
250                 new ContainerFilterByLoadRequest(this.entryLoadRequest);
251         BitSet JavaDoc result2 = cfblr.doFilterBySite(siteId,containerDefinitionName,listId);
252         result.and(result2);
253         return result;
254     }
255
256     //--------------------------------------------------------------------------
257
/**
258      * Return the select statement, build with the clauses for a given site.
259      * If siteId = -1 -> build query for all sites
260      *
261      * If the containerDefinitionName is null, return result from all containers
262      * no regards to it definition !
263      *
264      * @param siteId
265      * @param containerDefinitionName
266      * @return
267      */

268     public String JavaDoc getSelectBySiteID(int siteId, String JavaDoc containerDefinitionName)
269     {
270         // It's a dummy select
271
String JavaDoc loadRequest = "";
272         if ( this.entryLoadRequest != null ){
273           loadRequest = this.entryLoadRequest.toString();
274         }
275         return (getCategoriesSearchQuery() + "_" + loadRequest);
276     }
277
278     private ArrayList JavaDoc getContainers(Set JavaDoc categories)
279         throws ClassNotFoundException JavaDoc, JahiaException {
280
281         ArrayList JavaDoc val = new ArrayList JavaDoc();
282         Iterator JavaDoc iterator = categories.iterator();
283         Category category = null;
284         while ( iterator.hasNext() ){
285             category = (Category)iterator.next();
286             ArrayList JavaDoc allChildrenObjectKeys = category.getChildObjectKeys();
287             Iterator JavaDoc allChildObjectKeysIter = allChildrenObjectKeys.iterator();
288             ArrayList JavaDoc childJahiaObjects = new ArrayList JavaDoc();
289             while (allChildObjectKeysIter.hasNext()) {
290                 ObjectKey curObjectKey = (ObjectKey) allChildObjectKeysIter.
291                                          next();
292                 if (curObjectKey.getType().equals("ContentContainer")) {
293                     try {
294                         ContentContainer contentContainer =
295                                 (ContentContainer)ContentContainer.getInstance(curObjectKey);
296                         if ( contentContainer != null ){
297                             if ( containerDefinitionName != null ) {
298                                 try {
299                                     ContentDefinition definition = ContentDefinition
300                                             .getInstance(contentContainer.getDefinitionKey(null));
301                                     if ( definition != null &&
302                                             containerDefinitionName.equalsIgnoreCase(definition.getName()) ){
303                                         val.add(contentContainer);
304                                     }
305                                 } catch ( Throwable JavaDoc t) {
306                                     logger.debug("Error retrieving container definition for container "
307                                             + contentContainer.getID(),t);
308                                 }
309                             } else {
310                                 val.add(contentContainer);
311                             }
312                         }
313                     } catch (Throwable JavaDoc t){
314                         logger.debug("Error loading contentContainer " + curObjectKey.toString());
315                     }
316                 }
317             }
318         }
319         return val;
320     }
321
322     private String JavaDoc getCategoriesSearchQuery(){
323         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("(");
324         Iterator JavaDoc iterator = this.catetories.iterator();
325         while ( iterator.hasNext() ){
326             Category cat = (Category)iterator.next();
327             buff.append(JahiaSearchConstant.CONTAINER_CATEGORY_KEY);
328             buff.append(":");
329             if ( cat != null ){
330                 buff.append(cat.getKey());
331             }
332             if ( iterator.hasNext() ){
333                 buff.append(" OR ");
334             }
335         }
336         buff.append(")");
337         return buff.toString();
338     }
339 }
340
Popular Tags