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

Java tutorial

Introduction

Here is the source code for com.nec.harvest.service.impl.ActualViewServiceImpl.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.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nec.core.exception.ObjectNotFoundException;
import com.nec.core.exception.TooManyObjectsException;
import com.nec.crud.hibernate.HibernateSessionManager;
import com.nec.harvest.bean.mapping.ActualViewABean;
import com.nec.harvest.exception.ServiceException;
import com.nec.harvest.helper.BusinessDayHelper;
import com.nec.harvest.model.VJiseki;
import com.nec.harvest.repository.ActualViewRepository;
import com.nec.harvest.service.ActualViewService;
import com.nec.harvest.service.ConsumptionTaxRateService;
import com.nec.harvest.service.InventoryService;
import com.nec.harvest.util.DateFormatUtil;
import com.nec.harvest.util.DateFormatUtil.DateFormat;

/**
 * 
 * @author hungpd
 * 
 */
public class ActualViewServiceImpl implements ActualViewService {

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

    private ActualViewRepository repository;

    private ConsumptionTaxRateService consumptionTaxRateService;

    private InventoryService inventoryService;

    public ActualViewServiceImpl(ActualViewRepository repository,
            ConsumptionTaxRateService consumptionTaxRateService, InventoryService inventoryService) {
        this.consumptionTaxRateService = consumptionTaxRateService;
        this.repository = repository;
        this.inventoryService = inventoryService;
    }

