1 package org.jahia.services.categories; 2 3 import org.jahia.content.*; 4 import org.jahia.exceptions.JahiaException; 5 import org.jahia.registries.ServicesRegistry; 6 7 import java.util.*; 8 9 22 23 public class Category extends JahiaObject implements PropertiesInterface { 24 25 private static org.apache.log4j.Logger logger = 26 org.apache.log4j.Logger.getLogger (Category.class); 27 28 private CategoryBean categoryBean = null; 29 private Properties properties = null; 30 private boolean propertiesLoaded = false; 31 32 protected Category (CategoryBean categoryBean) { 33 super (new CategoryKey (categoryBean.getId ())); 34 this.categoryBean = categoryBean; 35 } 36 37 49 static public Category createCategory (String key, Category parentCategory) 50 throws JahiaException { 51 CategoryBean categoryBean = new CategoryBean (); 52 categoryBean.setKey (key); 53 Category category = new Category (categoryBean); 54 ServicesRegistry.getInstance ().getCategoryService ().addCategory ( 55 category, parentCategory); 56 return category; 57 } 58 59 66 static public Category getRootCategory () 67 throws JahiaException { 68 return ServicesRegistry.getInstance ().getCategoryService (). 69 getRootCategory (); 70 } 71 72 81 static public Category getCategory (String key) 82 throws JahiaException { 83 return ServicesRegistry.getInstance ().getCategoryService ().getCategory ( 84 key); 85 } 86 87 96 static public Category getCategory (int categoryID) 97 throws JahiaException { 98 return ServicesRegistry.getInstance ().getCategoryService ().getCategory (categoryID); 99 } 100 101 111 static public JahiaObject getChildInstance (ObjectKey objectKey) { 112 try { 113 return ServicesRegistry.getInstance ().getCategoryService (). 114 getCategory (objectKey.getIdInType ()); 115 } catch (JahiaException je) { 116 logger.error ("Error while trying to load category from object key " + 117 objectKey.toString (), je); 118 return null; 119 } 120 } 121 122 133 static public Set getObjectCategories (ObjectKey objectKey) 134 throws JahiaException { 135 return ServicesRegistry.getInstance ().getCategoryService ().getObjectCategories ( 136 objectKey); 137 } 138 139 157 static public ArrayList findCategoriesByPropNameAndValue (String propName, 158 String propValue) { 159 ArrayList foundCategories = new ArrayList (); 160 try { 161 ArrayList categoryIDs = CategoryPropDB.getInstance (). 162 findCategoryIDsByPropNameAndValue (propName, 163 propValue); 164 Iterator categoryIDIter = categoryIDs.iterator (); 165 while (categoryIDIter.hasNext ()) { 166 Integer curCategoryID = (Integer ) categoryIDIter.next (); 167 Category curCategory = getCategory (curCategoryID.intValue ()); 168 foundCategories.add (curCategory); 169 } 170 } catch (JahiaException je) { 171 logger.error ( 172 "Error while trying to find categories by property name " + propName + "and property value " + propValue, 173 je); 174 } 175 return foundCategories; 176 } 177 178 183 static public Date getLastModificationDate() { 184 return ServicesRegistry.getInstance().getCategoryService().getLastModificationDate(); 185 } 186 187 public static Category getChildInstance (String IDInType) { 188 try { 189 return getCategory (Integer.parseInt (IDInType)); 190 } catch (JahiaException je) { 191 logger.debug ("Error retrieving container instance for id : " + IDInType, je); 192 } 193 return null; 194 } 195 196 199 public String getKey () { 200 if (this.categoryBean == null) { 201 return null; 202 } 203 return categoryBean.getKey (); 204 } 205 206 protected CategoryBean getCategoryBean () { 207 return categoryBean; 208 } 209 210 214 public ObjectKey getObjectKey () { 215 if (categoryBean == null) { 216 return null; 217 } 218 if (categoryBean.getId () == 0) { 219 return null; 220 } 221 return new CategoryKey (categoryBean.getId ()); 222 } 223 224 233 public ArrayList getChildCategories () 234 throws JahiaException { 235 ArrayList childKeys = getChildObjectKeys (); 236 ArrayList childCategories = new ArrayList (); 237 Iterator childKeyIter = childKeys.iterator (); 238 while (childKeyIter.hasNext ()) { 239 ObjectKey curKey = (ObjectKey) childKeyIter.next (); 240 if (curKey instanceof CategoryKey) { 241 Category curChildCategory = (Category) Category.getChildInstance (curKey); 242 childCategories.add (curChildCategory); 243 } 244 } 245 return childCategories; 246 } 247 248 258 public ArrayList getChildObjectKeys () 259 throws JahiaException { 260 return ServicesRegistry.getInstance ().getCategoryService (). 261 getCategoryChildKeys (this); 262 } 263 264 273 public ArrayList getChildContentObjects () 274 throws JahiaException { 275 ArrayList childKeys = getChildObjectKeys (); 276 ArrayList childContentObjects = new ArrayList (); 277 Iterator childKeyIter = childKeys.iterator (); 278 while (childKeyIter.hasNext ()) { 279 ObjectKey curKey = (ObjectKey) childKeyIter.next (); 280 if (curKey instanceof ContentObjectKey) { 281 try { 282 JahiaObject curChildCategory = JahiaObject.getInstance ( 283 curKey); 284 childContentObjects.add (curChildCategory); 285 } catch (ClassNotFoundException cnfe) { 286 logger.error ( 287 "Error while loading content object for object key " + 288 curKey, cnfe); 289 } 290 } 291 } 292 return childContentObjects; 293 } 294 295 305 public String getTitle (Locale locale) { 306 try { 307 return ServicesRegistry.getInstance ().getCategoryService (). 308 getTitleForCategory (this, locale); 309 } catch (JahiaException je) { 310 logger.error ( 311 "Error while trying to retrieve title for category " + this.getObjectKey () 312 .toString (), 313 je); 314 return null; 315 } 316 } 317 318 324 public void setTitle (Locale locale, String title) { 325 try { 326 ServicesRegistry.getInstance ().getCategoryService ().setTitleForCategory (this, 327 locale, title); 328 } catch (JahiaException je) { 329 logger.error ( 330 "Error while trying to set title " + title + " for category " + this.getObjectKey () 331 .toString (), 332 je); 333 } 334 } 335 336 344 public void delete () 345 throws JahiaException { 346 CategoryPropDB.getInstance ().removeProperties (categoryBean.getId ()); 347 ServicesRegistry.getInstance ().getCategoryService ().removeCategory (this); 348 } 349 350 359 public void addChildObjectKey (ObjectKey childObjectKey) 360 throws JahiaException { 361 ServicesRegistry.getInstance ().getCategoryService ().addObjectKeyToCategory (this, 362 childObjectKey); 363 } 364 365 375 public void removeChildObjectKey (ObjectKey childObjectKey) 376 throws JahiaException { 377 ServicesRegistry.getInstance ().getCategoryService ().removeObjectKeyFromCategory ( 378 this, childObjectKey); 379 } 380 381 384 public Properties getProperties () { 385 if (propertiesLoaded) { 386 return properties; 387 } else { 388 try { 389 properties = CategoryPropDB.getInstance ().getProperties (categoryBean.getId ()); 390 } catch (JahiaException je) { 391 logger.error ( 392 "Error while loading category " + categoryBean.getId () + " properties", 393 je); 394 } 395 propertiesLoaded = true; 396 return properties; 397 } 398 } 399 400 407 public void setProperties (Properties newProperties) { 408 properties = newProperties; 409 try { 410 CategoryPropDB.getInstance ().setProperties (categoryBean.getId (), properties); 411 ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate(); 412 propertiesLoaded = true; 413 } catch (JahiaException je) { 414 logger.error ( 415 "Error while setting new properties for category " + categoryBean.getId (), 416 je); 417 } 418 } 419 420 426 public String getProperty (String propertyName) { 427 if (getProperties () != null) { 428 return getProperties ().getProperty (propertyName); 429 } else { 430 return null; 431 } 432 } 433 434 441 public void setProperty (String propertyName, String propertyValue) { 442 if (getProperties () == null) { 443 properties = new Properties (); 444 propertiesLoaded = true; 445 } 446 try { 447 getProperties ().setProperty (propertyName, propertyValue); 448 CategoryPropDB.getInstance ().setProperty (categoryBean.getId (), propertyName, propertyValue); 449 ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate(); 450 } catch (JahiaException e) { 451 logger.error ( 452 "Error while setting new property for category " + categoryBean.getId (), 453 e); 454 } 455 } 456 457 463 public void removeProperty (String propertyName) { 464 if (getProperties () != null) { 465 getProperties ().remove (propertyName); 466 try { 467 CategoryPropDB.getInstance ().removeProperty (categoryBean.getId (), propertyName); 468 ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate(); 469 } catch (JahiaException e) { 470 logger.error ( 471 "Error while removing property for category " + categoryBean.getId (), 472 e); 473 } 474 } 475 } 476 } 477 | Popular Tags |