1 31 32 package org.opencms.frontend.photoalbum; 33 34 import org.opencms.file.CmsPropertyDefinition; 35 import org.opencms.file.CmsResource; 36 import org.opencms.file.CmsResourceFilter; 37 import org.opencms.file.types.CmsResourceTypeImage; 38 import org.opencms.i18n.CmsEncoder; 39 import org.opencms.i18n.CmsMessages; 40 import org.opencms.jsp.CmsJspActionElement; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.util.CmsStringUtil; 44 45 import java.io.IOException ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpServletResponse ; 51 import javax.servlet.jsp.PageContext ; 52 53 import org.apache.commons.logging.Log; 54 55 64 public class CmsPhotoAlbumBean extends CmsJspActionElement { 65 66 67 public static final int ACTION_DETAIL = 1; 68 69 70 public static final int ACTION_ORIGINAL = 2; 71 72 73 public static final int ACTION_THUMBNAIL = 0; 74 75 76 public static final String PARAM_ACTION = "action"; 77 78 79 public static final String PARAM_IMAGE = "image"; 80 81 82 public static final String PARAM_PAGE = "thumbpage"; 83 84 85 public static final String VALUE_ACTION_DETAIL = "detail"; 86 87 88 public static final String VALUE_ACTION_ORIGINAL = "original"; 89 90 91 public static final String VALUE_ACTION_THUMBNAIL = "thumbnail"; 92 93 94 private static final Log LOG = CmsLog.getLog(CmsPhotoAlbumBean.class); 95 96 97 private List m_albumPhotos; 98 99 100 private List m_configErrors; 101 102 103 private CmsPhotoAlbumConfiguration m_configuration; 104 105 106 private int m_currentPage; 107 108 109 private int m_displayAction; 110 111 112 private CmsMessages m_messages; 113 114 115 private int m_pageCount; 116 117 118 private int m_photosPerPage; 119 120 121 private CmsPhotoAlbumStyle m_style; 122 123 130 public CmsPhotoAlbumBean(PageContext context, HttpServletRequest req, HttpServletResponse res) { 131 132 this(context, req, res, null); 133 } 134 135 143 public CmsPhotoAlbumBean(PageContext context, HttpServletRequest req, HttpServletResponse res, String configUri) { 144 145 super(context, req, res); 146 init(configUri); 147 } 148 149 155 public String displayAlbum() throws IOException { 156 157 buildHtmlConfigurationErrors(); 159 switch (getDisplayAction()) { 161 case ACTION_DETAIL: 162 return buildHtmlViewDetail(); 163 case ACTION_ORIGINAL: 164 return ""; 165 case ACTION_THUMBNAIL: 166 default: 167 return buildHtmlViewThumbNail(); 168 } 169 } 170 171 176 public List getAlbumPhotos() { 177 178 if (m_albumPhotos == null) { 179 CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(CmsResourceTypeImage.getStaticTypeId()); 180 try { 181 m_albumPhotos = getCmsObject().readResources(getConfiguration().getVfsPathGallery(), filter, false); 182 } catch (CmsException e) { 183 m_albumPhotos = new ArrayList (0); 185 if (LOG.isErrorEnabled()) { 187 LOG.error(Messages.get().getBundle().key( 188 Messages.LOG_ERR_IMAGEFOLDER_NOT_FOUND_1, 189 getConfiguration().getVfsPathGallery())); 190 } 191 addConfigError(m_messages.key( 192 Messages.LOG_ERR_IMAGEFOLDER_NOT_FOUND_1, 193 getConfiguration().getVfsPathGallery())); 194 } 195 } 196 return m_albumPhotos; 197 } 198 199 204 public CmsPhotoAlbumConfiguration getConfiguration() { 205 206 return m_configuration; 207 } 208 209 214 public int getCurrentPage() { 215 216 return m_currentPage; 217 } 218 219 224 public int getDisplayAction() { 225 226 return m_displayAction; 227 } 228 229 234 public int getPageCount() { 235 236 return m_pageCount; 237 } 238 239 244 public int getPhotosPerPage() { 245 246 return m_photosPerPage; 247 } 248 249 254 public CmsPhotoAlbumStyle getStyle() { 255 256 return m_style; 257 } 258 259 264 public void init(String configUri) { 265 266 m_messages = Messages.get().getBundle(getRequestContext().getLocale()); 268 setConfigErrors(new ArrayList ()); 270 setStyle(new CmsPhotoAlbumStyle()); 272 try { 274 setConfiguration(new CmsPhotoAlbumConfiguration(this, configUri)); 275 } catch (Exception e) { 276 setConfiguration(new CmsPhotoAlbumConfiguration()); 278 if (e instanceof CmsException) { 279 addConfigError(((CmsException)e).getLocalizedMessage(getRequestContext().getLocale())); 281 } else { 282 addConfigError(e.getLocalizedMessage()); 283 } 284 } 285 String action = getRequest().getParameter(PARAM_ACTION); 287 if (VALUE_ACTION_DETAIL.equals(action)) { 288 setDisplayAction(ACTION_DETAIL); 290 } else if (VALUE_ACTION_ORIGINAL.equals(action)) { 291 setDisplayAction(ACTION_ORIGINAL); 293 } else { 294 setDisplayAction(ACTION_THUMBNAIL); 296 } 297 calculatePageData(); 299 } 300 301 306 public void setConfiguration(CmsPhotoAlbumConfiguration configuration) { 307 308 m_configuration = configuration; 309 } 310 311 316 public void setStyle(CmsPhotoAlbumStyle style) { 317 318 m_style = style; 319 } 320 321 326 protected void addConfigError(String configError) { 327 328 m_configErrors.add(configError); 329 } 330 331 336 protected String buildHtmlAlbumTitle() { 337 338 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getConfiguration().getAlbumTitle())) { 339 StringBuffer result = new StringBuffer (128); 341 result.append("<h1"); 342 result.append(getStyle().getClassPageTitle()); 343 result.append(">"); 344 result.append(getConfiguration().getAlbumTitle()); 345 result.append("</h1>\n"); 346 return result.toString(); 347 } 348 return ""; 349 } 350 351 358 protected void buildHtmlConfigurationErrors() throws IOException { 359 360 if (!getRequestContext().currentProject().isOnlineProject() && getConfigErrors().size() > 0) { 361 getJspContext().getOut().print("<h1>"); 363 getJspContext().getOut().print(m_messages.key(Messages.GUI_CONFIG_ERRORS_HEADLINE_0)); 364 getJspContext().getOut().print("</h1>"); 365 getJspContext().getOut().print("<p>"); 366 for (int i = 0; i < getConfigErrors().size(); i++) { 368 if (i > 0) { 369 getJspContext().getOut().println("<br>"); 370 } 371 getJspContext().getOut().print(getConfigErrors().get(i)); 372 } 373 getJspContext().getOut().print("</p>"); 374 } 375 } 376 377 385 protected String buildHtmlImageNavigation(String currentNavigationPosition, int photoIndex, CmsResource photo) { 386 387 if (!checkNavigationPosition(currentNavigationPosition)) { 388 return ""; 390 } 391 392 StringBuffer result = new StringBuffer (1024); 393 StringBuffer link = new StringBuffer (256); 394 result.append("<tr>\n\t<td"); 395 result.append(getStyle().getClassNavigation()); 396 result.append(getConfiguration().getStyleAlignAttribute(getConfiguration().getAlignNavigation())); 397 result.append(">"); 398 if (photoIndex > 0) { 399 result.append("<a"); 401 result.append(getStyle().getClassLink()); 402 result.append(" HREF=\""); 403 link.append(getRequestContext().getUri()); 404 link.append("?"); 405 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_DETAIL); 406 link.append("&"); 407 link.append(PARAM_IMAGE).append("=").append(photoIndex - 1); 408 result.append(link(link.toString())); 409 result.append("\">"); 410 result.append(m_messages.key(Messages.GUI_NAVIGATION_BACK_0)); 411 result.append("</a>"); 412 result.append(" - "); 413 } else { 414 result.append(fillNavSpaces(m_messages.key(Messages.GUI_NAVIGATION_BACK_0) + " - ")); 415 } 416 Object [] args = new Object [] {new Integer (photoIndex + 1), new Integer (getAlbumPhotos().size())}; 418 result.append(m_messages.key(Messages.GUI_DETAIL_IMAGEINFO_2, args)); 419 if (photoIndex < (getAlbumPhotos().size() - 1)) { 420 result.append(" - "); 422 result.append("<a"); 423 result.append(getStyle().getClassLink()); 424 result.append(" HREF=\""); 425 link = new StringBuffer (256); 426 link.append(getRequestContext().getUri()); 427 link.append("?"); 428 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_DETAIL); 429 link.append("&"); 430 link.append(PARAM_IMAGE).append("=").append(photoIndex + 1); 431 result.append(link(link.toString())); 432 result.append("\">"); 433 result.append(m_messages.key(Messages.GUI_NAVIGATION_NEXT_0)); 434 result.append("</a>"); 435 } else { 436 result.append(fillNavSpaces(" - " + m_messages.key(Messages.GUI_NAVIGATION_NEXT_0))); 437 } 438 result.append("<br>"); 439 440 int thumbPage = 1; 442 if (getConfiguration().showPageNavigation()) { 443 thumbPage = (photoIndex / getPhotosPerPage()) + 1; 445 } 446 result.append("<a"); 447 result.append(getStyle().getClassLink()); 448 result.append(" HREF=\""); 449 link = new StringBuffer (256); 450 link.append(getRequestContext().getUri()); 451 link.append("?"); 452 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_THUMBNAIL); 453 link.append("&"); 454 link.append(PARAM_PAGE).append("=").append(thumbPage); 455 result.append(link(link.toString())); 456 result.append("\">"); 457 result.append(m_messages.key(Messages.GUI_NAVIGATION_OVERVIEW_0)); 458 result.append("</a>"); 459 460 if (getConfiguration().showDetailOriginalLink()) { 462 result.append(" - <a"); 463 result.append(getStyle().getClassLink()); 464 result.append(" HREF=\""); 465 result.append(link(getCmsObject().getSitePath(photo))); 466 result.append("\" target=\"originalphoto\">"); 467 result.append(m_messages.key(Messages.GUI_NAVIGATION_ORIGINAL_0)); 468 result.append("</a>"); 469 } 470 result.append("</td>\n</tr>\n"); 471 472 return result.toString(); 473 } 474 475 481 protected String buildHtmlPageNavigation(String currentNavigationPosition) { 482 483 if (!checkNavigationPosition(currentNavigationPosition)) { 484 return ""; 486 } 487 488 StringBuffer result = new StringBuffer (1024); 489 490 if (getConfiguration().showPageNavigation() && getPageCount() > 1) { 491 result.append("<tr>\n\t<td colspan=\""); 493 result.append(getConfiguration().getThumbCols()); 494 result.append("\""); 495 result.append(getStyle().getClassNavigation()); 496 result.append(getConfiguration().getStyleAlignAttribute(getConfiguration().getAlignNavigation())); 497 result.append(">"); 498 StringBuffer link = new StringBuffer (256); 499 if (getCurrentPage() > 1) { 500 result.append("<a"); 502 result.append(getStyle().getClassLink()); 503 result.append(" HREF=\""); 504 link.append(getRequestContext().getUri()); 505 link.append("?"); 506 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_THUMBNAIL); 507 link.append("&"); 508 link.append(PARAM_PAGE).append("=").append(getCurrentPage() - 1); 509 result.append(link(link.toString())); 510 result.append("\">"); 511 result.append(m_messages.key(Messages.GUI_NAVIGATION_BACK_0)); 512 result.append("</a>"); 513 result.append(" - "); 514 } else { 515 result.append(fillNavSpaces(m_messages.key(Messages.GUI_NAVIGATION_BACK_0) + " - ")); 516 } 517 result.append(m_messages.key(Messages.GUI_THUMB_PAGEINFO_2, new Integer (getCurrentPage()), new Integer ( 519 getPageCount()))); 520 if (getCurrentPage() < getPageCount()) { 521 result.append(" - "); 523 result.append("<a class=\""); 524 result.append(getStyle().getClassLink()); 525 result.append("\" HREF=\""); 526 link = new StringBuffer (256); 527 link.append(getRequestContext().getUri()); 528 link.append("?"); 529 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_THUMBNAIL); 530 link.append("&"); 531 link.append(PARAM_PAGE).append("=").append(getCurrentPage() + 1); 532 result.append(link(link.toString())); 533 result.append("\">"); 534 result.append(m_messages.key(Messages.GUI_NAVIGATION_NEXT_0)); 535 result.append("</a>"); 536 } else { 537 result.append(fillNavSpaces(" - " + m_messages.key(Messages.GUI_NAVIGATION_NEXT_0))); 538 } 539 result.append("</td>\n</tr>\n"); 540 } 541 return result.toString(); 542 } 543 544 550 protected String buildHtmlThumbTextRow(String text) { 551 552 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(text)) { 553 StringBuffer result = new StringBuffer (2048); 554 result.append("<tr>\n\t<td colspan=\""); 555 result.append(getConfiguration().getThumbCols()); 556 result.append("\""); 557 result.append(getStyle().getClassThumbText()); 558 result.append(">"); 559 result.append(text); 560 result.append("</td>\n</tr>\n"); 561 return result.toString(); 562 } 563 return ""; 564 } 565 566 571 protected String buildHtmlViewDetail() { 572 573 StringBuffer result = new StringBuffer (4096); 574 575 result.append(buildHtmlAlbumTitle()); 577 578 String indexParam = getRequest().getParameter(PARAM_IMAGE); 580 int photoIndex = 0; 581 if (CmsStringUtil.isNotEmpty(indexParam)) { 582 photoIndex = Integer.parseInt(indexParam); 584 if (photoIndex > (getAlbumPhotos().size() - 1)) { 585 photoIndex = getAlbumPhotos().size() - 1; 586 } 587 } 588 589 CmsResource photo = (CmsResource)getAlbumPhotos().get(photoIndex); 591 String resourceName = getCmsObject().getSitePath(photo); 592 593 String title = ""; 595 if (getConfiguration().showResourceNameAsTitle()) { 596 title = CmsResource.getName(resourceName); 597 } 598 title = property(CmsPropertyDefinition.PROPERTY_TITLE, resourceName, title); 599 title = CmsEncoder.escapeXml(title); 600 601 result.append("<table border=\"0\""); 602 result.append(getStyle().getClassThumbTable()); 603 result.append(" width=\""); 604 result.append(getConfiguration().getDetailImageScaler().getWidth()); 605 result.append("\">\n"); 606 607 result.append(buildHtmlImageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_TOP_ABOVE, photoIndex, photo)); 609 610 if (getConfiguration().showDetailTitle() && CmsStringUtil.isNotEmptyOrWhitespaceOnly(title)) { 612 result.append("<tr>\n\t<td"); 613 result.append(getStyle().getClassDetailImageTitle()); 614 result.append(getConfiguration().getStyleAlignAttribute(getConfiguration().getDetailAlignTitle())); 615 result.append(">"); 616 result.append(title); 617 result.append("</td>\n</tr>\n"); 618 619 } 620 621 result.append(buildHtmlImageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_TOP_BELOW, photoIndex, photo)); 623 624 result.append("<tr>\n\t<td>"); 626 result.append("<img SRC=\""); 628 StringBuffer link = new StringBuffer (256); 629 link.append(resourceName); 630 link.append(getConfiguration().getDetailImageScaler().toRequestParam()); 631 result.append(link(link.toString())); 632 result.append("\" border=\"0\" width=\""); 633 result.append(getConfiguration().getDetailImageScaler().getWidth()); 634 result.append("\" height=\""); 635 result.append(getConfiguration().getDetailImageScaler().getHeight()); 636 result.append("\" alt=\""); 637 result.append(title); 638 result.append("\" title=\""); 639 result.append(title); 640 result.append("\">"); 641 result.append("</td>\n</tr>\n"); 642 643 result.append(buildHtmlImageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_BOTTOM_ABOVE, photoIndex, photo)); 645 646 if (getConfiguration().showDetailDescription()) { 648 String description = property(CmsPropertyDefinition.PROPERTY_DESCRIPTION, resourceName, ""); 649 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(description)) { 650 result.append("<tr>\n\t<td"); 651 result.append(getStyle().getClassDetailImageDescription()); 652 result.append(getConfiguration().getStyleAlignAttribute(getConfiguration().getDetailAlignTitle())); 653 result.append(">"); 654 result.append(description); 655 result.append("</td>\n</tr>\n"); 656 } 657 } 658 659 result.append(buildHtmlImageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_BOTTOM_BELOW, photoIndex, photo)); 661 662 result.append("</table>"); 663 return result.toString(); 664 } 665 666 671 protected String buildHtmlViewThumbNail() { 672 673 StringBuffer result = new StringBuffer (4096); 674 675 int startIndex = (getCurrentPage() - 1) * getPhotosPerPage(); 677 int endIndex = 0; 678 int rowCount = getConfiguration().getThumbRows(); 679 if (getConfiguration().showPageNavigation()) { 680 endIndex = (getCurrentPage() * getPhotosPerPage()) - 1; 682 if (endIndex > (getAlbumPhotos().size() - 1)) { 683 endIndex = getAlbumPhotos().size() - 1; 684 } 685 } else { 686 endIndex = getAlbumPhotos().size() - 1; 688 } 689 int photoCount = endIndex - startIndex + 1; 690 rowCount = photoCount / getConfiguration().getThumbCols(); 691 if ((photoCount % getConfiguration().getThumbCols()) > 0) { 692 rowCount += 1; 693 } 694 695 result.append(buildHtmlAlbumTitle()); 697 698 result.append("<table border=\"0\""); 699 result.append(getStyle().getClassThumbTable()); 700 result.append(" width=\""); 701 result.append(getConfiguration().getThumbCols() * getConfiguration().getThumbNailScaler().getWidth()); 702 result.append("\">\n"); 703 704 result.append(buildHtmlPageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_TOP_ABOVE)); 706 707 result.append(buildHtmlThumbTextRow(getConfiguration().getThumbTextTop())); 709 710 result.append(buildHtmlPageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_TOP_BELOW)); 712 713 String styleAttr = getConfiguration().getStyleAlignAttribute(getConfiguration().getThumbAlignTitle()); 714 715 int photoIndex = startIndex; 716 for (int i = 1; i <= rowCount; i++) { 717 result.append("<tr>\n"); 719 for (int k = 1; k <= getConfiguration().getThumbCols(); k++) { 720 result.append("\t<td width=\""); 722 result.append(getConfiguration().getThumbNailScaler().getWidth()); 723 result.append("\""); 724 result.append(getStyle().getClassThumbImageTitle()); 725 result.append(styleAttr); 726 result.append(">"); 727 if (photoIndex <= endIndex) { 728 CmsResource photo = (CmsResource)getAlbumPhotos().get(photoIndex); 730 String resourceName = getCmsObject().getSitePath(photo); 731 String title = ""; 732 if (getConfiguration().showResourceNameAsTitle()) { 733 title = CmsResource.getName(resourceName); 734 } 735 title = property(CmsPropertyDefinition.PROPERTY_TITLE, resourceName, title); 736 title = CmsEncoder.escapeXml(title); 737 result.append("<a HREF=\""); 739 StringBuffer link = new StringBuffer (256); 740 link.append(getRequestContext().getUri()); 741 link.append("?"); 742 link.append(PARAM_ACTION).append("=").append(VALUE_ACTION_DETAIL); 743 link.append("&").append(PARAM_IMAGE).append("=").append(photoIndex); 744 result.append(link(link.toString())); 745 result.append("\">"); 746 result.append("<img SRC=\""); 748 link = new StringBuffer (256); 749 link.append(resourceName); 750 link.append(getConfiguration().getThumbNailScaler().toRequestParam()); 751 result.append(link(link.toString())); 752 result.append("\" border=\"0\" width=\""); 753 result.append(getConfiguration().getThumbNailScaler().getWidth()); 754 result.append("\" height=\""); 755 result.append(getConfiguration().getThumbNailScaler().getHeight()); 756 result.append("\" alt=\""); 757 result.append(title); 758 result.append("\" title=\""); 759 result.append(title); 760 result.append("\">"); 761 result.append("</a>"); 762 if (getConfiguration().showThumbTitle() && CmsStringUtil.isNotEmptyOrWhitespaceOnly(title)) { 763 result.append("<br clear=\"all\"><span"); 765 result.append(getStyle().getClassThumbImageTitle()); 766 result.append(">"); 767 result.append(title); 768 result.append("</span>"); 769 } 770 photoIndex++; 771 } 772 result.append("</td>\n"); 773 } 774 result.append("</tr>\n"); 775 } 776 777 result.append(buildHtmlPageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_BOTTOM_ABOVE)); 779 780 result.append(buildHtmlThumbTextRow(getConfiguration().getThumbTextBottom())); 782 783 result.append(buildHtmlPageNavigation(CmsPhotoAlbumConfiguration.NAVPOS_BOTTOM_BELOW)); 785 786 result.append("</table>"); 787 return result.toString(); 788 } 789 790 800 protected void calculatePageData() { 801 802 if (getConfiguration().showPageNavigation()) { 803 setPhotosPerPage(getConfiguration().getThumbCols() * getConfiguration().getThumbRows()); 805 int pageCount = getAlbumPhotos().size() / getPhotosPerPage(); 806 if ((getAlbumPhotos().size() % getPhotosPerPage()) != 0) { 807 pageCount++; 808 } 809 setPageCount(pageCount); 810 String page = getRequest().getParameter(PARAM_PAGE); 812 if (CmsStringUtil.isNotEmpty(page) && getPageCount() > 1) { 813 int currentPage = Integer.parseInt(page); 814 if (currentPage > getPageCount()) { 815 currentPage = getPageCount(); 816 } 817 setCurrentPage(currentPage); 818 } else { 819 setCurrentPage(1); 820 } 821 } else { 822 setPhotosPerPage(0); 824 setPageCount(1); 825 setCurrentPage(1); 826 } 827 } 828 829 835 protected boolean checkNavigationPosition(String currentPosition) { 836 837 return getConfiguration().getNavigationPosition().indexOf(currentPosition) > -1; 838 } 839 840 846 protected String fillNavSpaces(String replaceValue) { 847 848 int centerIndex = getConfiguration().getAlignNavigation().indexOf("center"); 849 if (centerIndex > -1 && CmsStringUtil.isNotEmpty(replaceValue)) { 850 int length = replaceValue.length(); 851 StringBuffer result = new StringBuffer (6 * length); 852 for (int i = 0; i < length; i++) { 853 result.append(" "); 854 } 855 return result.toString(); 856 } 857 return ""; 858 } 859 860 865 protected List getConfigErrors() { 866 867 return m_configErrors; 868 } 869 870 875 protected void setConfigErrors(List configErrors) { 876 877 m_configErrors = configErrors; 878 } 879 880 885 protected void setCurrentPage(int currentPage) { 886 887 m_currentPage = currentPage; 888 } 889 890 895 protected void setDisplayAction(int displayAction) { 896 897 m_displayAction = displayAction; 898 } 899 900 905 protected void setPageCount(int pageCount) { 906 907 m_pageCount = pageCount; 908 } 909 910 915 protected void setPhotosPerPage(int photosPerPage) { 916 917 m_photosPerPage = photosPerPage; 918 } 919 920 } 921 | Popular Tags |