    /** {@inheritDoc}*/
    @Override
    public List<VJiseki> findByOrgCodeAndMonthies(String orgCode, String... monthlies) throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Organization must not be null or empty");
        }
        if (ArrayUtils.isEmpty(monthlies)) {
            throw new IllegalArgumentException("The monthlies must not be null");
        }

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

        List<VJiseki> jisekis = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            Query query = repository.getSQLQuery(session,
                    "SELECT  UriSkKG as uriSkKG, KtSkKG as ktSkKG, IdoSkKGU as idoSkKGU, IdoSkKGH as idoSkKGH, UriKrKG as uriKrKG,"
                            + " KtKrKG as ktKrKG, KnSrKG as knSrKG, KtSrKG as ktSrKG, KgcSrKG as kgcSrKG, IdoSrKGU as idoSrKGU, IdoSrKGH as idoSrKGH, KtJkKG as ktJkKG, JkJkKG as jkJkKG, KgcJkKG as kgcJkKG, IdoJkKGU as idoJkKGU, IdoJkKGH as idoJkKGH, HelpJkKGU as helpJkKGU, "
                            + " HelpJkKGH as helpJkKGH, KtKhKG as ktKhKG, KnKhKG as knKhKG, KgcKhKG as kgcKhKG, IdoKhKGU as idoKhKGU, IdoKhKGH as idoKhKGH, UriKhKG as uriKhKG FROM V_JISEKI WHERE StrCode = :strCode AND Getsudo in (:monthlies)");
            query.setParameter("strCode", orgCode);
            query.setParameterList("monthlies", monthlies);
            query.setResultTransformer(Transformers.aliasToBean(VJiseki.class));

            jisekis = repository.findByQuery(query);
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(jisekis)) {
                throw new ObjectNotFoundException("There is no actual view object");
            }
        } catch (SQLGrammarException | GenericJDBCException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException(
                    "An exception occured while getting VJiseki data for the organization " + orgCode, ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return jisekis;
    }

    /** {@inheritDoc} */
    @Override
    public boolean checkAvailableByOrgCodeAndMonthly(String orgCode, String... monthlies) throws ServiceException {
        if (StringUtils.isEmpty(orgCode) || monthlies == null) {
            throw new IllegalArgumentException(
                    "Can not check month available with organization code or monthly empty");
        }

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

        boolean isAvailable = Boolean.FALSE;
        try {
            tx = session.beginTransaction();
            Query query = repository.getSQLQuery(session,
                    "SELECT count(StrCode) FROM v_jiseki WHERE StrCode = :orgCode AND GetSudo in (:monthlies)");
            query.setParameter("orgCode", orgCode);
            query.setParameterList("monthlies", monthlies);

            Object count = query.uniqueResult();
            if (count != null) {
                isAvailable = Long.parseLong(count.toString()) > 0;
            }
            tx.commit();
        } catch (SQLGrammarException | GenericJDBCException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while checking data for the organization " + orgCode,
                    ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return isAvailable;
    }

    /** {@inheritDoc} */
    @Override
    public VJiseki findByOrgCodeAndMonthly(String orgCode, String monthly) throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Can not find jiseki object with organization code empty");
        }
        if (StringUtils.isEmpty(monthly)) {
            throw new IllegalArgumentException("Can not find jiseki object with null current monthly");
        }

        List<VJiseki> jisekis = findByOrgCodeAndMonthies(orgCode, monthly);
        if (CollectionUtils.isEmpty(jisekis)) {
            throw new ObjectNotFoundException("There is no actual view object");
        }
        if (jisekis.size() > 1) {
            throw new TooManyObjectsException("Too many VJiseki object found");
        }
        return jisekis.get(0);
    }

    /** {@inheritDoc}*/
    @Override
    public List<VJiseki> findByOrgCodeAndPeriodMonthly(String orgCode, String startMonth, String endMonth)
            throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Organization must not be null or empty");
        }
        if (StringUtils.isEmpty(startMonth)) {
            throw new IllegalArgumentException("Start month must not be null or empty");
        }
        if (StringUtils.isEmpty(endMonth)) {
            throw new IllegalArgumentException("End month must not be null or empty");
        }

        //
        logger.info(" Find soneki suii by organization's code " + orgCode + " startMonth " + startMonth
                + " endMonth " + endMonth);

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

        List<VJiseki> jisekis = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            StringBuilder sql = new StringBuilder(
                    " SELECT c.pk.organization.strCode as strCode, c.pk.getSudo as getSudo, c.uriSkKG as uriSkKG, c.KtSkKG as ktSkKG, c.IdoSkKGU as idoSkKGU, c.IdoSkKGH as idoSkKGH, c.uriKrKG as uriKrKG, c.ktKrKG as ktKrKG, c.knSrKG as knSrKG, ");
            sql.append(
                    " c.ktSrKG as ktSrKG, c.kgcSrKG as kgcSrKG, c.idoSrKGU as idoSrKGU, c.idoSrKGH as idoSrKGH, c.ktJkKG as ktJkKG, c.jkJkKG as jkJkKG, c.kgcJkKG as kgcJkKG, c.idoJkKGU as idoJkKGU, c.idoJkKGH as idoJkKGH, c.helpJkKGU as helpJkKGU, c.helpJkKGH as helpJkKGH, c.ktKhKG as ktKhKG, ");
            sql.append(
                    " c.knKhKG as knKhKG, c.kgcKhKG as kgcKhKG, c.idoKhKGU as idoKhKGU, c.idoKhKGH as idoKhKGH, c.uriKhKG as uriKhKG ");
            sql.append(" FROM VJiseki c ");
            sql.append(" WHERE c.pk.organization.strCode = :strCode ");
            sql.append(" AND c.pk.getSudo >= :startMonth ");
            sql.append(" AND c.pk.getSudo <= :endMonth ORDER BY c.pk.getSudo");

            // 
            Query query = repository.getQuery(session, sql.toString());
            query.setParameter("strCode", orgCode);
            query.setParameter("startMonth", startMonth);
            query.setParameter("endMonth", endMonth);
            query.setResultTransformer(Transformers.aliasToBean(VJiseki.class));

            // 
            jisekis = repository.findByQuery(query);
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(jisekis)) {
                throw new ObjectNotFoundException("There is no actual view object");
            }
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while getting the data for organization " + orgCode,
                    ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return jisekis;
    }

    /** {@inheritDoc} */
    @Override
    public ActualViewABean findActualViewAByOrgCodeAndMonth(String orgCode, Date monthly) throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Orginazation's code must not be null or empty");
        }
        if (monthly == null) {
            throw new IllegalArgumentException("Monthly must not be null");
        }

        /* Find jiseki object by organization code and monthly */
        String strMonthly = DateFormatUtil.format(monthly, DateFormat.DATE_WITHOUT_DAY);

        VJiseki jiseki = null;
        try {
            jiseki = findByOrgCodeAndMonthly(orgCode, strMonthly); // [A]
        } catch (ObjectNotFoundException ex) {
            logger.warn(ex.getMessage());

            //
            jiseki = new VJiseki();
        }

        // Find total Amount of Current month
        double totalAmountCurrentMonth = 0d;
        try {
            totalAmountCurrentMonth = inventoryService.findTotalAmountByOrgCodeAndMonthly(orgCode, strMonthly);
        } catch (ObjectNotFoundException ex) {
            logger.warn(ex.getMessage());

            // default setting
            totalAmountCurrentMonth = -1d;
        }

        ActualViewABean jisekiActualViewBean = new ActualViewABean();

        // Find total Amount of Previous month
        double totalAmountPreviousMonth = 0d;
        try {
            String previousMonthly = BusinessDayHelper.getPreviousMonthly(monthly, DateFormat.DATE_WITHOUT_DAY);
            totalAmountPreviousMonth = inventoryService.findTotalAmountByOrgCodeAndMonthly(orgCode,
                    previousMonthly);
        } catch (ObjectNotFoundException ex) {
            logger.warn(ex.getMessage());

            // default setting
            totalAmountPreviousMonth = -1d;
        }

        // ???
        if (totalAmountCurrentMonth == -1d) {
            jisekiActualViewBean.setTotalAmountCurrentMonth(null);
        } else {
            jisekiActualViewBean.setTotalAmountCurrentMonth(totalAmountCurrentMonth);
        }

        // ???? ?_?????????????_?????   
        if (jisekiActualViewBean.getTotalAmountCurrentMonth() == null) {
            jisekiActualViewBean.setTotalAmountPreviousMonth(null);
        } else {
            if (totalAmountPreviousMonth == -1d) {
                jisekiActualViewBean.setTotalAmountPreviousMonth(null);
            } else {
                jisekiActualViewBean.setTotalAmountPreviousMonth(totalAmountPreviousMonth);
            }
        }

        double step1 = 0d;
        double step2 = 0d;
        double valueTmp = 0d;

        //? ?????
        double taxRate;
        try {
            taxRate = consumptionTaxRateService.findActualTaxRateByDate(monthly);
        } catch (IllegalArgumentException | ObjectNotFoundException e) {
            // default setting
            taxRate = 0;
        }

        if (jiseki.getUriSkKG() == null && jiseki.getKtSkKG() == null && jiseki.getIdoSkKGU() == null
                && jiseki.getIdoSkKGH() == null) {
            // default setting
            jisekiActualViewBean.setFoodFee(null);
        } else {
            Double uriSkKG = jiseki.getUriSkKG();
            Double ktSkKG = jiseki.getKtSkKG();
            Double idoSkKGU = jiseki.getIdoSkKGU();
            Double idoSkKGH = jiseki.getIdoSkKGH();
            double tmpStep1 = uriSkKG == null ? 0 : uriSkKG - Math.floor((uriSkKG / (100 + taxRate)) * taxRate);
            double tmpStep2 = ktSkKG == null ? 0 : ktSkKG;
            double tmpStep3 = idoSkKGU == null ? 0 : idoSkKGU;
            double tmpStep4 = idoSkKGH == null ? 0 : idoSkKGH;
            valueTmp = tmpStep1 + tmpStep2 + tmpStep3 - tmpStep4;
            jisekiActualViewBean.setFoodFee(valueTmp);
        }

        // ? ????, ? ??
        if (jiseki.getUriKrKG() == null && jiseki.getKtKrKG() == null) {
            // default setting
            jisekiActualViewBean.setManageFee(null);
        } else {
            step1 = (jiseki.getUriKrKG() == null ? 0 : jiseki.getUriKrKG()) - Math
                    .floor(((jiseki.getUriKrKG() == null ? 0 : jiseki.getUriKrKG()) / (100 + taxRate)) * taxRate);
            valueTmp = step1 + (jiseki.getKtKrKG() == null ? 0 : jiseki.getKtKrKG());

            jisekiActualViewBean.setManageFee(valueTmp);
        }

        // ??_??  ?_??
        if (jisekiActualViewBean.getFoodFee() == null && jisekiActualViewBean.getManageFee() == null) {
            //default setting
            jisekiActualViewBean.setTotalRevenue(null);
        } else {
            valueTmp = (jisekiActualViewBean.getFoodFee() == null ? 0 : jisekiActualViewBean.getFoodFee())
                    + (jisekiActualViewBean.getManageFee() == null ? 0 : jisekiActualViewBean.getManageFee());

            jisekiActualViewBean.setTotalRevenue(valueTmp);
        }

        // 1. ? ?
        // 2. ? ?
        // 3. ? ??????
        // 4. ? ??
        // 5. ? ?
        //  1 + 2 + 3 + 4 -5
        if (jiseki.getKgcSrKG() == null && jiseki.getKnSrKG() == null && jiseki.getKtSrKG() == null
                && jiseki.getIdoSrKGU() == null && jiseki.getIdoSrKGH() == null) {

            // default setting
            jisekiActualViewBean.setPurcharseAmountCurrentMonth(null);
        } else {
            step1 = (jiseki.getKgcSrKG() == null ? 0 : jiseki.getKgcSrKG()) - Math
                    .floor(((jiseki.getKgcSrKG() == null ? 0 : jiseki.getKgcSrKG()) / (100 + taxRate)) * taxRate);
            valueTmp = (jiseki.getKnSrKG() == null ? 0 : jiseki.getKnSrKG())
                    + (jiseki.getKtSrKG() == null ? 0 : jiseki.getKtSrKG()) + step1
                    + (jiseki.getIdoSrKGU() == null ? 0 : jiseki.getIdoSrKGU())
                    - (jiseki.getIdoSrKGH() == null ? 0 : jiseki.getIdoSrKGH());

            jisekiActualViewBean.setPurcharseAmountCurrentMonth(valueTmp);
        }

        // 1. ? ?
        // 2. ? ?
        // 3. ? ??????
        // 4. ? ??
        // 5. ? ?
        // 6. ? ??
        // 7. ? ?
        //  1+ 2 + 3 + 4 ?5 6?7
        if (jiseki.getKgcJkKG() == null && jiseki.getKtJkKG() == null && jiseki.getJkJkKG() == null
                && jiseki.getIdoJkKGU() == null && jiseki.getIdoJkKGH() == null && jiseki.getHelpJkKGU() == null
                && jiseki.getHelpJkKGH() == null) {

            // default setting
            jisekiActualViewBean.setHumanCost(null);
        } else {
            step1 = (jiseki.getKgcJkKG() == null ? 0 : jiseki.getKgcJkKG()) - Math
                    .floor(((jiseki.getKgcJkKG() == null ? 0 : jiseki.getKgcJkKG()) / (100 + taxRate)) * taxRate);
            valueTmp = (jiseki.getKtJkKG() == null ? 0 : jiseki.getKtJkKG())
                    + (jiseki.getJkJkKG() == null ? 0 : jiseki.getJkJkKG()) + step1
                    + (jiseki.getIdoJkKGU() == null ? 0 : jiseki.getIdoJkKGU())
                    - (jiseki.getIdoJkKGH() == null ? 0 : jiseki.getIdoJkKGH())
                    + (jiseki.getHelpJkKGU() == null ? 0 : jiseki.getHelpJkKGU())
                    - (jiseki.getHelpJkKGH() == null ? 0 : jiseki.getHelpJkKGH());

            jisekiActualViewBean.setHumanCost(valueTmp);
        }

        // 1. ? ?
        // 2. ? ?
        // 3. ? ??????
        // 4. ? ??
        // 5. ? ?
        // 6. ? ??
        //  1 + 2 + 3 + 4?5 6
        if (jiseki.getKgcKhKG() == null && jiseki.getUriKhKG() == null && jiseki.getKtKhKG() == null
                && jiseki.getKnKhKG() == null && jiseki.getIdoKhKGU() == null && jiseki.getIdoKhKGH() == null) {

            // default setting
            jisekiActualViewBean.setFunding(null);
        } else {
            step1 = (jiseki.getKgcKhKG() == null ? 0 : jiseki.getKgcKhKG()) - Math
                    .floor(((jiseki.getKgcKhKG() == null ? 0 : jiseki.getKgcKhKG()) / (100 + taxRate)) * taxRate);
            step2 = (jiseki.getUriKhKG() == null ? 0 : jiseki.getUriKhKG()) - Math
                    .floor(((jiseki.getUriKhKG() == null ? 0 : jiseki.getUriKhKG()) / (100 + taxRate)) * taxRate);
            valueTmp = (jiseki.getKtKhKG() == null ? 0 : jiseki.getKtKhKG())
                    + (jiseki.getKnKhKG() == null ? 0 : jiseki.getKnKhKG()) + step1
                    + (jiseki.getIdoKhKGU() == null ? 0 : jiseki.getIdoKhKGU())
                    - (jiseki.getIdoKhKGH() == null ? 0 : jiseki.getIdoKhKGH()) + step2;

            jisekiActualViewBean.setFunding(valueTmp);
        }

        // ?_????_?_???_??_??_?
        if (jisekiActualViewBean.getTotalRevenue() == null
                && jisekiActualViewBean.getTotalAmountPreviousMonth() == null
                && jisekiActualViewBean.getPurcharseAmountCurrentMonth() == null
                && jisekiActualViewBean.getTotalAmountCurrentMonth() == null
                && jisekiActualViewBean.getHumanCost() == null && jisekiActualViewBean.getFunding() == null) {

            // default setting
            jisekiActualViewBean.setDedicationProfit(null);
        } else {
            valueTmp = (jisekiActualViewBean.getTotalRevenue() == null ? 0 : jisekiActualViewBean.getTotalRevenue())
                    - (jisekiActualViewBean.getTotalAmountPreviousMonth() == null ? 0
                            : jisekiActualViewBean.getTotalAmountPreviousMonth())
                    - (jisekiActualViewBean.getPurcharseAmountCurrentMonth() == null ? 0
                            : jisekiActualViewBean.getPurcharseAmountCurrentMonth())
                    + (jisekiActualViewBean.getTotalAmountCurrentMonth() == null ? 0
                            : jisekiActualViewBean.getTotalAmountCurrentMonth())
                    - (jisekiActualViewBean.getHumanCost() == null ? 0 : jisekiActualViewBean.getHumanCost())
                    - (jisekiActualViewBean.getFunding() == null ? 0 : jisekiActualViewBean.getFunding());

            jisekiActualViewBean.setDedicationProfit(valueTmp);
        }
        return jisekiActualViewBean;
    }

    /** {@inheritDoc} */
    @Override
    public List<VJiseki> findByOrgCodesAndMonth(String month, String... orgCodes) throws ServiceException {
        if (StringUtils.isEmpty(month)) {
            throw new IllegalArgumentException("Can not find actual view objects with month empty");
        }
        if (ArrayUtils.isEmpty(orgCodes)) {
            throw new IllegalArgumentException("Can not find actual view objects with organization codes null");
        }

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

        List<VJiseki> jisekis = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            Query query = repository.getSQLQuery(session,
                    " SELECT StrCode as strCode, UriSkKG as uriSkKG, KtSkKG as ktSkKG, IdoSkKGU as idoSkKGU, IdoSkKGH as idoSkKGH, UriKrKG as uriKrKG, "
                            + " KtKrKG as ktKrKG, KnSrKG as knSrKG, KtSrKG as ktSrKG, "
                            + " KgcSrKG as kgcSrKG, IdoSrKGU as idoSrKGU, IdoSrKGH as idoSrKGH, "
                            + " KtJkKG as ktJkKG, JkJkKG as jkJkKG, KgcJkKG as kgcJkKG, "
                            + " IdoJkKGU as idoJkKGU, IdoJkKGH as idoJkKGH, HelpJkKGU as helpJkKGU, HelpJkKGH as helpJkKGH, "
                            + " KtKhKG as ktKhKG, KnKhKG as knKhKG, KgcKhKG as kgcKhKG, "
                            + " IdoKhKGU as idoKhKGU, IdoKhKGH as idoKhKGH, UriKhKG as uriKhKG "
                            + " FROM V_JISEKI WHERE StrCode IN (:orgCodes) AND Getsudo = :month");

            query.setParameterList("orgCodes", orgCodes);
            query.setString("month", month);
            query.setResultTransformer(Transformers.aliasToBean(VJiseki.class));

            // 
            jisekis = repository.findByQuery(query);
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(jisekis)) {
                throw new ObjectNotFoundException("No jisekis (actual view object) found");
            }
        } catch (SQLGrammarException | GenericJDBCException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException(
                    "An exception occur when get a list of actual view objects of multi organization and month",
                    ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return jisekis;
    }

    /** {@inheritDoc}*/
    @Override
    public List<VJiseki> findByPeriodMonthlyAndOrgCodes(String startMonth, String endMonth, List<String> orgCodes)
            throws ServiceException {
        if (CollectionUtils.isEmpty(orgCodes)) {
            throw new IllegalArgumentException("Organizations must not be null or empty");
        }
        if (StringUtils.isEmpty(startMonth)) {
            throw new IllegalArgumentException("Start month must not be null or empty");
        }
        if (StringUtils.isEmpty(endMonth)) {
            throw new IllegalArgumentException("End month must not be null or empty");
        }

        //
        logger.info(" Find soneki suii by organization's code " + StringUtils.join(orgCodes, ",") + " startMonth "
                + startMonth + " endMonth " + endMonth);
        final Session session = HibernateSessionManager.getSession();
        Transaction tx = null;

        List<VJiseki> jisekis = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            StringBuilder sql = new StringBuilder(
                    " SELECT c.getSudo as getSudo, c.uriSkKG, c.ktSkKG, c.idoSkKGU, c.idoSkKGH, c.uriKrKG, ");
            sql.append(
                    " c.ktKrKG, c.knSrKG, c.ktSrKG, c.kgcSrKG, c.idoSrKGU, c.idoSrKGH, c.jkJkKG, c.kgcJkKG, c.idoJkKGU, c.idoJkKGH, ");
            sql.append(
                    " c.helpJkKGU, c.helpJkKGH, c.ktKhKG, c.knKhKG, c.kgcKhKG, c.idoKhKGU, c.idoKhKGH, c.uriKhKG, c.ktJkKG");
            sql.append(" FROM v_jiseki c WHERE c.strCode IN (:strCode) ");
            sql.append(" AND c.getSudo >= :startMonth AND c.getSudo <= :endMonth ");
            sql.append(" ORDER BY c.getSudo ");

            // get data in v_jiseki view  
            Query query = repository.getSQLQuery(session, sql.toString());
            query.setParameterList("strCode", orgCodes);
            query.setParameter("startMonth", startMonth);
            query.setParameter("endMonth", endMonth);
            query.setResultTransformer(Transformers.aliasToBean(VJiseki.class));

            jisekis = repository.findByQuery(query);
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(jisekis)) {
                throw new ObjectNotFoundException("There is no actual view object");
            }
        } catch (SQLGrammarException | GenericJDBCException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while getting the data for organization "
                    + StringUtils.join(orgCodes, ","), ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return jisekis;
    }

    /** {@inheritDoc} */
    @Override
    public boolean checkAvailableByOrgCodesAndMonthlies(List<String> orgCode, String... monthlies)
            throws ServiceException {
        if (CollectionUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Can not check month available with organization code empty");
        }
        if (ArrayUtils.isEmpty(monthlies)) {
            throw new IllegalArgumentException("Can not check month available with monthlies empty");
        }

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

        boolean isAvailable = Boolean.FALSE;
        try {
            tx = session.beginTransaction();
            Query query = repository.getSQLQuery(session,
                    "SELECT count(StrCode) FROM v_jiseki WHERE StrCode in (:orgCode) AND GetSudo in (:monthlies)");
            query.setParameterList("orgCode", orgCode);
            query.setParameterList("monthlies", monthlies);
            Object count = query.uniqueResult();
            if (count != null) {
                isAvailable = Long.parseLong(count.toString()) > 0;
            }
            tx.commit();
        } catch (SQLGrammarException | GenericJDBCException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while checking data for the organization " + orgCode,
                    ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return isAvailable;
    }
}