001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/ImportTemplateController.java $
003: * $Id:ImportTemplateController.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.HashMap;
024: import java.util.Hashtable;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.component.cover.ComponentManager;
031: import org.sakaiproject.content.api.ContentHostingService;
032: import org.sakaiproject.content.api.FilePickerHelper;
033: import org.sakaiproject.entity.api.EntityManager;
034: import org.sakaiproject.entity.api.Reference;
035: import org.sakaiproject.exception.IdUnusedException;
036: import org.sakaiproject.exception.PermissionException;
037: import org.sakaiproject.exception.TypeException;
038: import org.sakaiproject.metaobj.shared.model.InvalidUploadException;
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.validation.Validator;
044: import org.springframework.web.servlet.ModelAndView;
045: import org.theospi.portfolio.presentation.model.PresentationTemplate;
046: import org.theospi.portfolio.presentation.model.TemplateUploadForm;
047: import org.theospi.portfolio.shared.model.Node;
048: import org.theospi.portfolio.shared.model.OspException;
049:
050: public class ImportTemplateController extends
051: AbstractPresentationController implements Validator {
052: protected final transient Log logger = LogFactory
053: .getLog(getClass());
054:
055: private SessionManager sessionManager;
056: private ContentHostingService contentHosting = null;
057: private EntityManager entityManager;
058:
059: public ModelAndView handleRequest(Object requestModel, Map request,
060: Map session, Map application, Errors errors) {
061:
062: TemplateUploadForm templateForm = (TemplateUploadForm) requestModel;
063:
064: if (templateForm.getSubmitAction().equals("pickImport")) {
065: if (templateForm.getUploadedTemplate() != null) {
066: String id = getContentHosting().resolveUuid(
067: templateForm.getUploadedTemplate().getValue());
068: Reference ref;
069: List files = new ArrayList();
070: try {
071: ref = getEntityManager().newReference(
072: getContentHosting().getResource(id)
073: .getReference());
074: files.add(ref);
075: } catch (PermissionException e) {
076: logger.error("", e);
077: } catch (IdUnusedException e) {
078: logger.error("", e);
079: } catch (TypeException e) {
080: logger.error("", e);
081: }
082: session.put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
083: files);
084: }
085:
086: session
087: .put(
088: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
089: ComponentManager
090: .get("org.sakaiproject.content.api.ContentResourceFilter.templateImportFile"));
091: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
092: new Integer(1));
093:
094: return new ModelAndView("pickImport");
095: } else {
096: try {
097: Node file = getPresentationManager().getNode(
098: templateForm.getUploadedTemplate());
099: PresentationTemplate template = getPresentationManager()
100: .uploadTemplate(
101: file.getDisplayName(),
102: ToolManager.getCurrentPlacement()
103: .getContext(),
104: file.getInputStream());
105: Map model = new Hashtable();
106: model.put("newPresentationTemplateId", template.getId()
107: .getValue());
108:
109: return new ModelAndView("success", model);
110: } catch (InvalidUploadException e) {
111: logger.warn("Failed uploading template", e);
112: errors.rejectValue(e.getFieldName(), e.getMessage(), e
113: .getMessage());
114: return null;
115: } catch (Exception e) {
116: logger.error("Failed importing template", e);
117: throw new OspException(e);
118: }
119: }
120: }
121:
122: public Map referenceData(Map request, Object command, Errors errors) {
123: TemplateUploadForm templateForm = (TemplateUploadForm) command;
124: Map model = new HashMap();
125:
126: ToolSession session = getSessionManager()
127: .getCurrentToolSession();
128: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
129: && session
130: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
131: // here is where we setup the id
132: List refs = (List) session
133: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
134: if (refs.size() == 1) {
135: Reference ref = (Reference) refs.get(0);
136: Node node = getPresentationManager().getNode(ref);
137: templateForm.setUploadedTemplate(node.getId());
138: model.put("name", node.getDisplayName());
139: } else {
140: templateForm.setUploadedTemplate(null);
141: }
142: }
143:
144: session
145: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
146: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
147: return model;
148: }
149:
150: public boolean supports(Class clazz) {
151: return (TemplateUploadForm.class.isAssignableFrom(clazz));
152: }
153:
154: public void validate(Object obj, Errors errors) {
155: TemplateUploadForm templateForm = (TemplateUploadForm) obj;
156: if (templateForm.getUploadedTemplate() == null
157: && templateForm.isValidate()) {
158: errors.rejectValue("uploadedTemplate", "error.required",
159: "required");
160: }
161: }
162:
163: public SessionManager getSessionManager() {
164: return sessionManager;
165: }
166:
167: public void setSessionManager(SessionManager sessionManager) {
168: this .sessionManager = sessionManager;
169: }
170:
171: public ContentHostingService getContentHosting() {
172: return contentHosting;
173: }
174:
175: public void setContentHosting(ContentHostingService contentHosting) {
176: this .contentHosting = contentHosting;
177: }
178:
179: public EntityManager getEntityManager() {
180: return entityManager;
181: }
182:
183: public void setEntityManager(EntityManager entityManager) {
184: this.entityManager = entityManager;
185: }
186: }
|