1 13 17 package org.jahia.engines.deletecontainer; 18 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.Vector ; 26 27 import javax.servlet.http.HttpSession ; 28 29 import org.jahia.content.ContentContainerKey; 30 import org.jahia.content.ContentContainerListsXRefManager; 31 import org.jahia.content.ContentObject; 32 import org.jahia.data.JahiaData; 33 import org.jahia.data.containers.JahiaContainer; 34 import org.jahia.data.containers.JahiaContainerList; 35 import org.jahia.data.containers.JahiaContentContainerFacade; 36 import org.jahia.data.events.JahiaEvent; 37 import org.jahia.data.fields.FieldTypes; 38 import org.jahia.data.fields.JahiaField; 39 import org.jahia.data.fields.LoadFlags; 40 import org.jahia.engines.EngineToolBox; 41 import org.jahia.engines.JahiaEngine; 42 import org.jahia.engines.lock.LockEngine; 43 import org.jahia.exceptions.JahiaException; 44 import org.jahia.exceptions.JahiaForbiddenAccessException; 45 import org.jahia.exceptions.JahiaSessionExpirationException; 46 import org.jahia.params.ParamBean; 47 import org.jahia.registries.ServicesRegistry; 48 import org.jahia.services.containers.ContentContainer; 49 import org.jahia.services.lock.LockKey; 50 import org.jahia.services.lock.LockService; 51 import org.jahia.services.pages.JahiaPage; 52 import org.jahia.services.pages.PageLoadFlags; 53 import org.jahia.services.usermanager.JahiaUser; 54 import org.jahia.services.version.EntryLoadRequest; 55 import org.jahia.services.version.StateModificationContext; 56 import org.jahia.services.cache.HtmlCache; 57 import org.jahia.services.cache.CacheFactory; 58 import org.jahia.utils.LanguageCodeConverters; 59 import org.jahia.content.ContentPageKey; 60 import org.jahia.content.ObjectLink; 61 import org.jahia.content.ObjectKey; 62 import org.jahia.content.JahiaObject; 63 import org.jahia.content.ContentDefinition; 64 import org.jahia.services.version.ContentObjectEntryState; 65 import org.jahia.content.PageReferenceableInterface; 66 import org.jahia.services.fields.ContentField; 67 import org.jahia.services.pages.ContentPage; 68 import java.util.Map ; 69 70 public class DeleteContainer_Engine implements JahiaEngine { 71 72 73 public static final String ENGINE_NAME = "deletecontainer"; 74 75 76 private static final org.apache.log4j.Logger logger = 77 org.apache.log4j.Logger.getLogger (DeleteContainer_Engine.class); 78 79 80 private static DeleteContainer_Engine instance = null; 81 82 private static final String TEMPLATE_JSP = "delete_container"; 83 private EngineToolBox toolBox; 84 85 86 89 private DeleteContainer_Engine () { 90 logger.debug ( 91 "***** Starting " + DeleteContainer_Engine.class.getName () + " engine *****"); 92 toolBox = EngineToolBox.getInstance (); 93 } 94 95 100 public static DeleteContainer_Engine getInstance () { 101 if (instance == null) { 102 instance = new DeleteContainer_Engine (); 103 } 104 return instance; 105 } 106 107 110 public boolean authoriseRender (ParamBean jParams) { 111 return toolBox.authoriseRender (jParams); 112 } 114 117 public String renderLink (ParamBean jParams, Object theObj) 118 throws JahiaException { 119 ContentContainer contentContainer = (ContentContainer) theObj; 120 String params = EMPTY_STRING; 121 params += "?mode=display"; 122 params += "&cid=" + contentContainer.getID (); 123 return jParams.composeEngineUrl (ENGINE_NAME, params); 124 } 126 129 public boolean needsJahiaData (ParamBean jParams) { 130 return false; 131 } 133 139 public void handleActions (ParamBean jParams, JahiaData jData) 140 throws JahiaException, 141 JahiaSessionExpirationException, 142 JahiaForbiddenAccessException { 143 HashMap engineMap = initEngineMap (jParams); 145 146 JahiaContainer theContainer = (JahiaContainer) engineMap.get ( 148 "theContainer"); 149 JahiaUser theUser = jParams.getUser (); 150 151 if (theContainer.checkWriteAccess (theUser)) { 152 LockService lockRegistry = ServicesRegistry.getInstance (). 154 getLockService (); 155 if (jParams.settings ().areLocksActivated ()) { 156 LockKey lockKey = LockKey.composeLockKey (LockKey. 157 DELETE_CONTAINER_TYPE, 158 theContainer.getID (), theContainer.getPageID ()); 159 if (lockRegistry.acquire (lockKey, jParams.getUser (), 160 jParams.getSessionID (), 161 jParams.getSession (). 162 getMaxInactiveInterval ())) { 163 flushPageCacheThatDisplayContainer (jParams, theContainer); 165 engineMap.put ("writeAccess", Boolean.TRUE); 166 processScreen (jParams, engineMap); 167 } else { 169 LockEngine.getInstance ().redirect (jParams, engineMap, 172 lockKey); 173 } 174 } 175 } else { 177 throw new JahiaForbiddenAccessException (); 178 } 179 180 toolBox.displayScreen (jParams, engineMap); 182 183 } 185 186 191 public final String getName () { 192 return ENGINE_NAME; 193 } 194 195 196 201 public void processScreen (ParamBean jParams, HashMap engineMap) 202 throws JahiaException { 203 String theScreen = (String ) engineMap.get ("screen"); 206 JahiaContainer theContainer = (JahiaContainer) engineMap.get ( 207 "theContainer"); 208 209 LockKey lockKey = LockKey.composeLockKey (LockKey.DELETE_CONTAINER_TYPE, 211 theContainer.getID (), 212 theContainer.getPageID ()); 213 LockService lockRegistry = ServicesRegistry.getInstance (). 214 getLockService (); 215 217 if (theScreen.equals ("edit")) { 219 composeWarningMessages (jParams, engineMap); 220 } else if (theScreen.equals ("save")) { 221 if (engineMap.get ("writeAccess") != null) { 222 223 if (jParams.settings ().areLocksActivated () && 226 lockRegistry.isStealedInContext (lockKey, jParams.getUser (), 227 jParams.getSessionID ())) { 228 engineMap.put ("screen", 229 jParams.getRequest ().getParameter ("lastscreen")); 230 engineMap.put ("jspSource", "apply"); 231 return; 232 } 233 235 JahiaEvent theEvent = new JahiaEvent (this, jParams, 236 theContainer); 237 ServicesRegistry.getInstance ().getJahiaEventService (). 238 fireDeleteContainer (theEvent); 239 240 ContentContainer contentContainer = ContentContainer. 242 getContainer (theContainer.getID ()); 243 244 Set curLanguageCodes = new HashSet (); 247 curLanguageCodes.add (ContentObject.SHARED_LANGUAGE); 248 249 StateModificationContext stateModifContext = 250 new StateModificationContext (new 251 ContentContainerKey ( 252 theContainer.getID ()), curLanguageCodes, true); 253 stateModifContext.pushAllLanguages (true); 254 255 contentContainer.markLanguageForDeletion ( 256 jParams.getUser (), 257 ContentObject.SHARED_LANGUAGE, 258 stateModifContext); 259 260 flushPageCacheThatDisplayContainer (jParams, theContainer); 261 262 if (jParams.settings ().areLocksActivated ()) { 264 lockRegistry.release (lockKey, jParams.getUser (), 265 jParams.getSessionID ()); 266 } 267 } else { 269 throw new JahiaForbiddenAccessException (); 270 } 271 } else if (theScreen.equals ("cancel")) { 272 if (jParams.settings ().areLocksActivated ()) { 274 lockRegistry.release (lockKey, jParams.getUser (), 275 jParams.getSessionID ()); 276 flushPageCacheThatDisplayContainer (jParams, theContainer); 277 } 278 engineMap.put ("jspSource", "close"); 280 } 281 } 283 private void flushPageCacheThatDisplayContainer (ParamBean jParams, 284 JahiaContainer theContainer) 285 throws JahiaException { 286 EntryLoadRequest loadVersion = EntryLoadRequest.CURRENT; 287 if (ServicesRegistry.getInstance ().getJahiaVersionService (). 288 isStagingEnabled (theContainer.getJahiaID ())) 289 loadVersion = EntryLoadRequest.STAGED; 290 291 JahiaContainerList theList = ServicesRegistry.getInstance (). 292 getJahiaContainersService (). 293 loadContainerListInfo (theContainer. 294 getListID (), loadVersion); 295 296 HtmlCache htmlCache = CacheFactory.getHtmlCache (); 298 if (htmlCache == null) 299 logger.warn ("Could not get the HTML cache instance!!"); 300 301 if (theList != null) { 305 Set containerPageRefs = ContentContainerListsXRefManager. 306 getInstance (). 307 getAbsoluteContainerListPageIDs ( 308 theList.getID ()); 309 if (containerPageRefs != null) { 310 Iterator pageRefIDs = containerPageRefs.iterator (); 311 while (pageRefIDs.hasNext ()) { 312 Integer curPageID = (Integer ) pageRefIDs.next (); 313 314 if (htmlCache != null) 315 htmlCache.invalidatePageEntries (curPageID.toString (),jParams.getEntryLoadRequest().getWorkflowState()); 316 } 317 } else { 318 logger.debug ("Why is cross ref list empty ?"); 319 } 320 } else { 321 logger.debug ("Couldn't retrieve parent containerList, why is that ?"); 322 } 323 324 if (htmlCache != null) 327 htmlCache.invalidatePageEntries (Integer.toString (jParams.getPageID()).toString(), 328 jParams.getEntryLoadRequest().getWorkflowState()); 329 } 330 331 338 private HashMap initEngineMap (ParamBean jParams) 339 throws JahiaException, 340 JahiaSessionExpirationException { 341 HashMap engineMap; 342 JahiaContainer theContainer; 343 344 HttpSession theSession = jParams.getSession (); 347 348 String ctnidStr = jParams.getRequest ().getParameter ("cid"); 350 int ctnid; 351 try { 352 ctnid = Integer.parseInt (ctnidStr); 353 } catch (NumberFormatException nfe) { 354 String errorMsg = "Error in parameters : cid (" + ctnidStr + 355 ") cannot be converted in int"; 356 throw new JahiaException ("Error in parameters", errorMsg, 357 JahiaException.DATA_ERROR, 358 JahiaException.CRITICAL_SEVERITY); 359 } 360 361 String languageCode = jParams.getRequest ().getParameter ("engine_lang"); 363 if (languageCode == null) { 364 languageCode = jParams.settings ().getDefaultLanguageCode (); 365 } 366 String prevLanguageCode = jParams.getRequest ().getParameter ( 367 "prev_engine_lang"); 368 if (prevLanguageCode == null) { 369 prevLanguageCode = languageCode; 370 } 371 372 EntryLoadRequest entryLoadRequest = 373 new EntryLoadRequest (EntryLoadRequest.STAGING_WORKFLOW_STATE, 374 0, new ArrayList ()); 375 entryLoadRequest.getLocales () 376 .add (LanguageCodeConverters.languageCodeToLocale (languageCode)); 377 EntryLoadRequest prevEntryLoadRequest = 378 new EntryLoadRequest (EntryLoadRequest.STAGING_WORKFLOW_STATE, 379 0, new ArrayList ()); 380 prevEntryLoadRequest.getLocales () 381 .add (LanguageCodeConverters.languageCodeToLocale (prevLanguageCode)); 382 383 String theScreen = jParams.getRequest ().getParameter ("screen"); 385 if (theScreen != null) { 386 engineMap = (HashMap ) theSession.getAttribute ( 388 "jahia_session_engineMap"); 389 390 if (engineMap == null) { 398 throw new JahiaSessionExpirationException (); 399 } 400 401 theContainer = (JahiaContainer) engineMap.get ("theContainer"); 402 } else { 403 404 JahiaContentContainerFacade jahiaContentContainerFacade 405 = new JahiaContentContainerFacade (ctnid, 406 LoadFlags.ALL, 407 jParams, 408 jParams.getSite (). 409 getLanguageSettingsAsLocales (true), 410 false); 411 theContainer = jahiaContentContainerFacade.getContainer ( 412 entryLoadRequest, true); 413 if (theContainer == null && 414 jahiaContentContainerFacade.getContainers ().hasMoreElements ()) { 415 theContainer = 417 (JahiaContainer) jahiaContentContainerFacade.getContainers (). 418 nextElement (); 419 } 420 421 theScreen = "edit"; 422 engineMap = new HashMap (); 424 engineMap.put ("theContainer", theContainer); 425 engineMap.put (RENDER_TYPE_PARAM, 426 new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 427 engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 428 engineMap.put (ENGINE_URL_PARAM, 429 jParams.composeEngineUrl (ENGINE_NAME, 430 "?cid=" + theContainer.getID ())); 431 theSession.setAttribute ("jahia_session_engineMap", engineMap); 432 } 433 434 engineMap.put ("screen", theScreen); 436 if (!theScreen.equals ("save")) { 437 engineMap.put ("jspSource", TEMPLATE_JSP); 438 } else { 439 engineMap.put ("jspSource", "close"); 440 } 441 442 jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap", 444 engineMap); 445 446 return engineMap; 447 } 449 455 private void composeWarningMessages (ParamBean jParams, HashMap engineMap) 456 throws JahiaException { 457 JahiaContainer theContainer = (JahiaContainer) engineMap.get ( 458 "theContainer"); 459 460 engineMap.put ("deletedPages", new Vector ()); 461 engineMap.put ("deletedLinks", new Vector ()); 462 engineMap.put ("futureBrokenLinkObjects", new ArrayList ()); 463 engineMap.put ("warning", Boolean.FALSE); 464 465 Enumeration theFields = theContainer.getFields (); 466 467 JahiaPage page = ServicesRegistry.getInstance ().getJahiaPageService (). 468 lookupPage (theContainer.getPageID (), jParams); 469 int pageDefID = page.getPageTemplateID (); 470 471 Set objectKeysPointingToDeletedContent = new HashSet (); 472 473 while (theFields.hasMoreElements ()) { 474 JahiaField theField = (JahiaField) theFields.nextElement (); 475 int fieldType = theField.getDefinition ().getType (pageDefID); 476 fieldType = (fieldType != 0) ? fieldType : theField.getType (); 477 478 if (fieldType == FieldTypes.PAGE) { 480 JahiaPage thePage = (JahiaPage) theField.getObject (); 481 if (thePage != null) { 482 483 objectKeysPointingToDeletedContent.addAll ( 484 contentObjectPointingOnPage (thePage.getID ())); 485 486 Vector deletedPages = ServicesRegistry.getInstance(). 487 getJahiaPageService(). 488 getPageSubTree(thePage.getID(), 489 PageLoadFlags.DIRECT, jParams); 490 491 498 Vector pageChildsWithoutDeleted = new Vector (); 499 for (int i=0; i < deletedPages.size(); i++) { 500 JahiaPage curJahiaPage = (JahiaPage) deletedPages.get(i); 501 ContentPage curContentPage = curJahiaPage.getContentPage(); 502 if ((curContentPage.getDeleteVersionID() == -1)) { 503 Map languageStates = curContentPage.getLanguagesStates(false); 506 boolean markedInAllLanguages = true; 507 Iterator languageCodeIter = languageStates.keySet().iterator(); 508 while (languageCodeIter.hasNext()) { 509 String curLanguageCode = (String ) languageCodeIter.next(); 510 if (!curContentPage.isStagedEntryMarkedForDeletion(curLanguageCode)) { 511 markedInAllLanguages = false; 512 break; 513 } 514 } 515 if (!markedInAllLanguages) { 516 pageChildsWithoutDeleted.add(curJahiaPage); 517 } 518 } 519 } 520 deletedPages = pageChildsWithoutDeleted; 521 522 Vector deletedLinks = new Vector (); 523 deletedLinks.addAll(ServicesRegistry.getInstance(). 524 getJahiaPageService(). 525 getPagesPointingOnPage(thePage.getID(), 526 jParams)); 527 for (int i = 0; i < deletedPages.size(); i++) { 528 JahiaPage aPage = (JahiaPage) deletedPages.elementAt(i); 529 objectKeysPointingToDeletedContent.addAll( 530 contentObjectPointingOnPage(aPage.getID())); 531 deletedLinks.addAll(ServicesRegistry.getInstance(). 532 getJahiaPageService(). 533 getPagesPointingOnPage(aPage.getID(), 534 jParams)); 535 } 536 engineMap.put ("deletedPages", deletedPages); 537 engineMap.put ("deletedLinks", deletedLinks); 538 ArrayList hardcodedLinkSourceObject = 539 buildHardcodedSourceInfo ( 540 objectKeysPointingToDeletedContent, jParams); 541 engineMap.put ("futureBrokenLinkObjects", 542 hardcodedLinkSourceObject); 543 if ((deletedPages.size () > 0) || (deletedLinks.size () > 0) || 544 (objectKeysPointingToDeletedContent.size () > 0)) { 545 engineMap.put ("warning", Boolean.TRUE); 546 } 547 } 548 } 549 550 } 551 552 556 557 } 559 private Set contentObjectPointingOnPage (int pageID) 560 throws JahiaException { 561 ArrayList objectLinks = ObjectLink.findByTypeAndRightObjectKey ( 562 "hardcodedLink", new ContentPageKey (pageID)); 563 Iterator objectLinkIter = objectLinks.iterator (); 564 Set objectKeysPointingToPage = new HashSet (); 565 while (objectLinkIter.hasNext ()) { 566 ObjectLink curLink = (ObjectLink) objectLinkIter.next (); 567 ObjectKey objectKey = curLink.getLeftObjectKey (); 568 try { 569 ContentObject contentObject = (ContentObject) JahiaObject. 570 getInstance (objectKey); 571 if (contentObject != null) { 572 if (!contentObject.getActiveAndStagingEntryStates ().isEmpty ()) { 573 objectKeysPointingToPage.add (objectKey); 574 } else { 575 logger.debug ( 576 "No active or staging entries found for objectKey=" + 577 objectKey + 578 ", not displaying deleted or archived references"); 579 } 580 } else { 581 logger.warn ( 582 "Hardcoded URL link found for object that no longer exists:" + 583 objectKey + ", removing reference"); 584 curLink.remove (); 585 } 586 } catch (ClassNotFoundException cnfe) { 587 logger.error ("Error while loading Jahia object from objectKey=" + 588 objectKey, cnfe); 589 } 590 } 591 return objectKeysPointingToPage; 592 } 593 594 private ArrayList buildHardcodedSourceInfo (Set objectKeys, 595 ParamBean paramBean) 596 throws JahiaException { 597 ArrayList sourceObjectList = new ArrayList (); 598 Iterator objectKeyIter = objectKeys.iterator (); 599 ContentObjectEntryState entryState = new ContentObjectEntryState ( 600 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 601 0, paramBean.getLocale ().toString ()); 602 while (objectKeyIter.hasNext ()) { 603 ObjectKey curObjectKey = (ObjectKey) objectKeyIter.next (); 604 HardcodedLinkSourceInfo curLinkInfo = new HardcodedLinkSourceInfo (); 605 curLinkInfo.setID (curObjectKey.getIDInType ()); 606 curLinkInfo.setObjectType (curObjectKey.getType ()); 607 try { 608 JahiaObject jahiaObject = JahiaObject.getInstance (curObjectKey); 609 if (jahiaObject instanceof ContentObject) { 610 ContentObject curContentObject = (ContentObject) 611 jahiaObject; 612 ObjectKey definitionKey = curContentObject.getDefinitionKey ( 613 paramBean.getEntryLoadRequest ()); 614 ContentDefinition definition = ContentDefinition. 615 getInstance (definitionKey); 616 curLinkInfo.setName (definition.getName ()); 617 curLinkInfo.setTitle (definition.getTitle (curContentObject, 618 entryState)); 619 if (curContentObject instanceof ContentField) { 620 ContentField curContentField = (ContentField) 621 curContentObject; 622 curLinkInfo.setObjectSubType (FieldTypes.typeName[ 623 curContentField.getType ()]); 624 } 625 } 626 if (jahiaObject instanceof PageReferenceableInterface) { 627 PageReferenceableInterface pageObject = ( 628 PageReferenceableInterface) jahiaObject; 629 curLinkInfo.setPageID (pageObject.getPageID ()); 630 } 631 sourceObjectList.add (curLinkInfo); 632 } catch (ClassNotFoundException cnfe) { 633 logger.error ( 634 "Error while loading content object, will not be added to hardcoded source objects", 635 cnfe); 636 } 637 } 638 return sourceObjectList; 639 } 640 641 } | Popular Tags |