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/AddXmlElementController.java $
003: * $Id: AddXmlElementController.java 20850 2007-02-01 00:05:58Z john.ellis@rsmart.com $
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.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.springframework.validation.Errors;
025: import org.springframework.web.servlet.ModelAndView;
026: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
027: import org.sakaiproject.metaobj.utils.mvc.intf.CancelableController;
028: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
029: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
030: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
031: import org.sakaiproject.metaobj.shared.model.ElementBean;
032: import org.sakaiproject.metaobj.shared.model.Artifact;
033: import org.sakaiproject.metaobj.shared.model.PersistenceException;
034: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
035: import org.sakaiproject.metaobj.shared.mgt.WritableObjectHome;
036: import org.sakaiproject.metaobj.shared.FormHelper;
037: import org.sakaiproject.tool.api.ToolSession;
038: import org.sakaiproject.content.api.FilePickerHelper;
039:
040: import java.util.Map;
041: import java.util.Hashtable;
042:
043: /**
044: * Created by IntelliJ IDEA.
045: * <p/>
046: * User: John Ellis
047: * <p/>
048: * Date: Apr 20, 2004
049: * <p/>
050: * Time: 3:31:02 PM
051: * <p/>
052: * To change this template use File | Settings | File Templates.
053: */
054: public class AddXmlElementController extends XmlControllerBase
055: implements Controller, CustomCommandController,
056: CancelableController {
057: protected final Log logger = LogFactory.getLog(getClass());
058:
059: public Object formBackingObject(Map request, Map session,
060: Map application) {
061: ElementBean returnedBean;
062: if (session.get(EditedArtifactStorage.STORED_ARTIFACT_FLAG) == null) {
063: StructuredArtifactHomeInterface home = getSchema(session);
064: StructuredArtifact bean = (StructuredArtifact) home
065: .createInstance();
066:
067: if (session.get(FormHelper.NEW_FORM_DISPLAY_NAME_TAG) != null) {
068: bean.setDisplayName((String) session
069: .get(FormHelper.NEW_FORM_DISPLAY_NAME_TAG));
070: }
071:
072: bean.setParentFolder((String) session
073: .get(FormHelper.PARENT_ID_TAG));
074: EditedArtifactStorage sessionBean = new EditedArtifactStorage(
075: bean.getCurrentSchema(), bean);
076: session
077: .put(
078: EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY,
079: sessionBean);
080: returnedBean = bean;
081: } else {
082: EditedArtifactStorage sessionBean = (EditedArtifactStorage) session
083: .get(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
084: returnedBean = sessionBean.getCurrentElement();
085: }
086:
087: if (session.get(FilePickerHelper.FILE_PICKER_CANCEL) != null
088: || session
089: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
090: retrieveFileAttachments(request, session, returnedBean);
091: }
092:
093: return returnedBean;
094: }
095:
096: public ModelAndView handleRequest(Object requestModel, Map request,
097: Map session, Map application, Errors errors) {
098: ElementBean bean = (ElementBean) requestModel;
099: if (request.get("cancel") != null) {
100: session.put(FormHelper.RETURN_ACTION_TAG,
101: FormHelper.RETURN_ACTION_CANCEL);
102: session.remove(FormHelper.PREVIEW_HOME_TAG);
103: session.remove(EditedArtifactStorage.STORED_ARTIFACT_FLAG);
104: return new ModelAndView("success");
105: }
106: if (request.get("submitButton") == null) {
107: return handleNonSubmit(bean, request, session, application,
108: errors);
109: }
110: getValidator().validate(bean, errors, true);
111: if (errors.hasErrors()) {
112: return null;
113: }
114: StructuredArtifact artifact = (StructuredArtifact) bean;
115: Artifact newArtifact;
116:
117: if (session.get(FormHelper.PREVIEW_HOME_TAG) != null) {
118: request.remove("fileHelper");
119: Map model = new Hashtable();
120: model.put("success", "validationSuccessful");
121: return handleNonSubmit(bean, request, session, application,
122: errors, model);
123: }
124:
125: try {
126: WritableObjectHome home = getSchema(session);
127: newArtifact = home.store(artifact);
128: if (newArtifact.getId() != null) {
129: session.put(FormHelper.RETURN_REFERENCE_TAG,
130: newArtifact.getId().getValue());
131: session.put(FormHelper.RETURN_ACTION_TAG,
132: FormHelper.RETURN_ACTION_SAVE);
133: }
134: } catch (PersistenceException e) {
135: errors.rejectValue(e.getField(), e.getErrorCode(), e
136: .getErrorInfo(), e.getDefaultMessage());
137: }
138: session.remove(EditedArtifactStorage.STORED_ARTIFACT_FLAG);
139: return new ModelAndView("success");
140: }
141:
142: }
|