org.vulpe.commons.util.VulpeBeanComparatorUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.vulpe.commons.util.VulpeBeanComparatorUtil.java

Source

/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 * 
 * Este programa  software livre; voc pode redistribu-lo e/ou
 * modific-lo sob os termos da Licena Pblica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a verso 2 da
 * Licena como (a seu critrio) qualquer verso mais nova.
 * 
 * Este programa  distribudo na expectativa de ser til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia implcita de
 * COMERCIALIZAO ou de ADEQUAO A QUALQUER PROPSITO EM
 * PARTICULAR. Consulte a Licena Pblica Geral GNU para obter mais
 * detalhes.
 * 
 * Voc deve ter recebido uma cpia da Licena Pblica Geral GNU
 * junto com este programa; se no, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 * 
 * This program 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
 * of the License, or (at your option) any later version.
 * 
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.vulpe.commons.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Transient;

import org.apache.commons.lang.IllegalClassException;
import org.apache.commons.lang.NullArgumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vulpe.commons.helper.VulpeConfigHelper;
import org.vulpe.model.annotations.SkipCompare;
import org.vulpe.model.entity.VulpeEntity;

/**
 * Utility class to compare beans.
 * 
 */
public class VulpeBeanComparatorUtil {

    private static final Logger LOG = LoggerFactory.getLogger(VulpeBeanComparatorUtil.class);

    /**
     * Compare beans and return map with differences by field.
     * 
     * @param bean1
     * @param bean2
     * @return Map with differences by field.
     */
    public static Map<String, Object[]> compare(final Object bean1, final Object bean2, boolean skipCollections,
            boolean skipTransient) {
        if (VulpeValidationUtil.isEmpty(bean1, bean2)) {
            throw new NullArgumentException("bean1(" + bean1 + ") bean2(" + bean2 + ")");
        }
        if (VulpeValidationUtil.isNotEmpty(bean1, bean2) && !bean1.getClass().equals(bean2.getClass())) {
            throw new IllegalClassException(bean1.getClass(), bean2.getClass());
        }
        final Class<?> baseClass = bean1 != null ? bean1.getClass() : bean2.getClass();
        final Map<String, Object[]> diffMap = new HashMap<String, Object[]>();
        final List<Field> fields = VulpeReflectUtil.getFields(baseClass);
        for (final Field field : fields) {
            if (VulpeReflectUtil.isAnnotationInField(SkipCompare.class, baseClass, field)
                    || (skipCollections && Collection.class.isAssignableFrom(field.getType()))
                    || (skipTransient && (Modifier.isTransient(field.getModifiers())
                            || field.isAnnotationPresent(Transient.class)))) {
                continue;
            }
            try {
                Object value1 = VulpeValidationUtil.isNotEmpty(bean1)
                        ? VulpeReflectUtil.getFieldValue(bean1, field.getName())
                        : null;
                Object value2 = VulpeValidationUtil.isNotEmpty(bean2)
                        ? VulpeReflectUtil.getFieldValue(bean2, field.getName())
                        : null;
                if (VulpeValidationUtil.isNull(value1, value2)) {
                    continue;
                }
                boolean diff = false;
                if ((VulpeValidationUtil.isEmpty(value1) && VulpeValidationUtil.isNotEmpty(value2))
                        || (VulpeValidationUtil.isNotEmpty(value1) && VulpeValidationUtil.isEmpty(value2))) {
                    diff = true;
                    if (Date.class.isAssignableFrom(field.getType())) {
                        if (VulpeValidationUtil.isNotEmpty(value1)) {
                            if (value1 instanceof Timestamp) {
                                value1 = VulpeDateUtil.getDate((Date) value1,
                                        VulpeConfigHelper.getDateTimePattern());
                            } else {
                                value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDatePattern());
                            }
                        }
                        if (VulpeValidationUtil.isNotEmpty(value2)) {
                            if (value2 instanceof Timestamp) {
                                value2 = VulpeDateUtil.getDate((Date) value2,
                                        VulpeConfigHelper.getDateTimePattern());
                            } else {
                                value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDatePattern());
                            }
                        }
                    } else if (VulpeEntity.class.isAssignableFrom(field.getType())) {
                        if (VulpeValidationUtil.isNotEmpty(value1)) {
                            value1 = ((VulpeEntity<?>) value1).getId();
                        }
                        if (VulpeValidationUtil.isNotEmpty(value2)) {
                            value2 = ((VulpeEntity<?>) value2).getId();
                        }
                    }
                } else {
                    if (Date.class.isAssignableFrom(field.getType())) {
                        if (((Date) value1).getTime() != ((Date) value2).getTime()) {
                            if (value1 instanceof Timestamp || value2 instanceof Timestamp) {
                                value1 = VulpeDateUtil.getDate((Date) value1,
                                        VulpeConfigHelper.getDateTimePattern());
                                value2 = VulpeDateUtil.getDate((Date) value2,
                                        VulpeConfigHelper.getDateTimePattern());
                            } else {
                                value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDatePattern());
                                value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDatePattern());
                            }
                            diff = true;
                        }
                    } else if (VulpeEntity.class.isAssignableFrom(field.getType())) {
                        if (!((VulpeEntity<?>) value1).getId().equals(((VulpeEntity<?>) value2).getId())) {
                            value1 = ((VulpeEntity<?>) value1).getId();
                            value2 = ((VulpeEntity<?>) value2).getId();
                            diff = true;
                        }
                    } else if (!value1.equals(value2)) {
                        diff = true;
                    }
                }
                if (diff) {
                    diffMap.put(field.getName(), new Object[] { value1, value2 });
                }
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
        }
        return diffMap;
    }

    public static boolean isDifferent(final Object bean1, final Object bean2) {
        return !compare(bean1, bean2, false, true).isEmpty();
    }

}