KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > containers > ContainerFactory


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  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.services.containers;
42
43 import java.util.*;
44
45 import javax.servlet.http.HttpSession JavaDoc;
46
47 import org.apache.log4j.Logger;
48 import org.jahia.data.containers.ContainerFilters;
49 import org.jahia.data.containers.ContainerSorterBean;
50 import org.jahia.data.containers.ContainerSorterInterface;
51 import org.jahia.data.containers.ContainersChangeEventListener;
52 import org.jahia.data.containers.JahiaContainer;
53 import org.jahia.data.containers.JahiaContainerList;
54 import org.jahia.data.containers.JahiaContainerListPagination;
55 import org.jahia.data.fields.JahiaField;
56 import org.jahia.data.search.JahiaSearchResult;
57 import org.jahia.exceptions.JahiaException;
58 import org.jahia.params.ParamBean;
59 import org.jahia.registries.JahiaListenersRegistry;
60 import org.jahia.registries.ServicesRegistry;
61 import org.jahia.services.acl.JahiaACLManagerService;
62 import org.jahia.services.acl.JahiaBaseACL;
63 import org.jahia.services.cache.Cache;
64 import org.jahia.services.cache.CacheFactory;
65 import org.jahia.services.search.ChainedJahiaSearcher;
66 import org.jahia.services.search.ContainerSearcher;
67 import org.jahia.services.search.JahiaSearcher;
68 import org.jahia.services.usermanager.JahiaUser;
69 import org.jahia.services.version.EntryLoadRequest;
70
71
72 /**
73  * Container Factory.
74  * Load Containers and Container Lists, applying filtering and pagination.
75  *
76  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
77  */

