001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/glossary/tool/src/java/org/theospi/portfolio/help/control/GlossaryImportController.java $
003: * $Id: GlossaryImportController.java 11372 2006-06-29 14:58:30Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.help.control;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.jdom.input.JDOMParseException;
032: import org.sakaiproject.component.cover.ComponentManager;
033: import org.sakaiproject.content.api.ContentHostingService;
034: import org.sakaiproject.content.api.FilePickerHelper;
035: import org.sakaiproject.entity.api.EntityManager;
036: import org.sakaiproject.entity.api.Reference;
037: import org.sakaiproject.exception.IdUnusedException;
038: import org.sakaiproject.exception.PermissionException;
039: import org.sakaiproject.exception.TypeException;
040: import org.sakaiproject.exception.UnsupportedFileTypeException;
041: import org.sakaiproject.metaobj.shared.model.InvalidUploadException;
042: import org.sakaiproject.metaobj.utils.mvc.intf.CancelableController;
043: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
044: import org.sakaiproject.tool.api.SessionManager;
045: import org.sakaiproject.tool.api.ToolSession;
046: import org.springframework.validation.Errors;
047: import org.springframework.validation.Validator;
048: import org.springframework.web.servlet.ModelAndView;
049: import org.theospi.portfolio.help.model.GlossaryUploadForm;
050: import org.theospi.portfolio.shared.model.Node;
051:
052: public class GlossaryImportController extends HelpController implements
053: Validator, CancelableController, FormController {
054: protected final transient Log logger = LogFactory
055: .getLog(getClass());
056:
057: public static final String PARAM_CANCEL = "_cancel";
058: private SessionManager sessionManager;
059: private ContentHostingService contentHosting = null;
060: private EntityManager entityManager;
061:
062: public ModelAndView handleRequest(Object requestModel, Map request,
063: Map session, Map application, Errors errors) {
064:
065: GlossaryUploadForm templateForm = (GlossaryUploadForm) requestModel;
066: if (templateForm == null)
067: return new ModelAndView("success");
068:
069: // if we are picking the file to import
070: if (templateForm.getSubmitAction() != null
071: && templateForm.getSubmitAction().equals("pickImport")) {
072:
073: // if there are files selected already, then put them into the session
074: if (templateForm.getUploadedGlossary() != null
075: && templateForm.getUploadedGlossary().length() > 0) {
076: Reference ref;
077: List files = new ArrayList();
078: String ids[] = templateForm.getUploadedGlossary()
079: .split(",");
080:
081: // get a list of references of the selected files
082: for (int i = 0; i < ids.length; i++) {
083: try {
084: String id = ids[i];
085: id = getContentHosting().resolveUuid(id);
086: String rid = getContentHosting()
087: .getResource(id).getReference();
088: ref = getEntityManager().newReference(rid);
089: files.add(ref);
090: } catch (PermissionException e) {
091: logger.error("", e);
092: } catch (IdUnusedException e) {
093: logger.error("", e);
094: } catch (TypeException e) {
095: logger.error("", e);
096: }
097: }
098: session.put(FilePickerHelper.FILE_PICKER_ATTACHMENTS,
099: files);
100: }
101: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
102: new Integer(1));
103: return new ModelAndView("pickImport");
104:
105: } else {
106:
107: // if there are files, then we want to import them
108: if (templateForm.getUploadedGlossary().length() > 0) {
109: String ids[] = templateForm.getUploadedGlossary()
110: .split(",");
111: for (int i = 0; i < ids.length; i++) {
112: try {
113: String id = ids[i];
114:
115: getHelpManager().importTermsResource(id,
116: templateForm.getReplaceExistingTerms());
117: session.put(
118: TRANSFER_CONTROLLER_SESSION_MESSAGE,
119: TRANSFER_MESSAGE_IMPORT_SUCCESS);
120: } catch (UnsupportedFileTypeException e) {
121: logger.error("Failed uploading glossary terms",
122: e);
123: session.put(
124: TRANSFER_CONTROLLER_SESSION_MESSAGE,
125: TRANSFER_MESSAGE_IMPORT_BAD_FILE);
126: } catch (InvalidUploadException e) {
127: logger.error("Failed uploading glossary terms",
128: e);
129: //errors.rejectValue(e.getFieldName(), e.getMessage(), e.getMessage());
130: session.put(
131: TRANSFER_CONTROLLER_SESSION_MESSAGE,
132: TRANSFER_MESSAGE_IMPORT_FAILED);
133: } catch (JDOMParseException e) {
134: logger
135: .error(
136: "Failed uploading glossary terms: Couldn't parse the file",
137: e);
138: //errors.rejectValue(e.getFieldName(), e.getMessage(), e.getMessage());
139: session.put(
140: TRANSFER_CONTROLLER_SESSION_MESSAGE,
141: TRANSFER_MESSAGE_IMPORT_BAD_PARSE);
142: } catch (Exception e) {
143: logger.error("Failed importing glossary terms",
144: e);
145: session.put(
146: TRANSFER_CONTROLLER_SESSION_MESSAGE,
147: TRANSFER_MESSAGE_IMPORT_FAILED);
148: }
149: }
150: }
151: session.remove(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
152: session
153: .remove(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS);
154: Map model = new Hashtable();
155: return new ModelAndView("success", model);
156: }
157: }
158:
159: public Map referenceData(Map request, Object command, Errors errors) {
160: GlossaryUploadForm templateForm = (GlossaryUploadForm) command;
161: Map model = new HashMap();
162:
163: ToolSession session = getSessionManager()
164: .getCurrentToolSession();
165: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
166: && session
167: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
168: // here is where we setup the id
169: List refs = (List) session
170: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
171: if (refs.size() >= 1) {
172: String ids = "";
173: String names = "";
174:
175: for (Iterator iter = refs.iterator(); iter.hasNext();) {
176: Reference ref = (Reference) iter.next();
177: String nodeId = getContentHosting().getUuid(
178: ref.getId());
179:
180: Node node = getHelpManager().getNode(
181: getIdManager().getId(nodeId));
182:
183: if (ids.length() > 0)
184: ids += ",";
185: ids += node.getId();
186: names += node.getDisplayName() + " ";
187: }
188: templateForm.setUploadedGlossary(ids);
189: model.put("name", names);
190: } else {
191: templateForm.setUploadedGlossary(null);
192: }
193: }
194:
195: session
196: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
197: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
198: session
199: .setAttribute(
200: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
201: ComponentManager
202: .get("org.sakaiproject.content.api.ContentResourceFilter.glossaryStyleFile"));
203: return model;
204: }
205:
206: public boolean supports(Class clazz) {
207: return (GlossaryUploadForm.class.isAssignableFrom(clazz));
208: }
209:
210: public void validate(Object obj, Errors errors) {
211: GlossaryUploadForm templateForm = (GlossaryUploadForm) obj;
212: if ((templateForm.getUploadedGlossary() == null || templateForm
213: .getUploadedGlossary().length() == 0)
214: && templateForm.isValidate()) {
215: errors.rejectValue("uploadedGlossary", "error.required",
216: "required");
217: }
218: }
219:
220: public SessionManager getSessionManager() {
221: return sessionManager;
222: }
223:
224: public void setSessionManager(SessionManager sessionManager) {
225: this .sessionManager = sessionManager;
226: }
227:
228: public ContentHostingService getContentHosting() {
229: return contentHosting;
230: }
231:
232: public void setContentHosting(ContentHostingService contentHosting) {
233: this .contentHosting = contentHosting;
234: }
235:
236: public EntityManager getEntityManager() {
237: return entityManager;
238: }
239:
240: public void setEntityManager(EntityManager entityManager) {
241: this .entityManager = entityManager;
242: }
243:
244: /**
245: * Return if cancel action is specified in the request.
246: * <p>Default implementation looks for "_cancel" parameter in the request.
247: * @param request current HTTP request
248: * @see #PARAM_CANCEL
249: */
250: public boolean isCancel(Map request) {
251: return request.containsKey(PARAM_CANCEL);
252: }
253:
254: public ModelAndView processCancel(Map request, Map session,
255: Map application, Object command, Errors errors)
256: throws Exception {
257:
258: return new ModelAndView("cancel");
259: }
260: }
|