com.jmmd.biz.manager.handle.task.impl.TaskManagerHandle.java Source code

Java tutorial

Introduction

Here is the source code for com.jmmd.biz.manager.handle.task.impl.TaskManagerHandle.java

Source

/*
 * Copyright (c) 2014, lingang.chen@gmail.com  All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jmmd.biz.manager.handle.task.impl;

import javax.validation.ConstraintViolationException;

import org.apache.commons.lang3.Validate;
import org.javasimon.aop.Monitored;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jmmd.biz.manager.bo.task.TaskBO;
import com.jmmd.biz.manager.handle.exception.ManagerExceptions;
import com.jmmd.biz.manager.handle.task.TaskManager;
import com.jmmd.common.utils.annotation.AspectLogger;
import com.jmmd.core.domain.task.TaskDomain;
import com.jmmd.core.persistant.po.task.TaskPO;

/**
 * Reason:    ?.
 * 
 * @author chenlg
 * @version $Id: TaskManagerHandle.java, v 0.1 2014418 ?4:59:33 chenlg Exp $
 * @since    JDK 1.7
 */
@Monitored
@Service("taskManager")
public class TaskManagerHandle implements TaskManager {

    private static Logger logger = LoggerFactory.getLogger(TaskManagerHandle.class);

    @Autowired
    @Qualifier("taskDomain")
    private TaskDomain taskDomain;

    /** 
     * @see com.jmmd.biz.manager.handle.task.TaskManager#getTask(java.lang.Integer)
     */
    @Override
    @AspectLogger(value = "?", discover = true)
    public TaskBO getTask(Integer taskId) {
        TaskBO taskBO = null;
        try {

            Validate.notNull(taskId, "id?");

            TaskPO taskPO = taskDomain.getTask(taskId);

            Validate.notNull(taskPO, "?(id:" + taskId + ")");

            /*
             * POBO
             */
            taskBO = new TaskBO();
            BeanUtils.copyProperties(taskPO, taskBO);

        } catch (NullPointerException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
        } catch (IllegalArgumentException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
        } catch (RuntimeException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
        }

        return taskBO;
    }

    /**
     * @see com.jmmd.biz.manager.handle.task.TaskManager#saveTask(com.jmmd.biz.manager.bo.task.TaskBO)
     */
    @Override
    @Transactional(readOnly = false)
    @AspectLogger(value = "?", discover = true)
    public Boolean saveTask(TaskBO taskBO) {
        TaskPO taskPO = null;

        try {
            Validate.notNull(taskBO, "?");

            taskPO = new TaskPO();
            BeanUtils.copyProperties(taskBO, taskPO);
            taskDomain.saveTask(taskPO);

            return Boolean.TRUE;
        } catch (ConstraintViolationException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
            return Boolean.FALSE;
        } catch (RuntimeException e) {
            if (ManagerExceptions.isCausedBy(e, DuplicateKeyException.class)) {
                String message = "??(:" + taskPO + ")";
                if (logger.isErrorEnabled())
                    logger.error(message, e.getMessage());
                return Boolean.FALSE;
            } else {
                if (logger.isErrorEnabled())
                    logger.error(e.getMessage());
                return Boolean.FALSE;
            }
        }
    }

    /** 
     * @see com.jmmd.biz.manager.handle.task.TaskManager#deleteTask(java.lang.Integer)
     */
    @Override
    @Transactional(readOnly = false)
    @AspectLogger(value = "", discover = true)
    public Boolean deleteTask(Integer taskId) {
        try {
            Validate.notNull(taskId, "?");

            taskDomain.deleteTask(taskId);

            return Boolean.TRUE;
        } catch (ConstraintViolationException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
            return Boolean.FALSE;
        } catch (RuntimeException e) {
            if (logger.isErrorEnabled())
                logger.error(e.getMessage());
            return Boolean.FALSE;
        }
    }

}