78
79 public class ContainerFactory {
80
81     private static Logger logger = Logger.getLogger (ContainerFactory.class);
82
83     private static ContainerFactory instance = null;
84
85     // the preloaded container acl id by container list.
86
public static final String JavaDoc PRELOADED_CTNR_ACLID_BY_CTNRLIST_CACHE = "PreloadedCtnrACLByCtnrListCache";
87     private static Cache preloadedCtnrACLIDByCtnrListCache;
88
89     private JahiaContainerUtilsDB c_utils;
90
91     //--------------------------------------------------------------------------
92
/**
93      * Constructor
94      */

95     private ContainerFactory () {
96
97         c_utils = JahiaContainerUtilsDB.getInstance();
98
99         try {
100             preloadedCtnrACLIDByCtnrListCache = CacheFactory
101                 .createCache(PRELOADED_CTNR_ACLID_BY_CTNRLIST_CACHE);
102         } catch ( JahiaException je ){
103             logger.debug("Error creating cache ",je);
104         }
105     }
106
107     public static synchronized ContainerFactory getInstance () {
108         if (instance == null) {
109             instance = new ContainerFactory ();
110         }
111         return instance;
112     }
113
114     /**
115      * Load all container lists for a given container. This is performed only if
116      * the container.isContainerListsLoaded() return false.
117      *
118      * @param container , a container that is not completely loaded.
119      * @param jParams , used to handle all filtering
120      * @param loadFlag
121      * @param cachedFieldsFromContainers map with key=ctnid, obj=Vector of the ctn's field ids.
122      * can be null, but for performance this map should be loaded with all fields of same page ID as the container's pageID.
123      *
124      * @throws JahiaException
125      */

126     public void fullyLoadContainer (JahiaContainer container, int loadFlag,
127                                               ParamBean jParams, EntryLoadRequest loadRequest,
128                                               Hashtable cachedFieldsFromContainers,
129                                               Hashtable cachedContainersFromContainerLists,
130                                               Hashtable cachedContainerListsFromContainers)
131     throws JahiaException {
132
133         if (container == null) {
134             return;
135         }
136
137         if (!container.isFieldsLoaded()) {
138             synchronized (container) {
139                 if (!container.isFieldsLoaded()) {
140                     container.clearFields();
141                     Vector fieldIDs = null;
142                     if (cachedFieldsFromContainers != null) {
143                         fieldIDs = (Vector) cachedFieldsFromContainers
144                                 .get(new Integer JavaDoc(container.getID()));
145                     }
146                     // not already in cache -> load it
147
if (fieldIDs == null) {
148                         fieldIDs = ServicesRegistry.getInstance()
149                                 .getJahiaContainersService()
150                                 .getFieldIDsInContainer(container.getID(),
151                                         loadRequest);
152                     }
153                     for (int i = 0; i < fieldIDs.size(); i++) {
154                         int fieldID = ((Integer JavaDoc) fieldIDs.elementAt(i))
155                                 .intValue();
156                         JahiaField aField = ServicesRegistry.getInstance()
157                                 .getJahiaFieldService().loadField(fieldID,
158                                         loadFlag, jParams, loadRequest);
159                         if (aField != null) {
160                             container.addField(aField);
161                         }
162                     }
163                     container.setFieldsLoaded(true);
164                 }
165             }
166         }
167
168         if (!container.isContainerListsLoaded()) {
169             synchronized (container) {
170                 if (!container.isContainerListsLoaded()) {
171                     container.clearContainerLists();
172                     JahiaContainersService ctnServ = ServicesRegistry
173                             .getInstance().getJahiaContainersService();
174
175                     // load container lists
176
// load the the containers for this one
177
// apply containers search and filtering.
178
Vector listIDs = null;
179                     if (cachedContainerListsFromContainers != null) {
180                         listIDs = (Vector) cachedContainerListsFromContainers
181                                 .get(new Integer JavaDoc(container.getID()));
182                     }
183                     if (listIDs == null) {
184                         listIDs = JahiaContainerUtilsDB.getInstance()
185                                 .getContainerSubContainerListIDs(
186                                         container.getID(), loadRequest);
187                     }
188                     int size = listIDs.size();
189                     Integer JavaDoc listID = null;
190                     for (int i = 0; i < size; i++) {
191                         listID = (Integer JavaDoc) listIDs.get(i);
192                         JahiaContainerList cList = ctnServ.loadContainerList(
193                                 listID.intValue(), loadFlag, jParams,
194                                 loadRequest, cachedFieldsFromContainers,
195                                 cachedContainersFromContainerLists,
196                                 cachedContainerListsFromContainers);
197
198                         if (cList != null) {
199                             container.addContainerList(cList);
200                         }
201                     }
202                     container.setContainerListsLoaded(true);
203                 }
204             }
205         }
206     }
207
208     /**
209      * Load all container lists for a given container. This is performed only if
210      * the container.isContainerListsLoaded() return false.
211      *
212      * @param container , a container that is not completely loaded.
213      * @param jParams , used to handle all filtering
214      * @param loadFlag
215      * @param cachedFieldsFromContainers map with key=ctnid, obj=Vector of the ctn's field ids.
216      * can be null, but for performance this map should be loaded with all fields of same page ID as the container's pageID.
217      *
218      * @throws JahiaException
219      */

220     public JahiaField loadContainerField (JahiaContainer container, int fieldDefID, int loadFlag,
221                                               ParamBean jParams, EntryLoadRequest loadRequest,
222                                               Map cachedFieldsAndDefsFromContainers)
223     throws JahiaException {
224
225         if (container == null) {
226             return null;
227         }
228         JahiaField aField = null;
229
230         Map fieldIDs = null;
231         if (cachedFieldsAndDefsFromContainers != null) {
232             fieldIDs = (Map) cachedFieldsAndDefsFromContainers.get(new Integer JavaDoc(
233                     container.getID()));
234         }
235         // not already in cache -> load it
236
if (fieldIDs == null) {
237             fieldIDs = ServicesRegistry.getInstance()
238                     .getJahiaContainersService().getFieldIDsAndDefsInContainer(
239                             container.getID(), loadRequest);
240             cachedFieldsAndDefsFromContainers.put(
241                     new Integer JavaDoc(container.getID()), fieldIDs);
242         }
243         Integer JavaDoc fieldID = (Integer JavaDoc) fieldIDs.get(new Integer JavaDoc(fieldDefID));
244         if (fieldID != null) {
245             aField = container.isFieldsLoaded() ? container.getField(fieldID
246                     .intValue()) : ServicesRegistry.getInstance()
247                     .getJahiaFieldService().loadField(fieldID.intValue(),
248                             loadFlag, jParams, loadRequest);
249         }
250          
251         return aField;
252     }
253     
254     /**
255      * Fully load a container list (containers and their own container lists...).
256      * This is performed only if the containerList.isContainersLoaded() return false.
257      *
258      * @param containerList , a container list that is not completely loaded.
259      * @param jParams , used to handle all filtering
260      * @param loadFlag
261      * @param cachedFieldsFromContainers, map with key=ctnid, obj=Vector of the ctn's field ids.
262      * can be null, but for performance this map should be loaded with all fields of same page ID as the container's pageID.
263      *
264      * @throws JahiaException
265      */

266     public JahiaContainerList fullyLoadContainerList (JahiaContainerList cList, int loadFlag,
267                                                       ParamBean jParams,
268                                                       EntryLoadRequest loadRequest,
269                                                       Hashtable cachedFieldsFromContainers,
270                                                       Hashtable cachedContainersFromContainerLists,
271                                                       Hashtable cachedContainerListsFromContainers)
272             throws JahiaException {
273         if (cList == null) {
274             return null;
275         }
276         if (!cList.isContainersLoaded ()) {
277             cList.clearContainers();
278             cList.setIsContainersLoaded (true);
279             // start check for correct rights.
280
if (jParams != null) { // no jParams, can't check for rights
281
JahiaUser currentUser = jParams.getUser();
282                 if (currentUser != null) {
283                     logger.debug("loadContainerList(): checking rights...");
284                     // if the user has no read rights, return an empty list.
285
if (!cList.checkReadAccess(currentUser)) {
286                         logger.debug("loadContainerList(): NO read rights! -> returning empty list");
287                         return cList;
288                     }
289                     logger.debug("loadContainerList(): read rights OK");
290                 } else {
291                     throw new JahiaException("No user present !",
292                                              "No current user defined in the params in loadContainerList() method.",
293                                              JahiaException.USER_ERROR, JahiaException.ERROR_SEVERITY);
294                 }
295                 Vector ctnids = doContainerFilterSearchSort(jParams, loadRequest,
296                                                             cList.getDefinition().getName(), cList,
297                                                             cachedContainersFromContainerLists);
298
299                 if (ctnids == null) {
300                     ctnids = ServicesRegistry.getInstance().getJahiaContainersService()
301                                 .getctnidsInList(cList.getID(), loadRequest);
302                 }
303                 if (ctnids == null) {
304                     ctnids = new Vector();
305                 }
306                 loadContainerListWithContainers(cList, ctnids, loadFlag,
307                                                 jParams, loadRequest, cachedFieldsFromContainers,
308                                                 cachedContainersFromContainerLists,
309                                                 cachedContainerListsFromContainers,currentUser);
310             }
311         }
312
313         return cList;
314     }
315
316     /**
317      * Fully load a container list (containers and their own container lists...).
318      *
319      * @param int ctnListID , a container list ID.
320      * @param jParams , used to handle all filtering
321      * @param loadFlag
322      * @param cachedFieldsFromContainers, map with key=ctnid, obj=Vector of the ctn's field ids.
323      * can be null, but for performance this map should be loaded with all fields of same page ID as the container's pageID.
324      *
325      * @return a fully loaded Container.
326      *
327      * @throws JahiaException
328      */

329     public JahiaContainerList fullyLoadContainerList (int ctnListID, int loadFlag,
330                                                       ParamBean jParams,
331                                                       EntryLoadRequest loadRequest,
332                                                       Hashtable cachedFieldsFromContainers,
333                                                       Hashtable cachedContainersFromContainerLists,
334                                                       Hashtable cachedContainerListsFromContainers)
335             throws JahiaException {
336         JahiaContainerList cList = ServicesRegistry.getInstance ().getJahiaContainersService ()
337                 .loadContainerListInfo (ctnListID, loadRequest);
338         cList = fullyLoadContainerList(cList, loadFlag, jParams, loadRequest,
339                         cachedFieldsFromContainers, cachedContainersFromContainerLists,
340                         cachedContainerListsFromContainers);
341
342         if ( cList != null ){
343             ContainerListFactoryProxy cListFactory =
344                     new ContainerListFactoryProxy(loadFlag,
345                                                   jParams,
346                                                   loadRequest,
347                                                   cachedFieldsFromContainers,
348                                                   cachedContainersFromContainerLists,
349                                                   cachedContainerListsFromContainers);
350             cList.setFactoryProxy(cListFactory);
351         }
352         return cList;
353     }
354
355
356     //-------------------------------------------------------------------------
357
/**
358      * Apply searching, filtering and sorting on the given container list.
359      *
360      * @param ParamBean jParams, the param bean.
361      * @param String the container list name.
362      * @param int the container list id.
363      *
364      * @return Vector of matching ctn ids, sorted and filtered.
365      *
366      * @auhtor NK
367      */

368     private Vector doContainerFilterSearchSort (ParamBean params,
369                                                 EntryLoadRequest loadVersion,
370                                                 String JavaDoc containerListName,
371                                                 JahiaContainerList clist,
372                                                 Hashtable cachedContainersFromContainerLists)
373             throws JahiaException {
374
375         if (params == null || containerListName == null || clist.getID() == -1) {
376             return null;
377         }
378
379         try {
380
381             BitSet resultBitSet = null;
382             BitSet filterBitSet = null;
383             JahiaSearcher cSearcher = null;
384             JahiaSearchResult searchResult = null;
385             ContainerFilters cFilters = null;
386             boolean resultHasChanged = false;
387
388             // Apply container list search if needed.
389
cSearcher = getCtnListSearcher (params, containerListName, clist.getID());
390             if (cSearcher != null) {
391                 searchResult = (JahiaSearchResult) cSearcher.getResult ();
392             }
393             if (searchResult != null) {
394                 if (searchResult.bits () != null) {
395                     resultBitSet = (BitSet) searchResult.bits ().clone ();
396                 }
397             }
398
399             // Apply container list filtering if needed.
400
if ((searchResult == null) ||
401                     !(searchResult != null && searchResult.getHitCount () == 0)) {
402                 cFilters = getCtnListFilters (params, containerListName, clist.getID());
403                 if (cFilters != null) {
404                     if (cFilters.bits () != null) {
405                         filterBitSet = (BitSet) cFilters.bits ().clone ();
406                     }
407                 }
408             }
409
410             // Retrieve previous search and filtering bitset from cache
411
HttpSession JavaDoc session = params.getSession ();
412             BitSet cachedResultBitSet = null;
413             if (session != null) {
414                 cachedResultBitSet =
415                         (BitSet) session.getAttribute (clist +
416                         "_searchfiltering_result_bitset" + "_" + params.getEntryLoadRequest().toString());
417
418             }
419  /*
420             resultHasChanged = ((cSearcher != null &&
421                     cSearcher.getUpdateStatus ()) ||
422                     (cFilters != null && cFilters.getUpdateStatus ())); */

423             resultHasChanged = ((cSearcher != null) ||
424                     (cFilters != null && cFilters.getUpdateStatus ()));
425             
426             if (resultHasChanged) {
427                 if (resultBitSet != null) {
428                     if (filterBitSet != null) {
429                         resultBitSet.and (filterBitSet);
430                     }
431                 } else if (filterBitSet != null) {
432                     resultBitSet = filterBitSet;
433                 }
434                 resultHasChanged = true;
435             } else {
436                 resultBitSet = cachedResultBitSet;
437             }
438
439             if (session != null) {
440                 session.setAttribute (clist + "_searchfiltering_result_bitset" + "_" + params.getEntryLoadRequest().toString(),
441                         resultBitSet);
442             }
443
444             if (resultBitSet != null && resultBitSet.length () == 0) {
445                 return (new Vector ());
446             }
447
448             // Apply Sorting if needed.
449
ContainerSorterInterface sorter = getContainerSorter (params,
450                     containerListName,
451                     clist,
452                     resultHasChanged,
453                     resultBitSet);
454             if (sorter != null && sorter.result () != null) {
455                 return sorter.result ();
456             } else {
457                 // it's a fake sorter or sorting failed
458
// so return the list of matching ctn ids, without sorting
459

460                 try {
461                     Vector ctnIds = null;
462
463                     // get all container ids
464
/*
465                     ctnIds = ServicesRegistry.getInstance ()
466                             .getJahiaContainersService ().getctnidsInList (clistID, loadVersion);
467                     */

468
469                     if (resultBitSet == null) {
470                         ctnIds =
471                                 ServicesRegistry.getInstance ()
472                                 .getJahiaContainersService ()
473                                 .getctnidsInList (clist.getID(), loadVersion);
474
475                         return ctnIds;
476                     } else {
477                         ctnIds = ServicesRegistry.getInstance ()
478                                 .getJahiaContainersService ().getCtnIds (true);
479
480                         Vector v = new Vector ();
481                         int size = ctnIds.size ();
482                         for (int i = 0; i < size; i++) {
483                             int ctnid = ((Integer JavaDoc) ctnIds.elementAt (i)).
484                                     intValue ();
485                             if (resultBitSet.get (ctnid)) {
486                                 v.add (ctnIds.elementAt (i));
487                             }
488                         }
489                         return v;
490                     }
491                 } catch (Throwable JavaDoc t) {
492                     logger.warn (t);
493                 }
494             }
495
496         } catch (Throwable JavaDoc t) {
497             logger.warn (t);
498
499             t.printStackTrace ();
500         }
501         return null; // on any error return null vector, so Jahia will return all
502
// containers without search, filtering or sorting of any sort.
503
}
504
505     //--------------------------------------------------------------------------
506
/**
507      * Apply container search if needed
508      *
509      * @param ParamBean jParams, the param bean.
510      * @param String the container list name.
511      * @param int the container list id.
512      *
513      * @return ContainerSearcher, can be null
514      *
515      * @author NK
516      */

517     private JahiaSearcher getCtnListSearcher (ParamBean params,
518                                                   String JavaDoc containerListName,
519                                                   int clistID)
520             throws JahiaException {
521
522         JahiaSearcher cSearcher = (JahiaSearcher) params.getRequest ()
523                 .getAttribute (containerListName +
524                 "_search_handler");
525
526         // retrieve the previous searcher cached in session
527
/*
528         JahiaSearcher cachedContainerSearcher = null;
529         HttpSession session = params.getSession ();
530         if (session != null) {
531             cachedContainerSearcher =
532                     (JahiaSearcher) session.getAttribute (clistID + "_"
533                     + params.getPageID () + "_" + containerListName +
534                     "_search_handler" + "_" + params.getEntryLoadRequest().toString());
535         }
536         */

537         // Check if a search must be applyed to this container list.
538
/*
539         String searchQuery =
540                 params.getParameter (
541                         JahiaSearchConstant.CLIST_SEARCHQUERY_INPUT_PREFIX
542                 + containerListName);
543         */

544
545         if (cSearcher == null || cSearcher.getQuery() == null || "".equals(cSearcher.getQuery().trim())) {
546             // create a fake searcher
547
cSearcher = new ContainerSearcher (clistID, "",
548                     params.getEntryLoadRequest ());
549             ((ContainerSearcher)cSearcher).setUpdateStatus();
550         } else {
551
552             boolean doNewSearch = true;
553
554             JahiaSearcher customSearcher = null;
555             if ( cSearcher instanceof ContainerSearcher ){
556                 ContainerSearcher cs = (ContainerSearcher)cSearcher;
557                 if (!cs.isSiteModeSearching ()) {
558                     customSearcher = new ContainerSearcher (clistID,
559                             cs.getContainerLevel (),
560                             cs.getQuery (),
561                             cs.getEntryLoadRequest ());
562                 } else {
563                     customSearcher = cs;
564                 }
565             } else if ( cSearcher instanceof ChainedJahiaSearcher ){
566                 JahiaSearcher[] ar = ((ChainedJahiaSearcher)cSearcher).getChain();
567                 ContainerSearcher cs = null;
568                 for ( int i=0; i<ar.length; i++ ){
569                     cs = (ContainerSearcher)ar[i];
570                     if (!cs.isSiteModeSearching ()) {
571                         customSearcher = new ContainerSearcher (clistID,
572                                 cs.getContainerLevel (),
573                                 cs.getQuery (),
574                                 cs.getEntryLoadRequest ());
575                         ar[i] = customSearcher;
576                     }
577                 }
578                 customSearcher = cSearcher;
579             }
580
581             // Due the delayed time of content indexation, we should not cache search, or
582
/*
583             if (cachedContainerSearcher != null
584                     && cachedContainerSearcher.getEntryLoadRequest().toString().equals(customSearcher.getEntryLoadRequest().toString())
585                     && (cachedContainerSearcher.getQuery () != null)
586                     && cachedContainerSearcher.getQuery ().equals (searchQuery)
587                     && cachedContainerSearcher.getLanguageCodes ().contains (params.getLocale ().
588                     toString ())) {
589                 // check if the container list has been changed the last time the search was performed
590                 long lastSearchTime = cachedContainerSearcher.getLastSearchTime ();
591
592                 ContainersChangeEventListener listener = (
593                         ContainersChangeEventListener)JahiaListenersRegistry.
594                         getInstance ()
595                         .getListener (ContainersChangeEventListener.class.getName ());
596                 if (listener != null) {
597                     long lastCtnListChangeTime = listener.getContainerLastChangeTime();
598                     if (lastCtnListChangeTime <= lastSearchTime) {
599                         cSearcher = cachedContainerSearcher;
600                         cSearcher.resetUpdateStatus (); // to indicate no new search has been launched.
601                         logger.debug ("Container Searcher found in session with same query and ctnlList did not change -> no need to run search again.");
602                         doNewSearch = false;
603                     }
604                 }
605             }*/

606
607             if (doNewSearch) {
608                 /*
609                 ArrayList languageCodes = new ArrayList ();
610                 languageCodes.add (params.getLocale ().toString ());
611                 cSearcher = new ContainerSearcher (clistID, languageCodes);
612                 */

613                 customSearcher.search (customSearcher.getQuery (), params);
614                 logger.debug ("Container Searcher launched new search");
615                 cSearcher = customSearcher;
616
617             }
618         }
619
620         // cache the container searcher in session
621
/*
622         if (session != null && cSearcher != null) {
623             logger.debug ("cSearcher is not null");
624             session.setAttribute (clistID + "_" + params.getPageID () + "_"
625                     + containerListName + "_search_handler" + "_" + params.getEntryLoadRequest().toString(),
626                     cSearcher);
627         }
628         */

629         return cSearcher;
630     }
631
632     //--------------------------------------------------------------------------
633
/**
634      * Apply container filtering if needed
635      *
636      * @param ParamBean jParams, the param bean.
637      * @param String the container list name.
638      * @param int the container list id.
639      *
640      * @return ContainerFilters, can be null
641      *
642      * @author NK
643      */

644     private ContainerFilters getCtnListFilters (ParamBean params,
645                                                 String JavaDoc containerListName,
646                                                 int clistID)
647             throws JahiaException {
648
649         if (logger.isDebugEnabled())
650           logger.debug ("Started for container list : " +
651                   containerListName + "[" + clistID + "]");
652
653         ContainerFilters cFilters = (ContainerFilters) params.getRequest ()
654                 .getAttribute (containerListName +
655                 "_filter_handler");
656
657         // retrieve the previous searcher cached in session
658
ContainerFilters cachedContainerFilters = null;
659         HttpSession JavaDoc session = params.getSession ();
660         if (session != null) {
661             cachedContainerFilters =
662                     (ContainerFilters) session.getAttribute (clistID + "_"
663                     + params.getPageID () + "_" + containerListName +
664                     "_filter_handler" + "_" + params.getEntryLoadRequest().toString());
665         }
666
667         if (cFilters == null || !cFilters.isQueryValid ()) {
668
669             logger.debug ("create fake filter");
670
671             // create a fake filter
672
cFilters = new ContainerFilters (clistID, new Vector ());
673             cFilters.setUpdateStatus();
674         } else {
675
676             boolean doNewFiltering = true;
677
678             // We need to create a container filter handler with the correct container list ID!
679
cFilters.setCtnListID (clistID);
680             /*
681             else {
682                 customCFilters = new ContainerFilters (
683                         cFilters.getContainerFilters (), cFilters.getSiteId(),
684                         cFilters.getContainerDefinitionName());
685             }*/

686
687
688             if (cachedContainerFilters != null
689                     && (cachedContainerFilters.getQuery () != null)
690                     &&
691                     cachedContainerFilters.getQuery ().equals (cFilters.getQuery ())) {
692                 // check if containers has changed the last time the filtering was performed
693
long lastFilteringTime = cachedContainerFilters.
694                         getLastFilteringTime ();
695                 ContainersChangeEventListener listener = (
696                         ContainersChangeEventListener)JahiaListenersRegistry.
697                         getInstance ()
698                         .getListener (ContainersChangeEventListener.class.getName ());
699                 if (listener != null) {
700                     long lastCtnChangeTime = listener.getContainerLastChangeTime();
701                     if (lastCtnChangeTime <= lastFilteringTime) {
702                         cFilters = cachedContainerFilters;
703                         cFilters.resetUpdateStatus (); // to indicate no new filtering has been launched.
704
logger.debug ("Container Filters found in session with same query and ctnlList did not change -> no need to run filtering again.");
705                         doNewFiltering = false;
706                     }
707                 }
708             }
709
710
711             if (doNewFiltering) {
712                 cFilters.doFilter ();
713                 logger.debug ("Container Filters launched new filtering");
714             }
715         }
716
717         // cache the container filters in session
718
if (session != null && cFilters != null) {
719             session.setAttribute (clistID + "_" + params.getPageID () + "_" +
720                     containerListName + "_filter_handler" + "_" + params.getEntryLoadRequest().toString(), cFilters);
721         }
722         return cFilters;
723     }
724
725     //--------------------------------------------------------------------------
726
/**
727      * Apply container sorting if needed
728      *
729      * @param ParamBean jParams, the param bean.
730      * @param String the container list name.
731      * @param int the container list id.
732      * @param boolean filteringResultHasChanged, does pre-filtering
733      * and searching 's result change ?.
734      * @param BitSet the filtering and search result as BitSet
735      *
736      * @return ContainerSorterBean, the container sort handler
737      *
738      * @author NK
739      */

740     private ContainerSorterInterface getContainerSorter (ParamBean params,
741                                                          String JavaDoc containerListName,
742                                                          JahiaContainerList clist,
743                                                          boolean resultHasChanged,
744                                                          BitSet resultBitSet)
745             throws JahiaException {
746         ContainerSorterInterface sorter = (ContainerSorterInterface) params.getRequest ()
747                 .getAttribute (containerListName +
748                 "_sort_handler");
749
750         // retrieve the previous sorter cached in session
751
ContainerSorterInterface cachedSorter = null;
752         HttpSession JavaDoc session = params.getSession ();
753         if (session != null) {
754             cachedSorter =
755                     (ContainerSorterInterface) session.getAttribute (clist + "_"
756                     + params.getPageID () + "_" + containerListName + "_sort_handler" + "_" + params.getEntryLoadRequest().toString());
757         }
758
759         if (sorter == null || !sorter.isValid ()) {
760
761             // create a fake sorter
762
sorter = new ContainerSorterBean (clist.getID(), "",
763                     params.getEntryLoadRequest ()); // with undefined sorting field
764
sorter.setUpdateStatus();
765         } else {
766
767             logger.debug ("Found sort hanlder on field [" +
768                     sorter.getSortingFieldName () + "]");
769
770             boolean doNewSorting = true;
771
772             // We need to create a container sort handler with the correct container list ID!
773
//ContainerSorterInterface sorter = null;
774

775             /*
776             // @todo : have these dynamic !!!
777             if (sorter instanceof ContainerSorterBean) {
778                 customCSorter = new ContainerSorterBean (clistID,
779                         sorter.getSortingFieldName (), sorter.getEntryLoadRequest ());
780                 customCSorter.setAscOrdering (sorter.isAscOrdering ());
781                 customCSorter.setNumberOrdering (sorter.isNumberOrdering ());
782             } else {
783                 customCSorter = sorter;
784             }*/

785
786             sorter.setCtnListID(clist.getID());
787
788             if (cachedSorter != null
789                 && !resultHasChanged
790                 && cachedSorter.getEntryLoadRequest().toString().equals(sorter.getEntryLoadRequest().toString())
791                 && cachedSorter.isValid ()
792                 && cachedSorter.getSortingFieldName ().equals (sorter.getSortingFieldName ())
793                 && (cachedSorter.isAscOrdering () == sorter.isAscOrdering ())
794                     ) {
795                 // check if containers has changed since the last time the sorting was performed
796
long lastSortingTime = cachedSorter.getLastSortingTime ();
797                 ContainersChangeEventListener listener = (
798                         ContainersChangeEventListener)JahiaListenersRegistry.
799                         getInstance ()
800                         .getListener (ContainersChangeEventListener.class.getName ());
801                 if (listener != null) {
802                     long lastCtnChangeTime = listener.getContainerLastChangeTime();
803                     if (lastCtnChangeTime <= lastSortingTime) {
804                         sorter = cachedSorter;
805                         sorter.resetUpdateStatus (); // to indicate no new sorting has been launched.
806
logger.debug ("Container Sorter found in session with same sorting field and ctnlList did not change -> no need to run sorting again.");
807                         doNewSorting = false;
808                     }
809                 }
810             }
811
812             if (doNewSorting) {
813                 sorter.doSort (resultBitSet);
814                 logger.debug ("Container Sorter launched new sort");
815             }
816         }
817
818         // cache the container filters in session
819
if (session != null && sorter != null) {
820             session.setAttribute (clist + "_" + params.getPageID () + "_"
821                     + containerListName + "_sort_handler" + "_" + params.getEntryLoadRequest().toString(), sorter);
822         }
823         return sorter;
824     }
825
826     /**
827      * Loads a set of containers for a given container list
828      * Loads container list info and containers, but not dependant container lists
829      * this method can load request-specific values (i.e. applications).
830      * DO NOT CACHE THIS METHOD (it depends on other caches values) !!
831      *
832      * @param containerListID the container list id
833      * @param ctnids vector of container ids to load.
834      * @param loadFlag the loadFlag
835      * @param jParams the ParamBean object, containing request and response
836      * @param loadVersion
837      * @param cachedFieldsInContainer
838      * @param cachedContainersFromContainerLists
839      * @return
840      * @throws JahiaException
841      */

842     protected void loadContainerListWithContainers (
843                                                  JahiaContainerList theContainerList,
844                                                  Vector ctnids,
845                                                  int loadFlag,
846                                                  ParamBean jParams,
847                                                  EntryLoadRequest loadVersion,
848                                                  Hashtable cachedFieldsInContainer,
849                                                  Hashtable cachedContainersFromContainerLists,
850                                                  Hashtable cachedContainerListsFromContainers,
851                                                  JahiaUser currentUser)
852             throws JahiaException {
853
854         if (theContainerList == null) {
855             return;
856         }
857
858         // start check for correct rights.
859
if (jParams != null) { // no jParams, can't check for rights
860

861             // Check container ACL access.
862
Map acls = new Hashtable();
863             if (ctnids.size () > 0) {
864                 
865                 JahiaACLManagerService.getInstance()
866                     .preloadContainerACLsByPage(theContainerList.getPageID());
867
868 // JahiaACLManagerService.getInstance()
869
// .preloadFieldACLsByPage(theContainerList.getPageID());
870
Integer JavaDoc listID = new Integer JavaDoc(theContainerList.getID());
871                 if ( (theContainerList.getID() > 0) &&
872                      !ContainerFactory.preloadedCtnrACLIDByCtnrListCache
873                      .containsKey(listID) ){
874                     acls = c_utils.db_get_all_containers_aclid(theContainerList.getID());
875                     if ( acls != null ){
876                         ContainerFactory.preloadedCtnrACLIDByCtnrListCache.put(listID, acls);
877                     }
878                 } else {
879                     acls = (Hashtable)ContainerFactory.preloadedCtnrACLIDByCtnrListCache
880                         .get(listID);
881                 }
882                 if ( acls == null ){
883                     acls = new Hashtable();
884                 }
885                 Vector v = new Vector ();
886                 int size = ctnids.size ();
887                 Integer JavaDoc ctnID = null;
888                 Integer JavaDoc aclID = null;
889                 for (int i = 0; i < size; i++) {
890                     try {
891                         ctnID = (Integer JavaDoc) ctnids.get (i);
892                         aclID = (Integer JavaDoc) acls.get (ctnID);
893                         if ( aclID == null ){
894                             aclID = new Integer JavaDoc(c_utils.getContainerACLID(ctnID.intValue()));
895                         }
896                         JahiaBaseACL acl = new JahiaBaseACL (aclID.intValue());
897                         if (acl != null &&
898                                 acl.getPermission (currentUser,
899                                         JahiaBaseACL.READ_RIGHTS)) {
900                             v.add (ctnID);
901                         }
902                     } catch (Throwable JavaDoc t) {
903                         logger.error("Exception when loading container list", t);
904                     }
905                 }
906                 ctnids = v;
907             }
908         }
909
910         // load partially the containers with correct entry load request
911
// we can do it now that containers are loaded on demand
912

913         Vector v = new Vector();
914         JahiaContainer thisContainer = null;
915         List listOfContainer = new ArrayList(ctnids.size());
916         final JahiaContainersService jahiaContainersService = ServicesRegistry.getInstance().getJahiaContainersService();
917         for (int i = 0; i < ctnids.size(); i++) {
918             int ctnid = ((Integer JavaDoc) ctnids.elementAt (i)).intValue ();
919             try {
920                 thisContainer = jahiaContainersService.loadContainer( ctnid, loadFlag, jParams, loadVersion,
921                         cachedFieldsInContainer,
922                         cachedContainerListsFromContainers,
923                         cachedContainerListsFromContainers);
924                 if (thisContainer != null && thisContainer.getID () != -1) { // no read rights on this container
925
v.add(new Integer JavaDoc(ctnid));
926                     listOfContainer.add(thisContainer);
927                 }
928             } catch (Throwable JavaDoc t) {
929                 String JavaDoc errorMsg = "Error loading container [" + ctnid + "]";
930                 if (loadVersion != null) {
931                     errorMsg += " loadVersion=" + loadVersion.toString ();
932                 }
933                 logger.debug (errorMsg);
934             }
935         }
936         ctnids = v;
937
938         // lets set the full size of the data for the container list.
939
theContainerList.setFullSize (ctnids.size ());
940
941         // init the container list pagination
942
JahiaContainerListPagination cListPagination = new
943                 JahiaContainerListPagination (theContainerList, jParams, -1);
944         theContainerList.setCtnListPagination (cListPagination);
945
946         int startPos = 0;
947         int endPos = cListPagination.getSize ();
948
949         if (cListPagination.isValid ()) {
950             startPos = cListPagination.getFirstItemIndex ();
951             endPos = cListPagination.getLastItemIndex ();
952         }
953         if (endPos < cListPagination.getSize ()) {
954             endPos += 1;
955         }
956         for (int i = startPos; i < endPos; i++) {
957             theContainerList.addContainer ((JahiaContainer) listOfContainer.get(i));
958         }
959         if (theContainerList != null) {
960             theContainerList.setIsContainersLoaded (true);
961         }
962
963     }
964 }
965
Popular Tags