001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/common/api-impl/src/java/org/theospi/portfolio/list/service/ListServiceImpl.java $
003: * $Id: ListServiceImpl.java 21798 2007-02-21 16:27:08Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.list.service;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.metaobj.security.AuthenticationManager;
032: import org.sakaiproject.metaobj.shared.mgt.IdManager;
033: import org.sakaiproject.metaobj.shared.model.Agent;
034: import org.sakaiproject.metaobj.shared.model.Id;
035: import org.sakaiproject.tool.api.Placement;
036: import org.sakaiproject.tool.api.ToolManager;
037: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
038: import org.theospi.portfolio.list.intf.CustomLinkListGenerator;
039: import org.theospi.portfolio.list.intf.ListGenerator;
040: import org.theospi.portfolio.list.intf.ListService;
041: import org.theospi.portfolio.list.model.Column;
042: import org.theospi.portfolio.list.model.ListConfig;
043:
044: public class ListServiceImpl extends HibernateDaoSupport implements
045: ListService {
046: protected final transient Log logger = LogFactory
047: .getLog(getClass());
048: public final static String LIST_GEN_ID_TAG = "listGenId";
049: public final static String SITE_TYPE_LIST_TAG = "siteTypeList";
050:
051: private Map listGenerators;
052: private IdManager idManager;
053: private AuthenticationManager authnManager;
054: private ToolManager toolManager;
055:
056: public List getCurrentDisplayColumns() {
057: return getCurrentGenerator().getColumns();
058: }
059:
060: public List getSortableColumns() {
061: return ((CustomLinkListGenerator) getCurrentGenerator())
062: .getSortableColumns();
063: }
064:
065: public List getBundleLookupColumns() {
066: return ((CustomLinkListGenerator) getCurrentGenerator())
067: .getBundleLookupColumns();
068: }
069:
070: public String getEntryLink(Object entry) {
071: if (getCurrentGenerator() instanceof CustomLinkListGenerator) {
072: String uri = ((CustomLinkListGenerator) getCurrentGenerator())
073: .getCustomLink(entry);
074: if (uri != null) {
075: return uri;
076: }
077: }
078:
079: return null;
080: }
081:
082: public String getDefaultSortColumn() {
083: return getCurrentGenerator().getDefaultSortColumn();
084: }
085:
086: public List getList() {
087: return getCurrentGenerator().getObjects();
088: }
089:
090: protected Placement getCurrentTool() {
091: return getToolManager().getCurrentPlacement();
092: }
093:
094: protected ListGenerator getCurrentGenerator() {
095: Placement current = getCurrentTool();
096:
097: String generatorName = current.getPlacementConfig()
098: .getProperty(LIST_GEN_ID_TAG);
099:
100: if (generatorName == null) {
101: generatorName = current.getTool().getMutableConfig()
102: .getProperty(LIST_GEN_ID_TAG);
103: }
104:
105: return getListGenerator(generatorName);
106: }
107:
108: public ListGenerator getListGenerator(String generatorName) {
109: return (ListGenerator) getListGenerators().get(generatorName);
110: }
111:
112: public ListConfig getCurrentConfig() {
113: ListGenerator listGen = getCurrentGenerator();
114: ListConfig currentConfig = loadCurrentConfig();
115:
116: if (currentConfig == null) {
117: currentConfig = initConfig(listGen);
118: }
119:
120: List columns = new ArrayList();
121: List columnStringList = listGen.getColumns();
122: List selected = currentConfig.getSelectedColumns();
123:
124: for (Iterator i = columnStringList.iterator(); i.hasNext();) {
125: String name = (String) i.next();
126: Column column = new Column(name, selected.contains(name));
127: columns.add(column);
128: }
129:
130: currentConfig.setColumns(columns);
131: return currentConfig;
132: }
133:
134: private ListConfig initConfig(ListGenerator listGen) {
135: ListConfig currentConfig = new ListConfig();
136: currentConfig.setSelectedColumns(listGen.getDefaultColumns());
137: currentConfig.setTitle(getCurrentTool().getTitle());
138: currentConfig.setToolId(getIdManager().getId(
139: getCurrentTool().getId()));
140: currentConfig.setOwner(getAuthnManager().getAgent());
141:
142: return currentConfig;
143: }
144:
145: public void saveOptions(ListConfig currentConfig) {
146: List newSelected = new ArrayList();
147:
148: for (Iterator i = currentConfig.getColumns().iterator(); i
149: .hasNext();) {
150: Column col = (Column) i.next();
151: if (col.isSelected()) {
152: newSelected.add(col.getName());
153: }
154: }
155:
156: currentConfig.setSelectedColumns(newSelected);
157: getHibernateTemplate().saveOrUpdate(currentConfig);
158: }
159:
160: public boolean isNewWindow(Object entry) {
161: return getCurrentGenerator().isNewWindow(entry);
162: }
163:
164: protected ListConfig loadCurrentConfig() {
165: Agent currentAgent = getAuthnManager().getAgent();
166: Id toolId = getIdManager().getId(getCurrentTool().getId());
167:
168: Collection configs = getHibernateTemplate().findByNamedQuery(
169: "loadCurrentConfig",
170: new Object[] { currentAgent, toolId });
171:
172: if (configs.size() >= 1) {
173: return (ListConfig) configs.iterator().next();
174: } else {
175: return null;
176: }
177: }
178:
179: public void register(String id, ListGenerator listGenerator) {
180: listGenerators.put(id, listGenerator);
181: }
182:
183: public List getSiteTypeList() {
184: Placement current = getCurrentTool();
185:
186: String types = current.getPlacementConfig().getProperty(
187: SITE_TYPE_LIST_TAG);
188: if (types != null && types.length() > 0) {
189: String siteTypes[] = types.split(",");
190: return Arrays.asList(siteTypes);
191: }
192: return new ArrayList();
193: }
194:
195: public Map getListGenerators() {
196: return listGenerators;
197: }
198:
199: public void setListGenerators(Map listGenerators) {
200: this .listGenerators = listGenerators;
201: }
202:
203: public AuthenticationManager getAuthnManager() {
204: return authnManager;
205: }
206:
207: public void setAuthnManager(AuthenticationManager authnManager) {
208: this .authnManager = authnManager;
209: }
210:
211: public IdManager getIdManager() {
212: return idManager;
213: }
214:
215: public void setIdManager(IdManager idManager) {
216: this .idManager = idManager;
217: }
218:
219: public ToolManager getToolManager() {
220: return toolManager;
221: }
222:
223: public void setToolManager(ToolManager toolManager) {
224: this.toolManager = toolManager;
225: }
226:
227: }
|