001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/mgt/impl/StructuredArtifactValidationServiceImpl.java $
003: * $Id: StructuredArtifactValidationServiceImpl.java 21936 2007-02-26 23:16:32Z 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.mgt.impl;
021:
022: import java.text.MessageFormat;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.jdom.Attribute;
028: import org.jdom.Element;
029: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactValidationService;
030: import org.sakaiproject.metaobj.shared.model.ElementBean;
031: import org.sakaiproject.metaobj.shared.model.ElementListBean;
032: import org.sakaiproject.metaobj.shared.model.ValidationError;
033: import org.sakaiproject.metaobj.shared.model.ElementListBeanWrapper;
034: import org.sakaiproject.metaobj.utils.mvc.intf.FieldValueWrapper;
035: import org.sakaiproject.metaobj.utils.xml.NormalizationException;
036: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
037:
038: /**
039: * Created by IntelliJ IDEA.
040: * User: John Ellis
041: * Date: Aug 18, 2005
042: * Time: 3:07:45 PM
043: * To change this template use File | Settings | File Templates.
044: */
045: public class StructuredArtifactValidationServiceImpl implements
046: StructuredArtifactValidationService {
047:
048: /**
049: * Validate this element from the root.
050: *
051: * @param element filled in element to be validated.
052: * @return list of ValidationError objects. If this list is
053: * returned empty, then the element validated successfully
054: * @see org.sakaiproject.metaobj.shared.model.ValidationError
055: */
056: public List validate(ElementBean element) {
057: List errors = new ArrayList();
058:
059: return validate(element, null, errors);
060: }
061:
062: /**
063: * Validate this element from the root.
064: *
065: * @param element filled in element to be validated.
066: * @param parentName this is the name of the parent of this object.
067: * All fields that have errors will have this name prepended with a "."
068: * @return list of ValidationError objects. If this list is
069: * returned empty, then the element validated successfully
070: * @see org.sakaiproject.metaobj.shared.model.ValidationError
071: */
072: public List validate(ElementBean element, String parentName) {
073: List errors = new ArrayList();
074:
075: return validate(element, parentName, errors);
076: }
077:
078: protected List validate(ElementBean elementBean, String parentName,
079: List errors) {
080: SchemaNode currentNode = elementBean.getCurrentSchema();
081: Element rootElement = elementBean.currentElement();
082:
083: List children = currentNode.getChildren();
084: for (Iterator i = children.iterator(); i.hasNext();) {
085: SchemaNode childSchema = (SchemaNode) i.next();
086:
087: try {
088: if (checkWrappedField(childSchema, elementBean, errors)) {
089: continue;
090: }
091:
092: Object value = elementBean.get(childSchema.getName());
093:
094: if (value instanceof ElementBean) {
095: validate((ElementBean) value, composeName(
096: parentName, childSchema.getName()), errors);
097: if (childSchema.isDataNode()) {
098: ElementBean bean = (ElementBean) value;
099: value = bean.currentElement().getTextTrim();
100: if (value.toString().length() == 0) {
101: value = null;
102: }
103: validateElement(rootElement, childSchema,
104: value, parentName, errors);
105: }
106: } else if (value instanceof ElementListBean) {
107: boolean found = false;
108: for (Iterator iter = ((ElementListBean) value)
109: .iterator(); iter.hasNext();) {
110: found = true;
111: Object currentValue = iter.next();
112: validate((ElementBean) currentValue,
113: composeName(parentName, childSchema
114: .getName()), errors);
115: if (childSchema.isDataNode()) {
116: ElementBean bean = (ElementBean) currentValue;
117: currentValue = bean.currentElement()
118: .getTextTrim();
119: if (currentValue.toString().length() == 0) {
120: currentValue = null;
121: }
122: try {
123: validateChildElement(bean
124: .currentElement(), childSchema,
125: currentValue, parentName,
126: errors);
127: } catch (NormalizationException exp) {
128: errors.add(new ValidationError(
129: childSchema.getLabel(),
130: composeName(parentName,
131: childSchema.getName()),
132: exp.getErrorCode(), exp
133: .getErrorInfo(),
134: MessageFormat.format(exp
135: .getErrorCode(), exp
136: .getErrorInfo())));
137: }
138: }
139: }
140: if (!found) {
141: try {
142: validateChildElement(null, childSchema,
143: null, parentName, errors);
144: } catch (NormalizationException exp) {
145: errors.add(new ValidationError(childSchema
146: .getLabel(), composeName(
147: parentName, childSchema.getName()),
148: exp.getErrorCode(), exp
149: .getErrorInfo(),
150: MessageFormat.format(exp
151: .getErrorCode(), exp
152: .getErrorInfo())));
153: }
154: }
155: } else if (value instanceof ElementListBeanWrapper) {
156: ((ElementListBeanWrapper) value).validate(errors);
157: if (((ElementListBeanWrapper) value).getList()
158: .size() < childSchema.getMinOccurs()) {
159: errors
160: .add(new ValidationError(
161: childSchema.getLabel(),
162: composeName(parentName,
163: childSchema.getName()),
164: NormalizationException.REQIRED_FIELD_ERROR_CODE,
165: new Object[0],
166: NormalizationException.REQIRED_FIELD_ERROR_CODE));
167: }
168: } else if (childSchema.isAttribute()) {
169: Attribute childAttribute = rootElement
170: .getAttribute(childSchema.getName());
171:
172: if (childAttribute != null) {
173: String stringValue = null;
174: if (value != null && value instanceof String) {
175: stringValue = (String) value;
176: value = childSchema
177: .getActualNormalizedValue(stringValue);
178: }
179:
180: childAttribute.setValue(childSchema
181: .getSchemaNormalizedValue(value));
182: } else if (childSchema.getMinOccurs() > 0) {
183: errors
184: .add(new ValidationError(
185: childSchema.getLabel(),
186: composeName(parentName,
187: childSchema.getName()),
188: NormalizationException.REQIRED_FIELD_ERROR_CODE,
189: new Object[0],
190: NormalizationException.REQIRED_FIELD_ERROR_CODE));
191: }
192:
193: } else {
194: validateElement(rootElement, childSchema, value,
195: parentName, errors);
196: }
197: } catch (NormalizationException exp) {
198: errors.add(new ValidationError(childSchema.getLabel(),
199: composeName(parentName, childSchema.getName()),
200: exp.getErrorCode(), exp.getErrorInfo(),
201: MessageFormat.format(exp.getErrorCode(), exp
202: .getErrorInfo())));
203: }
204: }
205:
206: return errors;
207: }
208:
209: protected void validateChildElement(Element parent,
210: Element childElement, SchemaNode childSchema, Object value,
211: String parentName, List errors) {
212: if (value instanceof FieldValueWrapper) {
213: FieldValueWrapper fieldValueWrapper = ((FieldValueWrapper) value);
214: fieldValueWrapper.validate(childSchema.getName(), errors,
215: childSchema.getLabel());
216: value = fieldValueWrapper.getValue();
217: if (value != null && childElement == null) {
218: childElement = new Element(childSchema.getName());
219: parent.addContent(childElement);
220: }
221: }
222: validateChildElement(childElement, childSchema, value,
223: parentName, errors);
224: }
225:
226: protected void validateChildElement(Element childElement,
227: SchemaNode childSchema, Object value, String parentName,
228: List errors) {
229: if (childElement != null) {
230: String stringValue = null;
231: if (value != null && value instanceof String) {
232: stringValue = (String) value;
233: value = childSchema
234: .getActualNormalizedValue(stringValue);
235: }
236:
237: childElement.setText(childSchema
238: .getSchemaNormalizedValue(value));
239: } else if (childSchema.getMinOccurs() > 0) {
240: errors.add(new ValidationError(childSchema.getLabel(),
241: composeName(parentName, childSchema.getName()),
242: NormalizationException.REQIRED_FIELD_ERROR_CODE,
243: new Object[0],
244: NormalizationException.REQIRED_FIELD_ERROR_CODE));
245: }
246: }
247:
248: protected void validateElement(Element rootElement,
249: SchemaNode childSchema, Object value, String parentName,
250: List errors) {
251: validateChildElement(rootElement, rootElement
252: .getChild(childSchema.getName()), childSchema, value,
253: parentName, errors);
254: }
255:
256: protected String composeName(String parentName, String name) {
257: if (parentName == null) {
258: return name;
259: } else {
260: return parentName + "." + name;
261: }
262: }
263:
264: protected boolean checkWrappedField(SchemaNode childSchema,
265: ElementBean elementBean, List errors) {
266: return false;
267: }
268: }
|