001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsWorkplaceEditorManager.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.13 $
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.db.CmsUserSettings;
035: import org.opencms.file.CmsFile;
036: import org.opencms.file.CmsFolder;
037: import org.opencms.file.CmsObject;
038: import org.opencms.file.CmsRequestContext;
039: import org.opencms.file.CmsResourceFilter;
040: import org.opencms.file.types.CmsResourceTypeXmlPage;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.CmsLog;
043: import org.opencms.main.OpenCms;
044: import org.opencms.util.CmsStringUtil;
045: import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
046:
047: import java.util.ArrayList;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.SortedMap;
053: import java.util.TreeMap;
054:
055: import org.apache.commons.logging.Log;
056:
057: /**
058: * The editor manager stores information about all available configured editors in OpenCms.<p>
059: *
060: * This class provides methods and constants to select the right editor according to:
061: * <ul>
062: * <li>the user preferences</li>
063: * <li>the users current browser</li>
064: * <li>the resource type</li>
065: * <li>the editor rankings</li>
066: * </ul>
067: * <p>
068: *
069: * @author Andreas Zahner
070: *
071: * @version $Revision: 1.13 $
072: *
073: * @since 6.0.0
074: */
075: public class CmsWorkplaceEditorManager {
076:
077: /** The filename of the editor configuration XML file. */
078: public static final String EDITOR_CONFIGURATION_FILENAME = "editor_configuration.xml";
079:
080: /** The filename of the editor JSP. */
081: public static final String EDITOR_FILENAME = "editor.jsp";
082:
083: /** The log object for this class. */
084: private static final Log LOG = CmsLog
085: .getLog(CmsWorkplaceEditorManager.class);
086:
087: private List m_editorConfigurations;
088: private Map m_preferredEditors;
089:
090: /**
091: * Creates a new editor manager.<p>
092: *
093: * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
094: */
095: public CmsWorkplaceEditorManager(CmsObject cms) {
096:
097: // get all subfolders of the workplace editor folder
098: List editorFolders;
099: try {
100: editorFolders = cms.getSubFolders(CmsEditor.PATH_EDITORS);
101: } catch (CmsException e) {
102: LOG.error(Messages.get().getBundle().key(
103: Messages.LOG_READ_EDITIR_FOLDER_FAILED_1,
104: CmsEditor.PATH_EDITORS));
105: // can not throw exception here since then OpenCms would not even start in shell mode (runlevel 2)
106: editorFolders = new ArrayList();
107: }
108:
109: m_editorConfigurations = new ArrayList(editorFolders.size());
110:
111: // try to read the configuration files and create configuration objects for valid configurations
112: Iterator i = editorFolders.iterator();
113: while (i.hasNext()) {
114: CmsFolder currentFolder = (CmsFolder) i.next();
115: String folderName = CmsEditor.PATH_EDITORS
116: + currentFolder.getName();
117: if (!folderName.endsWith("/")) {
118: folderName += "/";
119: }
120: CmsFile configFile = null;
121: try {
122: configFile = cms.readFile(folderName
123: + EDITOR_CONFIGURATION_FILENAME,
124: CmsResourceFilter.IGNORE_EXPIRATION);
125: } catch (CmsException e) {
126: // no configuration file present, ignore this folder
127: if (LOG.isInfoEnabled()) {
128: LOG.info(e);
129: }
130: continue;
131: }
132: // get the file contents
133: byte[] xmlData = configFile.getContents();
134: CmsWorkplaceEditorConfiguration editorConfig = new CmsWorkplaceEditorConfiguration(
135: xmlData, folderName + EDITOR_FILENAME);
136: if (editorConfig.isValidConfiguration()) {
137: m_editorConfigurations.add(editorConfig);
138: }
139: }
140: m_preferredEditors = new HashMap(m_editorConfigurations.size());
141: }
142:
143: /**
144: * Returns a map of configurable editors for the workplace preferences dialog.<p>
145: *
146: * This map has the resource type name as key, the value is a sorted map with
147: * the ranking as key and a CmsWorkplaceEditorConfiguration object as value.<p>
148: *
149: * @return configurable editors for the workplace preferences dialog
150: */
151: public Map getConfigurableEditors() {
152:
153: Map configurableEditors = new HashMap();
154: Iterator i = m_editorConfigurations.iterator();
155: while (i.hasNext()) {
156: CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration) i
157: .next();
158: // get all resource types specified for the current editor configuration
159: Iterator k = currentConfig.getResourceTypes().keySet()
160: .iterator();
161: while (k.hasNext()) {
162: // key is the current resource type of the configuration
163: String key = (String) k.next();
164:
165: // check if the current resource type is only a reference to another resource type
166: CmsExplorerTypeSettings settings = OpenCms
167: .getWorkplaceManager().getExplorerTypeSetting(
168: key);
169: if (settings == null
170: || CmsStringUtil.isNotEmpty(settings
171: .getReference())) {
172: // skip this resource type
173: continue;
174: }
175:
176: if (currentConfig.getMappingForResourceType(key) == null) {
177: // editor is configurable for specified resource type
178: SortedMap editorConfigs = (SortedMap) configurableEditors
179: .get(key);
180: if (editorConfigs == null) {
181: // no configuration map present for resource type, create one
182: editorConfigs = new TreeMap();
183: }
184: // put the current editor configuration to the resource map with ranking value as key
185: editorConfigs.put(new Float(currentConfig
186: .getRankingForResourceType(key)),
187: currentConfig);
188: // put the resource map to the result map with resource type as key
189: configurableEditors.put(key, editorConfigs);
190: }
191: }
192: }
193: return configurableEditors;
194: }
195:
196: /**
197: * Returns the editor URI for the current resource type.<p>
198: *
199: * @param context the request context
200: * @param userAgent the user agent String that identifies the browser
201: * @return a valid editor URI for the resource type or null, if no editor matches
202: */
203: public String getWidgetEditor(CmsRequestContext context,
204: String userAgent) {
205:
206: // step 1: check if the user specified a preferred editor for the resource type xmlpage
207: CmsUserSettings settings = new CmsUserSettings(context
208: .currentUser());
209: String resourceType = CmsResourceTypeXmlPage
210: .getStaticTypeName();
211: String preferredEditorSetting = settings
212: .getPreferredEditor(resourceType);
213: if (preferredEditorSetting == null) {
214: // no preferred editor setting found for this resource type, look for mapped resource type preferred editor
215: Iterator i = m_editorConfigurations.iterator();
216: while (i.hasNext()) {
217: CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration) i
218: .next();
219: String mapping = currentConfig
220: .getMappingForResourceType(resourceType);
221: if (mapping != null) {
222: preferredEditorSetting = settings
223: .getPreferredEditor(mapping);
224: }
225: if (preferredEditorSetting != null) {
226: break;
227: }
228: }
229: }
230: if (preferredEditorSetting != null) {
231: CmsWorkplaceEditorConfiguration preferredConf = filterPreferredEditor(preferredEditorSetting);
232: if ((preferredConf != null)
233: && preferredConf.isWidgetEditor()
234: && preferredConf.matchesBrowser(userAgent)) {
235: // return preferred editor only if it matches the current users browser
236: return preferredConf.getWidgetEditor();
237: }
238: }
239:
240: // step 2: filter editors for the given resoure type
241: SortedMap filteredEditors = filterEditorsForResourceType(resourceType);
242:
243: // step 3: check if one of the editors matches the current users browser
244: while (filteredEditors.size() > 0) {
245: // check editor configuration with highest ranking
246: Float key = (Float) filteredEditors.lastKey();
247: CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration) filteredEditors
248: .get(key);
249: if (conf.isWidgetEditor() && conf.matchesBrowser(userAgent)) {
250: return conf.getWidgetEditor();
251: }
252: filteredEditors.remove(key);
253: }
254:
255: // no valid editor found
256: return null;
257: }
258:
259: /**
260: * Returns the default editor URI for the current resource type.<p>
261: *
262: * @param context the request context
263: * @param resourceType the current resource type
264: * @param userAgent the user agent String that identifies the browser
265: * @return a valid default editor URI for the resource type or null, if no editor matches
266: */
267: protected String getDefaultEditorUri(CmsRequestContext context,
268: String resourceType, String userAgent) {
269:
270: SortedMap filteredEditors = filterEditorsForResourceType(resourceType);
271: while (filteredEditors.size() > 0) {
272: // get the configuration with the lowest key value from the map
273: Float key = (Float) filteredEditors.firstKey();
274: CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration) filteredEditors
275: .get(key);
276: // match the found configuration with the current users browser
277: if (conf.matchesBrowser(userAgent)) {
278: return conf.getEditorUri();
279: }
280: filteredEditors.remove(key);
281: }
282: if (context == null) {
283: // this is just so that all parameters are used, signature should be identical to getEditorUri(...)
284: return null;
285: }
286: // no valid default editor found
287: return null;
288: }
289:
290: /**
291: * Returns the editor configuration objects.<p>
292: *
293: * @return the editor configuration objects
294: */
295: protected List getEditorConfigurations() {
296:
297: return m_editorConfigurations;
298: }
299:
300: /**
301: * Returns the editor URI for the current resource type.<p>
302: *
303: * @param context the request context
304: * @param resourceType the current resource type
305: * @param userAgent the user agent String that identifies the browser
306: * @return a valid editor URI for the resource type or null, if no editor matches
307: */
308: protected String getEditorUri(CmsRequestContext context,
309: String resourceType, String userAgent) {
310:
311: // step 1: check if the user specified a preferred editor for the given resource type
312: CmsUserSettings settings = new CmsUserSettings(context
313: .currentUser());
314: String preferredEditorSetting = settings
315: .getPreferredEditor(resourceType);
316: if (preferredEditorSetting == null) {
317: // no preferred editor setting found for this resource type, look for mapped resource type preferred editor
318: Iterator i = m_editorConfigurations.iterator();
319: while (i.hasNext()) {
320: CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration) i
321: .next();
322: String mapping = currentConfig
323: .getMappingForResourceType(resourceType);
324: if (mapping != null) {
325: preferredEditorSetting = settings
326: .getPreferredEditor(mapping);
327: }
328: if (preferredEditorSetting != null) {
329: break;
330: }
331: }
332: }
333: if (preferredEditorSetting != null) {
334: CmsWorkplaceEditorConfiguration preferredConf = filterPreferredEditor(preferredEditorSetting);
335: if ((preferredConf != null)
336: && preferredConf.matchesBrowser(userAgent)) {
337: // return preferred editor only if it matches the current users browser
338: return preferredConf.getEditorUri();
339: }
340: }
341:
342: // step 2: filter editors for the given resoure type
343: SortedMap filteredEditors = filterEditorsForResourceType(resourceType);
344:
345: // step 3: check if one of the editors matches the current users browser
346: while (filteredEditors.size() > 0) {
347: // check editor configuration with highest ranking
348: Float key = (Float) filteredEditors.lastKey();
349: CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration) filteredEditors
350: .get(key);
351: if (conf.matchesBrowser(userAgent)) {
352: return conf.getEditorUri();
353: }
354: filteredEditors.remove(key);
355: }
356:
357: // no valid editor found
358: return null;
359: }
360:
361: /**
362: * Filters the matching editors for the given resource type from the list of all available editors.<p>
363: *
364: * @param resourceType the resource type to filter
365: * @return a map of filtered editor configurations sorted asceding by the ranking for the current resource type, with the (Float) ranking as key
366: */
367: private SortedMap filterEditorsForResourceType(String resourceType) {
368:
369: SortedMap filteredEditors = new TreeMap();
370: Iterator i = m_editorConfigurations.iterator();
371: while (i.hasNext()) {
372: CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration) i
373: .next();
374: if (currentConfig.matchesResourceType(resourceType)) {
375: float key = currentConfig
376: .getRankingForResourceType(resourceType);
377: if (key >= 0) {
378: filteredEditors.put(new Float(key), currentConfig);
379: }
380: }
381: }
382: return filteredEditors;
383: }
384:
385: /**
386: * Filters the preferred editor from the list of all available editors.<p>
387: *
388: * @param preferredEditor the preferred editor identification String
389: * @return the preferred editor configuration object or null, if none is found
390: */
391: private CmsWorkplaceEditorConfiguration filterPreferredEditor(
392: String preferredEditor) {
393:
394: if (m_preferredEditors.size() == 0) {
395: Iterator i = m_editorConfigurations.iterator();
396: while (i.hasNext()) {
397: CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration) i
398: .next();
399: m_preferredEditors.put(currentConfig.getEditorUri(),
400: currentConfig);
401: }
402: }
403: return (CmsWorkplaceEditorConfiguration) m_preferredEditors
404: .get(preferredEditor);
405: }
406:
407: }
|