1 17 package org.jahia.services.pages; 18 19 import org.jahia.content.ContentDefinition; 20 import org.jahia.content.ContentObject; 21 import org.jahia.content.PageDefinitionKey; 22 import org.jahia.exceptions.JahiaException; 23 import org.jahia.registries.ServicesRegistry; 24 import org.jahia.services.acl.ACLNotFoundException; 25 import org.jahia.services.acl.ACLResourceInterface; 26 import org.jahia.services.acl.JahiaBaseACL; 27 import org.jahia.services.version.ContentObjectEntryState; 28 import org.jahia.services.version.EntryLoadRequest; 29 30 import java.util.Comparator ; 31 import java.util.Properties ; 32 import java.io.Serializable ; 33 34 35 50 public class JahiaPageDefinition extends ContentDefinition implements Comparator , 51 ACLResourceInterface, Serializable { 52 53 private static org.apache.log4j.Logger logger = 54 org.apache.log4j.Logger.getLogger (JahiaPageDefinition.class); 55 56 static { 57 ContentDefinition.registerType (PageDefinitionKey.PAGE_TYPE, 58 JahiaPageDefinition.class.getName ()); 59 } 60 61 private static final String CLASS_NAME = JahiaPageDefinition.class.getName (); 62 63 protected static final String ACLID_PROP = "acl_id"; 65 66 private int mID; 67 private int mSiteID; 68 private String mName; 69 private String mSourcePath; private boolean mAvailable; private String mImage; 72 73 private int mTempSiteID; 74 private String mTempName; 75 private String mTempSourcePath; 76 private boolean mTempAvailable; 77 private String mTempImage; 78 79 private Properties mProps = new Properties (); 80 81 private boolean mDataChanged; 82 83 94 protected JahiaPageDefinition (int ID, int siteID, String name, 95 String sourcePath, boolean isAvailable, 96 String image) { 97 super (new PageDefinitionKey (ID)); 98 mID = ID; 99 mSiteID = siteID; 100 101 if (name == null) { 102 name = "Untitled-" + ID; 103 } 104 mName = name; 105 106 if (sourcePath == null) { 107 sourcePath = "<no source path>"; 108 } 109 mSourcePath = sourcePath; 110 111 mAvailable = isAvailable; 112 113 if (image == null) { 114 image = ""; 115 } 116 mImage = image; 117 118 mTempSiteID = siteID; 120 mTempName = name; 121 mTempSourcePath = sourcePath; 122 mTempAvailable = isAvailable; 123 mTempImage = image; 124 125 mDataChanged = false; 126 } 127 128 133 public static ContentDefinition getChildInstance (String IDInType) { 134 135 try { 136 return ServicesRegistry.getInstance ().getJahiaPageTemplateService () 137 .lookupPageTemplate (Integer.parseInt (IDInType)); 138 } catch (JahiaException je) { 139 logger.debug ("Error retrieving ContentDefinition instance for id : " + IDInType, 140 je); 141 } 142 return null; 143 } 144 145 152 public void commitChanges () 153 throws JahiaException { 154 if (mDataChanged) { 156 157 mSiteID = mTempSiteID; 158 mName = mTempName; 159 mSourcePath = mTempSourcePath; 160 mAvailable = mTempAvailable; 161 mImage = mTempImage; 162 ServicesRegistry.getInstance().getJahiaPageTemplateService().updatePageTemplate (this); 163 mDataChanged = false; 164 } 165 } 166 167 168 public void disableChanges () { 169 if (mDataChanged) { 170 mTempSiteID = mSiteID; 171 mTempName = mName; 172 mTempSourcePath = mSourcePath; 173 mTempAvailable = mAvailable; 174 mTempImage = mImage; 175 } 176 } 177 178 184 public final int getID () { 185 return mID; 186 } 187 188 189 195 public String getImage () { 196 return mImage; 197 } 198 199 200 206 public final int getJahiaID () { 207 return mSiteID; 208 } 209 210 211 217 public final String getName () { 218 return mName; 219 } 220 221 227 public String getTitle (ContentObject contentObject, 228 ContentObjectEntryState entryState) { 229 try { 230 return ((ContentPage) contentObject) 231 .getTitle (new EntryLoadRequest (entryState)); 232 } catch (Throwable t) { 233 logger.debug ("Exception retrieving Definition Title", t); 234 } 235 return null; 236 } 237 238 244 public final String getSourcePath () { 245 return mSourcePath; 246 } 247 248 249 256 public final boolean isAvailable () { 257 return mAvailable; 258 } 259 260 266 public void setImage (String value) { 267 mTempImage = value; 268 mDataChanged = true; 269 } 270 271 272 278 public void setJahiaID (int value) { 279 mTempSiteID = value; 280 mDataChanged = true; 281 } 282 283 284 290 public void setName (String value) { 291 mTempName = value; 292 mDataChanged = true; 293 } 294 295 301 public void setSourcePath (String value) { 302 mTempSourcePath = value; 303 mDataChanged = true; 304 } 305 306 313 public void setAvailable (boolean value) { 314 mTempAvailable = value; 315 mDataChanged = true; 316 } 317 318 324 public final JahiaBaseACL getACL () { 325 326 if (mProps == null) 327 return null; 328 329 String value = mProps.getProperty (ACLID_PROP); 330 if (value == null) 331 return null; 332 333 JahiaBaseACL acl = null; 334 try { 335 acl = new JahiaBaseACL (Integer.parseInt (value)); 336 } catch (ACLNotFoundException ex) { 337 ex.printStackTrace (); 338 } catch (Throwable t) { 339 t.printStackTrace (); 340 } 341 return acl; 342 } 343 344 350 public final int getAclID () { 351 352 if (mProps == null) 353 return -1; 354 355 String value = mProps.getProperty (ACLID_PROP); 356 if (value == null) 357 return -1; 358 359 JahiaBaseACL acl = null; 360 try { 361 acl = new JahiaBaseACL (Integer.parseInt (value)); 362 return acl.getID (); 363 } catch (ACLNotFoundException ex) { 364 ex.printStackTrace (); 365 } catch (Throwable t) { 366 t.printStackTrace (); 367 } 368 return -1; 369 } 370 371 377 protected final void setACL (int id) { 378 if (mProps == null) { 379 logger.debug ("ACL cannot be set because properties is null"); 380 return; 381 } 382 mProps.setProperty (ACLID_PROP, String.valueOf (id)); 383 mDataChanged = true; 384 } 385 386 395 public int compare (Object c1, Object c2) throws ClassCastException { 396 397 return ((JahiaPageDefinition) c1) 398 .getName ().toLowerCase () 399 .compareTo (((JahiaPageDefinition) c2).getName ().toLowerCase ()); 400 401 } 402 403 404 405 406 412 public void setProperties (Properties props) { 413 mProps = props; 414 } 415 416 422 public Properties getProperties () { 423 return mProps; 424 } 425 426 432 public String getProperty (String name) { 433 if (mProps != null) { 434 return mProps.getProperty (name); 435 } else { 436 return null; 437 } 438 } 439 440 447 public void setProperty (String name, String value) { 448 if (mProps != null) { 449 mProps.setProperty (name, value); 450 } 451 } 452 453 454 } | Popular Tags |