001: /*
002: * CollectionStyleSelection.java
003: *
004: * Version: $Revision: 1 $
005: *
006: * Date: $Date: 2007-10-25 09:00:00 +0100 (thu, 25 oct 2007) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.app.webui.util;
041:
042: import java.sql.SQLException;
043: import java.util.Enumeration;
044: import java.util.HashMap;
045:
046: import org.apache.log4j.Logger;
047: import org.dspace.content.Collection;
048: import org.dspace.content.Item;
049: import org.dspace.core.ConfigurationManager;
050:
051: /**
052: * This is the standard (until 1.4.x) configuration mode based on owning collection handle
053: * Style name is case insensitive.
054: *
055: * @author Andrea Bollini
056: * @version $Revision: 1 $
057: *
058: */
059: public class CollectionStyleSelection extends AKeyBasedStyleSelection {
060: /** Hashmap of collection Handles to styles to use, from dspace.cfg */
061: private static java.util.Map<String, String> styles;
062:
063: /** log4j logger */
064: private static Logger log = Logger
065: .getLogger(CollectionStyleSelection.class);
066:
067: /**
068: * Get the style using the owning collection handle
069: */
070: public String getStyleForItem(Item item) throws SQLException {
071: Collection c = item.getOwningCollection();
072:
073: if (c != null) {
074: // Style specified & exists
075: return getFromMap(c.getHandle());
076: } else
077: return "default"; //no specific style - item is an in progress Submission
078: }
079:
080: /**
081: * Put collection handle/style name mapping in an in-memory map.
082: */
083: private void readKeyStyleConfig() {
084: styles = new HashMap();
085:
086: Enumeration e = ConfigurationManager.propertyNames();
087:
088: while (e.hasMoreElements()) {
089: String key = (String) e.nextElement();
090:
091: if (key.startsWith("webui.itemdisplay.")
092: && key.endsWith(".collections")) {
093: String styleName = key.substring("webui.itemdisplay."
094: .length(), key.length()
095: - ".collections".length());
096:
097: String[] collections = ConfigurationManager
098: .getProperty(key).split(",");
099:
100: for (int i = 0; i < collections.length; i++) {
101: styles.put(collections[i].trim(), styleName
102: .toLowerCase());
103: }
104: }
105: }
106: }
107:
108: /**
109: * Get the style associated with the handle from the in-memory map. If the map is not already
110: * initialized read it from dspace.cfg
111: * Check for the style configuration: return the default style if no configuration has found.
112: *
113: * @param handle
114: * @return the specific style or the default if not properly defined
115: */
116: public String getFromMap(String handle) {
117: if (styles == null) {
118: readKeyStyleConfig();
119: }
120:
121: String styleName = (String) styles.get(handle);
122:
123: if (styleName == null) {
124: // No specific style specified for this collection
125: return "default";
126: }
127:
128: // Specific style specified. Check style exists
129: if (isConfigurationDefinedForStyle(styleName)) {
130: log
131: .warn("dspace.cfg specifies undefined item display style '"
132: + styleName
133: + "' for collection handle "
134: + handle + ". Using default");
135: return "default";
136: }
137:
138: return styleName;
139: }
140: }
|