All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.cornell.mannlib.vedit.util.FormUtils Maven / Gradle / Ivy

/* $This file is distributed under the terms of the license in LICENSE$ */

package edu.cornell.mannlib.vedit.util;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vedit.beans.EditProcessObject;
import edu.cornell.mannlib.vedit.beans.FormObject;
import edu.cornell.mannlib.vedit.beans.Option;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;

public class FormUtils {

    protected static final Log log = LogFactory.getLog(FormUtils.class.getName());
    protected static final int BASE_10 = 10;
    protected static final Class[] SUPPORTED_TYPES = { String.class,
            int.class,
            Integer.class,
            boolean.class,
            Date.class
          };

    protected static final List SUPPORTED_TYPE_LIST = Arrays
            .asList(SUPPORTED_TYPES);

    /* this class needs to be reworked */

    public static void populateFormFromBean (Object bean,
    		                                 String action,
    		                                 FormObject foo) {
        populateFormFromBean(bean,action,null,foo,new HashMap());
    }

    public static void populateFormFromBean (Object bean,
    		                                 String action,
    		                                 FormObject foo,
    		                                 Map badValuesHash) {
        populateFormFromBean(bean,action,null,foo,badValuesHash);
    }

    /**
     * Populates form objects with bean values
     */
    public static void populateFormFromBean (Object bean,
    		                                 String action,
    		                                 EditProcessObject epo,
    		                                 FormObject foo,
    		                                 Map BadValuesHash) {
        Class beanClass =
        	    (epo != null && epo.getBeanClass() != null)
        	            ? epo.getBeanClass()
        	            : bean.getClass();

        Method[] meths = beanClass.getMethods();

        for (Method currMeth : meths) {

            if (currMeth.getName().indexOf("set") == 0) {

                // we have a setter method
                Class[] currMethParamTypes = currMeth.getParameterTypes();
                Class currMethType = currMethParamTypes[0];

                if (SUPPORTED_TYPE_LIST.contains(currMethType)) {
                    //we only want people directly to type in ints, strings, and dates
                    //of course, most of the ints are probably foreign keys anyway...

                    String elementName = currMeth.getName().substring(
                            3, currMeth.getName().length());

                    //see if there's something in the bean using
                    //the related getter method
                    try {
                        Method getter = beanClass.getMethod(
                                "get" + elementName, (Class[]) null);
                        Object existingData = null;
                        try {
                            existingData = getter.invoke(bean, (Object[]) null);
                        } catch (Exception e) {
                            log.error("Exception invoking getter method");
                        }
                        String value = "";
                        if (existingData != null) {
                            if (existingData instanceof String) {
                                value += existingData;
                            } else if (!(existingData instanceof Integer
                                    && (Integer) existingData < 0)) {
                                value += existingData.toString();
                            }
                        }
                        String badValue = (String) BadValuesHash.get(elementName);
                        if (badValue != null) {
                            value = badValue;
                        }
                        foo.getValues().put(elementName, value);
                    } catch (NoSuchMethodException e) {
                        //ignore it
                    }
                }
            }
        }
    }

    public static List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy