1 13 18 package org.jahia.data.containers; 19 20 import java.util.ArrayList ; 21 import java.util.Enumeration ; 22 import java.util.Properties ; 23 import java.util.Vector ; 24 25 import org.apache.log4j.Logger; 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.registries.JahiaContainerDefinitionsRegistry; 28 import org.jahia.services.acl.ACLNotFoundException; 29 import org.jahia.services.acl.JahiaBaseACL; 30 import org.jahia.services.containers.ContainerListFactoryProxy; 31 import org.jahia.services.containers.ContentContainerList; 32 import org.jahia.services.usermanager.JahiaUser; 33 import java.io.Serializable ; 34 35 44 45 public class JahiaContainerList implements Cloneable , Serializable { 46 47 private static Logger logger = Logger.getLogger(JahiaContainerList.class); 48 49 private int ID; 50 private int parentEntryID; 51 private int pageID; 52 private int ctndefid; 53 private int aclID; 54 private int fullDataSize = 0; 55 56 private boolean isContainersLoaded = false; 58 private Properties ctnListProperties = new Properties (); 59 60 private JahiaContainerListPagination ctnListPagination; 61 62 63 66 private ArrayList containers = new ArrayList (); 67 68 private ContainerListFactoryProxy cListFactoryProxy; 69 70 public JahiaContainerList( int anID, 72 int aParentEntryID, 73 int aPageID, 74 int aCtndefid, 75 int anAclID ) 76 { 77 this.ID = anID; 78 this.parentEntryID = aParentEntryID; 79 this.pageID = aPageID; 80 this.ctndefid = aCtndefid; 81 this.aclID = anAclID; 82 } 84 90 public void setFactoryProxy(ContainerListFactoryProxy aCListFactoryProxy){ 91 this.cListFactoryProxy = aCListFactoryProxy; 92 } 93 94 public int getID() { return ID; } 96 public int getParentEntryID() { return parentEntryID; } 97 public int getPageID() { return pageID; } 98 public int getctndefid() { return ctndefid; } 99 public final int getAclID() { return aclID; } 100 101 106 public boolean isContainersLoaded() 107 { 108 return isContainersLoaded; 109 } 110 111 public JahiaContainerListPagination getCtnListPagination() { 112 checkProxy(); 113 return ctnListPagination; 114 } 115 116 public final JahiaBaseACL getACL() { 117 JahiaBaseACL acl = null; 118 try { 119 acl = new JahiaBaseACL(getAclID()); 120 } catch ( Throwable t ) { 121 t.printStackTrace(); 122 } 123 return acl; 124 } 125 126 public void setID( int anID ) { this.ID = anID; } 127 public void setAclID( int anAclID ) { this.aclID = anAclID; } 128 public void setParentEntryID(int aParentEntryID) { this.parentEntryID = aParentEntryID; } 129 130 public void setCtnListPagination( int windowSize, int windowOffset ) 131 { 132 this.ctnListPagination = new JahiaContainerListPagination(this.getFullSize(),windowSize,windowOffset); 133 } 134 135 public void setCtnListPagination( JahiaContainerListPagination cListPagination ) 136 { 137 this.ctnListPagination = cListPagination; 138 } 139 140 146 public void setIsContainersLoaded( boolean value ) 147 { 148 this.isContainersLoaded = value; 149 } 150 151 152 154 155 156 159 public JahiaContainer getContainer( int index ) throws JahiaException 160 { 161 checkProxy(); 162 if (index < containers.size()) { 163 return (JahiaContainer) containers.get(index); 164 } else { 165 String errorMsg = "Error in JahiaContainerList : trying to get entry " + index + " for container " + getDefinition().getName(); 166 logger.error(errorMsg + " -> BAILING OUT"); 167 throw new JahiaException( "Error in database synchronisation", 168 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY ); 169 } 170 } 172 173 174 177 public Enumeration getContainers() 178 { 179 checkProxy(); 180 Vector tempVector = new Vector (containers); 181 return tempVector.elements(); 182 } 184 191 public int size() { 192 checkProxy(); 193 if ( containers != null ) 194 { 195 return containers.size(); 196 } 197 return 0; 198 } 199 200 205 public void setFullSize(int aFullDataSize) { 206 if (aFullDataSize >= 0) { 207 this.fullDataSize = aFullDataSize; 208 } 209 } 210 211 217 public int getFullSize() { 218 checkProxy(); 219 return this.fullDataSize; 220 } 221 222 226 public JahiaContainerDefinition getDefinition() 227 throws JahiaException 228 { 229 JahiaContainerDefinition theDef = JahiaContainerDefinitionsRegistry.getInstance( 230 ).getDefinition( ctndefid ); 231 if (theDef != null) { 232 return theDef; 233 } else { 234 String msg = "JahiaContainer definition " + ctndefid + " not found in definition registry !"; 235 throw new JahiaException( "Synchronisation error in database", 236 msg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY ); 237 } 238 } 240 241 242 247 public void addContainer( JahiaContainer theContainer ) 248 { 249 checkProxy(); 250 262 containers.add(theContainer); 263 264 } 266 267 277 public final boolean isFieldEditable(String fieldDefName, JahiaUser user){ 278 boolean result = false; 279 if ( fieldDefName == null || user == null ) 280 return false; 281 282 String val = this.getProperty("view_field_acl_"+fieldDefName); 283 if ( val != null ){ 284 try { 285 int anAclID = Integer.parseInt(val); 286 JahiaBaseACL theACL = null; 287 try { 288 theACL = new JahiaBaseACL (anAclID); 289 } 290 catch (ACLNotFoundException ex) { 291 } 292 catch (JahiaException ex) { 293 } 294 return theACL.getPermission(user,JahiaBaseACL.WRITE_RIGHTS); 295 } catch ( Throwable t ){ 296 } 297 } 298 return result; 299 } 300 301 311 public final boolean isFieldReadable(String fieldDefName, JahiaUser user){ 312 if ( fieldDefName == null || user == null ) 313 return false; 314 315 String val = this.getProperty("view_field_acl_"+fieldDefName); 316 if ( val != null ){ 317 try { 318 int anAclID = Integer.parseInt(val); 319 JahiaBaseACL theACL = null; 320 theACL = new JahiaBaseACL (anAclID); 321 return theACL.getPermission(user,JahiaBaseACL.READ_RIGHTS); 322 } catch ( Throwable t ){ 323 logger.error("error dureing guessing of readable field ",t); 325 return false; 326 } 327 } 328 return true; 330 } 331 332 333 343 public final boolean checkReadAccess (JahiaUser user) 344 { 345 return checkAccess (user, JahiaBaseACL.READ_RIGHTS); 346 } 347 348 358 public final boolean checkWriteAccess (JahiaUser user) 359 { 360 return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS); 361 } 362 363 364 374 public final boolean checkAdminAccess (JahiaUser user) 375 { 376 return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS); 377 } 378 379 380 private boolean checkAccess (JahiaUser user, int permission) 382 { 383 if (user == null) { 384 return false; 385 } 386 387 if ( aclID == 0 ) 390 return true; 391 392 393 396 boolean result = false; 397 try 398 { 399 JahiaBaseACL containerListACL = new JahiaBaseACL (aclID); 401 402 result = containerListACL.getPermission (user, permission); 404 405 containerListACL = null; 407 } 408 catch (JahiaException ex) { 409 logger.debug("Problem getting ACL on container list.", ex); 410 } 411 412 return result; 418 } 419 420 421 public void setProperties(Properties newProperties) { 422 this.ctnListProperties = newProperties; 423 } 424 425 public Properties getProperties() { 426 return this.ctnListProperties; 427 } 428 429 public String getProperty(String propertyName) { 430 if (this.ctnListProperties != null) { 431 return this.ctnListProperties.getProperty(propertyName); 432 } else { 433 return null; 434 } 435 } 436 437 public void setProperty(String propertyName, String propertyValue) { 438 if (this.ctnListProperties != null) { 439 this.ctnListProperties.setProperty(propertyName, propertyValue); 440 } else { 441 logger.error("ERROR: Properties object is not defined, ignoring property insertion."); 442 } 443 } 444 445 454 public boolean mergeProperties(Properties newProperties) { 455 boolean mustSave = false; 456 Enumeration newPropKeys = newProperties.keys(); 457 while (newPropKeys.hasMoreElements()) { 458 String curNewPropName = (String ) newPropKeys.nextElement(); 459 String curNewPropValue = newProperties.getProperty(curNewPropName); 460 if (this.ctnListProperties.containsKey(curNewPropName)) { 461 String internalPropValue = this.ctnListProperties.getProperty(curNewPropName); 462 if (!internalPropValue.equals(curNewPropValue)) { 463 this.ctnListProperties.setProperty(curNewPropName, curNewPropValue); 465 mustSave = true; 466 } 467 } else { 468 this.ctnListProperties.setProperty(curNewPropName, curNewPropValue); 470 mustSave = true; 471 } 472 } 473 return mustSave; 474 } 475 476 481 public ContentContainerList getContentContainerList() { 482 ContentContainerList contentContainerList = null; 483 try { 484 if (ID != 0) { 485 contentContainerList = ContentContainerList.getContainerList(ID); 486 } else { 487 return null; 491 } 492 } catch (JahiaException je) { 493 logger.error( 494 "Error while trying to retrieve ContentContainerList from JahiaContainerList", 495 je); 496 } 497 return contentContainerList; 498 } 499 500 504 public Object clone () { 505 JahiaContainerList containerList = 506 new JahiaContainerList(this.ID,this.parentEntryID,this.pageID, 507 this.ctndefid,this.aclID); 508 if ( this.ctnListProperties != null ){ 509 containerList.setProperties( (Properties )this.ctnListProperties. 510 clone()); 511 } 512 return containerList; 513 } 514 515 private void checkProxy(){ 516 if ( this.cListFactoryProxy != null ){ 517 this.cListFactoryProxy.load(this); 518 } 519 } 520 521 public void clearContainers(){ 522 this.containers = new ArrayList (); 523 } 524 } 525 | Popular Tags |