com.soule.base.service.DefaultService.java Source code

Java tutorial

Introduction

Here is the source code for com.soule.base.service.DefaultService.java

Source

package com.soule.base.service;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;

/*import com.soule.crm.license.LicenseMgr;*/
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.soule.MsgConstants;
import com.soule.base.media.DbAccessException;
import com.soule.base.media.IMediaForIbatis;
import com.soule.base.media.IMediaForJdbc;
import com.soule.comm.tools.AppUtils;

@Aspect
public class DefaultService implements IDefaultService {
    private static final Log logger = LogFactory.getLog(DefaultService.class);
    private IMediaForIbatis ibatisMediator;
    private IMediaForJdbc jdbcMediator;
    private String TABLE_NAMESPACE = "single.";
    private String SAVE = TABLE_NAMESPACE + "save";
    private String UPDATE = TABLE_NAMESPACE + "upd";
    private String DELETE = TABLE_NAMESPACE + "del";
    private String GET = TABLE_NAMESPACE + "get";

    public IMediaForIbatis getIbatisMediator() {
        return ibatisMediator;
    }

    public void setIbatisMediator(IMediaForIbatis ibatisMediator) {
        this.ibatisMediator = ibatisMediator;
    }

    public IMediaForJdbc getJdbcMediator() {
        return jdbcMediator;
    }

    public void setJdbcMediator(IMediaForJdbc jdbcMediator) {
        this.jdbcMediator = jdbcMediator;
    }

    public final Object getRecordByKey(String tableName, Serializable pk) {
        try {
            String name = new StringBuilder(GET).append(tableName).append("ByKey").toString();
            return this.ibatisMediator.findById(name, pk);
        } catch (DbAccessException e) {
            logger.debug("", e);
        }
        return null;
    }

    public final Boolean saveRecord(String tableName, Serializable record) {
        try {
            this.ibatisMediator.save(SAVE + tableName, record);
            return Boolean.TRUE;
        } catch (DbAccessException e) {
            logger.debug("", e);
        }
        return Boolean.FALSE;
    }

    public Boolean updateRecord(String tableName, Serializable record) {
        try {
            this.ibatisMediator.update(UPDATE + tableName, record);
            return Boolean.TRUE;
        } catch (DbAccessException e) {
            logger.debug("", e);
        }
        return Boolean.FALSE;
    }

    public final Boolean deleteReocrd(String tableName, Serializable pk) {
        try {
            this.ibatisMediator.delete(DELETE + tableName, pk);
            return Boolean.TRUE;
        } catch (DbAccessException e) {
            logger.debug("", e);
        }
        return Boolean.FALSE;
    }

    public List getRecordByMap(String tableName, Map condition) {
        try {
            String name = new StringBuilder(GET).append(tableName).toString();
            return this.ibatisMediator.find(name, condition);
        } catch (DbAccessException e) {
            logger.debug("", e);
        }
        return null;
    }

    //@Before("execution(* com.soule..query*(..)) || execution(* com.soule..insert*(..))")
    /*public void check(JoinPoint jp) throws ServiceException{
    try {
       Object[] args = jp.getArgs();
        if ( !LicenseMgr.getInstance().isValid()) {
            logger.info(LicenseMgr.getInstance().getErrorInfo());
            throw new ServiceException(MsgConstants.E0016,AppUtils.getMessage(MsgConstants.E0016));
        }
    } catch (ServiceException e1) {
        logger.error("" + e1.getErrorMsg());
        throw e1;
    } catch (Exception e) {
        logger.error("" +e.getMessage());
        throw new ServiceException(MsgConstants.E0016,e.getMessage());
    }
    }*/

    /**
     * ???
     * @throws ServiceException
     */
    //@Before("execution(* com.soule..*ServiceImpl.*(..))")
    /* public void _copyInParams() throws ServiceException{
    try {
        if ( !LicenseMgr.getInstance().isValid()) {
            logger.info(LicenseMgr.getInstance().getErrorInfo());
            throw new ServiceException(MsgConstants.E0016,AppUtils.getMessage(MsgConstants.E0016));
        }
    } catch (ServiceException e1) {
        logger.error("" + e1.getErrorMsg());
        throw e1;
    } catch (Exception e) {
        logger.error("" +e.getMessage());
        throw new ServiceException(MsgConstants.E0016,e.getMessage());
    }
     }*/
    /**
     * ???
     * @throws ServiceException
     */
    //@After("execution(* com.soule..*ServiceImpl.*(..))")
    /*public void _copyOutParams() throws ServiceException{
    try {
        if ( !LicenseMgr.getInstance().isValid()) {
            logger.info(LicenseMgr.getInstance().getErrorInfo());
            throw new ServiceException(MsgConstants.E0016,AppUtils.getMessage(MsgConstants.E0016));
        }
    } catch (ServiceException e1) {
        logger.error("" + e1.getErrorMsg());
        throw e1;
    } catch (Exception e) {
        logger.error("" +e.getMessage());
        throw new ServiceException(MsgConstants.E0016,e.getMessage());
    }
    }*/

    @Override
    public int executeBatch(final String namespace, final List paramObjects) throws ServiceException {
        int ret = 0;
        try {
            ret = (Integer) this.ibatisMediator.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                    executor.startBatch();
                    for (Object paramObject : paramObjects) {
                        executor.insert(namespace, paramObject);
                    }
                    return executor.executeBatch();
                }
            });
        } catch (DataAccessException e) {
            logger.error("DB", e);
            throw new ServiceException(MsgConstants.E0002, e.getMessage());
        } catch (DbAccessException e) {
            logger.error("DB", e);
            throw new ServiceException(MsgConstants.E0002, e.getMessage());
        }
        return ret;
    }
}