MultipleListCriterion.java :  » J2EE » Mdarad » org » mdarad » framework » util » struts » criteria » Java Open Source

Java Open Source » J2EE » Mdarad 
Mdarad » org » mdarad » framework » util » struts » criteria » MultipleListCriterion.java
/*
    Mdarad-Toolobox is a collection of tools for Architected RAD
    (Rapid Application Development) based on an MDA approach.
    The toolbox contains frameworks and generators for many environments
    (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
    applications from a design Model
    Copyright (C) 2004-2005  Elapse Technologies Inc.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.mdarad.framework.util.struts.criteria;

import java.util.Locale;

import org.mdarad.framework.expr.Criterion;
import org.mdarad.framework.expr.OperatorTypes;
import org.mdarad.framework.util.ClassUtils;
import org.mdarad.framework.util.ClassUtilsException;

/**
 * This class represents a search criterion that presents a list of values to the user.
 * To use this criterion, you need to set a {@link List List} of {@link CriterionListElement CriterionListElement} 
 * to the criterion.  This list will be displayed as the selection of values and can have a multiple
 * selection.
 * @author Philippe Brouillette
 * @version 1.0
 */
public class MultipleListCriterion extends ListCriterion {

    /**
     * Constructor that takes all the properties to initialize a search
     * criterion.  By default, the criterion is dynamic.
     * @param name name of the criterion.  This name must be unique
     *             as it is a key in a map.
     * @param associatedEntity class type associated to this criterion
     * @param property property used for the query criterion
     * @param bundleName bundle name
     * @param locale locale information
     * @see FormCriterion#FormCriterion(String, CriterionProperty, String, Locale) FormCriterion
     */
    public MultipleListCriterion(String name, Class associatedEntity, CriterionProperty property, String bundleName, Locale locale, Class elementType) {
        super(name, associatedEntity, property, bundleName, locale);
        this.elementType = elementType; 
    }

    /**
     * Constructor that clones a query criterion.  This constructor is used
     * to instanciate a criterion in a search form when the form
     * contains dynamic criteria.
     * @param criterion query criterion that must be of
     *                  type <code>MultipleListCriterion</code>
     * @see FormCriterion#FormCriterion(FormCriterion) FormCriterion
     */
    public MultipleListCriterion(MultipleListCriterion criterion) {
        super(criterion);
        setList(criterion.getList());
        this.elementType = criterion.getElementType();
    }
    
    /**
     * Method that assigns the value of the criterion as a String. This method
     * is needed for the user interface which uses only string values.
     * @param value value to be assigned
     * @see FormCriterion#setPropertyValue(java.lang.String)
     */
    public void setPropertyValues(String[] values) {

        if (values != null) {
            Object[] objValues = new Object[values.length];
            // construct the object type with the string constructor
            for (int i = 0; i < objValues.length; i++) {
                Class[] argTypes = new Class[] { String.class };
                try {
                objValues[i] = ClassUtils.getInstance(
                                    getElementType(),
                                    argTypes,
                                    new Object[] { values[i] }
                                    );
                } catch (ClassUtilsException cue) {
                    throw new CriterionException(cue);
                }
            }
            setValues(objValues);
        }
    }

    /**
     * Method that returns the values selected by the user
     * in string format.
     * @return array of the string values selected by the
     *         user.
     */    
    public String[] getPropertyValues() {
        return (String[]) getValue();
    }

    /**
     * Method used to keep the list of elements selected by the user
     * @param values selected values.
     */
    public void setValues(Object[] values) {
        // vrifier que les valeurs existents        
        if (values != null) {
            setValue(values);
        }
    }
    
    /**
     * Property that keeps the object type that is kept in
     * the list of objects.
     * By default, the type is <code>Object[]</code>
     */
    private Class objectType = Object[].class;
    
    /**
     * Method that sets the object type
     * @param type object class type
     */
    private void setObjectType(Class type) {
        this.objectType = type; 
    }

    /**
     * Returns the object class type used by the list of possible values.
     * 
     * @see FormCriterion#getObjectType()
     */
    public Class getObjectType() {
        return objectType;
    }

    /**
     * This method is not implemented because it should not be used.
     * @exception UnsupportedOperationException
     * @see @see FormCriterion#setPropertyValue(java.lang.String)
     */
    public void setPropertyValue(String object) {
        throw new UnsupportedOperationException("The setPropertyValue with String argument is not supported");
    }

    /**
     * Method that returns the criterion from the expression
     * framework.  ({@link Criterion})
     * <p><b>NOTE:</b> The implementation must take care of the type
     * of operator that is used to determine the criterion to use.
     * @return the criterion
     */
    public Criterion getExprCriterion() {
        Criterion crit = 
            new Criterion(getAssociatedEntity(), 
                          getProperty().getName(), 
                          OperatorTypes.IN, 
                          (Object[]) getValue());
        return crit;
    }

    /**
     * Method that returns the displaying pattern.
     * 
     * @see FormCriterion#getFormPattern()
     */
    public CriterionFormPattern getFormPattern() {
        return CriterionFormPatterns.NO_OPERATOR_MULTIPLE_LIST;
    }

    /**
     * Property that keeps the type of each element of the list
     */
    private Class elementType = String.class;
    
    public Class getElementType() {
        return elementType;
    }

    public void setElementType(Class type) {
        elementType = type;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.