001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsEditorDisplayOptions.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.15 $
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.CmsResourceFilter;
037: import org.opencms.jsp.CmsJspActionElement;
038: import org.opencms.jsp.CmsJspNavBuilder;
039: import org.opencms.jsp.CmsJspNavElement;
040: import org.opencms.main.CmsException;
041: import org.opencms.main.CmsLog;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Properties;
049:
050: import org.apache.commons.collections.map.LRUMap;
051: import org.apache.commons.logging.Log;
052:
053: /**
054: * Provides methods to determine the display options of a workplace editor for the current user.<p>
055: *
056: * On the editor JSP, do the following:
057: * <ul>
058: * <li>get the object instance with <code>OpenCms.getWorkplaceManager().getEditorDisplayOptions()</code>.</li>
059: * <li>get the Properties for the current user with <code>getDisplayOptions(CmsJspActionElement)</code>.</li>
060: * <li>use <code>showElement(key, Properties)</code> to determine if an element is shown.</li>
061: * </ul>
062: *
063: * Define your editor display options in property files located in the VFS folder
064: * <code>/system/workplace/editors/configuration/</code>.<p>
065: *
066: * Set navigation position property values on the configuration files
067: * and use the permission system to determine which groups and users
068: * should use which configuration file.<p>
069: *
070: * The configuration with the most enabled options should be the first in navigation,
071: * followed by configurations with less enabled options, because
072: * the first file readable for the current user will be used for configuration.<p>
073: *
074: * If no configuration file can be found for the current user,
075: * all display options will be disabled by default.<p>
076: *
077: * @author Andreas Zahner
078: *
079: * @version $Revision: 1.15 $
080: *
081: * @since 6.0.0
082: */
083: public class CmsEditorDisplayOptions {
084:
085: /** The name of the configuration folder.<p> */
086: public static final String FOLDER_EDITORCONFIGURATION = CmsEditor.PATH_EDITORS
087: + "configuration/";
088:
089: /** Mapping entry name that is used if no mapping is available for the user.<p> */
090: public static final String NO_MAPPING_FOR_USER = "na";
091:
092: /** Maximum size of the stored editor configurations.<p> */
093: public static final int SIZE_CONFIGURATIONFILES = 12;
094:
095: /** Maximum size of the user editor configuration mappings.<p> */
096: public static final int SIZE_USERENTRIES = 100;
097:
098: /** The log object for this class. */
099: private static final Log LOG = CmsLog
100: .getLog(CmsEditorDisplayOptions.class);
101:
102: /** Stores all loaded editor configuration options.<p> */
103: private Map m_loadedConfigurations;
104:
105: /** Stores the mappings of users to their configuration options to use.<p> */
106: private Map m_userMappings;
107:
108: /**
109: * Constructor that initializes the editor display options for the workplace.<p>
110: */
111: public CmsEditorDisplayOptions() {
112:
113: // initialize members
114: m_userMappings = new LRUMap(SIZE_USERENTRIES);
115: m_loadedConfigurations = new LRUMap(SIZE_CONFIGURATIONFILES);
116: }
117:
118: /**
119: * Clears the cached user configuration data, casing a reload off all configurations.<p>
120: */
121: public synchronized void clearCache() {
122:
123: m_userMappings.clear();
124: m_loadedConfigurations.clear();
125: }
126:
127: /**
128: * Reads the editor configuration file valid for the current user and caches the result in a Map.<p>
129: *
130: * The configuration settings of the found file are stored in a Map holding the loaded configuration
131: * with the configuration file name as key.<p>
132: *
133: * The configuration file name to use for the current user is stored in another Map with the user name
134: * as key.<p>
135: *
136: * @param jsp the JSP action element to access the VFS and current user information
137: * @return the display options to use for the current user or null if no display options were found
138: */
139: public Properties getDisplayOptions(CmsJspActionElement jsp) {
140:
141: return getDisplayOptions(jsp.getCmsObject());
142: }
143:
144: /**
145: * Reads the editor configuration file valid for the current user and caches the result in a Map.<p>
146: *
147: * The configuration settings of the found file are stored in a Map holding the loaded configuration
148: * with the configuration file name as key.<p>
149: *
150: * The configuration file name to use for the current user is stored in another Map with the user name
151: * as key.<p>
152: *
153: * @param cms the CmsObject to access the VFS and current user information
154: * @return the display options to use for the current user or null if no display options were found
155: */
156: public Properties getDisplayOptions(CmsObject cms) {
157:
158: // get the configuration file name for the current user
159: String mappedConfigFile = (String) m_userMappings.get(cms
160: .getRequestContext().currentUser().getName());
161: Properties displayOptions;
162: if (mappedConfigFile == null) {
163: // no configuration file name stored for user, get the navigation items of the configuration folder
164: List items = CmsJspNavBuilder.getNavigationForFolder(cms,
165: FOLDER_EDITORCONFIGURATION);
166: if (items.size() > 0) {
167: // get first found configuration file
168: CmsJspNavElement nav = (CmsJspNavElement) items.get(0);
169: mappedConfigFile = nav.getFileName();
170: synchronized (m_loadedConfigurations) {
171: // must sync read/write access to shared map
172: displayOptions = (Properties) m_loadedConfigurations
173: .get(nav.getFileName());
174: if (displayOptions == null) {
175: // configuration file has not yet been loaded, load it
176: try {
177: // read configuration file
178: CmsFile optionFile = cms
179: .readFile(
180: nav.getResourceName(),
181: CmsResourceFilter.IGNORE_EXPIRATION);
182: InputStream in = new ByteArrayInputStream(
183: optionFile.getContents());
184: displayOptions = new Properties();
185: displayOptions.load(in);
186: // store loaded options
187: m_loadedConfigurations.put(nav
188: .getFileName(), displayOptions);
189: } catch (CmsException e) {
190: // set configuration to not available
191: if (LOG.isInfoEnabled()) {
192: LOG.info(e);
193: }
194: mappedConfigFile = NO_MAPPING_FOR_USER;
195: } catch (IOException e) {
196: // set configuration to not available
197: if (LOG.isInfoEnabled()) {
198: LOG.info(e);
199: }
200: mappedConfigFile = NO_MAPPING_FOR_USER;
201: displayOptions = null;
202: }
203: }
204: }
205: } else {
206: // no configuration available for current user, store this in mapping
207: mappedConfigFile = NO_MAPPING_FOR_USER;
208: displayOptions = null;
209: }
210: if (LOG.isDebugEnabled()) {
211: // check which mapping has been stored
212: LOG.debug(Messages.get().getBundle()
213: .key(
214: Messages.LOG_MAP_CONFIG_FILE_TO_USER_2,
215: mappedConfigFile,
216: cms.getRequestContext().currentUser()
217: .getName()));
218: }
219: // store the file name of the configuration file for the current user
220: m_userMappings.put(cms.getRequestContext().currentUser()
221: .getName(), mappedConfigFile);
222: } else {
223: // configuration file for current user is known, get options from loaded configurations
224: displayOptions = (Properties) m_loadedConfigurations
225: .get(mappedConfigFile);
226: }
227: // return the editor display options for this user
228: return displayOptions;
229: }
230:
231: /**
232: * Determines if the given element should be shown in the editor.<p>
233: *
234: * @param key the element key name which should be displayed
235: * @param displayOptions the display options for the current user
236: * @return true if the element should be shown, otherwise false
237: */
238: public boolean showElement(String key, Properties displayOptions) {
239:
240: return ((displayOptions != null) && Boolean.valueOf(
241: displayOptions.getProperty(key)).booleanValue());
242: }
243: }
|