com.cnd.greencube.server.business.impl.BaseBusinessImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.cnd.greencube.server.business.impl.BaseBusinessImpl.java

Source

/*
 * Copyright 2005-2020 Top Team All rights reserved.
 * Support: 
 * License: top team license
 */
package com.cnd.greencube.server.business.impl;

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.cnd.greencube.server.business.BaseBusiness;
import com.cnd.greencube.server.dao.BaseDao;
import com.cnd.greencube.server.entity.BaseEntity;
import com.cnd.greencube.server.util.PageInfo;

/**
 * Service - 
 */
@Transactional
public class BaseBusinessImpl<T, ID extends Serializable> implements BaseBusiness<T, ID> {
    /**  */
    @SuppressWarnings("unused")
    private Class<T> entityClass;

    /**  */
    private static final String[] UPDATE_IGNORE_PROPERTIES = new String[] { BaseEntity.ID_PROPERTY_NAME };

    /** baseDao */
    protected BaseDao<T, ID> dao;

    @SuppressWarnings("unchecked")
    public BaseBusinessImpl() {
        Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
            entityClass = (Class<T>) parameterizedType[0];
        }
    }

    public void setDao(BaseDao<T, ID> baseDao) {
        this.dao = baseDao;
    }

    public BaseDao<T, ID> getDao() {
        return this.dao;
    }

    @Transactional(readOnly = true)
    public T get(ID id) {
        return dao.get(id);
    }

    @Transactional(readOnly = true)
    public List<T> findAll() {
        return findList(null, null, null, null);
    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public List<T> findList(ID... ids) {
        List<T> result = new ArrayList<T>();
        if (ids != null) {
            for (ID id : ids) {
                T entity = get(id);
                if (entity != null) {
                    result.add(entity);
                }
            }
        }
        return result;
    }

    @Transactional(readOnly = true)
    public List<T> findList(String jpql) {
        return dao.findList(jpql);
    }

    @Transactional(readOnly = true)
    public List<T> findList(String jpql, Object[] params) {
        return dao.findList(jpql, params);
    }

    @Transactional(readOnly = true)
    public List<T> findList(final String jpql, final Object[] params, final int firstResult, final int maxResults) {
        return dao.findList(jpql, params, firstResult, maxResults);
    }

    @Transactional(readOnly = true)
    public List<T> findList(final String jpql, final String countQl, final Object[] params, PageInfo pageInfo) {
        return dao.findList(jpql, countQl, params, pageInfo);
    }

    @Transactional(readOnly = true)
    public int queryCount(String jpql, Object[] params) {
        return dao.queryCount(jpql, params);
    }

    @Transactional
    public void save(T entity) {
        dao.save(entity);
    }

    @Transactional
    public T update(T entity) {
        return dao.update(entity);
    }

    @Transactional
    public T update(T entity, String... ignoreProperties) {
        Assert.notNull(entity);
        if (dao.isManaged(entity)) {
            throw new IllegalArgumentException("Entity must not be managed");
        }
        T persistant = dao.get(dao.getIdentifier(entity));
        if (persistant != null) {
            copyProperties(entity, persistant,
                    (String[]) ArrayUtils.addAll(ignoreProperties, UPDATE_IGNORE_PROPERTIES));
            return update(persistant);
        } else {
            return update(entity);
        }
    }

    @Transactional
    public void delete(ID id) {
        delete(dao.get(id));
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public void delete(ID... ids) {
        if (ids != null) {
            for (ID id : ids) {
                delete(dao.get(id));
            }
        }
    }

    @Transactional
    public void delete(T entity) {
        dao.delete(entity);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
        List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
        for (PropertyDescriptor targetPd : targetPds) {
            if (targetPd.getWriteMethod() != null
                    && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
                PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                        targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object sourceValue = readMethod.invoke(source);
                        Object targetValue = readMethod.invoke(target);
                        if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                            Collection collection = (Collection) targetValue;
                            collection.clear();
                            collection.addAll((Collection) sourceValue);
                        } else {
                            Method writeMethod = targetPd.getWriteMethod();
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, sourceValue);
                        }
                    } catch (Throwable ex) {
                        throw new FatalBeanException("Could not copy properties from source to target", ex);
                    }
                }
            }
        }
    }

    public String makeSelectQuery() {
        return dao.makeSelectQuery();
    }
}