001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsDialogElements.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.20 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.editors;
033:
034: import org.opencms.file.CmsFile;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsPropertyDefinition;
037: import org.opencms.file.CmsResource;
038: import org.opencms.file.CmsResourceFilter;
039: import org.opencms.i18n.CmsLocaleManager;
040: import org.opencms.jsp.CmsJspActionElement;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.CmsLog;
043: import org.opencms.util.CmsStringUtil;
044: import org.opencms.workplace.CmsDialog;
045: import org.opencms.workplace.CmsWorkplaceSettings;
046: import org.opencms.xml.page.CmsXmlPage;
047: import org.opencms.xml.page.CmsXmlPageFactory;
048:
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Locale;
054:
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057: import javax.servlet.jsp.JspException;
058: import javax.servlet.jsp.PageContext;
059:
060: import org.apache.commons.logging.Log;
061:
062: /**
063: * Provides methods for the editor elements dialog.<p>
064: *
065: * The following files use this class:
066: * <ul>
067: * <li>/jsp/editors/dialogs/elements.html
068: * </ul>
069: * <p>
070: *
071: * @author Andreas Zahner
072: *
073: * @version $Revision: 1.20 $
074: *
075: * @since 6.0.0
076: */
077: public class CmsDialogElements extends CmsDialog {
078:
079: /** Value for the action: update the elements of the page. */
080: public static final int ACTION_UPDATE_ELEMENTS = 210;
081:
082: /** The dialog type. */
083: public static final String DIALOG_TYPE = "elementselector";
084:
085: /** Request parameter value for the action: update the elements of the page. */
086: public static final String DIALOG_UPDATE_ELEMENTS = "updateelements";
087:
088: /** Prefix for the html input field for the body. */
089: public static final String PREFIX_PARAM_BODY = "element-";
090:
091: /** The log object for this class. */
092: private static final Log LOG = CmsLog
093: .getLog(CmsDialogElements.class);
094:
095: /** Stores the element to change to after an element update operation. */
096: private String m_changeElement;
097:
098: /** List used to store information of all possible elements of the page. */
099: private List m_elementList;
100:
101: /** The element locale. */
102: private Locale m_elementLocale;
103:
104: // Special parameters used by this dialog
105: private String m_paramElementlanguage;
106: private String m_paramElementname;
107: private String m_paramTempFile;
108:
109: /**
110: * Public constructor.<p>
111: *
112: * @param jsp an initialized JSP action element
113: */
114: public CmsDialogElements(CmsJspActionElement jsp) {
115:
116: super (jsp);
117: m_changeElement = "";
118: }
119:
120: /**
121: * Public constructor with JSP variables.<p>
122: *
123: * @param context the JSP page context
124: * @param req the JSP request
125: * @param res the JSP response
126: */
127: public CmsDialogElements(PageContext context,
128: HttpServletRequest req, HttpServletResponse res) {
129:
130: this (new CmsJspActionElement(context, req, res));
131: m_changeElement = "";
132: }
133:
134: /**
135: * Creates a list of possible elements of a template from the template property "template-elements"
136: * and the elements available in the provided xmlPage.<p>
137: *
138: * @param cms the CmsObject
139: * @param xmlPage the resource to read the elements from
140: * @param xmlPageUri the URI of the resouirce to read the template property from
141: * @param locale the current element locale
142: * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
143: */
144: public static List computeElements(CmsObject cms,
145: CmsXmlPage xmlPage, String xmlPageUri, Locale locale) {
146:
147: List result = new ArrayList();
148:
149: if (xmlPage != null) {
150: List elementNames = xmlPage.getNames(locale);
151:
152: Iterator i = elementNames.iterator();
153: while (i.hasNext()) {
154: String name = (String) i.next();
155: CmsDialogElement element = new CmsDialogElement(name,
156: null, false, false, true);
157: result.add(element);
158: }
159: }
160:
161: String currentTemplate = null;
162: try {
163: currentTemplate = cms.readPropertyObject(xmlPageUri,
164: CmsPropertyDefinition.PROPERTY_TEMPLATE, true)
165: .getValue();
166: } catch (CmsException e) {
167: if (LOG.isWarnEnabled()) {
168: LOG.warn(e.getLocalizedMessage(), e);
169: }
170: }
171: if ((currentTemplate != null) && (currentTemplate.length() > 0)) {
172: // template found, check template-elements property
173: String elements = null;
174: try {
175: // read the property from the template file
176: elements = cms
177: .readPropertyObject(
178: currentTemplate,
179: CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS,
180: false).getValue(null);
181: } catch (CmsException e) {
182: if (LOG.isWarnEnabled()) {
183: LOG.warn(e.getLocalizedMessage(), e);
184: }
185: }
186: if (elements != null) {
187: // elements are defined on template file, merge with available elements
188: List tokens = CmsStringUtil.splitAsList(elements, ',',
189: true);
190: Iterator it = tokens.iterator();
191: while (it.hasNext()) {
192: String currentElement = (String) it.next();
193: String niceName = null;
194: boolean mandatory = false;
195: int sepIndex = currentElement.indexOf("|");
196: if (sepIndex != -1) {
197: // nice name found for current element, extract it
198: niceName = currentElement
199: .substring(sepIndex + 1);
200: currentElement = currentElement.substring(0,
201: sepIndex);
202: }
203: if (currentElement.endsWith("*")) {
204: // element is mandatory
205: mandatory = true;
206: currentElement = currentElement.substring(0,
207: currentElement.length() - 1);
208: }
209:
210: CmsDialogElement element = new CmsDialogElement(
211: currentElement, niceName, mandatory, true,
212: false);
213: if (result.contains(element)) {
214: element.setExisting(true);
215: result.remove(element);
216: }
217: result.add(element);
218: }
219: }
220: }
221:
222: Collections.sort(result);
223: return result;
224: }
225:
226: /**
227: * Creates a list of possible elements of a template from the template property "template-elements"
228: * and the elements available in the provided resource file.<p>
229: *
230: * @param cms the CmsObject
231: * @param xmlPageUri the resource to read the elements from
232: * @param locale the current element locale
233: * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
234: */
235: public static List computeElements(CmsObject cms,
236: String xmlPageUri, Locale locale) {
237:
238: CmsXmlPage page = null;
239: try {
240: // read the xmlpage file
241: CmsFile pageFile = cms.readFile(xmlPageUri,
242: CmsResourceFilter.IGNORE_EXPIRATION);
243: page = CmsXmlPageFactory.unmarshal(cms, pageFile);
244: } catch (CmsException e) {
245: LOG.warn(Messages.get().getBundle().key(
246: Messages.LOG_READ_XMLPAGE_FAILED_1, xmlPageUri), e);
247: // xmlpage will be null, only "template-elements" property on template will be checked
248: }
249: return computeElements(cms, page, xmlPageUri, locale);
250: }
251:
252: /**
253: * Updates the enabled/diabled status of all elements of the current page.<p>
254: *
255: * @throws JspException if there is an error including the error page
256: */
257: public void actionUpdateElements() throws JspException {
258:
259: try {
260: List elementList = computeElements();
261: CmsFile file = getCms().readFile(getParamTempfile(),
262: CmsResourceFilter.IGNORE_EXPIRATION);
263: CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(),
264: file);
265: boolean foundMandatory = false;
266: m_changeElement = "";
267: Iterator i = elementList.iterator();
268: while (i.hasNext()) {
269: // get the current list element
270: CmsDialogElement element = (CmsDialogElement) i.next();
271: if (element.isMandantory()
272: || element.getName().equals(
273: getParamElementname())
274: || Boolean.valueOf(
275: getJsp().getRequest().getParameter(
276: PREFIX_PARAM_BODY
277: + element.getName()))
278: .booleanValue()) {
279: if (!element.isExisting()) {
280: // create element in order to enable it properly
281: page.addValue(element.getName(),
282: getElementLocale());
283: }
284: page.setEnabled(element.getName(),
285: getElementLocale(), true);
286: if (element.isMandantory() && !foundMandatory) {
287: m_changeElement = element.getName();
288: foundMandatory = true;
289: }
290: } else {
291: if (element.isExisting()) {
292: // remove element if it is already existing
293: page.removeValue(element.getName(),
294: getElementLocale());
295: }
296: }
297: }
298: // write the temporary file
299: file.setContents(page.marshal());
300: getCms().writeFile(file);
301: // set the javascript functions which should be executed
302: if (page.isEnabled(getParamElementname(),
303: getElementLocale())) {
304: m_changeElement = getParamElementname();
305: } else if (!foundMandatory) {
306: if (elementList.size() > 0) {
307: m_changeElement = ((CmsDialogElement) elementList
308: .get(0)).getName();
309: }
310: }
311: } catch (Throwable e) {
312: // show error dialog
313: setParamMessage(Messages.get().getBundle(getLocale()).key(
314: Messages.ERR_UPDATE_ELEMENTS_0));
315: includeErrorpage(this , e);
316: }
317: }
318:
319: /**
320: * Builds the html String for a form list of all possible page elements.<p>
321: *
322: * @return the html String for a form list
323: */
324: public String buildElementList() {
325:
326: StringBuffer retValue = new StringBuffer(512);
327: retValue.append("<table border=\"0\">\n");
328: retValue.append("<tr>\n");
329: retValue
330: .append("\t<td class=\"textbold\" unselectable=\"on\">"
331: + key(Messages.GUI_EDITOR_DIALOG_ELEMENTS_PAGEELEMENT_0)
332: + "</td>\n");
333: retValue
334: .append("\t<td class=\"textbold\" unselectable=\"on\"> "
335: + key(Messages.GUI_EDITOR_DIALOG_ELEMENTS_ENABLED_0)
336: + " </td>\n");
337: retValue.append("</tr>\n");
338: retValue
339: .append("<tr><td colspan=\"2\"><span style=\"height: 6px;\"></span></td></tr>\n");
340:
341: try {
342:
343: // get the list of all possible elements
344: List elementList = computeElements();
345:
346: // get all present bodies from the temporary file
347: CmsFile file = getCms().readFile(this .getParamTempfile(),
348: CmsResourceFilter.IGNORE_EXPIRATION);
349: CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(),
350: file);
351:
352: // show all possible elements
353: Iterator i = elementList.iterator();
354: while (i.hasNext()) {
355: // get the current list element
356: CmsDialogElement element = (CmsDialogElement) i.next();
357: // build an element row
358: retValue.append("<tr>\n");
359: retValue
360: .append("\t<td style=\"white-space: nowrap;\" unselectable=\"on\">"
361: + element.getNiceName());
362: retValue.append("</td>\n");
363: retValue
364: .append("\t<td class=\"textcenter\" unselectable=\"on\"><input type=\"checkbox\" name=\"");
365: retValue.append(PREFIX_PARAM_BODY);
366: retValue.append(element.getName());
367: retValue.append("\" value=\"true\"");
368:
369: if ((!page.hasValue(element.getName(),
370: getElementLocale()) && element.isMandantory())
371: || page.isEnabled(element.getName(),
372: getElementLocale())) {
373: retValue.append(" checked=\"checked\"");
374: }
375: if (element.isMandantory()
376: || element.getName().equals(
377: getParamElementname())) {
378: retValue.append(" disabled=\"disabled\"");
379: }
380: retValue.append(">");
381: retValue
382: .append("<script type=\"text/javascript\">registerElement(\"");
383:
384: retValue.append(element.getName());
385: retValue.append("\", ");
386: retValue.append(page.isEnabled(element.getName(),
387: getElementLocale()));
388: retValue.append(");</script>");
389:
390: retValue.append("</td>\n");
391: retValue.append("</tr>\n");
392: }
393:
394: } catch (CmsException e) {
395: // should usually never happen
396: if (LOG.isInfoEnabled()) {
397: LOG.info(e);
398: }
399: }
400:
401: retValue.append("</table>\n");
402: return retValue.toString();
403: }
404:
405: /**
406: * Creates a list of possible elements of a template from the template property "template-elements".<p>
407: *
408: * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
409: */
410: public List computeElements() {
411:
412: if (m_elementList == null) {
413: m_elementList = computeElements(getCms(),
414: getParamTempfile(), getElementLocale());
415: }
416: return m_elementList;
417: }
418:
419: /**
420: * Returns the element name that has to be changed.<p>
421: *
422: * @return the element name that has to be changed
423: */
424: public String getChangeElement() {
425:
426: return m_changeElement;
427: }
428:
429: /**
430: * Returns the current element locale.<p>
431: *
432: * @return the current element locale
433: */
434: public Locale getElementLocale() {
435:
436: if (m_elementLocale == null) {
437: m_elementLocale = CmsLocaleManager
438: .getLocale(getParamElementlanguage());
439: }
440: return m_elementLocale;
441: }
442:
443: /**
444: * Returns the current element language.<p>
445: *
446: * @return the current element language
447: */
448: public String getParamElementlanguage() {
449:
450: return m_paramElementlanguage;
451: }
452:
453: /**
454: * Returns the current element name.<p>
455: *
456: * @return the current element name
457: */
458: public String getParamElementname() {
459:
460: return m_paramElementname;
461: }
462:
463: /**
464: * Returns the name of the temporary file.<p>
465: *
466: * @return the name of the temporary file
467: */
468: public String getParamTempfile() {
469:
470: return m_paramTempFile;
471: }
472:
473: /**
474: * Sets the current element language.<p>
475: *
476: * @param elementLanguage the current element language
477: */
478: public void setParamElementlanguage(String elementLanguage) {
479:
480: m_paramElementlanguage = elementLanguage;
481: }
482:
483: /**
484: * Sets the current element name.<p>
485: *
486: * @param elementName the current element name
487: */
488: public void setParamElementname(String elementName) {
489:
490: m_paramElementname = elementName;
491: }
492:
493: /**
494: * Sets the name of the temporary file.<p>
495: *
496: * @param fileName the name of the temporary file
497: */
498: public void setParamTempfile(String fileName) {
499:
500: m_paramTempFile = fileName;
501: }
502:
503: /**
504: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
505: */
506: protected void initWorkplaceRequestValues(
507: CmsWorkplaceSettings settings, HttpServletRequest request) {
508:
509: // fill the parameter values in the get/set methods
510: fillParamValues(request);
511: // set the dialog type
512: setParamDialogtype(DIALOG_TYPE);
513: // set the action for the JSP switch
514: if (DIALOG_UPDATE_ELEMENTS.equals(getParamAction())) {
515: setAction(ACTION_UPDATE_ELEMENTS);
516: } else {
517: setAction(ACTION_DEFAULT);
518: // build title for delete dialog
519: setParamTitle(key(
520: Messages.GUI_EDITOR_DIALOG_ELEMENTS_TITLE_1,
521: new Object[] { CmsResource
522: .getName(getParamResource()) }));
523: }
524: }
525: }
|