001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/PresentationValidator.java $
003: * $Id:PresentationValidator.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.portfolio.presentation.control;
021:
022: import org.jdom.Element;
023: import org.jdom.IllegalNameException;
024: import org.sakaiproject.metaobj.utils.TypedMap;
025: import org.springframework.validation.Errors;
026: import org.theospi.portfolio.presentation.model.Presentation;
027: import org.theospi.portfolio.presentation.model.PresentationItem;
028: import org.theospi.portfolio.presentation.model.PresentationItemDefinition;
029: import org.theospi.portfolio.presentation.model.PresentationLayout;
030: import org.theospi.portfolio.presentation.model.PresentationTemplate;
031: import org.theospi.portfolio.presentation.model.TemplateFileRef;
032: import org.theospi.utils.mvc.impl.ValidatorBase;
033:
034: /**
035: * Created by IntelliJ IDEA.
036: * User: John Ellis
037: * Date: Apr 23, 2004
038: * Time: 2:37:33 PM
039: * To change this template use File | Settings | File Templates.
040: */
041: public class PresentationValidator extends ValidatorBase {
042:
043: /**
044: * Return whether or not this object can validate objects
045: * of the given class.
046: */
047: public boolean supports(Class clazz) {
048: if (PresentationTemplate.class.isAssignableFrom(clazz))
049: return true;
050: if (PresentationItemDefinition.class.isAssignableFrom(clazz))
051: return true;
052: if (Presentation.class.isAssignableFrom(clazz))
053: return true;
054: if (PresentationItem.class.isAssignableFrom(clazz))
055: return true;
056: if (TemplateFileRef.class.isAssignableFrom(clazz))
057: return true;
058: if (PresentationLayout.class.isAssignableFrom(clazz))
059: return true;
060: return false;
061: }
062:
063: /**
064: * Validate a presentation object, which must be of a class for which
065: * the supports() method returned true.
066: *
067: * @param obj Populated object to validate
068: * @param errors Errors object we're building. May contain
069: * errors for this field relating to types.
070: */
071: public void validate(Object obj, Errors errors) {
072: if (obj instanceof PresentationTemplate)
073: validateTemplate(obj, errors);
074: if (obj instanceof PresentationItem)
075: validateItem(obj, errors);
076: if (obj instanceof PresentationItemDefinition)
077: validateItemDefinition(obj, errors);
078: if (obj instanceof Presentation)
079: validatePresentation(obj, errors);
080: if (obj instanceof TemplateFileRef)
081: validateTemplateFileRef((TemplateFileRef) obj, errors);
082: if (obj instanceof PresentationLayout)
083: validateLayout((PresentationLayout) obj, errors);
084: }
085:
086: protected void validateTemplateFileRef(
087: TemplateFileRef templateFileRef, Errors errors) {
088: if (templateFileRef.getUsage() == null
089: || templateFileRef.getUsage().equals("")) {
090: errors.rejectValue("usage", "error.required", "required");
091: } else if (!isValidXMLElementName(templateFileRef.getUsage())) {
092: errors.rejectValue("usage", "error.invalidXmlElementName",
093: "invalid name");
094: }
095: if (templateFileRef.getFileId() == null) {
096: errors.rejectValue("fileId", "error.required", "required");
097: }
098: }
099:
100: protected void validateTemplateFirstPage(Object obj, Errors errors) {
101: PresentationTemplate template = (PresentationTemplate) obj;
102: if (template.getName() == null
103: || template.getName().length() == 0) {
104: errors.rejectValue("name", "error.required", "required");
105: }
106: if (template.getDescription() != null
107: && template.getDescription().length() > 255) {
108: errors.rejectValue("description", "error.lengthExceded",
109: new Object[] { "255" },
110: "Value must be less than {0} characters");
111: }
112: }
113:
114: protected void validateTemplateSecondPage(Object obj, Errors errors) {
115: PresentationTemplate template = (PresentationTemplate) obj;
116: if (template.getRenderer() == null
117: || template.getRenderer().getValue() == null
118: || template.getRenderer().getValue().length() == 0) {
119: errors
120: .rejectValue("renderer", "error.required",
121: "required");
122: }
123: }
124:
125: protected void validateTemplateThirdPage(Object obj, Errors errors) {
126: PresentationTemplate template = (PresentationTemplate) obj;
127:
128: if (template.getItem().getAction() != null
129: && template.getItem().getAction().equals("addItem")) {
130: if (template.getItem().getType() == null
131: || template.getItem().getType().length() == 0) {
132: errors.rejectValue("item.type", "error.required",
133: "required");
134: }
135: if (template.getItem().getName() == null
136: || template.getItem().getName().length() == 0) {
137: errors.rejectValue("item.name", "error.required",
138: "required");
139: } else if (!isValidXMLElementName(template.getItem()
140: .getName())) {
141: errors.rejectValue("item.name",
142: "error.invalidXmlElementName", "invalid name");
143: }
144: if (template.getItem().getTitle() == null
145: || template.getItem().getTitle().length() == 0) {
146: errors.rejectValue("item.title", "error.required",
147: "required");
148: }
149: }
150: }
151:
152: protected void validateTemplateFourthPage(Object obj, Errors errors) {
153: PresentationTemplate template = (PresentationTemplate) obj;
154:
155: if (template.getFileRef().getAction() != null
156: && template.getFileRef().getAction().equals("addFile")) {
157: if (template.getFileRef().getUsage() == null
158: || template.getFileRef().getUsage().length() == 0) {
159: errors.rejectValue("fileRef.usage", "error.required",
160: "required");
161: } else if (!isValidXMLElementName(template.getFileRef()
162: .getUsage())) {
163: errors.rejectValue("fileRef.usage",
164: "error.invalidXmlElementName", "invalid name");
165: }
166: if (template.getFileRef().getFileId() == null
167: || template.getFileRef().getFileId().length() == 0) {
168: errors.rejectValue("fileRef.fileId", "error.required",
169: "required");
170: }
171: }
172: }
173:
174: protected void validateTemplate(Object obj, Errors errors) {
175: validateTemplateFirstPage(obj, errors);
176: validateTemplateSecondPage(obj, errors);
177: validateTemplateThirdPage(obj, errors);
178: validateTemplateFourthPage(obj, errors);
179: }
180:
181: protected void validateItem(Object obj, Errors errors) {
182: PresentationItem item = (PresentationItem) obj;
183:
184: }
185:
186: protected boolean isValidXMLElementName(String name) {
187: try {
188: Element element = new Element(name);
189: } catch (IllegalNameException e) {
190: return false;
191: }
192: return true;
193: }
194:
195: protected void validateItemDefinition(Object obj, Errors errors) {
196: PresentationItemDefinition itemDef = (PresentationItemDefinition) obj;
197: if (itemDef.getName() == null
198: || itemDef.getName().length() == 0) {
199: errors.rejectValue("name", "error.required",
200: "name is required");
201: }
202: if (itemDef.getTitle() == null
203: || itemDef.getTitle().length() == 0) {
204: errors.rejectValue("title", "error.required",
205: "title is required");
206: }
207:
208: }
209:
210: protected void validatePresentation(Object obj, Errors errors) {
211: validatePresentationInitialPage(obj, errors);
212: validatePresentationFirstPage(obj, errors);
213: validatePresentationSecondPage(obj, errors);
214: validatePresentationThirdPage(obj, errors);
215: }
216:
217: protected void validatePresentationInitialPage(Object obj,
218: Errors errors) {
219: Presentation presentation = (Presentation) obj;
220: if (presentation.getPresentationType().equals(
221: Presentation.TEMPLATE_TYPE)) {
222: if (presentation.getTemplate().getId() == null
223: || presentation.getTemplate().getId().getValue() == null
224: || presentation.getTemplate().getId().getValue()
225: .length() == 0) {
226: errors.rejectValue("template.id", "error.required",
227: "template is required");
228: }
229: }
230: }
231:
232: protected void validatePresentationFirstPage(Object obj,
233: Errors errors) {
234: Presentation presentation = (Presentation) obj;
235: if (presentation.getName() == null
236: || presentation.getName().length() == 0) {
237: errors.rejectValue("name", "error.required",
238: "name is required");
239: }
240:
241: if (presentation.getPresentationType().equals(
242: Presentation.FREEFORM_TYPE)) {
243: presentation.getTemplate().setId(
244: Presentation.FREEFORM_TEMPLATE_ID);
245: }
246:
247: if (presentation.getTemplate().getId() == null
248: || presentation.getTemplate().getId().getValue() == null
249: || presentation.getTemplate().getId().getValue()
250: .length() == 0) {
251: errors.rejectValue("template.id", "error.required",
252: "template is required");
253: }
254:
255: if (presentation.getExpiresOnBean() != null) {
256: presentation.setExpiresOn(presentation.getExpiresOnBean()
257: .getDate());
258: }
259:
260: if (presentation.getDescription() != null
261: && presentation.getDescription().length() > 255) {
262: errors.rejectValue("description", "error.lengthExceded",
263: new Object[] { "255" },
264: "Value must be less than {0} characters");
265: }
266:
267: }
268:
269: protected void validatePresentationSecondPage(Object obj,
270: Errors errors) {
271: Presentation presentation = (Presentation) obj;
272: }
273:
274: protected void validatePresentationThirdPage(Object obj,
275: Errors errors) {
276: Presentation presentation = (Presentation) obj;
277:
278: }
279:
280: public void validatePresentationProperties(
281: Presentation presentation, Errors errors) {
282: TypedMap properties = presentation.getProperties();
283: if (properties != null
284: && presentation.getTemplate().getDocumentRoot()
285: .length() != 0) {
286: PresentationPropertiesValidator propertyValidator = new PresentationPropertiesValidator();
287: pushNestedPath("properties.", errors);
288: propertyValidator.validate(properties, errors);
289: popNestedPath(errors);
290: }
291: }
292:
293: protected void validateLayout(PresentationLayout layout,
294: Errors errors) {
295: if (layout.isValidate()) {
296: if (layout.getName() == null
297: || layout.getName().length() == 0) {
298: errors.rejectValue("name", "error.required",
299: "name is required");
300: }
301: if (layout.getXhtmlFileId() == null
302: || layout.getXhtmlFileId().getValue() == null
303: || layout.getXhtmlFileId().getValue().length() == 0) {
304: errors.rejectValue("xhtmlFileId", "error.required",
305: "XHTML file is required");
306: }
307: if (layout.getDescription() != null
308: && layout.getDescription().length() > 255) {
309: errors.rejectValue("description",
310: "error.lengthExceded", new Object[] { "255" },
311: "Value must be less than {0} characters");
312: }
313: }
314: }
315: }
|