001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/common/tool-lib/src/java/org/theospi/portfolio/style/tool/AddStyleController.java $
003: * $Id: AddStyleController.java 11962 2006-07-11 16:59:05Z andersjb@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.style.tool;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.sakaiproject.component.cover.ComponentManager;
028: import org.sakaiproject.content.api.ContentHostingService;
029: import org.sakaiproject.content.api.FilePickerHelper;
030: import org.sakaiproject.entity.api.EntityManager;
031: import org.sakaiproject.entity.api.Reference;
032: import org.sakaiproject.exception.IdUnusedException;
033: import org.sakaiproject.exception.PermissionException;
034: import org.sakaiproject.exception.TypeException;
035: import org.sakaiproject.metaobj.shared.model.Id;
036: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
037: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
038: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
039: import org.sakaiproject.tool.api.SessionManager;
040: import org.sakaiproject.tool.api.ToolSession;
041: import org.sakaiproject.tool.cover.ToolManager;
042: import org.springframework.validation.Errors;
043: import org.springframework.web.servlet.ModelAndView;
044: import org.theospi.portfolio.shared.model.Node;
045: import org.theospi.portfolio.style.model.Style;
046:
047: public class AddStyleController extends AbstractStyleController
048: implements CustomCommandController, FormController,
049: LoadObjectController {
050:
051: public static final String STYLE_FILE = "osp.style.styleFile";
052:
053: protected static final String STYLE_SESSION_TAG = "osp.style.AddStyleController.style";
054:
055: private SessionManager sessionManager;
056: private ContentHostingService contentHosting;
057: private EntityManager entityManager;
058:
059: public Object formBackingObject(Map request, Map session,
060: Map application) {
061: Style style;
062: if (request.get("style_id") != null
063: && !request.get("style_id").equals("")) {
064: Id id = getIdManager().getId(
065: (String) request.get("style_id"));
066: style = getStyleManager().getStyle(id);
067: } else {
068: style = new Style();
069: style.setOwner(getAuthManager().getAgent());
070: style.setSiteId(ToolManager.getCurrentPlacement()
071: .getContext());
072: }
073: return style;
074: }
075:
076: public Object fillBackingObject(Object incomingModel, Map request,
077: Map session, Map application) throws Exception {
078: if (session.get(STYLE_SESSION_TAG) != null) {
079: return session.remove(STYLE_SESSION_TAG);
080: } else {
081: return incomingModel;
082: }
083: }
084:
085: public ModelAndView handleRequest(Object requestModel, Map request,
086: Map session, Map application, Errors errors) {
087: Style style = (Style) requestModel;
088:
089: if (STYLE_FILE.equals(style.getFilePickerAction())) {
090: session.put(STYLE_SESSION_TAG, style);
091: //session.put(FilePickerHelper.FILE_PICKER_FROM_TEXT, request.get("filePickerFrom"));
092: String filter = "";
093:
094: List files = new ArrayList();
095: String id = "";
096: if (STYLE_FILE.equals(style.getFilePickerAction())) {
097: filter = "org.sakaiproject.content.api.ContentResourceFilter.styleFile";
098: if (style.getStyleFile() != null)
099: id = getContentHosting().resolveUuid(
100: style.getStyleFile().getValue());
101: }
102: if (id != null && !id.equals("")) {
103: Reference ref;
104: try {
105: ref = getEntityManager().newReference(
106: getContentHosting().getResource(id)
107: .getReference());
108: files.add(ref);
109: session.put(
110: FilePickerHelper.FILE_PICKER_ATTACHMENTS,
111: files);
112: } catch (PermissionException e) {
113: logger.error("", e);
114: } catch (IdUnusedException e) {
115: logger.error("", e);
116: } catch (TypeException e) {
117: logger.error("", e);
118: }
119: }
120:
121: if (!filter.equals(""))
122: session.put(
123: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
124: ComponentManager.get(filter));
125:
126: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
127: new Integer(1));
128:
129: return new ModelAndView("pickStyleFiles");
130: }
131:
132: if (request.get("save") != null) {
133: style.setSiteId(ToolManager.getCurrentPlacement()
134: .getContext());
135: save(style, errors);
136: }
137:
138: return new ModelAndView("success");
139: }
140:
141: protected void save(Style style, Errors errors) {
142: getStyleManager().mergeStyle(style);
143: }
144:
145: public Map referenceData(Map request, Object command, Errors errors) {
146: Map model = new HashMap();
147: model.put("STYLE_FILE", STYLE_FILE);
148:
149: Style style = (Style) command;
150:
151: ToolSession session = getSessionManager()
152: .getCurrentToolSession();
153: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
154: && session
155: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
156: // here is where we setup the id
157: List refs = (List) session
158: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
159: Id nodeId = null;
160: String nodeName = "";
161:
162: if (refs.size() == 1) {
163: Reference ref = (Reference) refs.get(0);
164: Node node = getStyleManager().getNode(ref);
165: nodeId = node.getId();
166: nodeName = node.getDisplayName();
167: }
168:
169: if (STYLE_FILE.equals(style.getFilePickerAction())) {
170: style.setStyleFile(nodeId);
171: style.setStyleFileName(nodeName);
172: }
173: }
174:
175: if (style.getStyleFile() != null) {
176: Node styleFile = getStyleManager().getNode(
177: style.getStyleFile());
178: model.put("styleFileName", styleFile.getDisplayName());
179: }
180:
181: session
182: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
183: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
184:
185: return model;
186: }
187:
188: public SessionManager getSessionManager() {
189: return sessionManager;
190: }
191:
192: public void setSessionManager(SessionManager sessionManager) {
193: this .sessionManager = sessionManager;
194: }
195:
196: public ContentHostingService getContentHosting() {
197: return contentHosting;
198: }
199:
200: public void setContentHosting(ContentHostingService contentHosting) {
201: this .contentHosting = contentHosting;
202: }
203:
204: public EntityManager getEntityManager() {
205: return entityManager;
206: }
207:
208: public void setEntityManager(EntityManager entityManager) {
209: this.entityManager = entityManager;
210: }
211:
212: }
|