1 41 42 43 package org.jahia.engines.workflow; 44 45 import java.io.File ; 46 import java.text.MessageFormat ; 47 import java.util.ArrayList ; 48 import java.util.Enumeration ; 49 import java.util.HashMap ; 50 import java.util.HashSet ; 51 import java.util.Hashtable ; 52 import java.util.Iterator ; 53 import java.util.Locale ; 54 import java.util.Map ; 55 import java.util.Set ; 56 import java.util.StringTokenizer ; 57 import java.util.Vector ; 58 59 import org.jahia.bin.Jahia; 60 import org.jahia.content.ContentPageKey; 61 import org.jahia.data.JahiaData; 62 import org.jahia.data.viewhelper.sitemap.PagesFilter; 63 import org.jahia.data.viewhelper.sitemap.SiteMapViewHelper; 64 import org.jahia.engines.EngineToolBox; 65 import org.jahia.engines.JahiaEngine; 66 import org.jahia.exceptions.JahiaException; 67 import org.jahia.exceptions.JahiaForbiddenAccessException; 68 import org.jahia.params.ParamBean; 69 import org.jahia.registries.ServicesRegistry; 70 import org.jahia.resourcebundle.JahiaResourceBundle; 71 import org.jahia.services.acl.JahiaACLException; 72 import org.jahia.services.acl.JahiaBaseACL; 73 import org.jahia.services.cache.CacheFactory; 74 import org.jahia.services.cache.HtmlCache; 75 import org.jahia.services.database.ConnectionDispenser; 76 import org.jahia.services.lock.LockKey; 77 import org.jahia.services.lock.LockService; 78 import org.jahia.services.pages.ContentPage; 79 import org.jahia.services.sitemap.JahiaSiteMapService; 80 import org.jahia.services.sites.JahiaSite; 81 import org.jahia.services.usermanager.JahiaUser; 82 import org.jahia.services.version.ActivationTestResults; 83 import org.jahia.services.version.EntryLoadRequest; 84 import org.jahia.services.version.IsValidForActivationResults; 85 import org.jahia.services.version.JahiaSaveVersion; 86 import org.jahia.services.version.StateModificationContext; 87 88 96 public class WorkflowEngine implements JahiaEngine { 97 98 private static final String TEMPLATE_JSP = "workflow"; 99 private final static String [] _tabOptions = {"notifyCompletedPages", "approvePendingPages"}; 100 private static final String LAST_DISPLAY_SESSION_ATTRNAME = "org.jahia.engines.workflow.lastDisplay"; 102 103 104 private EngineToolBox toolBox; 105 106 private static final org.apache.log4j.Logger logger = 107 org.apache.log4j.Logger.getLogger (WorkflowEngine.class); 108 109 110 private static WorkflowEngine instance = null; 111 112 private static final String WORKFLOW_MAIL_NOTIF_JSP = "workflow_mailnotif.jsp"; 113 private static final String DEFAULT_PATH_TO_JSP = 114 "/jsp/jahia/engines/workflow/workflow_mailnotif.jsp"; 115 116 public static final String WAITINGFORAPPROVAL_OPNAME = "waitingForApproval"; 117 public static final String APPROVAL_OPNAME = "approval"; 118 public static final String REFUSAL_OPNAME = "refusal"; 119 120 public static final String ENGINE_NAME = "workflow"; 121 122 125 private WorkflowEngine () { 126 logger.debug ("***** Starting " + WorkflowEngine.class.getName () + " engine *****"); 127 toolBox = EngineToolBox.getInstance (); 128 } 129 130 133 public static synchronized WorkflowEngine getInstance () { 134 if (instance == null) { 135 instance = new WorkflowEngine (); 136 } 137 return instance; 138 } 139 140 145 public boolean authoriseRender (ParamBean jParams) { 146 return true; 149 } 150 151 156 public boolean needsJahiaData (ParamBean jParams) { 157 return false; 158 } 159 160 170 public String renderLink (ParamBean jParams, Object theObj) 171 throws JahiaException { 172 JahiaUser user = jParams.getUser (); 173 String params = EMPTY_STRING; 174 if (jParams.getPage ().checkAdminAccess (user)) { 177 jParams.getSession().setAttribute(LAST_DISPLAY_SESSION_ATTRNAME, _tabOptions[1]); 178 params = "?screen=display&display=" + _tabOptions[1]; 180 } else if (jParams.getPage ().checkWriteAccess (user)) { 181 jParams.getSession().setAttribute(LAST_DISPLAY_SESSION_ATTRNAME, _tabOptions[0]); 182 params = "?screen=display&display=" + _tabOptions[0]; 184 } 185 186 String pagesFilter = jParams.getParameter(JahiaSiteMapService.PAGES_FILTER); 187 if (pagesFilter != null) 188 { 189 if (params.indexOf('?') != -1) 190 { 191 params = params + "&"; 192 } 193 else 194 { 195 params = params + "?"; 196 } 197 params = params + JahiaSiteMapService.PAGES_FILTER + "=" + pagesFilter; 198 } 199 200 return jParams.composeEngineUrl ("workflow", params); 201 } 202 203 215 public void handleActions (ParamBean jParams, JahiaData jData) 216 throws JahiaException { 217 218 JahiaUser user = jParams.getUser (); 219 if (!(jParams.getPage ().checkWriteAccess (user) || 220 jParams.getPage ().checkAdminAccess (user))) { 221 throw new JahiaForbiddenAccessException (); 222 } 223 int pageInfosFlag = ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS; 224 HashMap engineMap = new HashMap (); 225 String actionScreen = jParams.getParameter ("screen"); 226 if ("display".equals (actionScreen)) { 228 initEngineMap (jParams, engineMap); 229 ContentPage contentPage = ServicesRegistry.getInstance (). 230 getJahiaPageService ().lookupContentPage (jParams.getPageID (), false); 231 232 PagesFilter pagesFilter = JahiaSiteMapService.getCurrentPagesFilter(null, jParams); 233 JahiaSiteMapService siteMapService = ServicesRegistry.getInstance (). 234 getJahiaSiteMapService (); 235 SiteMapViewHelper treeSiteMapViewHelper = siteMapService. 236 getTreeSiteMapViewHelper (jParams.getUser (), contentPage, 237 jParams.getSessionID (), pageInfosFlag, null, 238 SiteMapViewHelper.DEFAULT_LEVEL, pagesFilter, jParams); 239 engineMap.put ("treeJahiaSiteMap", treeSiteMapViewHelper); 240 241 String siteMapParam = jParams.getParameter ("sitemap"); 244 if (siteMapParam != null) { 245 siteMapService.invokeTreeSiteMapViewHelperMethod (jParams.getUser (), 246 contentPage, jParams.getSessionID (), pageInfosFlag, 247 null, siteMapParam, pagesFilter, jParams); 248 } 249 250 if (jParams.settings ().areLocksActivated ()) { 252 HtmlCache htmlCache = CacheFactory.getHtmlCache(); 254 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService (); 256 for (int i = 0; i < treeSiteMapViewHelper.size (); i++) { 257 ContentPage siteMapContentPage = treeSiteMapViewHelper.getContentPage (i); 258 if (siteMapContentPage != null) { 259 LockKey lockKey = LockKey.composeLockKey (LockKey.WORKFLOW_TYPE, 260 siteMapContentPage.getID (), siteMapContentPage.getID ()); 261 if (lockRegistry.acquire (lockKey, user, jParams.getSessionID (), 262 jParams.getSession ().getMaxInactiveInterval ())) { 263 htmlCache.invalidatePageEntries(Integer.toString(siteMapContentPage.getID()),jParams.getEntryLoadRequest().getWorkflowState()); 264 logger.debug ( 265 "Lock acquired for page " + 266 siteMapContentPage.getTitles (true)); 267 } else { 268 logger.debug ( 269 "Cannot acquire lock for page " + 270 siteMapContentPage.getTitles (true)); 271 } 272 } 273 else { 274 int pageId = treeSiteMapViewHelper.getPageID(i); 276 277 try { 278 siteMapContentPage = ServicesRegistry.getInstance (). 279 getJahiaPageService ().lookupContentPage(pageId, false); 280 281 if(siteMapContentPage!=null) 282 { 283 LockKey lockKey = LockKey.composeLockKey (LockKey.WORKFLOW_TYPE, 284 siteMapContentPage.getID (), siteMapContentPage.getID ()); 285 286 lockRegistry.release (lockKey, jParams.getUser (), jParams.getSessionID ()); 287 htmlCache.invalidatePageEntries(Integer.toString(siteMapContentPage.getID()),jParams.getEntryLoadRequest().getWorkflowState()); 288 } 289 } 290 catch(JahiaException exc) { 291 } 293 } 294 } 295 } 296 298 String display = jParams.getParameter ("display"); 300 if ("notifyCompletedPages".equals (display)) { 301 jParams.getSession().setAttribute(LAST_DISPLAY_SESSION_ATTRNAME, _tabOptions[0]); 302 engineMap.put ("activeTabOption", new Integer (0)); 304 } else if ("approvePendingPages".equals (display)) { 305 jParams.getSession().setAttribute(LAST_DISPLAY_SESSION_ATTRNAME, _tabOptions[1]); 306 engineMap.put ("activeTabOption", new Integer (1)); 308 } 309 } else if ("apply".equals (actionScreen)) { 311 applyModifications (jParams); 312 initEngineMap (jParams, engineMap); 313 ContentPage contentPage = ServicesRegistry.getInstance (). 314 getJahiaPageService ().lookupContentPage (jParams.getPageID (), false); 315 316 PagesFilter pagesFilter = JahiaSiteMapService.getCurrentPagesFilter(null, jParams); 317 engineMap.put ("treeJahiaSiteMap", 318 ServicesRegistry.getInstance ().getJahiaSiteMapService (). 319 getTreeSiteMapViewHelper (jParams.getUser (), contentPage, 320 jParams.getSessionID (), pageInfosFlag, null, 321 SiteMapViewHelper.DEFAULT_LEVEL, pagesFilter, jParams)); 322 engineMap.put ("screen", EMPTY_STRING); engineMap.put(ENGINE_URL_PARAM, jParams.composeEngineUrl(ENGINE_NAME, "?screen=display&display=" + jParams.getSession().getAttribute(LAST_DISPLAY_SESSION_ATTRNAME))); 328 engineMap.put ("jspSource", "apply"); 329 } else if ("save".equals (actionScreen)) { 331 applyModifications (jParams); 332 engineMap.put ("jspSource", "close"); 333 releaseAllLock (jParams); 335 JahiaSiteMapService siteMapService = ServicesRegistry.getInstance().getJahiaSiteMapService(); 338 ContentPage contentPage = ServicesRegistry.getInstance (). 339 getJahiaPageService ().lookupContentPage(jParams.getPageID(), false); 340 341 siteMapService.removeSessionPageSiteMap(jParams.getUser(), contentPage, jParams.getSessionID()); 342 } else if ("cancel".equals (actionScreen)) { 344 releaseAllLock (jParams); 346 JahiaSiteMapService siteMapService = ServicesRegistry.getInstance().getJahiaSiteMapService(); 349 ContentPage contentPage = ServicesRegistry.getInstance (). 350 getJahiaPageService ().lookupContentPage(jParams.getPageID(), false); 351 352 siteMapService.removeSessionPageSiteMap(jParams.getUser(), contentPage, jParams.getSessionID()); 353 engineMap.put ("jspSource", "close"); 354 } 355 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 356 toolBox.displayScreen (jParams, engineMap); 357 } 358 359 364 public final String getName () { 365 return ENGINE_NAME; 366 } 367 368 375 private void initEngineMap (ParamBean jParams, HashMap engineMap) 376 throws JahiaException { 377 engineMap.clear (); 378 engineMap.put ("jspSource", TEMPLATE_JSP); 379 engineMap.put ("tabOptions", _tabOptions); 380 jParams.getRequest ().setAttribute ("engineTitle", "Workflow"); } 382 383 396 private void applyModifications (ParamBean jParams) 397 throws JahiaException { 398 399 409 ConnectionDispenser.getTxConnection (); 411 412 JahiaSaveVersion saveVersion = ServicesRegistry.getInstance ().getJahiaVersionService (). 413 getSiteSaveVersion (jParams.getSiteID ()); 414 Enumeration paramNames = jParams.getRequest ().getParameterNames (); 415 WorkflowMap workflowForm = new WorkflowMap (); 416 417 boolean areLocksActivated = Jahia.getSettings().areLocksActivated(); 418 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService (); 419 420 while (paramNames.hasMoreElements ()) { 421 String paramName = (String ) paramNames.nextElement (); 422 String [] paramValues = jParams.getRequest ().getParameterValues (paramName); 423 for (int i = 0; i < paramValues.length; i++) { 424 StringTokenizer paramToken = new StringTokenizer (paramName, "|"); 425 if (paramToken.hasMoreTokens ()) { 426 String nextParam = paramToken.nextToken (); 427 if ("checkbox".equals (nextParam)) { 429 String languageCode = paramToken.nextToken (); 430 String pageIDStr = paramToken.nextToken (); 431 if (areLocksActivated) { 433 int pageID = Integer.parseInt (pageIDStr); 435 LockKey lockKey = LockKey.composeLockKey (LockKey.WORKFLOW_TYPE, 436 pageID, pageID); 437 if (!lockRegistry.isAlreadyAcquiredInContext (lockKey, 438 jParams.getUser (), jParams.getSessionID ())) { 439 continue; 440 } 441 } 442 String acceptRefuse = paramToken.hasMoreTokens () ? 447 paramToken.nextToken () : null; 448 if (acceptRefuse == null) { workflowForm.setLanguage (WorkflowMap.FOR_NOTIFY_COMPLETION, 450 languageCode, pageIDStr); 451 } else { if ("refuse".equals (acceptRefuse)) { 453 workflowForm.setLanguage (WorkflowMap.FOR_REFUSE_PENDING, 454 languageCode, pageIDStr); 455 } else { workflowForm.setLanguage (WorkflowMap.FOR_ACCEPT_PENDING, 457 languageCode, pageIDStr); 458 } 459 } 460 } 461 if ("input".equals (nextParam)) { 463 String pageIDStr = paramToken.nextToken (); 464 if (!EMPTY_STRING.equals (paramValues[i])) { 465 workflowForm.setComment (pageIDStr, paramValues[i]); 466 } 467 } 468 } 469 } 470 } 471 472 473 Map userNotifData = new HashMap (); 474 ArrayList recipients; 475 476 for (int workflowOperation = 0; workflowOperation < 3; workflowOperation++) { 477 boolean shouldSendMessage = false; 479 HashMap languageMapForNotifyCompletion = workflowForm. 480 getLanguageMap( 481 workflowOperation); 482 Iterator languageMapIt = languageMapForNotifyCompletion.keySet(). 483 iterator(); 484 ArrayList pageIDs = new ArrayList (); 485 while (languageMapIt.hasNext ()) { 486 String pageIDStr = (String ) languageMapIt.next(); 487 try { 488 Integer I = new Integer (Integer.parseInt(pageIDStr)); 489 pageIDs.add(I); 490 } catch ( Throwable t ){ 491 logger.debug(t); 492 } 493 } 494 495 Vector sortedPages = ServicesRegistry.getInstance().getJahiaPageService() 497 .sortPages(pageIDs,jParams.getEntryLoadRequest(), 498 jParams.getUser(),jParams.getOperationMode()); 499 Enumeration enumeration = sortedPages.elements(); 500 try { 501 while (enumeration.hasMoreElements()) { 502 Integer I = (Integer ) enumeration.nextElement(); 503 int pageID = I.intValue(); 504 String pageIDStr = String.valueOf(pageID); 505 HashSet languageCodes = (HashSet ) workflowForm.getLanguages(workflowOperation, pageIDStr); 506 StateModificationContext stateModifContext = new StateModificationContext( 507 new ContentPageKey(pageID), languageCodes); 508 stateModifContext.setDescendingInSubPages(false); 509 try { 510 ContentPage contentPage = ContentPage.getPage(pageID, true, true); 515 516 switch (workflowOperation) { 517 case 0: contentPage.changeStagingStatus(languageCodes, EntryLoadRequest.WAITING_WORKFLOW_STATE, 519 jParams, stateModifContext); 520 521 commitAndFlushCaches(contentPage); 523 524 shouldSendMessage = true; 526 528 recipients = getRecipients(contentPage, jParams.getSiteID(), 0x2); 530 for (int i = 0; i < recipients.size(); i++) { 531 String curEmail = (String ) recipients.get(i); 532 Map operationMap = (Map ) userNotifData.get(curEmail); 533 if (operationMap == null) { 534 operationMap = new HashMap (); 535 } 536 Set pageList = (Set ) operationMap.get(WAITINGFORAPPROVAL_OPNAME); 537 if (pageList == null) { 538 pageList = new HashSet (); 539 } 540 PageNotifData pageNotifData = new PageNotifData( 541 jParams.getSiteURL(pageID, false, false), 542 workflowForm.getComment(pageIDStr, jParams 543 .getLocale()), contentPage, 544 languageCodes, true, null, null); 545 pageList.add(pageNotifData); 546 operationMap.put(WAITINGFORAPPROVAL_OPNAME, pageList); 547 userNotifData.put(curEmail, operationMap); 548 } 549 break; 550 case 1: contentPage.changeStagingStatus(languageCodes, EntryLoadRequest.STAGING_WORKFLOW_STATE, 552 jParams, stateModifContext); 553 LockKey lockKey = LockKey.composeLockKey(LockKey.WAITING_FOR_APPROVAL_TYPE, contentPage 554 .getID(), contentPage.getID()); 555 lockRegistry.release(lockKey, jParams.getUser(), jParams.getSessionID()); 556 557 commitAndFlushCaches(contentPage); 559 560 shouldSendMessage = true; 561 563 recipients = getRecipients(contentPage, jParams.getSiteID(), 0x1); 565 for (int i = 0; i < recipients.size(); i++) { 566 String curEmail = (String ) recipients.get(i); 567 Map operationMap = (Map ) userNotifData.get(curEmail); 568 if (operationMap == null) { 569 operationMap = new HashMap (); 570 } 571 Set pageList = (Set ) operationMap.get(REFUSAL_OPNAME); 572 if (pageList == null) { 573 pageList = new HashSet (); 574 } 575 PageNotifData pageNotifData = new PageNotifData( 576 jParams.getSiteURL(pageID, false, false), 577 workflowForm.getComment(pageIDStr, jParams 578 .getLocale()), contentPage, 579 languageCodes, true, null, null); 580 pageList.add(pageNotifData); 581 operationMap.put(REFUSAL_OPNAME, pageList); 582 userNotifData.put(curEmail, operationMap); 583 } 584 break; 585 586 case 2: logger.debug("Start activating page: " + pageID); 588 589 int activeVersionId = contentPage.getActiveVersionID(); 592 int deletedVersionId = contentPage.getDeleteVersionID(); 593 if (activeVersionId == saveVersion.getVersionID() 594 || deletedVersionId == saveVersion.getVersionID()) { 595 logger.debug("Either active version ID: " 598 + activeVersionId 599 + " or deleted version ID: " 600 + deletedVersionId 601 + " is the same as saved version ID: " 602 + saveVersion.getVersionID()); 603 continue; 604 } 605 606 int parentPageIDToFlushFromCache = contentPage.hasSameParentID(); 611 if (parentPageIDToFlushFromCache > 0) { 612 617 ServicesRegistry.getInstance().getJahiaSiteMapService().resetSiteMap(); 618 } 619 JahiaSite jahiaSite = ServicesRegistry.getInstance().getJahiaSitesService().getSite( 621 jParams.getSiteID()); 622 623 boolean shouldActivateStagedParentPage = false; 628 boolean shouldActivateCurrentParentPage = false; 629 ContentPage stagedParentPage = null; 630 ContentPage currentParentPage = null; 631 if (contentPage.hasSameParentID() != ContentPage.SAME_PARENT) { 632 try { 633 int stagedParentID = contentPage.getParentID(EntryLoadRequest.STAGED); 635 if (stagedParentID > 0) { 636 stagedParentPage = ContentPage.getPage(stagedParentID); 637 if (stagedParentPage.hasSameParentID() != ContentPage.SAME_PARENT) { 638 if (stagedParentPage.getParentID(EntryLoadRequest.CURRENT) == contentPage 639 .getID()) { 640 shouldActivateStagedParentPage = true; 643 } 644 } 645 } 646 } catch (Throwable t) { 647 logger.debug("Exception when activate staged parent page of page " 648 + contentPage.getID(), t); 649 } 650 try { 651 652 int currentParentID = contentPage.getParentID(EntryLoadRequest.CURRENT); 654 if (currentParentID > 0) { 655 currentParentPage = ContentPage.getPage(currentParentID); 656 if (currentParentPage.hasSameParentID() != ContentPage.SAME_PARENT) { 657 if (currentParentPage.getParentID(EntryLoadRequest.STAGED) == contentPage 658 .getID()) { 659 shouldActivateCurrentParentPage = true; 662 } 663 } 664 } 665 } catch (Throwable t) { 666 logger.debug("Exception when activate current parent page of page " 667 + contentPage.getID(), t); 668 } 669 } 670 671 if (shouldActivateStagedParentPage) { 672 logger.debug("Should activate staged parent page"); 673 activatePage(stagedParentPage, languageCodes, saveVersion, jParams); 674 } 675 if (shouldActivateCurrentParentPage) { 676 logger.debug("Should activate current parent page"); 677 activatePage(currentParentPage, languageCodes, saveVersion, jParams); 678 } 679 680 logger.debug("start change staging status"); 684 contentPage.changeStagingStatus(languageCodes, EntryLoadRequest.STAGING_WORKFLOW_STATE, 685 jParams, stateModifContext); 686 ActivationTestResults activationTestResults = contentPage.activeStagingEntries( 687 languageCodes, jahiaSite.isVersioningEnabled(), saveVersion, jParams.getUser(), 688 jParams, stateModifContext); 689 690 boolean workflowSuccessful = (activationTestResults.getStatus() != ActivationTestResults.FAILED_OPERATION_STATUS); 691 692 696 logger.debug("ActivationTestResult=" + activationTestResults); 697 710 lockKey = LockKey.composeLockKey(LockKey.WAITING_FOR_APPROVAL_TYPE, contentPage.getID(), 711 contentPage.getID()); 712 lockRegistry.release(lockKey, jParams.getUser(), jParams.getSessionID()); 713 714 commitAndFlushCaches(contentPage); 716 717 shouldSendMessage = true; 718 720 recipients = getRecipients(contentPage, jParams.getSiteID(), 0x3); 722 for (int i = 0; i < recipients.size(); i++) { 723 String curEmail = (String ) recipients.get(i); 724 Map operationMap = (Map ) userNotifData.get(curEmail); 725 if (operationMap == null) { 726 operationMap = new HashMap (); 727 } 728 Set pageList = (Set ) operationMap.get(APPROVAL_OPNAME); 729 if (pageList == null) { 730 pageList = new HashSet (); 731 } 732 PageNotifData pageNotifData = new PageNotifData( 733 jParams.getSiteURL(pageID, false, false), 734 workflowForm.getComment(pageIDStr, jParams 735 .getLocale()), contentPage, 736 languageCodes, workflowSuccessful, 737 activationTestResults.getErrors(), 738 activationTestResults.getWarnings()); 739 pageList.add(pageNotifData); 740 operationMap.put(APPROVAL_OPNAME, pageList); 741 userNotifData.put(curEmail, operationMap); 742 } 743 744 break; 745 } 746 } catch (JahiaException je) { 747 logger.error("Cannot change Jahia Page staging status", je); 748 throw je; 749 } 750 } 751 } finally { 752 if (shouldSendMessage) { 753 String jspMailNotifFileName = jParams.getPage ().getPageTemplate ().getSourcePath (); 755 jspMailNotifFileName = jspMailNotifFileName.substring (0, 756 jspMailNotifFileName.lastIndexOf ("/") + 1) + 757 WORKFLOW_MAIL_NOTIF_JSP; 758 File jspFile = new File (jParams.getContext ().getRealPath (jspMailNotifFileName)); 759 if (!jspFile.exists ()) { 760 jspMailNotifFileName = DEFAULT_PATH_TO_JSP; 761 } 762 jspFile = new File (jParams.getContext ().getRealPath (jspMailNotifFileName)); 763 if (jspFile.exists ()) { 764 Iterator userIter = userNotifData.keySet ().iterator (); 765 while (userIter.hasNext ()) { 766 String curEmail = (String ) userIter.next (); 767 Map operationMap = (Map ) userNotifData.get (curEmail); 768 logger.debug ("Sending custom notification data to email " + 769 curEmail); 770 jParams.getRequest ().setAttribute ("org.jahia.engines.workflow.paramBean", 771 jParams); 772 jParams.getRequest ().setAttribute ( 773 "org.jahia.engines.workflow.notifyOperationMap", operationMap); 774 String mailBody = ServicesRegistry.getInstance ().getJahiaFetcherService (). 777 fetchServlet (jParams, jspMailNotifFileName); 778 jParams.getRequest ().removeAttribute ( 779 "org.jahia.engines.workflow.notifyOperationMap"); 780 jParams.getRequest ().removeAttribute ("org.jahia.engines.workflow.paramBean"); 781 String from = (String ) jParams.getRequest ().getAttribute ( 782 "org.jahia.engines.workflow.notify.from"); 783 String cc = (String ) jParams.getRequest ().getAttribute ( 784 "org.jahia.engines.workflow.notify.cc"); 785 String bcc = (String ) jParams.getRequest ().getAttribute ( 786 "org.jahia.engines.workflow.notify.bcc"); 787 String subject = (String ) jParams.getRequest ().getAttribute ( 788 "org.jahia.engines.workflow.notify.subject"); 789 jParams.getRequest ().removeAttribute ( 790 "org.jahia.engines.workflow.notify.from"); 791 jParams.getRequest ().removeAttribute ("org.jahia.engines.workflow.notify.cc"); 792 jParams.getRequest ().removeAttribute ("org.jahia.engines.workflow.notify.bcc"); 793 jParams.getRequest ().removeAttribute ( 794 "org.jahia.engines.workflow.notify.subject"); 795 ServicesRegistry.getInstance ().getMailService ().sendMessage (from, curEmail, 796 cc, bcc, subject, mailBody, false); 797 } 798 } 799 } 800 } 801 } 802 workflowForm = null; } 804 805 private void releaseAllLock (ParamBean jParams) 807 throws JahiaException { 808 if (jParams.settings ().areLocksActivated ()) { 809 HtmlCache htmlCache = CacheFactory.getHtmlCache(); 810 ContentPage contentPage = ServicesRegistry.getInstance (). 811 getJahiaPageService ().lookupContentPage (jParams.getPageID (), false); 812 JahiaSiteMapService siteMapService = ServicesRegistry.getInstance (). 813 getJahiaSiteMapService (); 814 int pageInfosFlag = ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS; 817 PagesFilter pagesFilter = JahiaSiteMapService.getCurrentPagesFilter(null, jParams); 818 siteMapService.invokeTreeSiteMapViewHelperMethod (jParams.getUser (), 819 contentPage, jParams.getSessionID (), pageInfosFlag, 820 null, "expandall|0", pagesFilter, jParams); 821 822 SiteMapViewHelper treeSiteMapViewHelper = siteMapService. 825 getTreeSiteMapViewHelper (jParams.getUser (), contentPage, 826 jParams.getSessionID (), ContentPage.ACTIVE_PAGE_INFOS | 827 ContentPage.STAGING_PAGE_INFOS, 828 null, Integer.MAX_VALUE, pagesFilter, jParams); 829 830 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService (); 832 for (int i = 0; i < treeSiteMapViewHelper.size (); i++) { 833 ContentPage siteMapContentPage = treeSiteMapViewHelper.getContentPage (i); 834 if (siteMapContentPage != null) { 835 LockKey lockKey = LockKey.composeLockKey (LockKey.WORKFLOW_TYPE, 836 siteMapContentPage.getID (), siteMapContentPage.getID ()); 837 lockRegistry.release (lockKey, jParams.getUser (), jParams.getSessionID ()); 838 htmlCache.invalidatePageEntries(Integer.toString(siteMapContentPage.getID()),jParams.getEntryLoadRequest().getWorkflowState()); 839 } 840 } 841 } 842 } 843 845 866 private ArrayList getRecipients (ContentPage contentPage, int siteID, int access) { 867 ArrayList result = new ArrayList (); 868 result.add (ServicesRegistry.getInstance ().getMailService ().defaultRecipient ()); 869 875 JahiaBaseACL pageACL = contentPage.getACL (); 876 try { 877 Vector users = pageACL.getUsernameListAlsoGroupUsers (null); 878 for (int i = 0; i < users.size (); i++) { 879 JahiaUser user = ServicesRegistry.getInstance (). 880 getJahiaUserManagerService ().lookupUser ((String ) users.get ( 881 i)); 882 if (user == null) 883 continue; 884 String email = user.getProperty ("email"); 885 if (email != null && !EMPTY_STRING.equals (email)) { 886 887 if (email.indexOf ("@localhost") != -1) { 888 continue; 889 } 890 boolean shouldCheckWriteAccess = (access & 0x01) != 0; 891 boolean shouldCheckAdminAccess = (access & 0x02) != 0; 892 if (shouldCheckWriteAccess && 893 contentPage.checkWriteAccess (user) || 894 shouldCheckAdminAccess && 895 contentPage.checkAdminAccess (user)) { 896 result.add (email); 897 } 898 } 899 } 900 } catch (JahiaACLException jae) { 901 logger.error ("Error while retrieving users from ACL", jae); 902 } 903 logger.debug ("Using email recipient list :" + result); 904 return result; 905 } 906 907 908 909 910 911 public class PageNotifData implements Comparable { 912 private String pageURL; 913 private String pageComment; 914 private ContentPage contentPage; 915 private Set languageCodes; 916 private boolean workflowSuccessful; 917 private ArrayList errors; 918 private ArrayList warnings; 919 920 public PageNotifData(String thePageURL, String thePageComment, ContentPage theContentPage, HashSet langCodes, 921 boolean isWorkflowSuccessful, ArrayList errorList, ArrayList warningList) { 922 this.pageURL = thePageURL; 923 this.pageComment = thePageComment; 924 this.contentPage = theContentPage; 925 this.languageCodes = langCodes; 926 this.workflowSuccessful = isWorkflowSuccessful; 927 if (errorList != null) { 928 this.errors = errorList; 929 } else { 930 this.errors = new ArrayList (); 931 } 932 if (warningList != null) { 933 this.warnings = warningList; 934 } else { 935 this.warnings = new ArrayList (); 936 } 937 } 938 939 public String getPageURL () { 940 return pageURL; 941 } 942 943 public String getPageComment () { 944 return pageComment; 945 } 946 947 public ContentPage getContentPage () { 948 return contentPage; 949 } 950 951 public Set getLanguageCodes() { 952 return languageCodes; 953 } 954 955 public Map getTitles() { 956 Hashtable allTitles = contentPage.getTitles(true); 957 Map workflowTitles = new HashMap (); 958 Iterator languageIter = languageCodes.iterator(); 959 while (languageIter.hasNext()) { 960 String curLanguageCode = (String ) languageIter.next(); 961 String curLanguageTitle = (String ) allTitles.get(curLanguageCode); 962 workflowTitles.put(curLanguageCode, curLanguageTitle); 963 } 964 return workflowTitles; 965 } 966 967 public String getDisplayTitle() { 968 Map titles = getTitles(); 969 StringBuffer result = new StringBuffer (); 970 Iterator entryIter = titles.entrySet().iterator(); 971 while (entryIter.hasNext()) { 972 Map.Entry curEntry = (Map.Entry ) entryIter.next(); 973 result.append(curEntry.getValue()); 974 if (entryIter.hasNext()) { 975 result.append(", "); 976 } 977 } 978 return result.toString(); 979 } 980 981 public String getDisplayTitles() { 982 Map titles = getTitles(); 983 StringBuffer result = new StringBuffer (); 984 Iterator entryIter = titles.entrySet().iterator(); 985 while (entryIter.hasNext()) { 986 Map.Entry curEntry = (Map.Entry ) entryIter.next(); 987 result.append(curEntry.getKey()); 988 result.append("="); 989 result.append(curEntry.getValue()); 990 if (entryIter.hasNext()) { 991 result.append(", "); 992 } 993 } 994 return result.toString(); 995 } 996 997 public boolean isWorkflowSuccessful() { 998 return workflowSuccessful; 999 } 1000 1001 public ArrayList getErrors() { 1002 return errors; 1003 } 1004 1005 public ArrayList getWarnings() { 1006 return warnings; 1007 } 1008 1009 public String getDisplayErrors(Locale locale, String separator) { 1010 return getDisplayActivationResults(getErrors(), locale, separator); 1011 } 1012 1013 private String getDisplayActivationResults(ArrayList activationResults, Locale locale, String separator) { 1014 StringBuffer result = new StringBuffer (); 1015 Iterator activationIter = activationResults.iterator(); 1016 while (activationIter.hasNext()) { 1017 Object curActivationResultObj = activationIter.next(); 1018 if (!(curActivationResultObj instanceof IsValidForActivationResults)) { 1019 result.append(curActivationResultObj.toString()); 1020 } else { 1021 IsValidForActivationResults validationResult = ( 1022 IsValidForActivationResults) curActivationResultObj; 1023 String resultFormat = JahiaResourceBundle. 1024 getMessageResource( 1025 "org.jahia.engines.workflow.activationResultMessage", 1026 locale); 1027 Object [] arguments = { 1028 validationResult.getComment(), 1029 validationResult.getObjectType(), 1030 new Integer (validationResult. 1031 getObjectID()), 1032 validationResult.getLanguageCode() 1033 }; 1034 1035 result.append(MessageFormat.format(resultFormat, arguments)); 1036 } 1037 if (activationIter.hasNext()) { 1038 result.append(separator); 1039 } 1040 } 1041 return result.toString(); 1042 } 1043 1044 public boolean equals (Object o) { 1045 if (!(o instanceof PageNotifData)) { 1046 return false; 1047 } 1048 PageNotifData right = (PageNotifData) o; 1049 return ((pageURL.equals (right.pageURL)) && (pageComment.equals (right.pageComment))); 1050 } 1051 1052 public int hashCode () { 1053 return (pageURL + pageComment).hashCode (); 1054 } 1055 1056 public int compareTo (Object o) throws ClassCastException { 1057 PageNotifData right = (PageNotifData) o; 1058 int pageURLComp = pageURL.compareTo (right.pageURL); 1059 if (pageURLComp != 0) { 1060 return pageURLComp; 1061 } 1062 return pageComment.compareTo (right.pageComment); 1063 } 1064 } 1065 1066 1077 protected class WorkflowMap { 1078 1079 public static final int FOR_NOTIFY_COMPLETION = 0; 1080 public static final int FOR_REFUSE_PENDING = 1; 1081 public static final int FOR_ACCEPT_PENDING = 2; 1082 1083 public void setLanguage (int whatFor, String languageCode, String pageID) { 1084 Set languages = (HashSet ) languageMap[whatFor].get (pageID); 1085 if (languages == null) { 1086 Set _languages = new HashSet (); 1087 _languages.add (languageCode); 1088 languageMap[whatFor].put (pageID, _languages); 1089 } else { 1090 languages.add (languageCode); 1091 } 1092 } 1093 1094 public Set getLanguages (int whatFor, String pageID) { 1095 return (HashSet ) languageMap[whatFor].get (pageID); 1096 } 1097 1098 public void setComment (String pageID, String comment) { 1099 commentMap.put (pageID, comment); 1100 } 1101 1102 public String getComment (String pageID, Locale userLocale) { 1103 String comment = (String ) commentMap.get (pageID); 1104 if (comment == null) { 1105 comment = JahiaResourceBundle.getMessageResource( 1106 "org.jahia.engines.workflow.noComment", 1107 userLocale); 1108 } 1109 return comment; 1110 } 1111 1112 public HashMap getLanguageMap (int whatFor) { 1113 return languageMap[whatFor]; 1114 } 1115 1116 private HashMap commentMap = new HashMap (); 1117 private HashMap languageMapForNotifyCompletion = new HashMap (); 1118 private HashMap languageMapForRefusePending = new HashMap (); 1119 private HashMap languageMapForAcceptPending = new HashMap (); 1120 private HashMap [] languageMap = {languageMapForNotifyCompletion, 1121 languageMapForRefusePending, 1122 languageMapForAcceptPending}; 1123 } 1124 1125 private ActivationTestResults activatePage (ContentPage contentPage, 1126 HashSet languageCodes, 1127 JahiaSaveVersion saveVersion, 1128 ParamBean jParams) 1129 throws JahiaException { 1130 1131 StateModificationContext smc = new 1132 StateModificationContext(new 1133 ContentPageKey(contentPage.getID()), languageCodes); 1134 smc.setDescendingInSubPages(false); 1135 1136 contentPage. 1140 changeStagingStatus(languageCodes, 1141 EntryLoadRequest. 1142 STAGING_WORKFLOW_STATE, 1143 jParams, smc); 1144 1145 return contentPage.activeStagingEntries(languageCodes, 1146 jParams.getSite(). 1147 isVersioningEnabled(), 1148 saveVersion, 1149 jParams.getUser(), jParams, 1150 smc); 1151 } 1152 1153 private void commitAndFlushCaches(ContentPage contentPage) throws JahiaException { 1154 ConnectionDispenser.terminateConnection(); 1155 contentPage.invalidateHtmlCache(); 1156 ServicesRegistry.getInstance().getJahiaPageService().invalidatePageCache(contentPage.getID()); 1157 ConnectionDispenser.getTxConnection(); 1159 } 1160} 1161 | Popular Tags |