001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/A_CmsToolHandler.java,v $
003: * Date : $Date: 2008-02-27 12:05:31 $
004: * Version: $Revision: 1.28 $
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.tools;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsProperty;
036: import org.opencms.file.CmsPropertyDefinition;
037: import org.opencms.file.CmsResource;
038: import org.opencms.i18n.CmsEncoder;
039: import org.opencms.i18n.CmsMessages;
040: import org.opencms.jsp.CmsJspNavBuilder;
041: import org.opencms.jsp.CmsJspNavElement;
042: import org.opencms.main.CmsException;
043: import org.opencms.main.CmsLog;
044: import org.opencms.util.CmsStringUtil;
045: import org.opencms.workplace.CmsWorkplace;
046:
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Map;
050:
051: import org.apache.commons.logging.Log;
052:
053: /**
054: * Helper class to build easily other admin tool handlers.<p>
055: *
056: * @author Michael Moossen
057: *
058: * @version $Revision: 1.28 $
059: *
060: * @since 6.0.0
061: */
062: public abstract class A_CmsToolHandler implements I_CmsToolHandler {
063:
064: /** Property for the parameters argument.<p> */
065: public static final String ARG_PARAM_NAME = "params";
066:
067: /** Property for the path argument.<p> */
068: public static final String ARG_PATH_NAME = "path";
069:
070: /** Property definition for the arguments.<p> */
071: public static final String ARGS_PROPERTY_DEFINITION = "admintoolhandler-args";
072:
073: /** Argument separator.<p> */
074: public static final String ARGUMENT_SEPARATOR = "|";
075:
076: /** Default disabled help text constant.<p> */
077: public static final String DEFAULT_DISABLED_HELPTEXT = "${key."
078: + Messages.GUI_TOOLS_DISABLED_HELP_0 + "}";
079:
080: /** Argument name and value separator.<p> */
081: public static final String VALUE_SEPARATOR = ":";
082:
083: /** Property for the confirmation message argument.<p> */
084: private static final String ARG_CONFIRMATION_NAME = "confirmation";
085:
086: /** The static log object for this class. */
087: private static final Log LOG = CmsLog
088: .getLog(A_CmsToolHandler.class);
089:
090: /** Confirmation message. */
091: private String m_confirmationMessage;
092:
093: /** Help text or description if disabled. */
094: private String m_disabledHelpText;
095:
096: /** Group to be included in. */
097: private String m_group;
098:
099: /** Help text or description. */
100: private String m_helpText;
101:
102: /** Icon path (32x32). */
103: private String m_iconPath;
104:
105: /** Link pointer. */
106: private String m_link;
107:
108: /** Display name. */
109: private String m_name;
110:
111: /** Needed parameters. */
112: private String m_parameters;
113:
114: /** Tool path to install in. */
115: private String m_path;
116:
117: /** Relative position in group. */
118: private float m_position;
119:
120: /** Menu item name. */
121: private String m_shortName;
122:
123: /** Small icon path (16x16). */
124: private String m_smallIconPath;
125:
126: /**
127: * Returns the confirmation Message.<p>
128: *
129: * @return the confirmation Message
130: */
131: public String getConfirmationMessage() {
132:
133: return m_confirmationMessage;
134: }
135:
136: /**
137: * @see org.opencms.workplace.tools.I_CmsToolHandler#getDisabledHelpText()
138: */
139: public String getDisabledHelpText() {
140:
141: return m_disabledHelpText;
142: }
143:
144: /**
145: * @see org.opencms.workplace.tools.I_CmsToolHandler#getGroup()
146: */
147: public String getGroup() {
148:
149: return m_group;
150: }
151:
152: /**
153: * @see org.opencms.workplace.tools.I_CmsToolHandler#getHelpText()
154: */
155: public String getHelpText() {
156:
157: return m_helpText;
158: }
159:
160: /**
161: * @see org.opencms.workplace.tools.I_CmsToolHandler#getIconPath()
162: */
163: public String getIconPath() {
164:
165: return m_iconPath;
166: }
167:
168: /**
169: * @see org.opencms.workplace.tools.I_CmsToolHandler#getLink()
170: */
171: public String getLink() {
172:
173: return m_link;
174: }
175:
176: /**
177: * @see org.opencms.workplace.tools.I_CmsToolHandler#getName()
178: */
179: public String getName() {
180:
181: return m_name;
182: }
183:
184: /**
185: * @see org.opencms.workplace.tools.I_CmsToolHandler#getParameters(org.opencms.workplace.CmsWorkplace)
186: */
187: public Map getParameters(CmsWorkplace wp) {
188:
189: Map argMap = new HashMap();
190: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_parameters)) {
191: String toolParams = CmsEncoder.decode(wp
192: .resolveMacros(m_parameters));
193: Iterator itArgs = CmsStringUtil
194: .splitAsList(toolParams, "&").iterator();
195: while (itArgs.hasNext()) {
196: String arg = (String) itArgs.next();
197: int pos = arg.indexOf("=");
198: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(arg
199: .substring(pos + 1))) {
200: argMap.put(arg.substring(0, pos), arg
201: .substring(pos + 1));
202: }
203: }
204: }
205: return argMap;
206: }
207:
208: /**
209: * @see org.opencms.workplace.tools.I_CmsToolHandler#getPath()
210: */
211: public String getPath() {
212:
213: return m_path;
214: }
215:
216: /**
217: * @see org.opencms.workplace.tools.I_CmsToolHandler#getPosition()
218: */
219: public float getPosition() {
220:
221: return m_position;
222: }
223:
224: /**
225: * @see org.opencms.workplace.tools.I_CmsToolHandler#getShortName()
226: */
227: public String getShortName() {
228:
229: return m_shortName;
230: }
231:
232: /**
233: * @see org.opencms.workplace.tools.I_CmsToolHandler#getSmallIconPath()
234: */
235: public String getSmallIconPath() {
236:
237: return m_smallIconPath;
238: }
239:
240: /**
241: * @see org.opencms.workplace.tools.I_CmsToolHandler#isEnabled(org.opencms.workplace.CmsWorkplace)
242: */
243: public boolean isEnabled(CmsWorkplace wp) {
244:
245: return isEnabled(wp.getCms());
246: }
247:
248: /**
249: * @see org.opencms.workplace.tools.I_CmsToolHandler#isVisible(org.opencms.workplace.CmsWorkplace)
250: */
251: public boolean isVisible(CmsWorkplace wp) {
252:
253: return isVisible(wp.getCms());
254: }
255:
256: /**
257: * Sets the confirmation Message.<p>
258: *
259: * @param confirmationMessage the confirmation Message to set
260: */
261: public void setConfirmationMessage(String confirmationMessage) {
262:
263: m_confirmationMessage = confirmationMessage;
264: }
265:
266: /**
267: * Sets the help text if disabled.<p>
268: *
269: * @param disabledHelpText the help text to set
270: */
271: public void setDisabledHelpText(String disabledHelpText) {
272:
273: m_disabledHelpText = disabledHelpText;
274: }
275:
276: /**
277: * Sets the group.<p>
278: *
279: * @param group the group to set
280: */
281: public void setGroup(String group) {
282:
283: m_group = group;
284: }
285:
286: /**
287: * Sets the help text.<p>
288: *
289: * @param helpText the help text to set
290: */
291: public void setHelpText(String helpText) {
292:
293: m_helpText = helpText;
294: }
295:
296: /**
297: * Sets the icon path.<p>
298: *
299: * @param iconPath the icon path to set
300: */
301: public void setIconPath(String iconPath) {
302:
303: m_iconPath = iconPath;
304: }
305:
306: /**
307: * Sets the link.<p>
308: *
309: * @param link the link to set
310: */
311: public void setLink(String link) {
312:
313: m_link = link;
314: }
315:
316: /**
317: * Sets the name.<p>
318: *
319: * @param name the name to set
320: */
321: public void setName(String name) {
322:
323: m_name = name;
324: }
325:
326: /**
327: * Sets the parameter string.<p>
328: *
329: * @param paramString the parameter string to set
330: */
331: public void setParameterString(String paramString) {
332:
333: m_parameters = paramString;
334: }
335:
336: /**
337: * Sets the path.<p>
338: *
339: * @param path the path to set
340: */
341: public void setPath(String path) {
342:
343: m_path = path;
344: }
345:
346: /**
347: * Sets the position.<p>
348: *
349: * @param position the position to set
350: */
351: public void setPosition(float position) {
352:
353: m_position = position;
354: }
355:
356: /**
357: * Sets the short name.<p>
358: *
359: * @param shortName the short name to set
360: */
361: public void setShortName(String shortName) {
362:
363: m_shortName = shortName;
364: }
365:
366: /**
367: * Sets the small icon path.<p>
368: *
369: * @param smallIconPath the samll icon path to set
370: */
371: public void setSmallIconPath(String smallIconPath) {
372:
373: m_smallIconPath = smallIconPath;
374: }
375:
376: /**
377: * Default implementation.<p>
378: *
379: * It takes the icon path from <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVIMAGE}</code> property,
380: * or uses a default icon if undefined, the name is taken from the
381: * <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVTEXT}</code> property,
382: * or uses the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_TITLE}</code> property if undefined,
383: * or an default text, if still undefined. if you want 2 different names, one for the big icon tools and one for
384: * the menu/navbar entries, use a <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code> to separate them in the property.
385: * (if you do so, the first one is for big icons and the second one for menu/navbar entries). the help text is taken from the
386: * <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_DESCRIPTION}</code> property or a
387: * default text if undefined, if you want to customize a help text while disabled, use a
388: * <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code> as a separator in the same property.<p>
389: *
390: * The group is taken from the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVINFO}</code> property,
391: * the position from the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVPOS}</code>
392: * and the install path is given by the folder structure if the <code>{@link #ARGS_PROPERTY_DEFINITION}</code>
393: * property does not include path information.<p>
394: *
395: * For the icon path you can specify 2 paths separated by a <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code>,
396: * the first one will be used as a group icon (32x32), and the second as an menu icon (16x16). The paths are relative
397: * to the /system/workplace/resources/ folder. If the tool is disabled, the names of the icons are composed as
398: * ${name}_disabled.${ext}<p>
399: *
400: * The confirmation message is taken from the <code>{@link #ARGS_PROPERTY_DEFINITION}</code> with key
401: * <code>#ARG_CONFIRMATION_NAME</code>
402: *
403: * @see org.opencms.workplace.tools.I_CmsToolHandler#setup(org.opencms.file.CmsObject, CmsToolRootHandler, java.lang.String)
404: */
405: public boolean setup(CmsObject cms, CmsToolRootHandler root,
406: String resourcePath) {
407:
408: try {
409: resourcePath = cms.getSitePath(cms
410: .readResource(resourcePath));
411: } catch (CmsException e) {
412: // should not happen
413: if (LOG.isErrorEnabled()) {
414: LOG.error(e.getLocalizedMessage(), e);
415: }
416: }
417: CmsJspNavElement navElem = CmsJspNavBuilder
418: .getNavigationForResource(cms, resourcePath);
419:
420: String name = navElem.getNavText();
421: if (CmsMessages.isUnknownKey(name)) {
422: name = navElem.getTitle();
423: }
424: if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
425: name = "${key." + Messages.GUI_TOOLS_DEFAULT_NAME_0 + "}";
426: }
427: String shortName = name;
428: if (name.indexOf(VALUE_SEPARATOR) >= 0) {
429: shortName = name
430: .substring(0, name.indexOf(VALUE_SEPARATOR));
431: name = name.substring(name.indexOf(VALUE_SEPARATOR) + 1);
432: }
433: setName(name);
434: setShortName(shortName);
435:
436: String iconPath = navElem.getNavImage();
437: if (CmsStringUtil.isEmptyOrWhitespaceOnly(iconPath)) {
438: iconPath = "admin/images/default_tool_big.png:admin/images/default_tool_small.png";
439: }
440: String smallIconPath = iconPath;
441: if (iconPath.indexOf(VALUE_SEPARATOR) >= 0) {
442: smallIconPath = iconPath.substring(iconPath
443: .indexOf(VALUE_SEPARATOR) + 1);
444: iconPath = iconPath.substring(0, iconPath
445: .indexOf(VALUE_SEPARATOR));
446: }
447: setIconPath(iconPath);
448: setSmallIconPath(smallIconPath);
449:
450: String helpText = navElem.getDescription();
451: if (CmsStringUtil.isEmptyOrWhitespaceOnly(helpText)) {
452: helpText = "${key." + Messages.GUI_TOOLS_DEFAULT_HELP_0
453: + "}";
454: }
455: String disabledHelpText = DEFAULT_DISABLED_HELPTEXT;
456: if (helpText.indexOf(VALUE_SEPARATOR) >= 0) {
457: disabledHelpText = helpText.substring(helpText
458: .indexOf(VALUE_SEPARATOR) + 1);
459: helpText = helpText.substring(0, helpText
460: .indexOf(VALUE_SEPARATOR));
461: }
462: setHelpText(helpText);
463: setDisabledHelpText(disabledHelpText);
464:
465: String group = navElem.getInfo();
466: if (CmsStringUtil.isEmptyOrWhitespaceOnly(group)) {
467: group = "${key." + Messages.GUI_TOOLS_DEFAULT_GROUP_0 + "}";
468: }
469:
470: String path = resourcePath;
471: setLink(cms, resourcePath);
472: if (CmsResource.isFolder(path)) {
473: path = CmsToolManager.TOOLPATH_SEPARATOR
474: + resourcePath
475: .substring(
476: root.getUri().length(),
477: resourcePath
478: .lastIndexOf(CmsToolManager.TOOLPATH_SEPARATOR));
479: } else {
480: if (resourcePath.lastIndexOf('.') > -1) {
481: path = CmsToolManager.TOOLPATH_SEPARATOR
482: + resourcePath.substring(
483: root.getUri().length(), resourcePath
484: .lastIndexOf('.'));
485: } else {
486: path = CmsToolManager.TOOLPATH_SEPARATOR
487: + resourcePath
488: .substring(root.getUri().length());
489: }
490: }
491: // install point
492: setPath(path);
493: setGroup(group);
494: setPosition(navElem.getNavPosition());
495:
496: // parameters
497: setParameters(cms, resourcePath);
498:
499: return !path.equals(resourcePath);
500: }
501:
502: /**
503: * @see java.lang.Object#toString()
504: */
505: public String toString() {
506:
507: return m_path + " - " + m_group + " - " + m_position;
508: }
509:
510: /**
511: * Sets the link for the given resource.<p>
512: *
513: * Use the <code>resourcePath</code> as link if it is not a folder.
514: *
515: * If it is a folder, try to use the folder default file property value as link.
516: * if not use the {@link CmsToolManager#VIEW_JSPPAGE_LOCATION}.
517: *
518: * @param cms the cms context
519: * @param resourcePath the path to the resource to set the link for
520: */
521: protected void setLink(CmsObject cms, String resourcePath) {
522:
523: String link = resourcePath;
524: try {
525: // make sure the res is a folder
526: cms.readFolder(resourcePath);
527:
528: // adjust the path
529: if (resourcePath
530: .endsWith(CmsToolManager.TOOLPATH_SEPARATOR)) {
531: resourcePath = resourcePath
532: .substring(
533: 0,
534: resourcePath
535: .lastIndexOf(CmsToolManager.TOOLPATH_SEPARATOR));
536: }
537:
538: // set admin page as link
539: link = CmsToolManager.VIEW_JSPPAGE_LOCATION;
540:
541: // try to use the folder default file as link
542: CmsProperty prop = cms.readPropertyObject(resourcePath,
543: CmsPropertyDefinition.PROPERTY_DEFAULT_FILE, false);
544: String defFile = "index.jsp";
545: if (!prop.isNullProperty()) {
546: defFile = prop.getValue();
547: }
548: if (!defFile.startsWith(CmsToolManager.TOOLPATH_SEPARATOR)) {
549: // try to use this relative link
550: defFile = resourcePath
551: + CmsToolManager.TOOLPATH_SEPARATOR + defFile;
552: }
553: if (defFile.indexOf("?") > 0) {
554: if (cms.existsResource(defFile.substring(0, defFile
555: .indexOf("?")))) {
556: link = defFile;
557: }
558: } else if (cms.existsResource(defFile)) {
559: link = defFile;
560: }
561: } catch (CmsException e) {
562: // not a folder or no default file, ignore
563: }
564:
565: setLink(link);
566: }
567:
568: /**
569: * Sets the needed properties from the {@link #ARGS_PROPERTY_DEFINITION} property of the given resource.<p>
570: *
571: * @param cms the cms context
572: * @param resourcePath the path to the resource to read the property from
573: */
574: protected void setParameters(CmsObject cms, String resourcePath) {
575:
576: try {
577: CmsProperty prop = cms.readPropertyObject(resourcePath,
578: ARGS_PROPERTY_DEFINITION, false);
579: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(prop
580: .getValue())) {
581: Map argsMap = new HashMap();
582: Iterator itArgs = CmsStringUtil.splitAsList(
583: prop.getValue(), ARGUMENT_SEPARATOR).iterator();
584: while (itArgs.hasNext()) {
585: String arg = "";
586: try {
587: arg = (String) itArgs.next();
588: int pos = arg.indexOf(VALUE_SEPARATOR);
589: argsMap.put(arg.substring(0, pos), arg
590: .substring(pos + 1));
591: } catch (StringIndexOutOfBoundsException e) {
592: LOG.error("sep: " + VALUE_SEPARATOR + "arg: "
593: + arg);
594: throw e;
595: }
596: }
597: if (argsMap.get(ARG_PATH_NAME) != null) {
598: setPath((String) argsMap.get(ARG_PATH_NAME));
599: }
600: if (argsMap.get(ARG_CONFIRMATION_NAME) != null) {
601: setConfirmationMessage((String) argsMap
602: .get(ARG_CONFIRMATION_NAME));
603: }
604: if (argsMap.get(ARG_PARAM_NAME) != null) {
605: setParameterString((String) argsMap
606: .get(ARG_PARAM_NAME));
607: }
608: }
609: } catch (CmsException e) {
610: // should never happen
611: if (LOG.isErrorEnabled()) {
612: LOG.error(e.getLocalizedMessage(), e);
613: }
614: }
615: }
616: }
|