1 package org.jahia.data.beans; 2 3 import java.util.ArrayList ; 4 import java.util.Enumeration ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 import java.util.Vector ; 11 12 import org.jahia.data.containers.JahiaContainerList; 13 import org.jahia.data.fields.JahiaField; 14 import org.jahia.data.fields.LoadFlags; 15 import org.jahia.exceptions.JahiaException; 16 import org.jahia.params.ParamBean; 17 import org.jahia.registries.ServicesRegistry; 18 import org.jahia.services.fields.ContentField; 19 import org.jahia.services.pages.ContentPage; 20 import org.jahia.services.pages.JahiaPage; 21 import org.jahia.services.pages.JahiaPageDefinition; 22 import org.jahia.gui.GuiBean; 23 import org.jahia.gui.HTMLToolBox; 24 import org.jahia.services.lock.LockService; 25 import org.jahia.services.lock.LockKey; 26 import org.jahia.utils.InsertionSortedMap; 27 import org.jahia.content.JahiaObject; 28 29 41 42 public class PageBean extends ContentBean { 43 44 private static org.apache.log4j.Logger logger = 45 org.apache.log4j.Logger.getLogger(PageBean.class); 46 47 private PageBean parent = null; 48 private java.util.Map containerLists = null; 49 private java.util.Map fields = null; 50 private Map actionURIs = null; 51 private boolean completelyLocked = false; 52 53 private JahiaPage jahiaPage; 54 private ParamBean paramBean; 55 56 static { 57 registerType(ContentPage.class.getName(), PageBean.class.getName()); 58 } 59 60 public PageBean () { 61 } 62 63 public PageBean (JahiaPage aJahiaPage, ParamBean aParamBean) { 64 this.jahiaPage = aJahiaPage; 65 this.paramBean = aParamBean; 66 } 67 68 public static AbstractJahiaObjectBean getChildInstance (JahiaObject 69 jahiaObject, 70 ParamBean paramBean) { 71 ContentPage contentPage = (ContentPage) jahiaObject; 72 try { 73 return new PageBean(contentPage.getPage(paramBean. 74 getEntryLoadRequest(), paramBean.getOperationMode(), 75 paramBean.getUser()), paramBean); 76 } catch (JahiaException je) { 77 logger.error( 78 "Error while converting content container to jahia container", 79 je); 80 return null; 81 } 82 } 83 84 public PageBean getParentPage () { 85 if (parent != null) { 86 return parent; 88 } 89 int parentID = jahiaPage.getParentID(); 90 try { 91 ContentPage parentContentPage = ContentPage.getPage(parentID); 92 if (parentContentPage == null) { 93 return null; 94 } 95 JahiaPage parentJahiaPage = parentContentPage.getPage(paramBean. 96 getEntryLoadRequest(), paramBean.getOperationMode(), 97 paramBean.getUser()); 98 if (parentJahiaPage == null) { 99 return null; 100 } 101 parent = new PageBean(parentJahiaPage, paramBean); 102 return parent; 103 } catch (JahiaException je) { 104 logger.error("Error while trying to retrieve parent page " + 105 parentID + " for page " + getId() + " : ", je); 106 return null; 107 } 108 } 109 110 public int getId () { 111 return jahiaPage.getID(); 112 } 113 114 public String getTitle () { 115 return jahiaPage.getTitle(); 116 } 117 118 public java.util.Map getContainerLists () { 119 120 if (getType() != JahiaPage.TYPE_DIRECT) { 121 return null; 122 } 123 if (containerLists != null) { 124 return containerLists; 125 } 126 try { 127 containerLists = new HashMap (); 128 Set containerListIDs = ServicesRegistry.getInstance(). 129 getJahiaContainersService(). 130 getAllPageTopLevelContainerListIDs( 131 getId(), paramBean.getEntryLoadRequest()); 132 Iterator containerListIDIter = containerListIDs.iterator(); 133 while (containerListIDIter.hasNext()) { 134 Integer curContainerListID = (Integer ) containerListIDIter.next(); 135 JahiaContainerList curContainerList = ServicesRegistry.getInstance().getJahiaContainersService() 136 .loadContainerList(curContainerListID.intValue(), 137 LoadFlags.ALL, paramBean, 138 paramBean.getEntryLoadRequest(), null, null, null); 139 ContainerListBean containerListBean = new ContainerListBean( 140 curContainerList, paramBean); 141 containerLists.put(curContainerList.getDefinition().getName(), 142 containerListBean); 143 } 144 } catch (JahiaException je) { 145 logger.error("Error loading page top level container lists : ", je); 146 return null; 147 } catch (Throwable t) { 148 logger.error("Error loading page top level container lists : ", t); 149 return null; 150 } 151 return containerLists; 152 } 153 154 public java.util.Map getFields () { 155 if (getType() != JahiaPage.TYPE_DIRECT) { 156 return null; 157 } 158 if (fields != null) { 159 return fields; 160 } 161 try { 162 ContentField.preloadActiveOrStagedFieldsByPageID(getId()); 163 164 Vector fieldIDs = ServicesRegistry.getInstance(). 166 getJahiaFieldService(). 167 getNonContainerFieldIDsInPageByWorkflowState( 168 getId(), 169 paramBean.getEntryLoadRequest()); 170 fields = new HashMap (); 172 173 for (int i = 0; i < fieldIDs.size(); i++) { 174 Integer curFieldID = (Integer ) fieldIDs.elementAt(i); 175 JahiaField curField = ServicesRegistry.getInstance(). 178 getJahiaFieldService().loadField( 179 curFieldID.intValue(), 180 LoadFlags.ALL, paramBean, paramBean.getEntryLoadRequest()); 181 if (curField != null) { 182 FieldBean fieldBean = new FieldBean(curField, paramBean); 183 fields.put(curField.getDefinition().getName(), fieldBean); 184 } 185 } 186 } catch (JahiaException je) { 187 logger.error("Error loading field directly attached to page " + 188 getId() + ":", je); 189 return null; 190 } 191 return fields; 192 } 193 194 public int getAclID () { 195 return jahiaPage.getAclID(); 196 } 197 198 public int getCounter () { 199 return jahiaPage.getCounter(); 200 } 201 202 public String getCreator () { 203 return jahiaPage.getCreator(); 204 } 205 206 public String getDateOfCreation () { 207 return jahiaPage.getDoc(); 208 } 209 210 public int getSiteID () { 211 return jahiaPage.getJahiaID(); 212 } 213 214 public int getTemplateID () { 215 return jahiaPage.getPageTemplateID(); 216 } 217 218 public JahiaPage getJahiaPage () { 219 return jahiaPage; 220 } 221 222 public int getType () { 223 return jahiaPage.getPageType(); 224 } 225 226 public String getUrl () { 227 try { 228 return jahiaPage.getURL(paramBean); 229 } catch (JahiaException je) { 230 logger.error("Error while generating URL for page " + getId() + ":", 231 je); 232 return null; 233 } 234 } 235 236 public Map getLanguageStates () { 237 return jahiaPage.getLanguagesStates(false); 238 } 239 240 public Map getLanguageStatesWithContent () { 241 return jahiaPage.getLanguagesStates(true); 242 } 243 244 public List getPath () { 245 try { 246 Enumeration pathEnum = jahiaPage.getContentPagePath(paramBean. 247 getOperationMode(), paramBean.getUser()); 248 List pathList = new ArrayList (); 249 while (pathEnum.hasMoreElements()) { 250 ContentPage curJahiaPage = (ContentPage) pathEnum.nextElement(); 251 PageBean curPageBean = new PageBean(curJahiaPage.getPage(paramBean), paramBean); 252 pathList.add(curPageBean); 253 } 254 return pathList; 255 } catch (JahiaException je) { 256 logger.error("Error while retrieving page path for page " + getId() + 257 ":", je); 258 return null; 259 } 260 } 261 262 public List getPathWithLevels (int levels) { 263 try { 264 Enumeration pathEnum = jahiaPage.getContentPagePath(levels, paramBean. 265 getOperationMode(), paramBean.getUser()); 266 List pathList = new ArrayList (); 267 while (pathEnum.hasMoreElements()) { 268 ContentPage curJahiaPage = (ContentPage) pathEnum.nextElement(); 269 PageBean curPageBean = new PageBean(curJahiaPage.getPage(paramBean), paramBean); 270 pathList.add(curPageBean); 271 } 272 return pathList; 273 } catch (JahiaException je) { 274 logger.error("Error while retrieving page path for page " + getId() + 275 ":", je); 276 return null; 277 } 278 } 279 280 public int getLinkID () { 281 return jahiaPage.getPageLinkID(); 282 } 283 284 public JahiaPageDefinition getTemplate () { 285 return jahiaPage.getPageTemplate(); 286 } 287 288 public int getParentID () { 289 return jahiaPage.getParentID(); 290 } 291 292 public String getProperty (String propertyName) { 293 try { 294 return jahiaPage.getProperty(propertyName); 295 } catch (JahiaException je) { 296 logger.error("Error while retrieving property " + propertyName + 297 " for page " + getId() + ":", je); 298 return null; 299 } 300 } 301 302 public String getRemoteURL() { 303 return jahiaPage.getRemoteURL(); 304 } 305 306 public boolean isInCurrentPagePath() { 307 try { 308 Enumeration thePath = paramBean.getPage().getContentPagePath(paramBean. 309 getOperationMode(), paramBean.getUser()); 310 while (thePath.hasMoreElements()) { 311 ContentPage curContentPage = (ContentPage) thePath.nextElement(); 312 if (curContentPage.getID() == getId()) { 313 return true; 314 } 315 } 316 } catch (JahiaException je) { 317 logger.error("Error while loading current page path " + paramBean.getPageID() + ":", je); 318 return false; 319 } 320 return false; 321 322 } 323 324 public boolean isCurrentPage() { 325 if (jahiaPage.getID() == paramBean.getPageID()) { 326 return true; 327 } else { 328 return false; 329 } 330 } 331 332 public boolean isHomePage() { 333 int homePageID = paramBean.getSite().getHomePageID(); 334 if (homePageID == getId()) { 335 return true; 336 } else { 337 return false; 338 } 339 } 340 341 public int getLevel() { 342 int level = -1; 343 try { 344 Enumeration pathEnum = jahiaPage.getContentPagePath(paramBean 345 .getOperationMode(), paramBean.getUser()); 346 while (pathEnum.hasMoreElements()) { 347 ContentPage aPage = (ContentPage) pathEnum.nextElement(); 348 level++; 349 if (aPage.getID() == jahiaPage.getID()) { 350 return level; 351 } 352 } 353 } catch (JahiaException je) { 354 logger.error("Error while retrieving page path for page " + getId() 355 + ":", je); 356 } 357 return level; 358 } 359 360 public Map getActionURIBeans() { 361 if (actionURIs == null) { 362 buildActionURIs(); 363 } 364 return actionURIs; 365 } 366 367 public boolean isCompletelyLocked() { 368 if (actionURIs == null) { 369 buildActionURIs(); 370 } 371 return completelyLocked; 372 } 373 374 public boolean isPartiallyLocked() { 375 if (actionURIs == null) { 376 buildActionURIs(); 377 } 378 if (!completelyLocked) { 379 Iterator actionURIIter = actionURIs.entrySet().iterator(); 380 boolean partiallyLocked = false; 381 while (actionURIIter.hasNext()) { 382 Map.Entry curActionURIEntry = (Map.Entry ) actionURIIter.next(); 383 ActionURIBean curActionURIBean = (ActionURIBean) curActionURIEntry.getValue(); 384 if (curActionURIBean.isLocked()) { 385 partiallyLocked = true; 386 } 387 } 388 return partiallyLocked; 389 } else { 390 return false; 391 } 392 } 393 394 public boolean isActionURIsEmpty() { 395 if (actionURIs == null) { 396 buildActionURIs(); 397 } 398 return actionURIs.isEmpty(); 399 } 400 401 public ContentBean getParent() { 402 if (isHomePage()) { 405 return null; 406 } 407 408 try { 409 ContentPage contentPage = ContentPage.getPage(getId()); 410 411 ContentField parentContentField = (ContentField) contentPage. 412 getParent(paramBean.getUser(), 413 paramBean.getEntryLoadRequest(), paramBean.getOperationMode()); 414 if (parentContentField != null) { 415 JahiaField parentJahiaField = parentContentField.getJahiaField( 416 paramBean.getEntryLoadRequest()); 417 if (parentJahiaField != null) { 418 FieldBean parentFieldBean = new FieldBean(parentJahiaField, 419 paramBean); 420 return parentFieldBean; 421 } 422 } 423 } catch (Exception je) { 424 logger.error("Error while loading parent field for page " + getId(), je); 425 } 426 return null; 427 428 } 429 430 private void buildActionURIs() { 431 actionURIs = new InsertionSortedMap(); 432 GuiBean guiBean = new GuiBean(paramBean); 433 HTMLToolBox htmlToolBox = new HTMLToolBox(guiBean, paramBean); 434 completelyLocked = true; 435 try { 436 String curURL = guiBean.drawPagePropertiesUrl(); 437 String curLauncherURI = htmlToolBox.drawPagePropertiesLauncher(); 438 ActionURIBean curActionURIBean = new ActionURIBean("update", curURL, curLauncherURI); 439 LockService lockRegistry = ServicesRegistry.getInstance().getLockService(); 440 LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_PAGE_TYPE, paramBean.getPageID(), paramBean.getPageID()); 441 if (!lockRegistry.isAcquireable(lockKey, paramBean.getUser(), paramBean.getSessionID())) { 442 curActionURIBean.setLocked(true); 443 } else { 444 completelyLocked = false; 445 } 446 if (!lockRegistry.canRelease(lockKey, paramBean.getUser(), paramBean.getSessionID())) { 447 curActionURIBean.setReleaseable(true); 448 } 449 if ((curActionURIBean.getUri() != null) && (!"".equals(curActionURIBean.getUri()))) { 450 actionURIs.put(curActionURIBean.getName(), curActionURIBean); 451 } 452 } catch (JahiaException je) { 453 logger.error("Error while retrieving action URI map for page " + getId(), je); 454 } 455 } 456 457 } | Popular Tags |