001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/presentation/tool/src/java/org/theospi/portfolio/presentation/control/AddLayoutController.java $
003: * $Id: AddLayoutController.java 10835 2006-06-17 03:25:03Z lance@indiana.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.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.presentation.model.PresentationLayout;
045: import org.theospi.portfolio.shared.model.Node;
046:
047: public class AddLayoutController extends AbstractPresentationController
048: implements CustomCommandController, FormController,
049: LoadObjectController {
050:
051: public static final String XHTML_FILE = "osp.presentation.layout.xhtmlFile";
052: public static final String PREVIEW_IMAGE = "osp.presentation.layout.previewImage";
053: protected static final String LAYOUT_SESSION_TAG = "osp.presentation.AddLayoutController.layout";
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: PresentationLayout layout;
062: if (request.get("layout_id") != null
063: && !request.get("layout_id").equals("")) {
064: Id id = getIdManager().getId(
065: (String) request.get("layout_id"));
066: layout = getPresentationManager().getPresentationLayout(id);
067: } else {
068: layout = new PresentationLayout();
069: layout.setOwner(getAuthManager().getAgent());
070: layout.setToolId(ToolManager.getCurrentPlacement().getId());
071: layout.setSiteId(ToolManager.getCurrentPlacement()
072: .getContext());
073: }
074: return layout;
075: }
076:
077: public Object fillBackingObject(Object incomingModel, Map request,
078: Map session, Map application) throws Exception {
079: if (session.get(LAYOUT_SESSION_TAG) != null) {
080: return session.remove(LAYOUT_SESSION_TAG);
081: } else {
082: return incomingModel;
083: }
084: }
085:
086: public ModelAndView handleRequest(Object requestModel, Map request,
087: Map session, Map application, Errors errors) {
088: PresentationLayout layout = (PresentationLayout) requestModel;
089:
090: if (XHTML_FILE.equals(layout.getFilePickerAction())
091: || PREVIEW_IMAGE.equals(layout.getFilePickerAction())) {
092: session.put(LAYOUT_SESSION_TAG, layout);
093: //session.put(FilePickerHelper.FILE_PICKER_FROM_TEXT, request.get("filePickerFrom"));
094: String filter = "";
095:
096: List files = new ArrayList();
097: String id = "";
098: if (XHTML_FILE.equals(layout.getFilePickerAction())) {
099: filter = "org.sakaiproject.content.api.ContentResourceFilter.layoutFile";
100: if (layout.getXhtmlFileId() != null)
101: id = getContentHosting().resolveUuid(
102: layout.getXhtmlFileId().getValue());
103: } else if (PREVIEW_IMAGE.equals(layout
104: .getFilePickerAction())) {
105: filter = "org.sakaiproject.content.api.ContentResourceFilter.layoutImageFile";
106: if (layout.getPreviewImageId() != null)
107: id = getContentHosting().resolveUuid(
108: layout.getPreviewImageId().getValue());
109: }
110: if (id != null && !id.equals("")) {
111: Reference ref;
112: try {
113: ref = getEntityManager().newReference(
114: getContentHosting().getResource(id)
115: .getReference());
116: files.add(ref);
117: session.put(
118: FilePickerHelper.FILE_PICKER_ATTACHMENTS,
119: files);
120: } catch (PermissionException e) {
121: logger.error("", e);
122: } catch (IdUnusedException e) {
123: logger.error("", e);
124: } catch (TypeException e) {
125: logger.error("", e);
126: }
127: }
128:
129: if (!filter.equals(""))
130: session.put(
131: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
132: ComponentManager.get(filter));
133:
134: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
135: new Integer(1));
136:
137: return new ModelAndView("pickLayoutFiles");
138: }
139: /*
140: String action = "";
141: Object actionObj = request.get("action");
142: if (actionObj instanceof String) {
143: action = (String)actionObj;
144: }
145: else if (actionObj instanceof String[]) {
146: action = ((String[])actionObj)[0];
147: }
148: */
149: if (request.get("save") != null)
150: save(layout, errors);
151:
152: return new ModelAndView("success");
153: }
154:
155: protected void save(PresentationLayout layout, Errors errors) {
156: getPresentationManager().storeLayout(layout);
157: }
158:
159: public Map referenceData(Map request, Object command, Errors errors) {
160: Map model = new HashMap();
161: model.put("XHTML_FILE", XHTML_FILE);
162: model.put("PREVIEW_IMAGE", PREVIEW_IMAGE);
163: PresentationLayout layout = (PresentationLayout) command;
164:
165: ToolSession session = getSessionManager()
166: .getCurrentToolSession();
167: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
168: && session
169: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
170: // here is where we setup the id
171: List refs = (List) session
172: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
173: Id nodeId = null;
174: String nodeName = "";
175:
176: if (refs.size() == 1) {
177: Reference ref = (Reference) refs.get(0);
178: Node node = getPresentationManager().getNode(ref);
179: nodeId = node.getId();
180: nodeName = node.getDisplayName();
181: }
182:
183: if (XHTML_FILE.equals(layout.getFilePickerAction())) {
184: layout.setXhtmlFileId(nodeId);
185: layout.setXhtmlFileName(nodeName);
186: } else if (PREVIEW_IMAGE.equals(layout
187: .getFilePickerAction())) {
188: layout.setPreviewImageId(nodeId);
189: layout.setPreviewImageName(nodeName);
190: }
191: }
192:
193: if (layout.getXhtmlFileId() != null) {
194: Node xhtmlFile = getPresentationManager().getNode(
195: layout.getXhtmlFileId());
196: model.put("xhtmlFileName", xhtmlFile.getDisplayName());
197: }
198: if (layout.getPreviewImageId() != null) {
199: Node previewFile = getPresentationManager().getNode(
200: layout.getPreviewImageId());
201: model.put("previewImageName", previewFile.getDisplayName());
202: }
203:
204: session
205: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
206: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
207:
208: return model;
209: }
210:
211: public SessionManager getSessionManager() {
212: return sessionManager;
213: }
214:
215: public void setSessionManager(SessionManager sessionManager) {
216: this .sessionManager = sessionManager;
217: }
218:
219: public ContentHostingService getContentHosting() {
220: return contentHosting;
221: }
222:
223: public void setContentHosting(ContentHostingService contentHosting) {
224: this .contentHosting = contentHosting;
225: }
226:
227: public EntityManager getEntityManager() {
228: return entityManager;
229: }
230:
231: public void setEntityManager(EntityManager entityManager) {
232: this.entityManager = entityManager;
233: }
234:
235: }
|