1 31 32 package org.opencms.workplace.editors; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsRequestContext; 37 import org.opencms.i18n.CmsEncoder; 38 import org.opencms.i18n.CmsLocaleManager; 39 import org.opencms.jsp.CmsJspActionElement; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsLog; 42 import org.opencms.main.OpenCms; 43 import org.opencms.util.CmsHtmlConverter; 44 import org.opencms.util.CmsStringUtil; 45 import org.opencms.xml.CmsXmlException; 46 import org.opencms.xml.page.CmsXmlPage; 47 import org.opencms.xml.page.CmsXmlPageFactory; 48 49 import java.io.IOException ; 50 import java.util.ArrayList ; 51 import java.util.List ; 52 import java.util.Locale ; 53 import java.util.Properties ; 54 55 import javax.servlet.ServletException ; 56 import javax.servlet.jsp.JspException ; 57 58 import org.apache.commons.logging.Log; 59 60 71 public abstract class CmsDefaultPageEditor extends CmsEditor { 72 73 74 public static final String PARAM_ELEMENTNAME = "elementname"; 75 76 77 public static final String PARAM_OLDELEMENTNAME = "oldelementname"; 78 79 80 public static final String XML_BODY_ELEMENT = "body"; 81 82 83 private static final Log LOG = CmsLog.getLog(CmsDefaultPageEditor.class); 84 85 86 protected CmsFile m_file; 87 88 89 protected CmsXmlPage m_page; 90 91 private List m_elementList; 92 93 94 private Locale m_elementLocale; 95 96 private String m_paramElementname; 97 private String m_paramOldelementname; 98 99 100 public static final String [] SELECTBOX_FONTS = { 101 "Arial", 102 "Arial Narrow", 103 "System", 104 "Times New Roman", 105 "Verdana", 106 "Monospace", 107 "SansSerif"}; 108 109 114 public CmsDefaultPageEditor(CmsJspActionElement jsp) { 115 116 super(jsp); 117 } 118 119 122 public void actionChangeBodyElement() { 123 124 try { 125 Locale oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage()); 127 performSaveContent(getParamOldelementname(), oldLocale); 128 } catch (CmsException e) { 129 try { 131 showErrorPage(this, e); 132 } catch (JspException exc) { 133 if (LOG.isInfoEnabled()) { 135 LOG.info(exc); 136 } 137 } 138 } 139 if (!getParamElementlanguage().equals(getParamOldelementlanguage())) { 141 initBodyElementName(getParamOldelementname()); 142 } 143 initContent(); 145 } 146 147 150 public void actionCleanupBodyElement() { 151 152 try { 153 Locale oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage()); 155 performSaveContent(getParamOldelementname(), oldLocale); 156 } catch (CmsException e) { 157 try { 159 showErrorPage(this, e); 160 } catch (JspException exc) { 161 if (LOG.isInfoEnabled()) { 163 LOG.info(exc); 164 } 165 } 166 } 167 } 168 169 172 public void actionClear(boolean forceUnlock) { 173 174 deleteTempFile(); 176 boolean directEditMode = Boolean.valueOf(getParamDirectedit()).booleanValue(); 177 boolean modified = Boolean.valueOf(getParamModified()).booleanValue(); 178 if (directEditMode || forceUnlock || !modified) { 179 try { 181 getCms().unlockResource(getParamResource()); 182 } catch (CmsException e) { 183 if (LOG.isInfoEnabled()) { 185 LOG.info(e); 186 } 187 } 188 } 189 } 190 191 196 public void actionDeleteElementLocale() throws JspException { 197 198 try { 199 Locale loc = getElementLocale(); 200 m_page.removeLocale(loc); 201 m_file.setContents(m_page.marshal()); 203 m_file = getCms().writeFile(m_file); 204 List locales = m_page.getLocales(); 205 if (locales.size() > 0) { 206 Locale newLoc = (Locale )locales.get(0); 208 setParamElementlanguage(newLoc.toString()); 209 m_elementLocale = newLoc; 210 } else { 211 if (LOG.isErrorEnabled()) { 212 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, getParamResource())); 213 } 214 } 215 initContent(); 216 } catch (CmsXmlException e) { 217 showErrorPage(e); 219 } catch (CmsException e) { 220 if (LOG.isInfoEnabled()) { 222 LOG.info(e.getLocalizedMessage(), e); 223 } 224 } 225 } 226 227 236 public void actionDirectEdit() throws IOException , JspException , ServletException { 237 238 I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler(); 240 if (actionClass == null) { 241 actionSave(); 243 actionExit(); 244 } else { 245 actionClass.editorAction(this, getJsp()); 246 } 247 } 248 249 254 public void actionExit() throws IOException , JspException , ServletException { 255 256 if (getAction() == ACTION_CANCEL) { 257 return; 259 } 260 actionClear(false); 262 actionClose(); 264 } 265 266 272 public void actionPreview() throws IOException , JspException { 273 274 try { 275 performSaveContent(getParamElementname(), getElementLocale()); 277 } catch (CmsException e) { 278 showErrorPage(this, e); 280 } 281 282 String param = "?" + org.opencms.i18n.CmsLocaleManager.PARAMETER_LOCALE + "=" + getParamElementlanguage(); 284 sendCmsRedirect(getParamTempfile() + param); 285 } 286 287 290 public void actionSave() throws JspException { 291 292 try { 293 294 performSaveContent(getParamElementname(), getElementLocale()); 296 commitTempFile(); 298 setParamModified(Boolean.TRUE.toString()); 300 } catch (CmsException e) { 301 showErrorPage(e); 302 } 303 304 if (getAction() != ACTION_CANCEL) { 305 setAction(ACTION_SAVE); 307 } 308 } 309 310 318 public abstract String buildGalleryButtons( 319 CmsEditorDisplayOptions options, 320 int buttonStyle, 321 Properties displayOptions); 322 323 329 public String buildSelectElementLanguage(String attributes) { 330 331 return buildSelectElementLanguage(attributes, getParamTempfile(), getElementLocale()); 332 } 333 334 340 public String buildSelectElementName(String attributes) { 341 342 List elementList = getElementList(); 344 345 int counter = 0; 346 int currentIndex = -1; 347 List options = new ArrayList (elementList.size()); 348 List values = new ArrayList (elementList.size()); 349 String elementName = getParamElementname(); 350 if (CmsStringUtil.isEmpty(elementName)) { 351 elementName = getParamOldelementname(); 352 } 353 for (int i = 0; i < elementList.size(); i++) { 354 CmsDialogElement element = (CmsDialogElement)elementList.get(i); 356 357 if (CmsStringUtil.isNotEmpty(elementName) && elementName.equals(element.getName())) { 358 currentIndex = counter; 360 } 361 if ((!m_page.hasValue(element.getName(), getElementLocale()) && element.isMandantory()) 362 || m_page.isEnabled(element.getName(), getElementLocale())) { 363 options.add(element.getNiceName()); 365 values.add(element.getName()); 366 counter++; 367 } 368 } 369 return buildSelect(attributes, options, values, currentIndex, false); 370 } 371 372 378 public String buildSelectFonts(String attributes) { 379 380 List names = new ArrayList (); 381 for (int i = 0; i < CmsDefaultPageEditor.SELECTBOX_FONTS.length; i++) { 382 String value = CmsDefaultPageEditor.SELECTBOX_FONTS[i]; 383 names.add(value); 384 } 385 return buildSelect(attributes, names, names, -1, false); 386 } 387 388 393 public void escapeParams() { 394 395 setParamContent(CmsEncoder.escapeWBlanks(getParamContent(), CmsEncoder.ENCODING_UTF_8)); 397 } 398 399 404 public Locale getElementLocale() { 405 406 if (m_elementLocale == null) { 407 m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage()); 408 } 409 return m_elementLocale; 410 } 411 412 417 public String getParamElementname() { 418 419 return m_paramElementname; 420 } 421 422 427 public String getParamOldelementname() { 428 429 return m_paramOldelementname; 430 } 431 432 437 public String getUriStyleSheet() { 438 439 String result = ""; 440 try { 441 String currentTemplate = getUriTemplate(); 442 if (!"".equals(currentTemplate)) { 443 result = getCms().readPropertyObject(currentTemplate, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue( 445 ""); 446 } 447 } catch (CmsException e) { 448 LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_STYLESHEET_FAILED_0), e); 449 } 450 return result; 451 } 452 453 458 public String getUriTemplate() { 459 460 String result = ""; 461 try { 462 result = getCms().readPropertyObject(getParamTempfile(), CmsPropertyDefinition.PROPERTY_TEMPLATE, true).getValue( 463 ""); 464 } catch (CmsException e) { 465 LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_FAILED_0), e); 466 } 467 return result; 468 } 469 470 475 public void setParamElementname(String elementName) { 476 477 m_paramElementname = elementName; 478 } 479 480 485 public void setParamOldelementname(String oldElementName) { 486 487 m_paramOldelementname = oldElementName; 488 } 489 490 495 protected List getElementList() { 496 497 if (m_elementList == null) { 498 m_elementList = CmsDialogElements.computeElements(getCms(), m_page, getParamTempfile(), getElementLocale()); 499 } 500 return m_elementList; 501 } 502 503 506 protected void initBodyElementLanguage() { 507 508 List locales = m_page.getLocales(); 509 Locale defaultLocale = (Locale )OpenCms.getLocaleManager().getDefaultLocales( 510 getCms(), 511 getCms().getSitePath(m_file)).get(0); 512 513 if (locales.size() == 0) { 514 if (!m_page.hasValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale)) { 516 m_page.addValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale); 517 } 518 try { 519 m_file.setContents(m_page.marshal()); 520 getCms().writeFile(m_file); 521 } catch (CmsException e) { 522 try { 524 showErrorPage(this, e); 525 } catch (JspException exc) { 526 if (LOG.isInfoEnabled()) { 528 LOG.info(exc); 529 } 530 } 531 } 532 setParamElementlanguage(defaultLocale.toString()); 533 } else { 534 if (locales.contains(defaultLocale)) { 536 setParamElementlanguage(defaultLocale.toString()); 538 } else { 539 setParamElementlanguage(locales.get(0).toString()); 541 } 542 543 } 544 } 545 546 553 protected void initBodyElementName(String elementName) { 554 555 if (elementName == null 556 || (m_page.hasValue(elementName, getElementLocale()) && !m_page.isEnabled(elementName, getElementLocale()))) { 557 List allElements = m_page.getNames(getElementLocale()); 559 int elementCount = allElements.size(); 560 List elements = new ArrayList (elementCount); 561 for (int i = 0; i < elementCount; i++) { 562 if (m_page.isEnabled((String )allElements.get(i), getElementLocale())) { 564 elements.add(allElements.get(i)); 565 } 566 } 567 568 List elementList = getElementList(); 570 for (int i = 0; i < elementList.size(); i++) { 571 CmsDialogElement checkElement = (CmsDialogElement)elementList.get(i); 572 if (elements.contains(checkElement.getName())) { 573 setParamElementname(checkElement.getName()); 575 return; 576 } 577 } 578 579 if (elements.contains(CmsDefaultPageEditor.XML_BODY_ELEMENT)) { 581 setParamElementname(CmsDefaultPageEditor.XML_BODY_ELEMENT); 583 } else { 584 setParamElementname((String )elements.get(0)); 586 } 587 } else { 588 setParamElementname(elementName); 590 } 591 } 592 593 598 protected void initContent() { 599 600 if (CmsStringUtil.isNotEmpty(getParamContent())) { 601 if (CmsStringUtil.isNotEmpty(getParamElementname()) 602 && getParamElementname().equals(getParamOldelementname())) { 603 if (CmsStringUtil.isNotEmpty(getParamElementlanguage()) 604 && getParamElementlanguage().equals(getParamOldelementlanguage())) { 605 return; 606 } 607 } 608 } 609 getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean (true)); 610 611 String elementData; 612 if (m_page.hasValue(getParamElementname(), getElementLocale())) { 613 elementData = m_page.getStringValue(getCms(), getParamElementname(), getElementLocale()); 615 } else { 616 if (Boolean.valueOf(getParamDirectedit()).booleanValue()) { 618 m_page.addValue(getParamElementname(), getElementLocale()); 620 } 621 elementData = ""; 622 } 623 setParamContent(elementData); 624 } 625 626 633 protected void performSaveContent(String body, Locale locale) throws CmsException { 634 635 String content = prepareContent(true); 637 638 String contentConversion = m_page.getConversion(); 639 if (EDITOR_CLEANUP.equals(getParamAction())) { 641 if ((contentConversion == null) || (contentConversion.equals(CmsHtmlConverter.PARAM_DISABLED))) { 642 contentConversion = CmsHtmlConverter.PARAM_WORD; 644 } else { 645 contentConversion += ";" + CmsHtmlConverter.PARAM_WORD; 647 } 648 } 649 m_page.setConversion(contentConversion); 650 651 if (!m_page.hasValue(body, locale) && !"".equals(content)) { 653 m_page.addValue(body, locale); 654 } 655 656 boolean enabled = m_page.isEnabled(body, locale); 658 659 if (m_page.hasValue(body, locale)) { 661 m_page.setStringValue(getCms(), body, locale, content); 662 } 663 664 m_file.setContents(m_page.marshal()); 666 m_file = getCms().writeFile(m_file); 667 668 m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file); 670 if (m_page.hasValue(body, locale)) { 671 getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean (true)); 672 content = m_page.getStringValue(getCms(), body, locale); 673 if (content == null) { 674 content = ""; 675 } 676 setParamContent(content); 677 prepareContent(false); 678 m_page.setEnabled(body, locale, enabled); 679 } 680 } 681 682 688 protected abstract String prepareContent(boolean save); 689 690 } | Popular Tags |