001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/shared/control/ImportStructuredArtifactDefinitionController.java $
003: * $Id: ImportStructuredArtifactDefinitionController.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 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.sakaiproject.metaobj.shared.control;
021:
022: import org.sakaiproject.component.cover.ComponentManager;
023: import org.sakaiproject.content.api.ContentHostingService;
024: import org.sakaiproject.content.api.ContentResource;
025: import org.sakaiproject.content.api.FilePickerHelper;
026: import org.sakaiproject.entity.api.EntityManager;
027: import org.sakaiproject.entity.api.Reference;
028: import org.sakaiproject.exception.*;
029: import org.sakaiproject.metaobj.shared.model.FormUploadForm;
030: import org.sakaiproject.metaobj.shared.model.InvalidUploadException;
031: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
032: import org.sakaiproject.tool.api.SessionManager;
033: import org.sakaiproject.tool.api.ToolSession;
034: import org.springframework.validation.Errors;
035: import org.springframework.validation.Validator;
036: import org.springframework.web.servlet.ModelAndView;
037:
038: import java.util.*;
039:
040: public class ImportStructuredArtifactDefinitionController extends
041: AddStructuredArtifactDefinitionController implements
042: Controller, Validator {
043:
044: private SessionManager sessionManager;
045: private ContentHostingService contentHosting = null;
046: private EntityManager entityManager;
047:
048: public Object formBackingObject(Map request, Map session,
049: Map application) {
050:
051: FormUploadForm backingObject = new FormUploadForm();
052: return backingObject;
053: }
054:
055: /*
056: public ModelAndView handleRequest(Object requestModel, Map request, Map session, Map application, Errors errors) {
057: return prepareListView(request, null);
058: }
059: */
060: public ModelAndView handleRequest(Object requestModel, Map request,
061: Map session, Map application, Errors errors) {
062:
063: FormUploadForm templateForm = (FormUploadForm) requestModel;
064: if (templateForm == null) {
065: return new ModelAndView("success");
066: }
067: if (templateForm.getSubmitAction() != null
068: && templateForm.getSubmitAction().equals("pickImport")) {
069: if (templateForm.getUploadedForm() != null
070: && templateForm.getUploadedForm().length() > 0) {
071: Reference ref;
072: List files = new ArrayList();
073: String ids[] = templateForm.getUploadedForm()
074: .split(",");
075: for (int i = 0; i < ids.length; i++) {
076: try {
077: String id = ids[i];
078: id = getContentHosting().resolveUuid(id);
079: String rid = getContentHosting()
080: .getResource(id).getReference();
081: ref = getEntityManager().newReference(rid);
082: files.add(ref);
083: } catch (PermissionException e) {
084: logger.error("", e);
085: } catch (IdUnusedException e) {
086: logger.error("", e);
087: } catch (TypeException e) {
088: logger.error("", e);
089: }
090: }
091: session.put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
092: files);
093: }
094: return new ModelAndView("pickImport");
095: } else {
096: String view = "success";
097: if (templateForm.getUploadedForm().length() > 0) {
098: String ids[] = templateForm.getUploadedForm()
099: .split(",");
100: for (int i = 0; i < ids.length; i++) {
101: try {
102: String id = ids[i];
103: if (!getStructuredArtifactDefinitionManager()
104: .importSADResource(
105: getWorksiteManager()
106: .getCurrentWorksiteId(),
107: id, true)) {
108: errors.rejectValue("uploadedForm",
109: "error.format",
110: "File format not recognized");
111:
112: view = "failed";
113: }
114: } catch (InvalidUploadException e) {
115: logger.warn("Failed uploading template", e);
116: errors.rejectValue(e.getFieldName(), e
117: .getMessage(), e.getMessage());
118: view = "failed";
119: } catch (UnsupportedFileTypeException ufte) {
120: logger.warn("Failed uploading template", ufte);
121: errors.rejectValue("uploadedForm", ufte
122: .getMessage(), ufte.getMessage());
123: view = "failed";
124: } catch (ImportException ie) {
125: logger.warn("Failed uploading template", ie);
126: errors.rejectValue("uploadedForm", ie
127: .getMessage(), ie.getMessage());
128: view = "failed";
129: } catch (Exception e) {
130: logger.error("Failed importing template", e);
131: view = "failed";
132: }
133: }
134: }
135: Map model = new Hashtable();
136: return new ModelAndView(view, model);
137: }
138: }
139:
140: public Map referenceData(Map request, Object command, Errors errors) {
141: FormUploadForm templateForm = (FormUploadForm) command;
142: Map model = new HashMap();
143:
144: ToolSession session = getSessionManager()
145: .getCurrentToolSession();
146: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
147: && session
148: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
149: // here is where we setup the id
150: List refs = (List) session
151: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
152: if (refs.size() >= 1) {
153: String ids = "";
154: String names = "";
155:
156: for (Iterator iter = refs.iterator(); iter.hasNext();) {
157: Reference ref = (Reference) iter.next();
158: String nodeId = getContentHosting().getUuid(
159: ref.getId());
160: String id = getContentHosting().resolveUuid(nodeId);
161:
162: ContentResource resource = null;
163: try {
164: resource = getContentHosting().getResource(id);
165: } catch (PermissionException pe) {
166: logger
167: .warn(
168: "Failed loading content: no permission to view file",
169: pe);
170: } catch (TypeException pe) {
171: logger.warn("Wrong type", pe);
172: } catch (IdUnusedException pe) {
173: logger.warn("UnusedId: ", pe);
174: }
175:
176: if (ids.length() > 0) {
177: ids += ",";
178: }
179: ids += nodeId;
180: names += resource.getProperties().getProperty(
181: resource.getProperties()
182: .getNamePropDisplayName())
183: + " ";
184: }
185: templateForm.setUploadedForm(ids);
186: model.put("name", names);
187: } else {
188: templateForm.setUploadedForm(null);
189: }
190: }
191:
192: session
193: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
194: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
195: session
196: .setAttribute(
197: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
198: ComponentManager
199: .get("org.sakaiproject.content.api.ContentResourceFilter.formUploadStyleFile"));
200: return model;
201: }
202:
203: public boolean supports(Class clazz) {
204: return (FormUploadForm.class.isAssignableFrom(clazz));
205: }
206:
207: public void validate(Object obj, Errors errors) {
208: FormUploadForm templateForm = (FormUploadForm) obj;
209: if (templateForm.getUploadedForm() == null
210: && templateForm.isValidate()) {
211: errors.rejectValue("uploadedForm", "error.required",
212: "required");
213: }
214: }
215:
216: /**
217: * override to prevent pulling it from the session.
218: * @param incomingModel
219: * @param request
220: * @param session
221: * @param application
222: * @return
223: * @throws Exception
224: */
225: public Object fillBackingObject(Object incomingModel, Map request,
226: Map session, Map application) throws Exception {
227: return incomingModel;
228: }
229:
230: public SessionManager getSessionManager() {
231: return sessionManager;
232: }
233:
234: public void setSessionManager(SessionManager sessionManager) {
235: this .sessionManager = sessionManager;
236: }
237:
238: public ContentHostingService getContentHosting() {
239: return contentHosting;
240: }
241:
242: public void setContentHosting(ContentHostingService contentHosting) {
243: this .contentHosting = contentHosting;
244: }
245:
246: public EntityManager getEntityManager() {
247: return entityManager;
248: }
249:
250: public void setEntityManager(EntityManager entityManager) {
251: this.entityManager = entityManager;
252: }
253: }
|