001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/ImportScaffoldingController.java $
003: * $Id: ImportScaffoldingController.java 9472 2006-05-15 16:26:04Z 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.matrix.control;
021:
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.component.cover.ComponentManager;
028: import org.sakaiproject.content.api.FilePickerHelper;
029: import org.sakaiproject.entity.api.Reference;
030: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
031: import org.sakaiproject.metaobj.shared.model.InvalidUploadException;
032: import org.sakaiproject.metaobj.shared.model.OspException;
033: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
034: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
035: import org.sakaiproject.site.api.SiteService;
036: import org.sakaiproject.tool.api.SessionManager;
037: import org.sakaiproject.tool.api.ToolManager;
038: import org.sakaiproject.tool.api.ToolSession;
039: import org.springframework.validation.Errors;
040: import org.springframework.web.servlet.ModelAndView;
041: import org.theospi.portfolio.matrix.MatrixManager;
042: import org.theospi.portfolio.matrix.model.Scaffolding;
043: import org.theospi.portfolio.matrix.model.ScaffoldingUploadForm;
044: import org.theospi.portfolio.shared.model.Node;
045:
046: public class ImportScaffoldingController implements Controller,
047: FormController {
048: protected final transient Log logger = LogFactory
049: .getLog(getClass());
050:
051: private MatrixManager matrixManager;
052: private HomeFactory homeFactory;
053: private SessionManager sessionManager;
054: private ToolManager toolManager;
055: private SiteService siteService;
056:
057: public Map referenceData(Map request, Object command, Errors errors) {
058: ScaffoldingUploadForm scaffoldingForm = (ScaffoldingUploadForm) command;
059:
060: ToolSession session = getSessionManager()
061: .getCurrentToolSession();
062: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
063: && session
064: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
065:
066: List refs = (List) session
067: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
068: Reference ref = (Reference) refs.get(0);
069: scaffoldingForm.setUploadedScaffolding(ref);
070: Node file = getMatrixManager().getNode(ref);
071: scaffoldingForm.setScaffoldingFileName(file
072: .getDisplayName());
073: }
074:
075: session
076: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
077: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
078:
079: return null;
080: }
081:
082: public ModelAndView handleRequest(Object requestModel, Map request,
083: Map session, Map application, Errors errors) {
084:
085: String formAction = (String) request.get("formAction");
086: if (formAction.equals("filePicker")) {
087:
088: session
089: .put(
090: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
091: ComponentManager
092: .get("org.sakaiproject.content.api.ContentResourceFilter.scaffoldingImportFile"));
093: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
094: new Integer(1));
095: return new ModelAndView("filePicker");
096: }
097:
098: session.remove(FilePickerHelper.FILE_PICKER_RESOURCE_FILTER);
099: session.remove(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS);
100:
101: ScaffoldingUploadForm scaffoldingForm = (ScaffoldingUploadForm) requestModel;
102:
103: if (scaffoldingForm.getUploadedScaffolding() == null) {
104: errors.rejectValue("uploadedScaffolding", "Required",
105: "required");
106: return null;
107: }
108:
109: Scaffolding scaffolding = null;
110:
111: try {
112: scaffolding = getMatrixManager()
113: .uploadScaffolding(
114: scaffoldingForm.getUploadedScaffolding(),
115: getToolManager().getCurrentPlacement()
116: .getContext());
117: } catch (InvalidUploadException e) {
118: logger.warn("Failed uploading scaffolding", e);
119: errors.rejectValue(e.getFieldName(), e.getMessage(), e
120: .getMessage());
121: return null;
122: } catch (Exception e) {
123: logger.error("Failed importing scaffolding", e);
124: throw new OspException(e);
125: }
126:
127: return new ModelAndView("success", "scaffolding_id",
128: scaffolding.getId());
129: }
130:
131: /**
132: * @return Returns the matrixManager.
133: */
134: public MatrixManager getMatrixManager() {
135: return matrixManager;
136: }
137:
138: /**
139: * @param matrixManager The matrixManager to set.
140: */
141: public void setMatrixManager(MatrixManager matrixManager) {
142: this .matrixManager = matrixManager;
143: }
144:
145: /**
146: * @return Returns the homeFactory.
147: */
148: public HomeFactory getHomeFactory() {
149: return homeFactory;
150: }
151:
152: /**
153: * @param homeFactory The homeFactory to set.
154: */
155: public void setHomeFactory(HomeFactory homeFactory) {
156: this .homeFactory = homeFactory;
157: }
158:
159: public SessionManager getSessionManager() {
160: return sessionManager;
161: }
162:
163: public void setSessionManager(SessionManager sessionManager) {
164: this .sessionManager = sessionManager;
165: }
166:
167: public ToolManager getToolManager() {
168: return toolManager;
169: }
170:
171: public void setToolManager(ToolManager toolManager) {
172: this .toolManager = toolManager;
173: }
174:
175: public SiteService getSiteService() {
176: return siteService;
177: }
178:
179: public void setSiteService(SiteService siteService) {
180: this.siteService = siteService;
181: }
182: }
|