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/EditStructuredArtifactDefinitionController.java $
003: * $Id: EditStructuredArtifactDefinitionController.java 22427 2007-03-12 15:41:41Z 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.Map;
023: import java.util.Iterator;
024: import java.util.Collection;
025:
026: import org.sakaiproject.metaobj.shared.SharedFunctionConstants;
027: import org.sakaiproject.metaobj.shared.ArtifactFinder;
028: import org.sakaiproject.metaobj.shared.ArtifactFinderManager;
029: import org.sakaiproject.metaobj.shared.model.PersistenceException;
030: import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
031: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
032: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
033: import org.sakaiproject.metaobj.utils.mvc.impl.BindExceptionBase;
034: import org.sakaiproject.authz.api.SecurityService;
035: import org.sakaiproject.authz.api.SecurityAdvisor;
036: import org.springframework.validation.Errors;
037: import org.springframework.validation.ObjectError;
038:
039: public class EditStructuredArtifactDefinitionController extends
040: AddStructuredArtifactDefinitionController implements
041: LoadObjectController {
042: private ArtifactFinder artifactFinder;
043: private SecurityService securityService;
044:
045: public Object fillBackingObject(Object incomingModel, Map request,
046: Map session, Map application) throws Exception {
047: if (session.get(SAD_SESSION_TAG) != null) {
048: return session.remove(SAD_SESSION_TAG);
049: }
050:
051: StructuredArtifactDefinitionBean home = (StructuredArtifactDefinitionBean) incomingModel;
052: home = getStructuredArtifactDefinitionManager().loadHome(
053: home.getId());
054: return home;
055: }
056:
057: protected void save(StructuredArtifactDefinitionBean sad,
058: Errors errors) {
059: //check to see if you have edit permissions
060: checkPermission(SharedFunctionConstants.EDIT_ARTIFACT_DEF);
061:
062: //TODO verify user is system admin, if editting global SAD
063:
064: // todo this should all be done on the server
065: // check only if new xsd has been submitted
066: if (sad.getSchemaFile() != null) {
067:
068: String type = sad.getType().getId().getValue();
069:
070: getSecurityService().pushAdvisor(new SecurityAdvisor() {
071: public SecurityAdvice isAllowed(String userId,
072: String function, String reference) {
073: return SecurityAdvice.ALLOWED;
074: }
075: });
076:
077: try {
078: Collection artifacts = artifactFinder.findByType(type);
079: StructuredArtifactValidator validator = new StructuredArtifactValidator();
080:
081: // validate every artifact against new xsd to determine
082: // whether or not an xsl conversion file is necessary
083: for (Iterator i = artifacts.iterator(); i.hasNext();) {
084: Object obj = i.next();
085: if (obj instanceof StructuredArtifact) {
086: StructuredArtifact artifact = (StructuredArtifact) obj;
087: artifact
088: .setHome(getStructuredArtifactDefinitionManager()
089: .convertToHome(sad));
090: Errors artifactErrors = new BindExceptionBase(
091: artifact, "bean");
092: validator.validate(artifact, artifactErrors);
093: if (artifactErrors.getErrorCount() > 0) {
094: if (sad.getXslConversionFileId() == null
095: || sad.getXslConversionFileId()
096: .getValue().length() == 0) {
097:
098: errors
099: .rejectValue(
100: "schemaFile",
101: "invalid_schema_file_edit",
102: "key missing: invalid_schema_file_edit");
103:
104: for (Iterator x = artifactErrors
105: .getAllErrors().iterator(); x
106: .hasNext();) {
107: ObjectError error = (ObjectError) x
108: .next();
109: logger.warn(error.toString());
110: }
111:
112: return;
113:
114: } else {
115: sad.setRequiresXslFile(true);
116: break;
117: }
118: }
119: }
120: }
121: } finally {
122: getSecurityService().popAdvisor();
123: }
124: }
125:
126: try {
127: getStructuredArtifactDefinitionManager().save(sad);
128: } catch (PersistenceException e) {
129: errors.rejectValue(e.getField(), e.getErrorCode(), e
130: .getErrorInfo(), e.getDefaultMessage());
131: }
132: }
133:
134: public ArtifactFinder getArtifactFinder() {
135: return artifactFinder;
136: }
137:
138: public void setArtifactFinder(ArtifactFinder artifactFinder) {
139: this .artifactFinder = artifactFinder;
140: }
141:
142: public SecurityService getSecurityService() {
143: return securityService;
144: }
145:
146: public void setSecurityService(SecurityService securityService) {
147: this.securityService = securityService;
148: }
149: }
|