Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / Java / Java SE / J2EE

Modular J2EE, Using Spring DM, Zk and EclipseLink

Rate me:
Please Sign up or sign in to vote.
4.92/5 (9 votes)
21 Apr 2011CPOL15 min read 53K   8  
This article describes how to create a basic modular Java Enterprise Application, by using these Open Source frameworks
package com.cp.adrabi.web.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ValidatorFactory;
import javax.validation.groups.Default;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.WrongValuesException;
import org.zkoss.zk.ui.util.GenericForwardComposer;

/**
 * Base of all controllers
 * 
 * @author ADRABI Abderrahim (z3vil)
 *
 */
public class BasicComposer extends GenericForwardComposer
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ValidatorFactory validatorFatory;
	
	private List<WrongValueException> wrongValues = new ArrayList<WrongValueException>();

	/**
	 * Validation JSR 303
	 * 
	 * @param <T>: anonymous type
	 * @param comp: component
	 * @param beanClass: bean class to validate
	 * @param propertyName: property name to check and validate
	 * @param value: value in question
	 */
	protected <T> void validateValue(final Component comp,final Class<T> beanClass, final String propertyName, final Object value) 
	{
		final Set<ConstraintViolation<T>> violations = this.validatorFatory.getValidator().validateValue(beanClass,propertyName, value, Default.class);
		
		// only one violation
		if( violations.size() == 1 )
		{
			this.wrongValues.add(new WrongValueException(comp, violations.iterator().next().getMessage()));
		}
		// many violations
		else if( violations.size() > 1 )
		{
			StringBuilder builder = new StringBuilder();
			for(ConstraintViolation<T> cv : violations)
			{
				builder.append(cv.getMessage() + "\n");
			}
			builder.deleteCharAt(builder.length() - 1);
			this.wrongValues.add(new WrongValueException(comp, builder.toString()));
		}
	}
	
	/**
	 * Fire validation for throwing exceptions
	 * 
	 * @throws WrongValuesException
	 */
	protected void fireValidation() throws WrongValuesException
	{
		if(!this.wrongValues.isEmpty())
		{
			WrongValueException[] wrongs = this.wrongValues.toArray(new WrongValueException[]{});
			this.wrongValues.clear();
			throw new WrongValuesException(wrongs);
		}
	}
	
	public ValidatorFactory getValidatorFatory()
	{
		return validatorFatory;
	}

	public void setValidatorFatory(ValidatorFactory validatorFatory)
	{
		this.validatorFatory = validatorFatory;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Morocco Morocco
Adrabi!, Just another Ghost in the Shell =)

Comments and Discussions