1 17 18 package org.jahia.data.containers; 19 20 21 import java.io.Serializable ; 22 import java.util.ArrayList ; 23 import java.util.BitSet ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import java.util.Vector ; 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 49 public class ContainerFilterByCategories implements Serializable , 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 containerDefinitionName; 57 58 private ContainerFilters containerFilters = null; 59 60 private Set catetories; 61 62 private boolean withAllContainersOfCompoundContainerList = false; 63 64 77 public ContainerFilterByCategories(Set 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 (); 87 } 88 this.withAllContainersOfCompoundContainerList = 89 withAllContainersOfCompoundContainerList; 90 } 91 92 100 public BitSet doFilter(int ctnListID) 101 throws JahiaException 102 { 103 BitSet result = null; 104 105 result = doFiltering(ctnListID); 106 107 return result; 108 } 109 110 117 private BitSet doFiltering(int ctnListID) 118 throws JahiaException 119 { 120 BitSet result = new BitSet (); 121 try { 122 ArrayList 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 t ){ 132 logger.debug("Exception :",t); 133 } 134 135 if ( this.withAllContainersOfCompoundContainerList ){ 136 int ctnListId = this.containerFilters.getCtnListID(); 137 Vector ids = ServicesRegistry.getInstance().getJahiaContainersService() 138 .getctnidsInList(ctnListId,this.entryLoadRequest); 139 int size = ids.size(); 140 for ( int i=0 ; i<size; i++ ){ 141 Integer I = (Integer )ids.get(i); 142 result.set(I.intValue()); 143 } 144 } 145 ContainerFilterByLoadRequest cfblr = 146 new ContainerFilterByLoadRequest(this.entryLoadRequest); 147 BitSet result2 = cfblr.doFilter(ctnListID); 148 149 result.and(result2); 150 return result; 151 152 } 153 154 161 public String getSelect(int ctnListID) 162 { 163 String loadRequest = ""; 164 if ( this.entryLoadRequest != null ){ 165 loadRequest = this.entryLoadRequest.toString(); 166 } 167 return (getCategoriesSearchQuery() + "_" + loadRequest); 168 } 169 170 177 public void setContainerFilters(ContainerFilters containerFilters){ 178 this.containerFilters = containerFilters; 179 } 180 181 199 public BitSet doFilterBySite(int siteId, String containerDefinitionName, int listId) 200 throws JahiaException 201 { 202 203 BitSet result = null; 204 result = doFilteringBySite(siteId, containerDefinitionName,listId); 205 return result; 206 } 207 208 222 private BitSet doFilteringBySite(int siteId, 223 String containerDefinitionName,int listId) 224 throws JahiaException 225 { 226 BitSet result = new BitSet (); 227 try { 228 ArrayList 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 t ){ 237 logger.debug("Exception :",t); 238 } 239 240 if ( this.withAllContainersOfCompoundContainerList ){ 241 Vector ids = ServicesRegistry.getInstance().getJahiaContainersService() 242 .getctnidsInList(listId,this.entryLoadRequest); 243 int size = ids.size(); 244 for ( int i=0 ; i<size; i++ ){ 245 Integer I = (Integer )ids.get(i); 246 result.set(I.intValue()); 247 } 248 } 249 ContainerFilterByLoadRequest cfblr = 250 new ContainerFilterByLoadRequest(this.entryLoadRequest); 251 BitSet result2 = cfblr.doFilterBySite(siteId,containerDefinitionName,listId); 252 result.and(result2); 253 return result; 254 } 255 256 268 public String getSelectBySiteID(int siteId, String containerDefinitionName) 269 { 270 String loadRequest = ""; 272 if ( this.entryLoadRequest != null ){ 273 loadRequest = this.entryLoadRequest.toString(); 274 } 275 return (getCategoriesSearchQuery() + "_" + loadRequest); 276 } 277 278 private ArrayList getContainers(Set categories) 279 throws ClassNotFoundException , JahiaException { 280 281 ArrayList val = new ArrayList (); 282 Iterator iterator = categories.iterator(); 283 Category category = null; 284 while ( iterator.hasNext() ){ 285 category = (Category)iterator.next(); 286 ArrayList allChildrenObjectKeys = category.getChildObjectKeys(); 287 Iterator allChildObjectKeysIter = allChildrenObjectKeys.iterator(); 288 ArrayList childJahiaObjects = new ArrayList (); 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 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 t){ 314 logger.debug("Error loading contentContainer " + curObjectKey.toString()); 315 } 316 } 317 } 318 } 319 return val; 320 } 321 322 private String getCategoriesSearchQuery(){ 323 StringBuffer buff = new StringBuffer ("("); 324 Iterator 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 |