001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/ListPresentationController.java $
003: * $Id:ListPresentationController.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 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.presentation.control;
021:
022: import java.util.ArrayList;
023: import java.util.Hashtable;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.component.api.ServerConfigurationService;
030: import org.sakaiproject.entity.api.ResourceProperties;
031: import org.sakaiproject.exception.IdUnusedException;
032: import org.sakaiproject.exception.IdUsedException;
033: import org.sakaiproject.exception.InUseException;
034: import org.sakaiproject.exception.PermissionException;
035: import org.sakaiproject.metaobj.shared.model.Agent;
036: import org.sakaiproject.metaobj.utils.mvc.intf.ListScrollIndexer;
037: import org.sakaiproject.tool.cover.ToolManager;
038: import org.sakaiproject.tool.cover.SessionManager;
039: import org.sakaiproject.tool.api.Tool;
040: import org.sakaiproject.user.api.Preferences;
041: import org.sakaiproject.user.api.PreferencesEdit;
042: import org.sakaiproject.user.cover.PreferencesService;
043: import org.springframework.validation.Errors;
044: import org.springframework.web.servlet.ModelAndView;
045:
046: public class ListPresentationController extends
047: AbstractPresentationController {
048:
049: protected final Log logger = LogFactory.getLog(getClass());
050: private ListScrollIndexer listScrollIndexer;
051: private ServerConfigurationService serverConfigurationService;
052:
053: private final static String HIDDEN_PRES_PLACEMENT_PREF = "org.theospi.portfolio.presentation.placement.";
054: private final static String HIDDEN_PRES_PREF = "org.theospi.portfolio.presentation.hidden.";
055:
056: public ModelAndView handleRequest(Object requestModel, Map request,
057: Map session, Map application, Errors errors) {
058: Hashtable<String, Object> model = new Hashtable<String, Object>();
059: Agent currentAgent = getAuthManager().getAgent();
060: String currentToolId = ToolManager.getCurrentPlacement()
061: .getId();
062: String worksiteId = getWorksiteManager().getCurrentWorksiteId()
063: .getValue();
064:
065: boolean showHidden = getUserPresHiddenProperty();
066: String showHiddenKey = (String) request.get("showHiddenKey");
067: if (showHiddenKey != null)
068: showHidden = setUserPresHiddenProperty(showHiddenKey);
069:
070: List presentations = new ArrayList(getPresentationManager()
071: .findPresentationsByViewer(currentAgent, currentToolId,
072: showHidden));
073:
074: model.put("presentations", getListScrollIndexer().indexList(
075: request, model, presentations));
076:
077: String baseUrl = getServerConfigurationService().getServerUrl();
078:
079: String url = baseUrl
080: + "/osp-presentation-tool/viewPresentation.osp?";
081: url += Tool.PLACEMENT_ID
082: + "="
083: + SessionManager.getCurrentToolSession()
084: .getPlacementId();
085: model.put("baseUrl", url);
086: model.put("worksite", getWorksiteManager().getSite(worksiteId));
087: model.put("tool", getWorksiteManager().getTool(currentToolId));
088: model.put("isMaintainer", isMaintainer());
089: model.put("osp_agent", currentAgent);
090: model.put("showHidden", showHidden);
091: return new ModelAndView("success", model);
092: }
093:
094: /**
095: *
096: * @return
097: */
098: protected boolean getUserPresHiddenProperty() {
099: boolean prop = true;
100:
101: try {
102: Preferences userPreferences = PreferencesService
103: .getPreferences(getAuthManager().getAgent().getId()
104: .getValue());
105: ResourceProperties evalPrefs = userPreferences
106: .getProperties(HIDDEN_PRES_PLACEMENT_PREF
107: + ToolManager.getCurrentPlacement().getId());
108: String tmpProp = evalPrefs.getProperty(HIDDEN_PRES_PREF);
109: if (tmpProp != null)
110: prop = Boolean.getBoolean(tmpProp);
111: } catch (Exception e) {
112: logger
113: .debug("Couldn't get user prefs for showing hidden presentations. Using defaults.");
114: }
115: return prop;
116: }
117:
118: /**
119: *
120: * @param evalType
121: */
122: protected boolean setUserPresHiddenProperty(String hiddenValue) {
123: PreferencesEdit prefEdit = null;
124: try {
125: prefEdit = (PreferencesEdit) PreferencesService
126: .add(getAuthManager().getAgent().getId().getValue());
127: } catch (PermissionException e) {
128: logger
129: .warn(
130: "Problem saving preferences for site hidden presentations in setUserPresHiddenProperty().",
131: e);
132: } catch (IdUsedException e) {
133: // Preferences already exist, just edit
134: try {
135: prefEdit = (PreferencesEdit) PreferencesService
136: .edit(getAuthManager().getAgent().getId()
137: .getValue());
138: } catch (PermissionException e1) {
139: logger
140: .warn(
141: "Problem saving preferences for site hidden presentations in setUserPresHiddenProperty().",
142: e1);
143: } catch (InUseException e1) {
144: logger
145: .warn(
146: "Problem saving preferences for site hidden presentations in setUserPresHiddenProperty().",
147: e1);
148: } catch (IdUnusedException e1) {
149: // This should be safe to ignore since we got here because it existed
150: logger
151: .warn(
152: "Problem saving preferences for site hidden presentations in setUserPresHiddenProperty().",
153: e1);
154: }
155: }
156: if (prefEdit != null) {
157: ResourceProperties propEdit = prefEdit
158: .getPropertiesEdit(HIDDEN_PRES_PLACEMENT_PREF
159: + ToolManager.getCurrentPlacement().getId());
160: if (hiddenValue.equals(Boolean.toString(Boolean.TRUE)))
161: propEdit.removeProperty(HIDDEN_PRES_PREF);
162: else
163: propEdit.addProperty(HIDDEN_PRES_PREF, hiddenValue);
164: try {
165: PreferencesService.commit(prefEdit);
166: } catch (Exception e) {
167: logger
168: .warn(
169: "Problem saving preferences for site hidden presentations in setUserPresHiddenProperty().",
170: e);
171: }
172: }
173: return hiddenValue.equals(Boolean.toString(Boolean.TRUE));
174:
175: }
176:
177: public ListScrollIndexer getListScrollIndexer() {
178: return listScrollIndexer;
179: }
180:
181: public void setListScrollIndexer(ListScrollIndexer listScrollIndexer) {
182: this .listScrollIndexer = listScrollIndexer;
183: }
184:
185: public ServerConfigurationService getServerConfigurationService() {
186: return serverConfigurationService;
187: }
188:
189: public void setServerConfigurationService(
190: ServerConfigurationService serverConfigurationService) {
191: this.serverConfigurationService = serverConfigurationService;
192: }
193:
194: }
|