package nl.quintor.commons.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

/**
 * Utility class (reduced version) with generic functionality neutral to any application.
 *  
 * @author Benny Bottema
 */
public final class GenericUtils {

	/**
	 * Private constructor for this <code>final</code> class; this class cannot 
	 * be instantiated; it is a utility class.
	 */
	private GenericUtils() {
	};

	/**
	 * Clones bean using Apache's {@link BeanUtils#cloneBean}
	 */
	public static Object cloneBean(final Object bean) {
		try {
			return BeanUtils.cloneBean(bean);
		} catch (final IllegalAccessException e) {
			logger.error("error cloning bean", e);
			throw new RuntimeException(e.getMessage(), e);
		} catch (final InstantiationException e) {
			logger.error("error cloning bean", e);
			throw new RuntimeException(e.getMessage(), e);
		} catch (final InvocationTargetException e) {
			logger.error("error cloning bean", e);
			throw new RuntimeException(e.getMessage(), e);
		} catch (final NoSuchMethodException e) {
			logger.error("error cloning bean", e);
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * Takes over all properties from the source bean through getters, 
	 * and projects them on the target bean through setters, using Apache's 
	 * {@link BeanUtils#copyProperties(Object, Object)}.
	 */
	public static void projectBean(final Object source, final Object target) {
		try {
			BeanUtils.copyProperties(target, source);
		} catch (IllegalAccessException e) {
			logger.error("fout bij overnemen bean properties", e);
			throw new RuntimeException(e.getMessage(), e);
		} catch (InvocationTargetException e) {
			logger.error("fout bij overnemen bean properties", e);
			throw new RuntimeException(e.getMessage(), e);
		}
	}
}

