001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/utils/mvc/impl/SimpleValidator.java $
003: * $Id:SimpleValidator.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.utils.mvc.impl;
021:
022: import java.beans.IntrospectionException;
023: import java.beans.PropertyDescriptor;
024: import java.lang.reflect.InvocationTargetException;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.springframework.validation.Errors;
032: import org.springframework.validation.Validator;
033: import org.theospi.portfolio.shared.model.OspException;
034:
035: public class SimpleValidator extends ValidatorBase implements Validator {
036: protected final transient Log logger = LogFactory
037: .getLog(getClass());
038:
039: private List requiredFields;
040: private String messageCode = "Required";
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: return true;
048: }
049:
050: /**
051: * Validate an object, which must be of a class for which
052: * the supports() method returned true.
053: *
054: * @param obj Populated object to validate
055: * @param errors Errors object we're building. May contain
056: * errors for this field relating to types.
057: */
058: public void validate(Object obj, Errors errors) {
059: for (Iterator i = requiredFields.iterator(); i.hasNext();) {
060: String field = (String) i.next();
061: validate(field, obj, errors);
062: }
063: }
064:
065: protected void validate(String field, Object obj, Errors errors) {
066: if (obj instanceof Map) {
067: Map map = (Map) obj;
068: if (map.get(field) == null) {
069: errors.rejectValue(field, messageCode, messageCode);
070: }
071: } else {
072: PropertyDescriptor prop = null;
073: try {
074: prop = new PropertyDescriptor(field, obj.getClass());
075:
076: Object value = prop.getReadMethod().invoke(obj,
077: new Object[] {});
078: if (value == null || value.toString().length() == 0) {
079: errors.rejectValue(field, messageCode, messageCode);
080: }
081: } catch (IntrospectionException e) {
082: logger.error("", e);
083: throw new OspException(e);
084: } catch (IllegalAccessException e) {
085: logger.error("", e);
086: throw new OspException(e);
087: } catch (InvocationTargetException e) {
088: logger.error("", e);
089: throw new OspException(e);
090: }
091: }
092: }
093:
094: public String getMessageCode() {
095: return messageCode;
096: }
097:
098: public void setMessageCode(String messageCode) {
099: this .messageCode = messageCode;
100: }
101:
102: public List getRequiredFields() {
103: return requiredFields;
104: }
105:
106: public void setRequiredFields(List requiredFields) {
107: this.requiredFields = requiredFields;
108: }
109: }
|