001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.content;
019:
020: import java.io.IOException;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import de.finix.contelligent.CallData;
027: import de.finix.contelligent.Component;
028: import de.finix.contelligent.ComponentContext;
029: import de.finix.contelligent.category.CategoryCombinationNotSupportedException;
030: import de.finix.contelligent.category.CategoryManager;
031: import de.finix.contelligent.exception.ComponentPersistenceException;
032: import de.finix.contelligent.logging.LoggingService;
033: import de.finix.contelligent.resource.Resource;
034:
035: public abstract class ContelligentContent implements Content {
036: final static org.apache.log4j.Logger log = LoggingService
037: .getLogger(ContelligentContent.class);
038:
039: private Collection sensitiveCategories;
040:
041: private Map resourceMap = null;
042:
043: final private Component component;
044:
045: final private CategoryManager categoryManager;
046:
047: public ContelligentContent(Component component) throws Exception {
048: this .component = component;
049: this .categoryManager = component.getComponentContext()
050: .getSystem().getCategoryManager();
051: }
052:
053: protected Component getComponent() {
054: return component;
055: }
056:
057: protected CategoryManager getCategoryManager() {
058: return categoryManager;
059: }
060:
061: public void setSensitiveCategories(Collection sensitiveCategories) {
062: this .sensitiveCategories = sensitiveCategories;
063: }
064:
065: public Collection getSensitiveCategories() {
066: if (resourceMap == null) {
067: try {
068: loadResources(component);
069: } catch (ComponentPersistenceException cpe) {
070: log.error("Unable to load resources.", cpe);
071: }
072: }
073: return sensitiveCategories;
074: }
075:
076: public long lastModified(CallData callData) throws IOException,
077: ComponentPersistenceException,
078: CategoryCombinationNotSupportedException {
079: return -1;
080: }
081:
082: /**
083: * Answer the content map. The answer is the map used internally by this
084: * instance, it is no copy.
085: *
086: * @return internal content map
087: */
088: protected Map getResourceMap() {
089: if (resourceMap == null) {
090: try {
091: loadResources(component);
092: } catch (ComponentPersistenceException cpe) {
093: log.error("Unable to load resources.", cpe);
094: }
095: }
096: return resourceMap;
097: }
098:
099: protected void setResourceMap(Map resourceMap) {
100: this .resourceMap = resourceMap;
101: }
102:
103: protected Resource getResource(String key)
104: throws ComponentPersistenceException {
105: return (Resource) getResourceMap().get(key);
106: }
107:
108: protected void loadResources(Component component)
109: throws ComponentPersistenceException {
110: Map resources = new HashMap();
111: ComponentContext context = component.getComponentContext();
112: Collection sensitiveCategories = context
113: .getSensitiveContentCategories();
114: setSensitiveCategories(sensitiveCategories);
115: Iterator iterator = context.getContentResourceIdentifiers()
116: .iterator();
117: while (iterator.hasNext()) {
118: String s = (String) iterator.next();
119: resources.put(s, context.getContentResource(s));
120: }
121: categoryManager.completeCategoryToResourceMapping(
122: sensitiveCategories, resources);
123: setResourceMap(resources);
124: }
125:
126: protected Resource getResource(CallData callData)
127: throws IOException, ComponentPersistenceException {
128: Map categoryMap = callData.getCategoryMap();
129: String identifier = categoryManager
130: .createUniqueCategoryIdentifier(
131: getSensitiveCategories(), categoryMap);
132: Resource answer = getResource(identifier);
133: if (answer == null) {
134: throw new IOException("Resource not found for identifier '"
135: + identifier + "'");
136: }
137: return answer;
138: }
139:
140: protected String getResourceIdentifier(CallData callData) {
141: Map categoryMap = callData.getCategoryMap();
142: return categoryManager.createUniqueCategoryIdentifier(
143: getSensitiveCategories(), categoryMap);
144: }
145:
146: protected void setResource(Resource resource, CallData callData)
147: throws ComponentPersistenceException {
148: String identifier = categoryManager
149: .createUniqueCategoryIdentifier(
150: getSensitiveCategories(), callData
151: .getCategoryMap());
152: getComponent().getComponentContext().setContentResource(
153: identifier, resource);
154: loadResources(getComponent());
155: }
156: }
|