com.nec.harvest.service.impl.ConsumptionTaxRateServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.service.impl.ConsumptionTaxRateServiceImpl.java

Source

/**
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
/**
 * 
 */
package com.nec.harvest.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;

import com.nec.core.exception.ObjectNotFoundException;
import com.nec.crud.hibernate.HibernateSessionManager;
import com.nec.harvest.constant.Constants;
import com.nec.harvest.exception.ServiceException;
import com.nec.harvest.helper.BusinessDayHelper;
import com.nec.harvest.model.ConsumptionTaxRate;
import com.nec.harvest.repository.ConsumptionTaxRateRepository;
import com.nec.harvest.service.ConsumptionTaxRateService;
import com.nec.harvest.util.DateFormatUtil.DateFormat;

/**
 * {@link ConsumptionTaxRateService}
 * 
 * @author QuanDK
 * 
 */
public class ConsumptionTaxRateServiceImpl implements ConsumptionTaxRateService {

    private ConsumptionTaxRateRepository repository;

    public ConsumptionTaxRateServiceImpl(ConsumptionTaxRateRepository consumptionTaxRateRepository) {
        this.repository = consumptionTaxRateRepository;
    }

    /** {@inheritDoc} */
    @Override
    public Double findRateDefByDate(Calendar date) throws ServiceException {
        if (date == null) {
            throw new IllegalArgumentException("Date must not be null");
        }

        final Session session = HibernateSessionManager.getSession();
        Transaction tx = null;

        double updatedRecordsNo = 0;
        try {
            tx = session.beginTransaction();
            Query query = repository.getQuery(session,
                    "SELECT a.rateDef FROM ConsumptionTaxRate a " + " WHERE a.enfDate = ( "
                            + " SELECT MAX(b.enfDate) FROM ConsumptionTaxRate b "
                            + " WHERE b.enfDate < ? AND b.delKbn = ? ) ");
            query.setParameter(0, date);
            query.setParameter(1, Constants.STATUS_ACTIVE);

            Object result = query.uniqueResult();
            // Release transaction
            tx.commit();
            if (result == null) {
                throw new ObjectNotFoundException("Can not find tax rate");
            }
            //
            updatedRecordsNo = Double.valueOf(result.toString());
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while getting rateDef by date " + date, ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return updatedRecordsNo;
    }

    /** {@inheritDoc} */
    @Override
    public Map<String, Double> findRateDefByMonth(Date monthly) {
        if (monthly == null) {
            throw new IllegalArgumentException("date must not be null or empty");
        }

        final Session session = HibernateSessionManager.getSession();
        Transaction tx = null;

        TreeMap<String, Double> rateDefs = new TreeMap<String, Double>();
        try {
            tx = session.beginTransaction();
            Query query = repository.getQuery(session,
                    " SELECT a.enfDate as enfDate, a.rateDef * 0.01 as rateDef " + " FROM ConsumptionTaxRate a "
                            + " WHERE a.enfDate < ? " + " AND a.enfDate >= ? " + " AND a.delKbn = ? "
                            + " ORDER BY a.enfDate ");
            String nextMonth = BusinessDayHelper.getNextMonthly(monthly, DateFormat.DATE_WITHOUT_DAY);

            // Set parameters for the given query
            SimpleDateFormat format = new SimpleDateFormat(DateFormat.DATE_WITHOUT_DAY.getFormatPattern());
            Calendar beginDate = Calendar.getInstance();
            beginDate.setTime(format.parse(nextMonth));
            query.setParameter(0, beginDate);

            // Added a end of date for the query
            Calendar endDate = Calendar.getInstance();
            endDate.setTime(monthly);
            query.setParameter(1, endDate);

            query.setParameter(2, Constants.STATUS_ACTIVE);
            query.setResultTransformer(Transformers.aliasToBean(ConsumptionTaxRate.class));
            List<ConsumptionTaxRate> result = repository.findByQuery(query);
            query = repository.getQuery(session,
                    " SELECT b.enfDate as enfDate, b.rateDef * 0.01 as rateDef FROM ConsumptionTaxRate b "
                            + " WHERE b.enfDate = " + " (SELECT MAX(c.enfDate) "
                            + " FROM ConsumptionTaxRate c WHERE c.enfDate < ? ) " + " AND b.delKbn = ? "
                            + " ORDER BY b.enfDate ");

            beginDate.setTime(monthly);
            query.setParameter(0, beginDate).setParameter(1, Constants.STATUS_ACTIVE);
            query.setResultTransformer(Transformers.aliasToBean(ConsumptionTaxRate.class));
            if (CollectionUtils.isEmpty(result)) {
                result = repository.findByQuery(query);
            } else {
                result.addAll(repository.findByQuery(query));
            }

            format = new SimpleDateFormat("dd/MM/yyyy");
            if (result != null) {
                for (ConsumptionTaxRate objs : result) {
                    if (objs.getEnfDate() != null && objs.getRateDef() != null) {
                        rateDefs.put(format.format((objs.getEnfDate()).getTime()), objs.getRateDef());
                    }
                }
            }
            tx.commit();
        } catch (HibernateException | NullPointerException | ParseException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while query get rateDef ", ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }

        return rateDefs;
    }

    /** {@inheritDoc} */
    @Override
    public Double findTaxRateByDate(Date month) throws ServiceException {
        if (month == null) {
            throw new IllegalArgumentException("Can not find tax rate with given processing month null");
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Double taxRate = null;
        try {
            taxRate = findRateDefByDate(calendar);
            return (new Double(taxRate.doubleValue() * 0.01));
        } catch (IllegalArgumentException | ObjectNotFoundException ex) {
            throw ex;
        }
    }

    /** {@inheritDoc} */
    @Override
    public Double findActualTaxRateByDate(Date month) throws ServiceException {
        if (month == null) {
            throw new IllegalArgumentException("Can not find tax rate with given processing month null");
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Double taxRate;
        try {
            taxRate = findRateDefByDate(calendar);

            // 
            return taxRate != null ? taxRate : 0;
        } catch (IllegalArgumentException | ObjectNotFoundException ex) {
            throw ex;
        }
    }
}