1 package org.jahia.data.beans; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.Iterator ; 6 7 import org.jahia.content.comparators.ContainerComparator; 8 import org.jahia.content.comparators.ContainerListComparator; 9 import org.jahia.content.comparators.ObjectTypeDispatcherComparator; 10 import org.jahia.content.comparators.PageByTitleComparator; 11 import org.jahia.exceptions.JahiaException; 12 import org.jahia.params.ParamBean; 13 import org.jahia.services.categories.Category; 14 15 import java.util.Properties ; 16 import org.jahia.services.fields.ContentField; 17 import org.jahia.services.containers.ContentContainer; 18 import org.jahia.services.containers.ContentContainerList; 19 import org.jahia.content.*; 20 import org.jahia.registries.ServicesRegistry; 21 import org.jahia.data.fields.JahiaField; 22 import org.jahia.data.fields.LoadFlags; 23 import org.jahia.data.containers.JahiaContainer; 24 import org.jahia.data.containers.JahiaContainerList; 25 import org.jahia.services.pages.ContentPage; 26 import org.jahia.services.pages.JahiaPage; 27 28 39 40 public class CategoryBean extends AbstractJahiaObjectBean { 41 42 private static org.apache.log4j.Logger logger = 43 org.apache.log4j.Logger.getLogger(CategoryBean.class); 44 45 private ParamBean paramBean; 46 private Category category; 47 48 static { 49 registerType(Category.class.getName(), CategoryBean.class.getName()); 50 } 51 52 55 public CategoryBean () { 56 } 57 58 64 public CategoryBean (Category category, ParamBean paramBean) { 65 this.category = category; 66 this.paramBean = paramBean; 67 } 68 69 78 public static AbstractJahiaObjectBean getChildInstance (JahiaObject 79 jahiaObject, 80 ParamBean paramBean) { 81 return new CategoryBean( (Category) jahiaObject, paramBean); 82 } 83 84 87 public ParamBean getParamBean () { 88 return paramBean; 89 } 90 91 94 public Category getCategory () { 95 return category; 96 } 97 98 109 public ArrayList getChildsOfType (String type) { 110 ArrayList filteredChildsOfType = new ArrayList (); 111 ArrayList fullChildsOfType = null; 112 try { 113 fullChildsOfType= getSortedChildJahiaObjects(type); 114 } catch (Throwable t) { 115 logger.error("Error while retrieving child objects", t); 116 return filteredChildsOfType; 117 } 118 Iterator childsIter = fullChildsOfType.iterator(); 119 124 while (childsIter.hasNext()) { 125 JahiaObject curJahiaObject = (JahiaObject) childsIter.next(); 126 if (curJahiaObject instanceof ContentObject) { 127 ContentObject curContentObject = (ContentObject) curJahiaObject; 128 129 139 boolean acceptObject = false; 140 try { 141 if (curContentObject instanceof ContentField) { 142 int fieldID = ( (ContentField) curContentObject).getID(); 143 JahiaField jahiaField = ServicesRegistry.getInstance(). 144 getJahiaFieldService(). 145 loadField(fieldID, 146 LoadFlags.ALL, paramBean, 147 paramBean.getEntryLoadRequest()); 148 149 if (jahiaField != null) { 150 acceptObject = true; 151 } 152 } else if (curContentObject instanceof ContentContainer) { 153 ContentContainer contentContainer = (ContentContainer) curContentObject; 154 JahiaContainer jahiaContainer = contentContainer.getJahiaContainer(paramBean, paramBean.getEntryLoadRequest()); 155 if (jahiaContainer != null) { 156 acceptObject = true; 157 } 158 } else if (curContentObject instanceof ContentContainerList) { 159 ContentContainerList contentContainerList = (ContentContainerList) curContentObject; 160 JahiaContainerList jahiaContainerList = contentContainerList.getJahiaContainerList(paramBean, paramBean.getEntryLoadRequest()); 161 if (jahiaContainerList != null) { 162 acceptObject = true; 163 } 164 } else if (curContentObject instanceof ContentPage) { 165 ContentPage contentPage = (ContentPage) curContentObject; 166 JahiaPage jahiaPage = contentPage.getPage(paramBean.getEntryLoadRequest(), paramBean.getOperationMode(), paramBean.getUser()); 167 if (jahiaPage != null) { 168 acceptObject = true; 169 } 170 } 171 } catch (JahiaException je) { 172 182 acceptObject = false; 183 } 184 185 if (acceptObject) { 186 try { 187 AbstractJahiaObjectBean curObjectBean = 188 AbstractJahiaObjectBean. 189 getInstance(curContentObject, paramBean); 190 filteredChildsOfType.add(curObjectBean); 191 } catch (ClassNotFoundException cnfe) { 192 logger.error("Class not found while converting category child object to beans", cnfe); 193 } 194 } 195 } 196 } 197 198 return filteredChildsOfType; 199 } 200 201 215 public ArrayList getChildsOfTypeNoChecks (String type) { 216 ArrayList childsOfType = new ArrayList (); 217 try { 218 ArrayList childJahiaObjects = getSortedChildJahiaObjects(type); 219 220 childsOfType = jahiaObjectToBeans(childJahiaObjects); 221 } catch (ClassNotFoundException cnfe) { 222 logger.error("Error while retrieving category children of type [" + 223 type + "]", cnfe); 224 } catch (JahiaException je) { 225 logger.error("Error while retrieving category children of type [" + 226 type + "]", je); 227 } 228 return childsOfType; 229 } 230 231 private ArrayList jahiaObjectToBeans (ArrayList childJahiaObjects) 232 throws ClassNotFoundException { 233 ArrayList beanList = new ArrayList (); 234 Iterator sortedJahiaObjectIter = childJahiaObjects.iterator(); 235 while (sortedJahiaObjectIter.hasNext()) { 236 JahiaObject curObject = (JahiaObject) sortedJahiaObjectIter. 237 next(); 238 AbstractJahiaObjectBean curObjectBean = AbstractJahiaObjectBean. 239 getInstance(curObject, paramBean); 240 beanList.add(curObjectBean); 241 } 242 return beanList; 243 } 244 245 private ArrayList getSortedChildJahiaObjects (String type) 246 throws ClassNotFoundException , JahiaException { 247 ArrayList allChildrenObjectKeys = category.getChildObjectKeys(); 248 Iterator allChildObjectKeysIter = allChildrenObjectKeys.iterator(); 249 ArrayList childJahiaObjects = new ArrayList (); 250 while (allChildObjectKeysIter.hasNext()) { 251 ObjectKey curObjectKey = (ObjectKey) allChildObjectKeysIter. 252 next(); 253 if (curObjectKey.getType().equals(type)) { 254 JahiaObject curObject = JahiaObject.getInstance( 256 curObjectKey); 257 if(curObject!=null) 259 childJahiaObjects.add(curObject); 260 } 261 } 262 267 ObjectTypeDispatcherComparator comparator = new 268 ObjectTypeDispatcherComparator(); 269 comparator.addTypeComparator(ContentContainerKey.CONTAINER_TYPE, 270 new ContainerComparator(paramBean)); 271 comparator.addTypeComparator(ContentContainerListKey. 272 CONTAINERLIST_TYPE, 273 new ContainerListComparator()); 274 comparator.addTypeComparator(ContentPageKey.PAGE_TYPE, 275 new PageByTitleComparator(paramBean)); 276 Collections.sort(childJahiaObjects, comparator); 277 return childJahiaObjects; 278 } 279 280 285 public ArrayList getChildCategoryBeans () { 286 ArrayList childCategoryBeans = new ArrayList (); 287 try { 288 ArrayList childCategories = category.getChildCategories(); 289 Iterator childCategoriesIter = childCategories.iterator(); 290 while (childCategoriesIter.hasNext()) { 291 Category curCategory = (Category) childCategoriesIter.next(); 292 CategoryBean curCategoryBean = new CategoryBean(curCategory, 293 paramBean); 294 childCategoryBeans.add(curCategoryBean); 295 } 296 } catch (JahiaException je) { 297 logger.error("Error while retrieving child categories of category " + 298 category, je); 299 } 300 return childCategoryBeans; 301 } 302 303 307 public String getTitle () { 308 return category.getTitle(paramBean.getLocale()); 309 } 310 311 314 public Properties getProperties() { 315 return category.getProperties(); 316 } 317 318 } | Popular Tags |