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/AddStructuredArtifactDefinitionController.java $
003: * $Id: AddStructuredArtifactDefinitionController.java 20019 2006-12-28 04:49:02Z 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 java.util.List;
023: import java.util.Map;
024:
025: import org.sakaiproject.component.cover.ComponentManager;
026: import org.sakaiproject.content.api.FilePickerHelper;
027: import org.sakaiproject.entity.api.Reference;
028: import org.sakaiproject.metaobj.security.AuthorizationFailedException;
029: import org.sakaiproject.metaobj.shared.SharedFunctionConstants;
030: import org.sakaiproject.metaobj.shared.FormHelper;
031: import org.sakaiproject.metaobj.shared.model.PersistenceException;
032: import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
033: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
034: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
035: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
036: import org.sakaiproject.tool.api.SessionManager;
037: import org.sakaiproject.tool.api.ToolSession;
038: import org.springframework.validation.Errors;
039: import org.springframework.web.servlet.ModelAndView;
040:
041: /**
042: * @author chmaurer
043: */
044: public class AddStructuredArtifactDefinitionController extends
045: AbstractStructuredArtifactDefinitionController implements
046: CustomCommandController, FormController, LoadObjectController {
047:
048: protected static final String SAD_SESSION_TAG = "org.sakaiproject.metaobj.shared.control.AddStructuredArtifactDefinitionController.sad";
049: private SessionManager sessionManager;
050:
051: public Object formBackingObject(Map request, Map session,
052: Map application) {
053:
054: //check to see if you have create permissions
055: checkPermission(SharedFunctionConstants.CREATE_ARTIFACT_DEF);
056:
057: StructuredArtifactDefinitionBean backingObject = new StructuredArtifactDefinitionBean();
058: backingObject.setOwner(getAuthManager().getAgent());
059: return backingObject;
060: }
061:
062: public Object fillBackingObject(Object incomingModel, Map request,
063: Map session, Map application) throws Exception {
064: if (session.get(SAD_SESSION_TAG) != null) {
065: return session.remove(SAD_SESSION_TAG);
066: } else {
067: return incomingModel;
068: }
069: }
070:
071: public ModelAndView handleRequest(Object requestModel, Map request,
072: Map session, Map application, Errors errors) {
073: StructuredArtifactDefinitionBean sad = (StructuredArtifactDefinitionBean) requestModel;
074:
075: if (StructuredArtifactDefinitionValidator.PICK_SCHEMA_ACTION
076: .equals(sad.getFilePickerAction())
077: || StructuredArtifactDefinitionValidator.PICK_TRANSFORM_ACTION
078: .equals(sad.getFilePickerAction())) {
079: session.put(SAD_SESSION_TAG, sad);
080: session
081: .put(
082: FilePickerHelper.FILE_PICKER_RESOURCE_FILTER,
083: ComponentManager
084: .get("org.sakaiproject.content.api.ContentResourceFilter.metaobjFile"));
085: session.put(FilePickerHelper.FILE_PICKER_MAX_ATTACHMENTS,
086: new Integer(1));
087: return new ModelAndView("pickSchema");
088: }
089:
090: if (request.get("systemOnly") == null) {
091: sad.setSystemOnly(false);
092: }
093:
094: if (sad.getSchemaFile() != null) {
095: try {
096: getStructuredArtifactDefinitionManager()
097: .validateSchema(sad);
098: } catch (Exception e) {
099: logger.warn("", e);
100: String errorMessage = "error reading schema file: "
101: + e.getMessage();
102: sad.setSchemaFile(null);
103: errors.rejectValue("schemaFile", errorMessage,
104: errorMessage);
105: return new ModelAndView("failure");
106: }
107: }
108:
109: if ("preview".equals(request.get("previewAction"))) {
110: session.put(SAD_SESSION_TAG, sad);
111: session.put(FormHelper.PREVIEW_HOME_TAG, sad);
112: return new ModelAndView("preview");
113: }
114:
115: try {
116: if (!getStructuredArtifactDefinitionManager().isGlobal()) {
117: sad.setSiteId(getWorksiteManager()
118: .getCurrentWorksiteId().getValue());
119: }
120:
121: save(sad, errors);
122: } catch (AuthorizationFailedException e) {
123: throw e;
124: } catch (PersistenceException e) {
125: errors.rejectValue(e.getField(), e.getErrorCode(), e
126: .getErrorInfo(), e.getDefaultMessage());
127: } catch (Exception e) {
128: logger.warn("", e);
129: String errorMessage = "error transforming or saving artifacts: "
130: + e.getMessage();
131: errors.rejectValue("xslConversionFileId", errorMessage,
132: errorMessage);
133: sad.setXslConversionFileId(null);
134: return new ModelAndView("failure");
135: }
136:
137: if (errors.getErrorCount() > 0) {
138: return new ModelAndView("failure");
139: }
140:
141: return prepareListView(request, sad.getId().getValue());
142: }
143:
144: protected void save(StructuredArtifactDefinitionBean sad,
145: Errors errors) {
146: //check to see if you have create permissions
147: checkPermission(SharedFunctionConstants.CREATE_ARTIFACT_DEF);
148:
149: getStructuredArtifactDefinitionManager().save(sad);
150: }
151:
152: public Map referenceData(Map request, Object command, Errors errors) {
153: Map base = super .referenceData(request, command, errors);
154: StructuredArtifactDefinitionBean sad = (StructuredArtifactDefinitionBean) command;
155:
156: ToolSession session = getSessionManager()
157: .getCurrentToolSession();
158: if (session.getAttribute(FilePickerHelper.FILE_PICKER_CANCEL) == null
159: && session
160: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
161: // here is where we setup the id
162: List refs = (List) session
163: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
164: if (refs != null && refs.size() > 0) {
165: Reference ref = (Reference) refs.get(0);
166:
167: if (StructuredArtifactDefinitionValidator.PICK_SCHEMA_ACTION
168: .equals(sad.getFilePickerAction())) {
169: sad
170: .setSchemaFile(getIdManager().getId(
171: ref.getId()));
172: sad.setSchemaFileName(ref.getProperties()
173: .getProperty(
174: ref.getProperties()
175: .getNamePropDisplayName()));
176: } else if (StructuredArtifactDefinitionValidator.PICK_TRANSFORM_ACTION
177: .equals(sad.getFilePickerAction())) {
178: sad.setXslConversionFileId(getIdManager().getId(
179: ref.getId()));
180: sad.setXslFileName(ref.getProperties().getProperty(
181: ref.getProperties()
182: .getNamePropDisplayName()));
183: }
184: }
185: }
186:
187: session
188: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
189: session.removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
190:
191: if (sad.getSchemaFile() != null) {
192: try {
193: base.put("elements",
194: getStructuredArtifactDefinitionManager()
195: .getRootElements(sad));
196: } catch (Exception e) {
197: String errorMessage = "error reading schema file: "
198: + e.getMessage();
199: sad.setSchemaFile(null);
200: sad.setSchemaFileName(null);
201: errors.rejectValue("schemaFile", errorMessage,
202: errorMessage);
203: }
204: }
205: return base;
206: }
207:
208: public SessionManager getSessionManager() {
209: return sessionManager;
210: }
211:
212: public void setSessionManager(SessionManager sessionManager) {
213: this.sessionManager = sessionManager;
214: }
215:
216: }
|