1 31 32 package org.opencms.workplace.editors; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.jsp.CmsJspActionElement; 41 import org.opencms.lock.CmsLock; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.CmsLog; 44 import org.opencms.main.OpenCms; 45 import org.opencms.security.CmsPermissionSet; 46 import org.opencms.security.I_CmsPrincipal; 47 import org.opencms.util.CmsStringUtil; 48 import org.opencms.workplace.CmsDialog; 49 import org.opencms.workplace.CmsFrameset; 50 import org.opencms.workplace.CmsWorkplace; 51 import org.opencms.xml.content.CmsXmlContent; 52 import org.opencms.xml.content.CmsXmlContentFactory; 53 54 import java.io.IOException ; 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.List ; 58 import java.util.Locale ; 59 60 import javax.servlet.ServletException ; 61 import javax.servlet.jsp.JspException ; 62 63 import org.apache.commons.logging.Log; 64 65 76 public abstract class CmsEditor extends CmsDialog { 77 78 79 public static final int ACTION_CHANGE_BODY = 124; 80 81 82 public static final int ACTION_DELETELOCALE = 140; 83 84 85 public static final int ACTION_EXIT = 122; 86 87 88 public static final int ACTION_PREVIEW = 126; 89 90 91 public static final int ACTION_SAVE = 121; 92 93 94 public static final int ACTION_SAVEACTION = 130; 95 96 97 public static final int ACTION_SAVEEXIT = 123; 98 99 100 public static final int ACTION_SHOW = 125; 101 102 103 public static final int ACTION_SHOW_ERRORMESSAGE = 127; 104 105 106 public static final String EDITOR_CHANGE_ELEMENT = "changeelement"; 107 108 109 public static final String EDITOR_CLEANUP = "cleanup"; 110 111 112 public static final String EDITOR_CLOSEBROWSER = "closebrowser"; 113 114 115 public static final String EDITOR_DELETELOCALE = "deletelocale"; 116 117 118 public static final String EDITOR_EXIT = "exit"; 119 120 121 public static final String EDITOR_PREVIEW = "preview"; 122 123 124 public static final String EDITOR_SAVE = "save"; 125 126 127 public static final String EDITOR_SAVEACTION = "saveaction"; 128 129 130 public static final String EDITOR_SAVEEXIT = "saveexit"; 131 132 133 public static final String EDITOR_SHOW = "show"; 134 135 136 public static final String EDITOR_SHOW_ERRORMESSAGE = "error"; 137 138 139 public static final String EMPTY_LOCALE = " [-]"; 140 141 142 public static final String PATH_EDITORS = PATH_WORKPLACE + "editors/"; 143 144 145 public static final String PARAM_BACKLINK = "backlink"; 146 147 148 public static final String PARAM_CONTENT = "content"; 149 150 151 public static final String PARAM_DIRECTEDIT = "directedit"; 152 153 154 public static final String PARAM_EDITASTEXT = "editastext"; 155 156 157 public static final String PARAM_EDITORMODE = "editormode"; 158 159 160 public static final String PARAM_ELEMENTLANGUAGE = "elementlanguage"; 161 162 163 public static final String PARAM_LOADDEFAULT = "loaddefault"; 164 165 166 public static final String PARAM_MODIFIED = "modified"; 167 168 169 public static final String PARAM_OLDELEMENTLANGUAGE = "oldelementlanguage"; 170 171 172 public static final String PARAM_TEMPFILE = "tempfile"; 173 174 175 private static final Log LOG = CmsLog.getLog(CmsEditor.class); 176 177 178 private String m_fileEncoding; 179 180 private String m_paramBackLink; 182 private String m_paramContent; 183 private String m_paramDirectedit; 184 private String m_paramEditAsText; 185 private String m_paramEditormode; 186 private String m_paramElementlanguage; 187 private String m_paramLoadDefault; 188 private String m_paramModified; 189 private String m_paramOldelementlanguage; 190 private String m_paramTempFile; 191 192 193 private String m_picsUri; 194 195 200 public CmsEditor(CmsJspActionElement jsp) { 201 202 super(jsp); 203 } 204 205 210 public abstract void actionClear(boolean forceUnlock); 211 212 220 public abstract void actionExit() throws CmsException, IOException , ServletException , JspException ; 221 222 228 public abstract void actionSave() throws IOException , JspException ; 229 230 238 public String buildSelectElementLanguage(String attributes, String resource, Locale selectedLocale) { 239 240 List locales = OpenCms.getLocaleManager().getAvailableLocales(getCms(), resource); 242 List options = new ArrayList (locales.size()); 243 List selectList = new ArrayList (locales.size()); 244 int currentIndex = -1; 245 246 String filename = resource; 247 248 List contentLocales = new ArrayList (); 250 try { 251 252 CmsResource res = getCms().readResource(filename); 253 254 String temporaryFilename = CmsResource.getFolderPath(resource) 255 + CmsWorkplace.TEMP_FILE_PREFIX 256 + res.getName(); 257 if (getCms().existsResource(temporaryFilename)) { 258 res = getCms().readResource(temporaryFilename); 259 } 260 CmsFile file = CmsFile.upgrade(res, getCms()); 261 CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file); 262 contentLocales = xmlContent.getLocales(); 263 } catch (CmsException e) { 264 if (LOG.isErrorEnabled()) { 266 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, filename), e); 267 } 268 } 269 270 for (int counter = 0; counter < locales.size(); counter++) { 271 Locale curLocale = (Locale )locales.get(counter); 273 selectList.add(curLocale.toString()); 274 StringBuffer buf = new StringBuffer (); 275 buf.append(curLocale.getDisplayName(getLocale())); 276 if (!contentLocales.contains(curLocale)) { 277 buf.append(EMPTY_LOCALE); 278 } 279 options.add(buf.toString()); 280 if (curLocale.equals(selectedLocale)) { 281 currentIndex = counter; 283 } 284 } 285 286 if (currentIndex == -1) { 287 if (selectList.size() > 0) { 289 currentIndex = 0; 290 setParamElementlanguage((String )selectList.get(0)); 291 } 292 } 293 294 return buildSelect(attributes, options, selectList, currentIndex, false); 295 } 296 297 309 public String button(String href, String target, String image, String label, int type, boolean useCustomImage) { 310 311 if (useCustomImage) { 312 return button(href, target, image, label, type, getPicsUri()); 314 } else { 315 return button(href, target, image, label, type); 317 } 318 } 319 320 329 public String buttonActionCancel() { 330 331 String target = null; 332 if (Boolean.valueOf(getParamDirectedit()).booleanValue()) { 333 if (CmsStringUtil.isNotEmpty(getParamBacklink())) { 335 target = getParamBacklink(); 337 } else { 338 target = getParamResource(); 340 } 341 } else { 342 target = OpenCms.getLinkManager().substituteLink(getCms(), CmsFrameset.JSP_WORKPLACE_URI); 344 } 345 return "onclick=\"top.location.href='" + getJsp().link(target) + "';\""; 346 } 347 348 355 public String buttonActionDirectEdit(String jsFunction, int type) { 356 357 I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler(); 359 String url; 360 String name; 361 boolean active = false; 362 if (actionClass != null) { 363 url = actionClass.getButtonUrl(getJsp(), getParamResource()); 365 name = actionClass.getButtonName(); 366 active = actionClass.isButtonActive(getJsp(), getParamResource()); 367 } else { 368 url = getSkinUri() + "buttons/publish_in.png"; 370 name = Messages.GUI_EXPLORER_CONTEXT_PUBLISH_0; 371 } 372 String image = url.substring(url.lastIndexOf("/") + 1); 373 if (url.endsWith(".gif")) { 374 image = image.substring(0, image.length() - 4); 375 } 376 377 if (active) { 378 return button("javascript:" + jsFunction, null, image, name, type, url.substring( 380 0, 381 url.lastIndexOf("/") + 1)); 382 } else { 383 return button(null, null, image, name, type, url.substring(0, url.lastIndexOf("/") + 1)); 385 } 386 } 387 388 391 public void checkLock(String resource, int mode) throws CmsException { 392 393 CmsResource res = getCms().readResource(resource, CmsResourceFilter.ALL); 394 if (!getCms().getLock(res).isNullLock()) { 395 setParamModified(Boolean.TRUE.toString()); 396 } 397 super.checkLock(resource, mode); 398 } 399 400 411 public String deleteLocaleButton(String href, String target, String image, String label, int type) { 412 413 String filename = getParamResource(); 414 415 try { 416 CmsResource res = getCms().readResource(filename); 417 418 String temporaryFilename = CmsResource.getFolderPath(filename) 419 + CmsWorkplace.TEMP_FILE_PREFIX 420 + res.getName(); 421 if (getCms().existsResource(temporaryFilename)) { 422 res = getCms().readResource(temporaryFilename); 423 } 424 CmsFile file = CmsFile.upgrade(res, getCms()); 425 CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file); 426 int locales = xmlContent.getLocales().size(); 427 if (locales < 2) { 429 href = null; 430 target = null; 431 image += "_in"; 432 } 433 } catch (CmsException e) { 434 if (LOG.isErrorEnabled()) { 436 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, filename), e); 437 } 438 } 439 return button(href, target, image, label, type, getSkinUri() + "buttons/"); 440 441 } 442 443 450 public CmsEditorDisplayOptions getEditorDisplayOptions() { 451 452 return OpenCms.getWorkplaceManager().getEditorDisplayOptions(); 453 } 454 455 460 public abstract String getEditorResourceUri(); 461 462 469 public String getOpenCmsContext() { 470 471 return OpenCms.getSystemInfo().getOpenCmsContext(); 472 } 473 474 479 public String getParamBacklink() { 480 481 if (m_paramBackLink == null) { 482 m_paramBackLink = ""; 483 } 484 return m_paramBackLink; 485 } 486 487 491 public String getParamContent() { 492 493 if (m_paramContent == null) { 494 m_paramContent = ""; 495 } 496 return m_paramContent; 497 } 498 499 504 public String getParamDirectedit() { 505 506 return m_paramDirectedit; 507 } 508 509 514 public String getParamEditastext() { 515 516 return m_paramEditAsText; 517 } 518 519 524 public String getParamEditormode() { 525 526 return m_paramEditormode; 527 } 528 529 534 public String getParamElementlanguage() { 535 536 return m_paramElementlanguage; 537 } 538 539 544 public String getParamLoaddefault() { 545 546 return m_paramLoadDefault; 547 } 548 549 554 public String getParamModified() { 555 556 return m_paramModified; 557 } 558 559 564 public String getParamOldelementlanguage() { 565 566 return m_paramOldelementlanguage; 567 } 568 569 574 public String getParamTempfile() { 575 576 return m_paramTempFile; 577 } 578 579 584 public String getPicsUri() { 585 586 if (m_picsUri == null) { 587 m_picsUri = getEditorResourceUri() + "pics/"; 588 } 589 return m_picsUri; 590 } 591 592 597 public void setParamBacklink(String backLink) { 598 599 m_paramBackLink = backLink; 600 } 601 602 607 public void setParamContent(String content) { 608 609 if (content == null) { 610 content = ""; 611 } 612 m_paramContent = content; 613 } 614 615 620 public void setParamDirectedit(String direct) { 621 622 m_paramDirectedit = direct; 623 } 624 625 630 public void setParamEditastext(String editAsText) { 631 632 m_paramEditAsText = editAsText; 633 } 634 635 640 public void setParamEditormode(String mode) { 641 642 m_paramEditormode = mode; 643 } 644 645 650 public void setParamElementlanguage(String elementLanguage) { 651 652 m_paramElementlanguage = elementLanguage; 653 } 654 655 660 public void setParamLoaddefault(String loadDefault) { 661 662 m_paramLoadDefault = loadDefault; 663 } 664 665 670 public void setParamModified(String modified) { 671 672 m_paramModified = modified; 673 } 674 675 680 public void setParamOldelementlanguage(String oldElementLanguage) { 681 682 m_paramOldelementlanguage = oldElementLanguage; 683 } 684 685 690 public void setParamTempfile(String fileName) { 691 692 m_paramTempFile = fileName; 693 } 694 695 702 protected void actionClose() throws IOException , JspException , ServletException { 703 704 if (Boolean.valueOf(getParamDirectedit()).booleanValue()) { 705 if (CmsStringUtil.isNotEmpty(getParamBacklink())) { 707 setParamCloseLink(getJsp().link(getParamBacklink())); 709 } else { 710 setParamCloseLink(getJsp().link(getParamResource())); 712 } 713 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this); 715 getJsp().include(FILE_DIALOG_CLOSE); 717 } else { 718 sendForward(CmsFrameset.JSP_WORKPLACE_URI, new HashMap ()); 720 } 721 } 722 723 728 protected void commitTempFile() throws CmsException { 729 730 switchToTempProject(); 731 CmsFile tempFile; 732 List properties; 733 try { 734 tempFile = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL); 735 properties = getCms().readPropertyObjects(getParamTempfile(), false); 736 } finally { 737 switchToCurrentProject(); 739 } 740 if (getCms().existsResource(getParamResource(), CmsResourceFilter.ALL)) { 741 getCms().writePropertyObjects(getParamResource(), properties); 743 CmsFile orgFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL); 745 orgFile.setContents(tempFile.getContents()); 746 getCms().writeFile(orgFile); 747 } else { 748 if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) { 750 getCms().rmacc( 751 getParamTempfile(), 752 I_CmsPrincipal.PRINCIPAL_GROUP, 753 OpenCms.getDefaultUsers().getGroupUsers()); 754 getCms().rmacc( 755 getParamTempfile(), 756 I_CmsPrincipal.PRINCIPAL_GROUP, 757 OpenCms.getDefaultUsers().getGroupProjectmanagers()); 758 } 759 getCms().copyResource(getParamTempfile(), getParamResource(), CmsResource.COPY_AS_NEW); 760 } 761 int flags = getCms().readResource(getParamResource(), CmsResourceFilter.ALL).getFlags(); 763 if ((flags & CmsResource.FLAG_TEMPFILE) == CmsResource.FLAG_TEMPFILE) { 764 flags ^= CmsResource.FLAG_TEMPFILE; 765 getCms().chflags(getParamResource(), flags); 766 } 767 } 768 769 775 protected String createTempFile() throws CmsException { 776 777 CmsResource file = getCms().readResource(getParamResource(), CmsResourceFilter.ALL); 779 780 String temporaryFilename = CmsResource.getFolderPath(getCms().getSitePath(file)) 782 + CmsWorkplace.TEMP_FILE_PREFIX 783 + file.getName(); 784 785 if (getCms().existsResource(temporaryFilename, CmsResourceFilter.ALL)) { 787 if (!getCms().getLock(temporaryFilename).equals(CmsLock.getNullLock())) { 789 getCms().changeLock(temporaryFilename); 791 } else { 792 getCms().lockResource(temporaryFilename); 794 } 795 getCms().deleteResource(temporaryFilename, CmsResource.DELETE_PRESERVE_SIBLINGS); 796 } 797 798 switchToTempProject(); 800 801 try { 803 getCms().copyResource(getCms().getSitePath(file), temporaryFilename, CmsResource.COPY_AS_NEW); 804 getCms().setDateLastModified(temporaryFilename, System.currentTimeMillis(), false); 805 CmsResource tempFile = getCms().readResource(temporaryFilename, CmsResourceFilter.ALL); 807 int flags = tempFile.getFlags(); 808 if ((flags & CmsResource.FLAG_TEMPFILE) == 0) { 809 flags += CmsResource.FLAG_TEMPFILE; 810 } 811 getCms().chflags(temporaryFilename, flags); 812 getCms().setDateReleased(temporaryFilename, CmsResource.DATE_RELEASED_DEFAULT, false); 814 getCms().setDateExpired(temporaryFilename, CmsResource.DATE_EXPIRED_DEFAULT, false); 815 if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) { 817 getCms().chacc( 818 temporaryFilename, 819 I_CmsPrincipal.PRINCIPAL_GROUP, 820 OpenCms.getDefaultUsers().getGroupUsers(), 821 "-v"); 822 getCms().chacc( 823 temporaryFilename, 824 I_CmsPrincipal.PRINCIPAL_GROUP, 825 OpenCms.getDefaultUsers().getGroupProjectmanagers(), 826 "-v"); 827 } 828 } catch (CmsException e) { 829 switchToCurrentProject(); 830 throw e; 831 } 832 833 switchToCurrentProject(); 835 836 return temporaryFilename; 837 } 838 839 848 protected String decodeContent(String content) { 849 850 return CmsEncoder.unescape(content, CmsEncoder.ENCODING_UTF_8); 851 } 852 853 864 protected String decodeParamValue(String paramName, String paramValue) { 865 866 if ((paramName != null) && (paramValue != null)) { 867 if (PARAM_CONTENT.equals(paramName)) { 868 return CmsEncoder.decode(paramValue, CmsEncoder.ENCODING_UTF_8); 870 } else if (PARAM_RESOURCE.equals(paramName) || PARAM_TEMPFILE.equals(paramName)) { 871 String filename = CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding()); 872 if (PARAM_TEMPFILE.equals(paramName) || CmsStringUtil.isEmpty(getParamTempfile())) { 873 setFileEncoding(getFileEncoding(getCms(), filename)); 875 } 876 return filename; 877 } else { 878 return CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding()); 879 } 880 } else { 881 return null; 882 } 883 } 884 885 888 protected void deleteTempFile() { 889 890 try { 891 switchToTempProject(); 893 getCms().deleteResource(getParamTempfile(), CmsResource.DELETE_PRESERVE_SIBLINGS); 895 switchToCurrentProject(); 897 } catch (CmsException e) { 898 if (LOG.isInfoEnabled()) { 900 LOG.info(e); 901 } 902 } 903 } 904 905 914 protected String encodeContent(String content) { 915 916 return CmsEncoder.escapeWBlanks(content, CmsEncoder.ENCODING_UTF_8); 917 } 918 919 924 protected String getFileEncoding() { 925 926 return m_fileEncoding; 927 } 928 929 937 protected String getFileEncoding(CmsObject cms, String filename) { 938 939 try { 940 return cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, false).getValue( 941 OpenCms.getSystemInfo().getDefaultEncoding()); 942 } catch (CmsException e) { 943 return OpenCms.getSystemInfo().getDefaultEncoding(); 944 } 945 } 946 947 950 protected abstract void initContent(); 951 952 957 protected void setFileEncoding(String value) { 958 959 m_fileEncoding = CmsEncoder.lookupEncoding(value, value); 960 } 961 962 968 protected void showErrorPage(Exception exception) throws JspException { 969 970 setParamAction(""); 972 showErrorPage(this, exception); 973 setAction(ACTION_CANCEL); 975 return; 976 } 977 978 985 protected void showErrorPage(Object editor, Exception exception) throws JspException { 986 987 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, editor); 989 990 setAction(ACTION_SHOW_ERRORMESSAGE); 992 setParamTitle(key("title.edit") + ": " + CmsResource.getName(getParamResource())); 993 if (exception != null) { 994 getJsp().getRequest().setAttribute(ATTRIBUTE_THROWABLE, exception); 995 if (CmsLog.getLog(editor).isWarnEnabled()) { 996 CmsLog.getLog(editor).warn(exception.getLocalizedMessage(), exception); 997 } 998 } 999 getJsp().include(FILE_DIALOG_SCREEN_ERRORPAGE); 1001 } 1002} | Popular Tags |