1 31 32 package org.opencms.workplace.list; 33 34 import org.opencms.i18n.CmsMessageContainer; 35 import org.opencms.jsp.CmsJspActionElement; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.CmsRuntimeException; 38 import org.opencms.util.CmsStringUtil; 39 import org.opencms.workplace.CmsDialog; 40 import org.opencms.workplace.CmsWorkplaceSettings; 41 42 import java.io.IOException ; 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 import java.util.Hashtable ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 import javax.servlet.ServletException ; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.jsp.JspException ; 53 import javax.servlet.jsp.JspWriter ; 54 55 64 public abstract class A_CmsListDialog extends CmsDialog { 65 66 67 public static final int ACTION_LIST_INDEPENDENT_ACTION = 83; 68 69 70 public static final int ACTION_LIST_MULTI_ACTION = 85; 71 72 73 public static final int ACTION_LIST_SEARCH = 81; 74 75 76 public static final int ACTION_LIST_SELECT_PAGE = 82; 77 78 79 public static final int ACTION_LIST_SINGLE_ACTION = 84; 80 81 82 public static final int ACTION_LIST_SORT = 80; 83 84 85 public static final String ICON_ACTIVE = "list/active.png"; 86 87 88 public static final String ICON_ADD = "list/add.png"; 89 90 91 public static final String ICON_DELETE = "list/delete.png"; 92 93 94 public static final String ICON_DETAILS_HIDE = "list/details_hide.png"; 95 96 97 public static final String ICON_DETAILS_SHOW = "list/details_show.png"; 98 99 100 public static final String ICON_DISABLED = "list/disabled.png"; 101 102 103 public static final String ICON_INACTIVE = "list/inactive.png"; 104 105 106 public static final String ICON_MINUS = "list/minus.png"; 107 108 109 public static final String ICON_MULTI_ACTIVATE = "list/multi_activate.png"; 110 111 112 public static final String ICON_MULTI_ADD = "list/multi_add.png"; 113 114 115 public static final String ICON_MULTI_DEACTIVATE = "list/multi_deactivate.png"; 116 117 118 public static final String ICON_MULTI_DELETE = "list/multi_delete.png"; 119 120 121 public static final String ICON_MULTI_MINUS = "list/multi_minus.png"; 122 123 124 public static final String LIST_INDEPENDENT_ACTION = "listindependentaction"; 125 126 127 public static final String LIST_MULTI_ACTION = "listmultiaction"; 128 129 130 public static final String LIST_SEARCH = "listsearch"; 131 132 133 public static final String LIST_SELECT_PAGE = "listselectpage"; 134 135 136 public static final String LIST_SINGLE_ACTION = "listsingleaction"; 137 138 139 public static final String LIST_SORT = "listsort"; 140 141 142 public static final String PARAM_FORMNAME = "formname"; 143 144 145 public static final String PARAM_LIST_ACTION = "listaction"; 146 147 148 public static final String PARAM_PAGE = "page"; 149 150 151 public static final String PARAM_SEARCH_FILTER = "searchfilter"; 152 153 154 public static final String PARAM_SEL_ITEMS = "selitems"; 155 156 157 public static final String PARAM_SORT_COL = "sortcol"; 158 159 160 private static Map m_metadatas = new HashMap (); 161 162 163 private boolean m_active; 164 165 166 private CmsHtmlList m_list; 167 168 169 private String m_listId; 170 171 172 private String m_paramFormName; 173 174 175 private String m_paramListAction; 176 177 178 private String m_paramPage; 179 180 181 private String m_paramSearchFilter; 182 183 184 private String m_paramSelItems; 185 186 187 private String m_paramSortCol; 188 189 190 private String m_searchColId; 191 192 201 protected A_CmsListDialog( 202 CmsJspActionElement jsp, 203 String listId, 204 CmsMessageContainer listName, 205 String sortedColId, 206 CmsListOrderEnum sortOrder, 207 String searchableColId) { 208 209 super(jsp); 210 m_listId = listId; 212 m_active = (getListId() + "-form").equals(getParamFormName()); 214 setParamFormName(getListId() + "-form"); 215 if (isForwarded()) { 217 return; 218 } 219 m_searchColId = searchableColId; 220 listRecovery(listId); 222 if (getList() == null) { 224 setList(new CmsHtmlList(listId, listName, getMetadata(this.getClass().getName(), listId))); 226 getList().setMaxItemsPerPage(getSettings().getUserSettings().getExplorerFileEntries()); 228 if (sortedColId != null && getList().getMetadata().getColumnDefinition(sortedColId) != null) { 230 getList().setSortedColumn(sortedColId, getLocale()); 231 if (sortOrder != null && sortOrder == CmsListOrderEnum.ORDER_DESCENDING) { 232 getList().setSortedColumn(sortedColId, getLocale()); 233 } 234 } 235 listSave(); 237 } 238 } 239 240 249 public static CmsHtmlList getListObject(Class listDialog, CmsWorkplaceSettings settings) { 250 251 return (CmsHtmlList)getListObjectMap(settings).get(listDialog.getName()); 252 } 253 254 261 private static Map getListObjectMap(CmsWorkplaceSettings settings) { 262 263 Map objects = (Map )settings.getListObject(); 264 if (objects == null) { 265 objects = new Hashtable (); 267 settings.setListObject(objects); 268 } 269 return objects; 270 } 271 272 279 public void actionDialog() throws JspException , ServletException , IOException { 280 281 if (getAction() == ACTION_CANCEL) { 282 actionCloseDialog(); 284 return; 285 } 286 if (isForwarded()) { 287 return; 288 } 289 refreshList(); 291 switch (getAction()) { 292 case ACTION_LIST_SEARCH: 294 case ACTION_LIST_SORT: 295 case ACTION_LIST_SELECT_PAGE: 296 executeDefaultActions(); 297 break; 298 299 case ACTION_LIST_SINGLE_ACTION: 301 if (getSelectedItem() != null) { 302 executeListSingleActions(); 303 } 304 break; 305 306 case ACTION_LIST_MULTI_ACTION: 308 executeListMultiActions(); 309 break; 310 311 case ACTION_LIST_INDEPENDENT_ACTION: 313 executeListIndepActions(); 314 break; 315 316 case ACTION_DEFAULT: 317 default: 318 setParamAction(DIALOG_INITIAL); 320 } 321 } 322 323 330 public void displayDialog() throws JspException , IOException , ServletException { 331 332 displayDialog(false); 333 } 334 335 345 public void displayDialog(boolean writeLater) throws JspException , IOException , ServletException { 346 347 actionDialog(); 348 if (writeLater) { 349 return; 350 } 351 writeDialog(); 352 } 353 354 357 public void executeDefaultActions() { 358 359 switch (getAction()) { 360 361 case ACTION_LIST_SEARCH: 362 executeSearch(); 363 break; 364 case ACTION_LIST_SORT: 365 executeSort(); 366 break; 367 case ACTION_LIST_SELECT_PAGE: 368 executeSelectPage(); 369 break; 370 default: 371 } 373 listSave(); 374 } 375 376 384 public void executeListIndepActions() { 385 386 if (getList().getMetadata().getItemDetailDefinition(getParamListAction()) != null) { 387 getList().getMetadata().toogleDetailState(getParamListAction()); 389 initializeDetail(getParamListAction()); 391 } 392 listSave(); 393 } 394 395 404 public abstract void executeListMultiActions() throws IOException , ServletException , CmsRuntimeException; 405 406 415 public abstract void executeListSingleActions() throws IOException , ServletException , CmsRuntimeException; 416 417 422 public CmsHtmlList getList() { 423 424 if (m_list != null && m_list.getMetadata() == null) { 425 m_list.setMetadata(getMetadata(getClass().getName(), m_list.getId())); 426 } 427 return m_list; 428 } 429 430 435 public final String getListId() { 436 437 return m_listId; 438 } 439 440 445 public String getParamFormName() { 446 447 return m_paramFormName; 448 } 449 450 455 public String getParamListAction() { 456 457 return m_paramListAction; 458 } 459 460 465 public String getParamPage() { 466 467 return m_paramPage; 468 } 469 470 475 public String getParamSearchFilter() { 476 477 return m_paramSearchFilter; 478 } 479 480 485 public String getParamSelItems() { 486 487 return m_paramSelItems; 488 } 489 490 495 public String getParamSortCol() { 496 497 return m_paramSortCol; 498 } 499 500 505 public CmsListItem getSelectedItem() { 506 507 try { 508 return getList().getItem( 509 CmsStringUtil.splitAsArray(getParamSelItems(), CmsHtmlList.ITEM_SEPARATOR)[0].trim()); 510 } catch (Exception e) { 511 return null; 512 } 513 } 514 515 520 public List getSelectedItems() { 521 522 Iterator it = CmsStringUtil.splitAsList(getParamSelItems(), CmsHtmlList.ITEM_SEPARATOR, true).iterator(); 523 List items = new ArrayList (); 524 while (it.hasNext()) { 525 String id = (String )it.next(); 526 items.add(getList().getItem(id)); 527 } 528 return items; 529 } 530 531 541 public boolean isActive() { 542 543 return m_active; 544 } 545 546 550 public synchronized void refreshList() { 551 552 if (getList() == null) { 553 return; 554 } 555 CmsListState ls = getList().getState(); 556 getList().clear(getLocale()); 557 fillList(); 558 getList().setState(ls, getLocale()); 559 listSave(); 560 } 561 562 567 public void removeList() { 568 569 setList(null); 570 listSave(); 571 } 572 573 578 public void setList(CmsHtmlList list) { 579 580 m_list = list; 581 } 582 583 589 public void setListObject(Class listDialog, CmsHtmlList listObject) { 590 591 if (listObject == null) { 592 getListObjectMap(getSettings()).remove(listDialog.getName()); 594 } else { 595 if (listObject.getMetadata() != null && listObject.getMetadata().isVolatile()) { 596 listObject.setMetadata(null); 597 } 598 getListObjectMap(getSettings()).put(listDialog.getName(), listObject); 599 } 600 } 601 602 607 public void setParamFormName(String formName) { 608 609 m_paramFormName = formName; 610 } 611 612 617 public void setParamListAction(String listAction) { 618 619 m_paramListAction = listAction; 620 } 621 622 627 public void setParamPage(String page) { 628 629 m_paramPage = page; 630 } 631 632 637 public void setParamSearchFilter(String searchFilter) { 638 639 m_paramSearchFilter = searchFilter; 640 } 641 642 647 public void setParamSelItems(String paramSelItems) { 648 649 m_paramSelItems = paramSelItems; 650 } 651 652 657 public void setParamSortCol(String sortCol) { 658 659 m_paramSortCol = sortCol; 660 } 661 662 667 public void writeDialog() throws IOException { 668 669 if (isForwarded()) { 670 return; 671 } 672 JspWriter out = getJsp().getJspContext().getOut(); 673 out.print(defaultActionHtml()); 674 } 675 676 681 protected String customHtmlEnd() { 682 683 return dialogContentEnd(); 684 } 685 686 691 protected String customHtmlStart() { 692 693 return ""; 694 } 695 696 701 protected String defaultActionHtml() { 702 703 if (getList() != null && getList().getAllContent().isEmpty()) { 704 refreshList(); 705 } 706 StringBuffer result = new StringBuffer (2048); 707 result.append(defaultActionHtmlStart()); 708 result.append(customHtmlStart()); 709 result.append(defaultActionHtmlContent()); 710 result.append(customHtmlEnd()); 711 result.append(defaultActionHtmlEnd()); 712 return result.toString(); 713 } 714 715 720 protected String defaultActionHtmlContent() { 721 722 StringBuffer result = new StringBuffer (2048); 723 result.append("<form name='"); 724 result.append(getList().getId()); 725 result.append("-form' action='"); 726 result.append(getDialogRealUri()); 727 result.append("' method='post' class='nomargin'"); 728 if (getList().getMetadata().isSearchable()) { 729 result.append(" onsubmit=\"listSearchAction('"); 730 result.append(getList().getId()); 731 result.append("', '"); 732 result.append(getList().getMetadata().getSearchAction().getId()); 733 result.append("', '"); 734 result.append(getList().getMetadata().getSearchAction().getConfirmationMessage().key(getLocale())); 735 result.append("');\""); 736 } 737 result.append(">\n"); 738 result.append(allParamsAsHidden()); 739 result.append("\n"); 740 result.append(getList().listHtml(this)); 741 result.append("\n</form>\n"); 742 return result.toString(); 743 } 744 745 750 protected String defaultActionHtmlEnd() { 751 752 StringBuffer result = new StringBuffer (2048); 753 result.append(dialogEnd()); 754 result.append(bodyEnd()); 755 result.append(htmlEnd()); 756 return result.toString(); 757 } 758 759 764 protected String defaultActionHtmlStart() { 765 766 StringBuffer result = new StringBuffer (2048); 767 result.append(htmlStart(null)); 768 result.append(getList().listJs(getLocale())); 769 result.append(bodyStart("dialog", null)); 770 result.append(dialogStart()); 771 result.append(dialogContentStart(getParamTitle())); 772 return result.toString(); 773 } 774 775 779 protected void executeSearch() { 780 781 getList().setSearchFilter(getParamSearchFilter(), getLocale()); 782 } 783 784 788 protected void executeSelectPage() { 789 790 int page = Integer.valueOf(getParamPage()).intValue(); 791 getList().setCurrentPage(page); 792 } 793 794 798 protected void executeSort() { 799 800 getList().setSortedColumn(getParamSortCol(), getLocale()); 801 } 802 803 812 protected abstract void fillDetails(String detailId); 813 814 817 protected void fillList() { 818 819 try { 820 getList().addAllItems(getListItems()); 821 Iterator itDetails = getList().getMetadata().getItemDetailDefinitions().iterator(); 823 while (itDetails.hasNext()) { 824 initializeDetail(((CmsListItemDetails)itDetails.next()).getId()); 825 } 826 } catch (Exception e) { 827 throw new CmsRuntimeException(Messages.get().container( 828 Messages.ERR_LIST_FILL_1, 829 getList().getName().key(getLocale()), 830 null), e); 831 } 832 } 833 834 841 protected abstract List getListItems() throws CmsException; 842 843 852 protected synchronized CmsListMetadata getMetadata(String listDialogName, String listId) { 853 854 if (m_metadatas.get(listDialogName) == null || ((CmsListMetadata)m_metadatas.get(listDialogName)).isVolatile()) { 855 CmsListMetadata metadata = new CmsListMetadata(listId); 856 857 setColumns(metadata); 858 setSearchAction(metadata, m_searchColId); 860 setIndependentActions(metadata); 861 metadata.addIndependentAction(new CmsListPrintIAction()); 862 setMultiActions(metadata); 863 metadata.checkIds(); 864 m_metadatas.put(listDialogName, metadata); 865 } 866 return (CmsListMetadata)m_metadatas.get(listDialogName); 867 } 868 869 872 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 873 874 super.initWorkplaceRequestValues(settings, request); 875 if (LIST_SEARCH.equals(getParamAction())) { 877 setAction(ACTION_LIST_SEARCH); 878 } else if (LIST_SORT.equals(getParamAction())) { 879 setAction(ACTION_LIST_SORT); 880 } else if (LIST_SELECT_PAGE.equals(getParamAction())) { 881 setAction(ACTION_LIST_SELECT_PAGE); 882 } else if (LIST_INDEPENDENT_ACTION.equals(getParamAction())) { 883 setAction(ACTION_LIST_INDEPENDENT_ACTION); 884 } else if (LIST_SINGLE_ACTION.equals(getParamAction())) { 885 setAction(ACTION_LIST_SINGLE_ACTION); 886 } else if (LIST_MULTI_ACTION.equals(getParamAction())) { 887 setAction(ACTION_LIST_MULTI_ACTION); 888 } 889 setParamStyle("new"); 890 try { 892 validateParamaters(); 893 } catch (Exception e) { 894 setAction(ACTION_CANCEL); 896 try { 897 actionCloseDialog(); 898 } catch (JspException e1) { 899 } 901 return; 902 } 903 } 904 905 912 protected synchronized void listRecovery(String listId) { 913 914 CmsHtmlList list = getListObject(this.getClass(), getSettings()); 915 if (list != null && !list.getId().equals(listId)) { 916 list = null; 917 } 918 setList(list); 919 } 920 921 924 protected synchronized void listSave() { 925 926 setListObject(this.getClass(), getList()); 927 } 928 929 936 protected abstract void setColumns(CmsListMetadata metadata); 937 938 945 protected abstract void setIndependentActions(CmsListMetadata metadata); 946 947 954 protected abstract void setMultiActions(CmsListMetadata metadata); 955 956 964 protected void setSearchAction(CmsListMetadata metadata, String columnId) { 965 966 CmsListColumnDefinition col = metadata.getColumnDefinition(columnId); 967 if (columnId != null && col != null) { 968 if (metadata.getSearchAction() == null) { 969 CmsListSearchAction searchAction = new CmsListSearchAction(col); 971 searchAction.useDefaultShowAllAction(); 972 metadata.setSearchAction(searchAction); 973 } 974 } 975 } 976 977 987 protected void throwListUnsupportedActionException() throws CmsRuntimeException { 988 989 throw new CmsRuntimeException(Messages.get().container( 990 Messages.ERR_LIST_UNSUPPORTED_ACTION_2, 991 getList().getName().key(getLocale()), 992 getParamListAction())); 993 } 994 995 1000 protected void validateParamaters() throws Exception { 1001 1002 } 1004 1005 1010 private void initializeDetail(String detailId) { 1011 1012 if (getList().getMetadata().getItemDetailDefinition(detailId).isVisible()) { 1014 if (!getList().getAllContent().isEmpty()) { 1016 if (((CmsListItem)getList().getAllContent().get(0)).get(detailId) == null) { 1018 fillDetails(detailId); 1019 } 1020 } 1021 } 1022 } 1023} | Popular Tags |