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/EditXmlElementController.java $
003: * $Id: EditXmlElementController.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.CancelableController;
027: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
028: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
029: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
030: import org.sakaiproject.metaobj.shared.model.ElementBean;
031: import org.sakaiproject.metaobj.shared.model.PersistenceException;
032: import org.sakaiproject.metaobj.shared.model.Id;
033: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
034: import org.sakaiproject.metaobj.shared.mgt.WritableObjectHome;
035: import org.sakaiproject.metaobj.shared.mgt.IdManager;
036: import org.sakaiproject.metaobj.shared.mgt.ReadableObjectHome;
037: import org.sakaiproject.metaobj.shared.ArtifactFinder;
038: import org.sakaiproject.metaobj.shared.FormHelper;
039: import org.sakaiproject.content.cover.ContentHostingService;
040: import org.sakaiproject.content.api.ResourceEditingHelper;
041: import org.sakaiproject.content.api.FilePickerHelper;
042: import org.sakaiproject.content.api.ResourceToolAction;
043: import org.sakaiproject.tool.api.ToolSession;
044:
045: import java.util.Map;
046:
047: /**
048: * Created by IntelliJ IDEA.
049: * <p/>
050: * User: John Ellis
051: * <p/>
052: * Date: Apr 20, 2004
053: * <p/>
054: * Time: 3:31:25 PM
055: * <p/>
056: * To change this template use File | Settings | File Templates.
057: */
058: public class EditXmlElementController extends XmlControllerBase
059: implements CustomCommandController, LoadObjectController,
060: CancelableController {
061:
062: protected final Log logger = LogFactory.getLog(getClass());
063:
064: private ArtifactFinder artifactFinder;
065: private IdManager idManager;
066:
067: public Object formBackingObject(Map request, Map session,
068: Map application) {
069: ElementBean returnedBean;
070: if (session.get(EditedArtifactStorage.STORED_ARTIFACT_FLAG) == null) {
071: if (getSchemaName(session) != null) {
072: StructuredArtifactHomeInterface home = getSchema(session);
073: StructuredArtifact bean = (StructuredArtifact) home
074: .createInstance();
075: bean.setParentFolder((String) session
076: .get(FormHelper.PARENT_ID_TAG));
077: EditedArtifactStorage sessionBean = new EditedArtifactStorage(
078: bean.getCurrentSchema(), bean);
079: session
080: .put(
081: EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY,
082: sessionBean);
083: returnedBean = bean;
084: } else {
085: return new ElementBean();
086: }
087: } else {
088: EditedArtifactStorage sessionBean = (EditedArtifactStorage) session
089: .get(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
090: returnedBean = sessionBean.getCurrentElement();
091: }
092:
093: if (session.get(FilePickerHelper.FILE_PICKER_CANCEL) != null
094: || session
095: .get(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
096: retrieveFileAttachments(request, session, returnedBean);
097: }
098:
099: return returnedBean;
100: }
101:
102: public Object fillBackingObject(Object incomingModel, Map request,
103: Map session, Map application) throws Exception {
104: if (session.get(EditedArtifactStorage.STORED_ARTIFACT_FLAG) == null) {
105: StructuredArtifact bean;
106:
107: if (session.get(ResourceToolAction.ACTION_PIPE) == null) {
108: Id id;
109: String idString = ContentHostingService
110: .getUuid((String) session
111: .get(ResourceEditingHelper.ATTACHMENT_ID));
112:
113: id = getIdManager().getId(idString);
114:
115: bean = (StructuredArtifact) getArtifactFinder()
116: .load(id);
117: } else {
118: ReadableObjectHome home = getSchema(session);
119: bean = (StructuredArtifact) home.load(null);
120: }
121:
122: session.put(ResourceEditingHelper.CREATE_SUB_TYPE,
123: ((StructuredArtifactHomeInterface) bean.getHome())
124: .getTypeId());
125:
126: EditedArtifactStorage sessionBean = new EditedArtifactStorage(
127: bean.getCurrentSchema(), bean);
128: session
129: .put(
130: EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY,
131: sessionBean);
132: return bean;
133: } else {
134: EditedArtifactStorage sessionBean = (EditedArtifactStorage) session
135: .get(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);
136: return sessionBean.getCurrentElement();
137: }
138: }
139:
140: public ModelAndView handleRequest(Object requestModel, Map request,
141: Map session, Map application, Errors errors) {
142: ElementBean bean = (ElementBean) requestModel;
143:
144: if (request.get("cancel") != null) {
145: session.put(FormHelper.RETURN_ACTION_TAG,
146: FormHelper.RETURN_ACTION_CANCEL);
147: session.remove(EditedArtifactStorage.STORED_ARTIFACT_FLAG);
148: return new ModelAndView("success");
149: } else if (request.get("submitButton") == null) {
150: return handleNonSubmit(bean, request, session, application,
151: errors);
152: }
153: getValidator().validate(bean, errors, true);
154: if (errors.hasErrors()) {
155: return null;
156: }
157: WritableObjectHome home = getSchema(session);
158: try {
159: home.store((StructuredArtifact) bean);
160: } catch (PersistenceException e) {
161: errors.rejectValue(e.getField(), e.getErrorCode(), e
162: .getErrorInfo(), e.getDefaultMessage());
163: }
164: session.put(FormHelper.RETURN_REFERENCE_TAG,
165: ((StructuredArtifact) bean).getId().getValue());
166: session.put(FormHelper.RETURN_ACTION_TAG,
167: FormHelper.RETURN_ACTION_SAVE);
168: session.remove(EditedArtifactStorage.STORED_ARTIFACT_FLAG);
169: return new ModelAndView("success", "schema",
170: getSchemaName(session));
171: }
172:
173: public ArtifactFinder getArtifactFinder() {
174: return artifactFinder;
175: }
176:
177: public void setArtifactFinder(ArtifactFinder artifactFinder) {
178: this .artifactFinder = artifactFinder;
179: }
180:
181: public IdManager getIdManager() {
182: return idManager;
183: }
184:
185: public void setIdManager(IdManager idManager) {
186: this.idManager = idManager;
187: }
188: }
|