1 37 38 package org.jahia.blogs.actions; 39 import org.jahia.blogs.ServletResources; 40 41 import org.jahia.blogs.model.PostInfo; 42 import org.jahia.blogs.model.MetaPostInfo; 43 44 import org.jahia.bin.Jahia; 45 46 import org.jahia.content.ContentContainerListsXRefManager; 47 import org.jahia.content.ContentContainerKey; 48 import org.jahia.content.ContentContainerListKey; 49 import org.jahia.content.ContentObject; 50 51 import org.jahia.services.fields.ContentField; 52 53 import org.jahia.services.usermanager.JahiaSiteUserManagerService; 54 import org.jahia.services.usermanager.JahiaUser; 55 56 import org.jahia.data.fields.LoadFlags; 57 import org.jahia.data.fields.JahiaField; 58 59 import org.jahia.data.containers.JahiaContainerList; 60 import org.jahia.data.containers.JahiaContainer; 61 62 import org.jahia.services.version.ActivationTestResults; 63 import org.jahia.services.version.EntryLoadRequest; 64 import org.jahia.services.version.JahiaSaveVersion; 65 import org.jahia.services.version.StateModificationContext; 66 67 import org.jahia.services.pages.ContentPage; 68 import org.jahia.services.pages.JahiaPage; 69 70 import org.jahia.services.containers.JahiaContainersService; 71 72 import org.jahia.services.cache.HtmlCache; 73 import org.jahia.services.cache.CacheFactory; 74 75 import org.jahia.services.categories.Category; 76 77 import org.jahia.registries.ServicesRegistry; 78 79 import org.jahia.params.ParamBean; 80 81 import org.jahia.exceptions.JahiaException; 82 83 import org.apache.log4j.Logger; 84 85 import java.util.Set ; 86 import java.util.HashSet ; 87 import java.util.Iterator ; 88 import java.util.Hashtable ; 89 import java.util.Date ; 90 import java.util.Enumeration ; 91 import java.util.Vector ; 92 93 99 public abstract class AbstractAction { 100 101 protected String appKey; 102 protected transient String userName; 103 protected transient String password; 104 105 protected ServicesRegistry servicesRegistry; 106 protected JahiaContainersService containerService; 107 protected ParamBean jParams; 108 109 protected BlogDefinitionNames containerNames; 110 111 static Logger log = Logger.getLogger(AbstractAction.class); 113 114 121 public abstract Object execute() throws JahiaException; 122 123 128 protected void init() throws JahiaException { 129 servicesRegistry = ServicesRegistry.getInstance(); 131 132 containerService = servicesRegistry.getJahiaContainersService(); 134 135 jParams = new ParamBean( 137 ServletResources.getCurrentRequest(), 138 ServletResources.getCurrentResponse(), 139 ServletResources.getCurrentConfig().getServletContext(), 140 Jahia.getSettings(), 141 System.currentTimeMillis(), 142 ParamBean.GET_METHOD); 143 144 containerNames = new BlogDefinitionNames(jParams); 146 log.debug("Init sucessfull"); 147 } 148 149 155 protected JahiaUser checkLogin() throws JahiaException { 156 157 final JahiaSiteUserManagerService userManagerService = servicesRegistry. 159 getJahiaSiteUserManagerService(); 160 161 final JahiaUser theUser = userManagerService.getMember( 163 jParams.getSiteID(), userName); 164 if (theUser != null) { 165 if (!theUser.verifyPassword(password)) { 166 JahiaException je = new JahiaException("Login error", 167 "User " + userName + " entered bad password", 168 JahiaException.SECURITY_ERROR, 169 JahiaException.WARNING_SEVERITY); 170 log.error("Couldn't validate password for user " + 171 theUser.getUserKey() + "!", je); 172 throw je; 173 174 } 175 jParams.purgeSession(); 176 jParams.setUser(theUser); 177 178 } else { 179 throw new JahiaException("Login error", 180 "Login error: Unknown User "+userName, 181 JahiaException.SECURITY_ERROR, 182 JahiaException.WARNING_SEVERITY); 183 } 184 185 log.debug("Login sucessfull"); 186 return theUser; 187 } 188 189 199 protected JahiaContainer getContainer(int id, String languageCode) 200 throws JahiaException { 201 JahiaContainer postContainer = containerService.loadContainer( 202 id, LoadFlags.ALL, jParams, EntryLoadRequest.STAGED); 203 204 if (postContainer == null) { 205 throw new JahiaException("Post: "+id+ 206 " does not exist", "Container: "+id+ " does not exist", 207 JahiaException.ENTRY_NOT_FOUND, 208 JahiaException.WARNING_SEVERITY); 209 } 210 211 if (languageCode != null) { 212 postContainer.setLanguageCode(languageCode); 213 postContainer.fieldsStructureCheck(jParams); 214 } 215 return postContainer; 216 } 217 218 226 protected JahiaContainer getContainer(int id) 227 throws JahiaException { 228 EntryLoadRequest elr = new EntryLoadRequest(EntryLoadRequest. 229 STAGING_WORKFLOW_STATE, 230 0, 231 jParams.getEntryLoadRequest().getLocales()); 232 233 jParams.setSubstituteEntryLoadRequest(elr); 234 JahiaContainer postContainer = containerService.loadContainer( 235 id, LoadFlags.ALL, jParams, elr); 236 jParams.resetSubstituteEntryLoadRequest(); 237 return postContainer; 238 } 239 240 246 protected void flushPageCacheThatDisplayContainer(JahiaContainer 247 theContainer) throws JahiaException { 248 249 log.debug("Flushing cache..."); 250 251 EntryLoadRequest loadVersion = EntryLoadRequest.CURRENT; 252 if (servicesRegistry.getJahiaVersionService(). 253 isStagingEnabled(theContainer.getJahiaID())) { 254 loadVersion = EntryLoadRequest.STAGED; 255 } 256 257 JahiaContainerList theList = containerService. 258 loadContainerListInfo(theContainer. 259 getListID(), loadVersion); 260 261 HtmlCache htmlCache = CacheFactory.getHtmlCache(); 263 if (htmlCache == null) 264 log.warn("Could not get the HTML cache instance!!"); 265 266 if (theList != null) { 270 Set containerPageRefs = ContentContainerListsXRefManager. 271 getInstance(). 272 getAbsoluteContainerListPageIDs( 273 theList.getID()); 274 if (containerPageRefs != null) { 275 Iterator pageRefIDs = containerPageRefs.iterator(); 276 while (pageRefIDs.hasNext()) { 277 Integer curPageID = (Integer ) pageRefIDs.next(); 278 279 if (htmlCache != null) 280 htmlCache.invalidatePageEntries(curPageID.toString(), 281 jParams.getEntryLoadRequest().getWorkflowState()); 282 } 283 } else { 284 log.debug("Why is cross ref list empty ?"); 285 } 286 } else { 287 log.debug("Couldn't retrieve parent containerList, why is that ?"); 288 } 289 290 if (htmlCache != null) { 293 htmlCache.invalidatePageEntries(Integer.toString( 294 jParams.getPageID()).toString(), 295 jParams.getEntryLoadRequest().getWorkflowState()); 296 } 297 } 298 299 306 protected ContentPage changePage(int pageID) throws JahiaException { 307 ContentPage blogContentPage = ContentPage.getPage(pageID); 308 309 if (blogContentPage == null) { 310 throw new JahiaException( 311 "Blog: "+pageID+ " does not exist", 312 "Page: "+pageID+ " does not exist", 313 JahiaException.ENTRY_NOT_FOUND, 314 JahiaException.WARNING_SEVERITY); 315 } 316 317 jParams.changePage(blogContentPage); 318 return blogContentPage; 319 } 320 321 330 protected ActivationTestResults activateContainer(int containerID, 331 JahiaUser user) 332 throws JahiaException { 333 log.debug("activating container: "+containerID); 334 335 Set languageCodes = new HashSet (); 336 languageCodes.add(ContentObject.SHARED_LANGUAGE); 337 languageCodes.add(jParams.getLocale().toString()); 338 339 JahiaSaveVersion saveVersion = servicesRegistry.getJahiaVersionService(). 340 getSiteSaveVersion(jParams.getSiteID()); 341 342 StateModificationContext smc = new StateModificationContext( 343 new ContentContainerKey(containerID), languageCodes); 344 smc.setDescendingInSubPages(false); 345 346 JahiaContainer container = this.getContainer(containerID); 347 Enumeration childs = container.getFields(); 348 349 while (childs.hasMoreElements()) { 350 JahiaField child = (JahiaField)childs.nextElement(); 351 ContentField field = child.getContentField(); 352 353 field.activate(languageCodes, saveVersion.getVersionID(), 354 jParams, smc); 355 } 356 357 ActivationTestResults res = containerService.activateStagedContainer( 358 languageCodes, containerID, user, saveVersion, jParams, smc); 359 360 log.debug(res); 361 return res; 362 } 363 364 373 protected ActivationTestResults activateContainerList(int containerListID, 374 JahiaUser user, int pageID) throws JahiaException { 375 log.debug("activating containerList: "+containerListID); 376 377 Set languageCodes = new HashSet (); 378 languageCodes.add(ContentObject.SHARED_LANGUAGE); 379 languageCodes.add(jParams.getLocale().toString()); 380 381 JahiaSaveVersion saveVersion = servicesRegistry.getJahiaVersionService(). 382 getSiteSaveVersion(jParams.getSiteID()); 383 384 StateModificationContext smc = new StateModificationContext( 385 new ContentContainerListKey(containerListID), languageCodes); 386 smc.setDescendingInSubPages(false); 387 388 ActivationTestResults res = containerService.activateStagedContainerLists( 389 languageCodes, pageID, user, saveVersion, smc); 390 391 log.debug(res); 392 return res; 393 } 394 395 403 protected Hashtable createPostInfo(JahiaContainer postContainer) 404 throws JahiaException { 405 Hashtable postInfo = new Hashtable (4); 406 String fieldName = containerNames.getValue(containerNames.POST_BODY); 407 408 StringBuffer buffer = new StringBuffer (); 409 buffer.append("<"); 410 buffer.append(containerNames.getValue(containerNames.POST_TITLE)); 411 buffer.append(">"); 412 buffer.append(postContainer.getFieldValue(containerNames.getValue( 413 containerNames.POST_TITLE), "n/a")); 414 buffer.append("</"); 415 buffer.append(containerNames.getValue(containerNames.POST_TITLE)); 416 buffer.append(">"); 417 418 buffer.append(this.getValue(postContainer.getField(fieldName))); 419 postInfo.put(PostInfo.CONTENT, buffer.toString()); 420 421 fieldName = containerNames.getValue(containerNames.POST_AUTHOR); 422 JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService(). 423 getMember(jParams.getSiteID(), postContainer.getField(fieldName). 424 getValue()); 425 426 String userName; 427 if (author == null) { 428 userName = "guest"; 429 430 } else { 431 userName = author.getUsername(); 432 } 433 434 postInfo.put(MetaPostInfo.USER_ID, userName); 435 436 postInfo.put(PostInfo.POST_ID, Integer.toString(postContainer.getID())); 437 438 fieldName = containerNames.getValue(containerNames.POST_DATE); 439 String dateValue = (String )postContainer.getField(fieldName).getObject(); 440 Date date = new Date (Long.parseLong(dateValue)); 441 postInfo.put(PostInfo.DATE_CREATED, date); 442 443 return postInfo; 444 } 445 446 455 protected Hashtable createMetaPostInfo(JahiaContainer postContainer, 456 Set categories) throws JahiaException { 457 458 Hashtable postInfo = new Hashtable (8); 459 460 String fieldName = containerNames.getValue(containerNames.POST_TITLE); 461 postInfo.put(MetaPostInfo.TITLE, 462 postContainer.getFieldValue(fieldName, "n/a")); 463 464 465 fieldName = containerNames.getValue(containerNames.POST_EXCERPT); 466 JahiaField f = postContainer.getField(fieldName); 467 if (f != null) { 468 postInfo.put(MetaPostInfo.MT_EXCERPT, 469 postContainer.getFieldValue(fieldName, "n/a")); 470 } 471 472 473 fieldName = containerNames.getValue(containerNames.POST_KEYWORDS); 474 f = postContainer.getField(fieldName); 475 if (f != null) { 476 postInfo.put(MetaPostInfo.MT_EXCERPT, 477 postContainer.getFieldValue(fieldName, "n/a")); 478 } 479 480 fieldName = containerNames.getValue(containerNames.POST_BODY); 481 postInfo.put(MetaPostInfo.DESCRIPTION, 482 this.getValue(postContainer.getField(fieldName))); 483 484 fieldName = containerNames.getValue(containerNames.POST_AUTHOR); 485 JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService(). 486 getMember(jParams.getSiteID(), postContainer.getField(fieldName). 487 getValue()); 488 489 String userName; 490 if (author == null) { 491 userName = "guest"; 492 493 } else { 494 userName = author.getUsername(); 495 } 496 497 postInfo.put(MetaPostInfo.USER_ID, userName); 498 499 postInfo.put(MetaPostInfo.POST_ID, Integer.toString(postContainer. 500 getID())); 501 502 fieldName = containerNames.getValue(containerNames.POST_DATE); 503 String dateValue = (String )postContainer.getField(fieldName).getObject(); 504 505 if (dateValue == null || dateValue.length() < 2) { 506 log.warn("No date for Container: "+ postContainer.getID()); 507 dateValue = "0"; 508 } 509 510 final Date date = new Date (Long.parseLong(dateValue)); 511 postInfo.put(MetaPostInfo.DATE_CREATED, date); 512 513 final JahiaPage blogPage = ContentPage.getPage(postContainer.getPageID()). 514 getPage(jParams); 515 516 final String url = getContainerURL(blogPage, postContainer); 517 postInfo.put(MetaPostInfo.LINK, url); 518 519 postInfo.put(MetaPostInfo.PERMANENT_LINK, url); 520 521 if (categories != null && categories.size() > 0) { 522 postInfo.put(MetaPostInfo.CATEGORIES, fromSet(categories)); 523 } 524 525 return postInfo; 526 } 527 528 531 protected Vector fromSet(Set categories) { 532 533 if (categories == null) return new Vector (0); 534 535 Vector cats = new Vector (categories.size()); 536 Iterator ite = categories.iterator(); 537 while (ite.hasNext()) { 538 Category cat = (Category)ite.next(); 539 String catName = cat.getTitle(jParams.getLocale()); 540 541 if (catName == null || catName.length() < 1) { 542 catName = cat.getKey(); 543 } 544 cats.addElement(catName); 545 } 546 return cats; 547 } 548 549 557 protected Hashtable createMovableTypePostInfo(JahiaContainer postContainer) 558 throws JahiaException { 559 560 Hashtable postInfo = new Hashtable (4); 561 562 String fieldName = containerNames.getValue(containerNames.POST_TITLE); 563 postInfo.put(MetaPostInfo.TITLE, postContainer.getField(fieldName). 564 getValue()); 565 566 fieldName = containerNames.getValue(containerNames.POST_AUTHOR); 567 JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService(). 568 getMember(jParams.getSiteID(), postContainer.getField(fieldName). 569 getValue()); 570 571 String userName; 572 if (author == null) { 573 userName = "guest"; 574 575 } else { 576 userName = author.getUsername(); 577 } 578 579 postInfo.put(MetaPostInfo.USER_ID, userName); 580 581 fieldName = containerNames.getValue(containerNames.POST_DATE); 582 String dateValue = (String )postContainer.getField(fieldName).getObject(); 583 584 if (dateValue == null || dateValue.length() < 2) { 585 log.warn("No date for Container: "+ postContainer.getID()); 586 dateValue = "0"; 587 } 588 589 final Date date = new Date (Long.parseLong(dateValue)); 590 postInfo.put(MetaPostInfo.DATE_CREATED, date); 591 592 postInfo.put(MetaPostInfo.POST_ID, String.valueOf(postContainer.getID())); 593 594 return postInfo; 595 } 596 597 604 protected String getPageURL(JahiaPage page) throws JahiaException { 605 StringBuffer buffer = new StringBuffer (); 606 buffer.append(ServletResources.getCurrentRequest(). 607 getScheme()); 608 buffer.append("://"); 609 buffer.append(ServletResources.getCurrentRequest(). 610 getServerName()); 611 buffer.append(":"); 612 buffer.append(ServletResources.getCurrentRequest(). 613 getServerPort()); 614 String savedCacheStatus = jParams.getCacheStatus(); 615 jParams.setCacheStatus(ParamBean.CACHE_BYPASS); 616 buffer.append(page.getURL(jParams)); 617 jParams.setCacheStatus(savedCacheStatus); 618 return buffer.toString(); 619 } 620 621 629 protected String getContainerURL(JahiaPage page, JahiaContainer container) 630 throws JahiaException { 631 StringBuffer buffer = new StringBuffer (); 632 buffer.append(getPageURL(page)); 633 buffer.append("?entryId="); 634 buffer.append(container.getID()); 635 return buffer.toString(); 636 } 637 638 645 protected void setCategories(Vector categories, 646 JahiaContainer postContainer) throws JahiaException { 647 648 if (categories == null) return; 649 650 Set oldCats = Category.getObjectCategories(postContainer. 651 getContentContainer().getObjectKey()); 652 653 if (categories.size() == 0) { 654 clearCategories(postContainer); 655 return; 656 } 657 658 Iterator ite = categories.iterator(); 659 int i = 0; 660 while (ite.hasNext()) { 661 String catKey = (String )ite.next(); 662 663 if (oldCats.contains(catKey)) continue; 665 666 Category cat = Category.getCategory(catKey); 667 668 if (cat != null) { 669 log.debug("Adding category: "+catKey); 670 cat.addChildObjectKey(postContainer. 671 getContentContainer().getObjectKey()); 672 i++; 673 } 674 } 675 676 if ((oldCats.size() + i) > categories.size()) { 678 Iterator ite2 = oldCats.iterator(); 679 680 while (ite2.hasNext()) { 681 Category cat = (Category)ite2.next(); 682 683 if (categories.contains(cat.getKey())) continue; 685 686 log.debug("Removing category: "+cat.getKey()); 687 cat.removeChildObjectKey(postContainer. 688 getContentContainer().getObjectKey()); 689 } 690 } 691 } 692 693 696 private void clearCategories(JahiaContainer postContainer) 697 throws JahiaException { 698 Set oldCats = Category.getObjectCategories(postContainer. 699 getContentContainer().getObjectKey()); 700 701 Iterator ite = oldCats.iterator(); 702 while(ite.hasNext()) { 703 String catKey = (String )ite.next(); 704 Category cat = Category.getCategory(catKey); 705 706 if (cat != null) { 707 cat.removeChildObjectKey(postContainer. 708 getContentContainer().getObjectKey()); 709 } 710 } 711 } 712 713 721 protected void setValue(JahiaField field, String value) 722 throws JahiaException { 723 StringBuffer buffer = new StringBuffer (); 724 buffer.append("<html>"); 725 buffer.append(value); 726 buffer.append("</html>"); 727 field.setValue(buffer.toString()); 728 } 729 730 738 protected String getValue(JahiaField field) 739 throws JahiaException { 740 if (field == null) return "n/a"; 741 String value = field.getValue(); 742 if (value == null || value.length() == 0) return "n/a"; 743 if (value.indexOf("<html>") != -1) { 744 String tag = "<html>"; 745 String endTag = "</html>"; 746 value = value.substring(value.indexOf(tag) + tag.length(), 748 value.indexOf(endTag)); 749 } 750 return value; 751 } 752 } 753 | Popular Tags |