001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/matrix/tool/src/java/org/theospi/portfolio/matrix/control/MatrixValidator.java $
003: * $Id: MatrixValidator.java 21737 2007-02-19 20:25:58Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006, 2007 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.matrix.control;
021:
022: import org.sakaiproject.metaobj.security.AuthenticationManager;
023: import org.springframework.validation.Errors;
024: import org.theospi.portfolio.matrix.model.CriterionTransport;
025: import org.theospi.portfolio.matrix.model.LevelTransport;
026: import org.theospi.portfolio.matrix.model.Scaffolding;
027: import org.theospi.portfolio.matrix.model.ScaffoldingCell;
028: import org.theospi.portfolio.matrix.model.ScaffoldingUploadForm;
029: import org.theospi.utils.mvc.impl.ValidatorBase;
030:
031: /**
032: * Created by IntelliJ IDEA.
033: * User: John Ellis
034: * Date: Apr 23, 2004
035: * Time: 2:37:33 PM
036: * To change this template use File | Settings | File Templates.
037: */
038: public class MatrixValidator extends ValidatorBase {
039:
040: private AuthenticationManager authManager;
041:
042: /**
043: * Return whether or not this object can validate objects
044: * of the given class.
045: */
046: public boolean supports(Class clazz) {
047: if (MatrixFormBean.class.isAssignableFrom(clazz))
048: return true;
049: else if (ScaffoldingUploadForm.class.isAssignableFrom(clazz))
050: return true;
051: else if (Scaffolding.class.isAssignableFrom(clazz))
052: return true;
053: else if (ScaffoldingCell.class.isAssignableFrom(clazz))
054: return true;
055: else if (LevelTransport.class.isAssignableFrom(clazz))
056: return true;
057: else if (CriterionTransport.class.isAssignableFrom(clazz))
058: return true;
059: else if (CellAndNodeForm.class.isAssignableFrom(clazz))
060: return true;
061: else if (MatrixGridBean.class.isAssignableFrom(clazz))
062: return true;
063: else
064: return false;
065: }
066:
067: /**
068: * Validate a presentation object, which must be of a class for which
069: * the supports() method returned true.
070: *
071: * @param obj Populated object to validate
072: * @param errors Errors object we're building. May contain
073: * errors for this field relating to types.
074: */
075: public void validate(Object obj, Errors errors) {
076: //if (obj instanceof ScaffoldingUploadForm)
077: // validateScaffoldingImport((ScaffoldingUploadForm)obj, errors);
078: if (obj instanceof CriterionTransport)
079: validateCriterion((CriterionTransport) obj, errors);
080: else if (obj instanceof LevelTransport)
081: validateLevel((LevelTransport) obj, errors);
082: else if (obj instanceof Scaffolding) {
083: Scaffolding scaffolding = (Scaffolding) obj;
084: if (scaffolding.isValidate())
085: validateScaffolding(scaffolding, errors);
086: } else if (obj instanceof ScaffoldingCell) {
087: ScaffoldingCell scaffoldingCell = (ScaffoldingCell) obj;
088: if (scaffoldingCell.isValidate())
089: validateScaffoldingCell(scaffoldingCell, errors);
090: } else if (obj instanceof CellAndNodeForm)
091: validateCellAttachment((CellAndNodeForm) obj, errors);
092: else if (obj instanceof MatrixGridBean)
093: validateScaffolding(
094: ((MatrixGridBean) obj).getScaffolding(), errors);
095: }
096:
097: /*
098: private void validateScaffoldingImport(ScaffoldingUploadForm obj, Errors errors) {
099: RepositoryNode node = (RepositoryNode)getRepositoryManager().getRootNode(getAuthManager().getAgent());
100: if (node.hasChild(obj.getDisplayName())) {
101: errors.rejectValue("displayName", "duplicate", "duplicate");
102: }
103: if (!obj.getUploadedScaffoldingForm().getContentType().equals("text/xml")) {
104: errors.rejectValue("uploadedScaffoldingForm", "invalid file", "invalid file");
105: }
106: }
107: */
108:
109: protected void validateCellAttachment(CellAndNodeForm form,
110: Errors errors) {
111: if (form.getNode_id() == null || form.getNode_id().equals("")) {
112: errors.rejectValue("node_id", "error.required", "required");
113: }
114: }
115:
116: protected void validateScaffoldingCell(
117: ScaffoldingCell scaffoldingCell, Errors errors) {
118: if (scaffoldingCell.getInitialStatus() == null
119: || scaffoldingCell.getInitialStatus().equals("")) {
120: errors.rejectValue("initialStatus", "error.required",
121: "required");
122: }
123: if (scaffoldingCell.getTitle() == null
124: || scaffoldingCell.getTitle().trim().equals("")) {
125: errors.rejectValue("title", "error.required", "required");
126: }
127: }
128:
129: protected void validateCriterion(CriterionTransport criterion,
130: Errors errors) {
131: if (criterion.getDescription() == null
132: || criterion.getDescription().trim().equals("")) {
133: errors.rejectValue("description", "error.required",
134: "required");
135: }
136: }
137:
138: protected void validateLevel(LevelTransport level, Errors errors) {
139: if (level.getDescription() == null
140: || level.getDescription().trim().equals("")) {
141: errors.rejectValue("description", "error.required",
142: "required");
143: }
144: }
145:
146: protected void validateScaffolding(Scaffolding scaffolding,
147: Errors errors) {
148: if (scaffolding.getTitle() == null
149: || scaffolding.getTitle().trim().equals("")) {
150: errors.rejectValue("title", "error.required", "required");
151: }
152: if (scaffolding.getLevels() == null
153: || scaffolding.getLevels().size() == 0) {
154: errors.rejectValue("levels", "error.required", "required");
155: }
156: if (scaffolding.getCriteria() == null
157: || scaffolding.getCriteria().size() == 0) {
158: errors
159: .rejectValue("criteria", "error.required",
160: "required");
161: }
162: }
163:
164: /*
165: private String stripHtml(String input) {
166: return input.replaceAll("<[\\w/]+[^<>]*>", "");
167: }
168: */
169: /**
170: * @return Returns the authManager.
171: */
172: public AuthenticationManager getAuthManager() {
173: return authManager;
174: }
175:
176: /**
177: * @param authManager The authManager to set.
178: */
179: public void setAuthManager(AuthenticationManager authManager) {
180: this.authManager = authManager;
181: }
182: }
|