001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.list;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.riotfamily.common.xml.ConfigurationEventListener;
033: import org.riotfamily.riot.list.command.Command;
034: import org.riotfamily.riot.list.ui.render.CellRenderer;
035: import org.riotfamily.riot.list.ui.render.ObjectRenderer;
036: import org.springframework.context.ApplicationContext;
037: import org.springframework.context.ApplicationContextAware;
038:
039: /**
040: *
041: */
042: public class ListRepository implements ApplicationContextAware {
043:
044: private Log log = LogFactory.getLog(ListRepository.class);
045:
046: private HashMap listConfigs = new HashMap();
047:
048: private HashMap listConfigsByClass = new HashMap();
049:
050: private HashMap commands = new HashMap();
051:
052: private ApplicationContext applicationContext;
053:
054: private CellRenderer defaultCellRenderer;
055:
056: private int defaultPageSize = 50;
057:
058: public ListRepository() {
059: setDefaultCellRenderer(new ObjectRenderer());
060: }
061:
062: public void setApplicationContext(
063: ApplicationContext applicationContext) {
064: this .applicationContext = applicationContext;
065: log.debug("Looking up command implementations ...");
066: Map commands = applicationContext.getBeansOfType(Command.class);
067: Iterator it = commands.values().iterator();
068: while (it.hasNext()) {
069: Command command = (Command) it.next();
070: this .commands.put(command.getId(), command);
071: }
072: }
073:
074: public void setRiotDaoService(RiotDaoService riotDaoService) {
075: riotDaoService.setListRepository(this );
076: }
077:
078: /**
079: * @return Returns the applicationContext.
080: */
081: protected ApplicationContext getApplicationContext() {
082: return applicationContext;
083: }
084:
085: public void addListener(ConfigurationEventListener listener) {
086: }
087:
088: public Command getCommand(String commandId) {
089: Command command = (Command) commands.get(commandId);
090: if (command == null) {
091: log.error("No such command: " + commandId);
092: }
093: return command;
094: }
095:
096: public ListConfig getListConfig(String listId) {
097: return (ListConfig) listConfigs.get(listId);
098: }
099:
100: public ListConfig getListConfig(Class entityClass) {
101: return (ListConfig) listConfigsByClass.get(entityClass);
102: }
103:
104: public void addListConfig(ListConfig listConfig) {
105: listConfigs.put(listConfig.getId(), listConfig);
106: listConfigsByClass.put(listConfig.getDao().getEntityClass(),
107: listConfig);
108: if (listConfig.getPageSize() == 0) {
109: listConfig.setPageSize(defaultPageSize);
110: }
111: }
112:
113: protected Map getListConfigsByClass() {
114: return this .listConfigsByClass;
115: }
116:
117: protected Map getListConfigs() {
118: return listConfigs;
119: }
120:
121: public CellRenderer getDefaultCellRenderer() {
122: return defaultCellRenderer;
123: }
124:
125: public void setDefaultCellRenderer(CellRenderer defaultCellRenderer) {
126: this .defaultCellRenderer = defaultCellRenderer;
127: }
128:
129: public int getDefaultPageSize() {
130: return defaultPageSize;
131: }
132:
133: public void setDefaultPageSize(int defaultPageSize) {
134: this.defaultPageSize = defaultPageSize;
135: }
136:
137: }
|