Java tutorial
/* * 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.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; 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.crud.hibernate.HibernateSessionManager; import com.nec.harvest.bean.mapping.BudgetPerformanceBean; import com.nec.harvest.constant.Constants; import com.nec.harvest.constant.TblConstants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.BudgetPerformance; import com.nec.harvest.repository.BudgetPerformanceRepository; import com.nec.harvest.service.BudgetPerformanceService; /** * {@link BudgetPerformenceService} * * @author hungpd * */ @SuppressWarnings("unchecked") public class BudgetPerformanceServiceImpl implements BudgetPerformanceService { private static final Logger logger = LoggerFactory.getLogger(InventoryServiceImpl.class); private BudgetPerformanceRepository repository; public BudgetPerformanceServiceImpl(BudgetPerformanceRepository budgetPerformanceRepository) { this.repository = budgetPerformanceRepository; } /** {@inheritDoc}*/ @Override public List<BudgetPerformance> findByOrgCodeAndMonthAndKmkCodeJ(String orgCode, String monthly, String kmkCodeJ) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException("Organization must not be null or empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException("Monthly must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<BudgetPerformance> budgetPerformances = new ArrayList<>(); try { tx = session.beginTransaction(); Criterion criterion = Restrictions.and(Restrictions.eq("pk.organization.strCode", orgCode), Restrictions.and(Restrictions.eq("pk.getSudo", monthly), Restrictions.and(Restrictions.eq("pk.kmkCodeJ", kmkCodeJ), Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)))); Criteria crit = session.createCriteria(BudgetPerformance.class); crit.add(criterion); // Execution starting budgetPerformances = repository.findByCriteria(crit); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while get budget performance data by organization code " + orgCode + " and monthly " + monthly + " and kmkCodeJ " + kmkCodeJ, ex); } finally { HibernateSessionManager.closeSession(session); } return budgetPerformances; } /** {@inheritDoc} */ @Override public Map<String, BudgetPerformance> findByOrgCodeAndMonthAndKmkCodeJs(String orgCode, String monthly, String... kmkCodeJs) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException("Organization must not be null or empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException("Monthly must not be null or empty"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException("KmkCodeJs must not be null"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; Map<String, BudgetPerformance> mapBudgetPerformances = new HashMap<String, BudgetPerformance>(); try { tx = session.beginTransaction(); Criterion criterion = Restrictions.and(Restrictions.eq("pk.organization.strCode", orgCode), Restrictions.and(Restrictions.eq("pk.getSudo", monthly), Restrictions.and(Restrictions.in("pk.kmkCodeJ", kmkCodeJs), Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)))); Criteria crit = session.createCriteria(BudgetPerformance.class); crit.add(criterion); List<BudgetPerformance> budgetPerformances = repository.findByCriteria(crit); if (CollectionUtils.isNotEmpty(budgetPerformances)) { for (int i = 0; i < budgetPerformances.size(); i++) { BudgetPerformance budgetPerformance = budgetPerformances.get(i); mapBudgetPerformances.put(budgetPerformance.getPk().getKmkCodeJ(), budgetPerformance); } } tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while get budget performance data by organization code " + orgCode, ex); } finally { HibernateSessionManager.closeSession(session); } return mapBudgetPerformances; } /** {@inheritDoc} */ @Override public boolean checkAvailableByOrgCodeAndMonthly(String orgCode, String monthly) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException( "Can not check budget performance available with oraganization code empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException( "Can not check budget performance available with current month in action empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; boolean isAvailable = Boolean.FALSE; try { tx = session.beginTransaction(); StringBuilder sql = new StringBuilder(" SELECT COUNT(YosanKingaku) + COUNT(JisekiKingaku) "); sql.append(" FROM " + TblConstants.TBL_BUDGET_PERFORMANCE); sql.append(" WHERE StrCode=:orgCode AND GetSudo=:monthly "); Query query = repository.getSQLQuery(session, sql.toString()); query.setString("orgCode", orgCode); query.setString("monthly", monthly); Object count = query.uniqueResult(); isAvailable = Double.valueOf(count.toString()) > 0; tx.commit(); } catch (SQLGrammarException | GenericJDBCException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while get budget performance data by organization code " + orgCode, ex); } finally { HibernateSessionManager.closeSession(session); } return isAvailable; } /** {@inheritDoc} */ @Override public Map<String, BudgetPerformance> findByOrgCodeAndStartMonthEndMonthAndKmkCodeJs(String orgCode, String startMonth, String endMonth, String... kmkCodeJs) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException( "Organization code or current month in acton 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 month must not be null or empty"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException("KmkCodeJs must not be null"); } // logger.info("Find by organization code:" + orgCode + " startMonth " + startMonth + " endMonth " + endMonth + "kmkcodej " + StringUtils.join(kmkCodeJs, ",")); final Session session = HibernateSessionManager.getSession(); Transaction tx = null; Map<String, BudgetPerformance> mapBudgetPerformances = new HashMap<String, BudgetPerformance>(); try { tx = session.beginTransaction(); Criterion criterion = Restrictions.and(Restrictions.eq("pk.organization.strCode", orgCode), Restrictions.and(Restrictions.ge("pk.getSudo", startMonth), Restrictions.and(Restrictions.le("pk.getSudo", endMonth), Restrictions.and(Restrictions.in("pk.kmkCodeJ", kmkCodeJs), Restrictions.eq("delKbn", Constants.STATUS_ACTIVE))))); Criteria crit = session.createCriteria(BudgetPerformance.class); crit.add(criterion); List<BudgetPerformance> budgetPerformances = repository.findByCriteria(crit); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(budgetPerformances)) { throw new ObjectNotFoundException("There is no the budget performance object"); } mapBudgetPerformances = new HashMap<String, BudgetPerformance>(); for (BudgetPerformance budgetPerformance : budgetPerformances) { mapBudgetPerformances.put( budgetPerformance.getPk().getGetSudo() + budgetPerformance.getPk().getKmkCodeJ(), budgetPerformance); } } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while get budget performance data by organization code " + orgCode, ex); } finally { HibernateSessionManager.closeSession(session); } return mapBudgetPerformances; } /** {@inheritDoc} */ @Override public Map<String, BudgetPerformanceBean> findByOrgCodesAndMonthAndKmkCodeJs(String month, String[] orgCodes, String... kmkCodeJs) throws ServiceException { if (StringUtils.isEmpty(month)) { throw new IllegalArgumentException("Can not find budget performance with given month is empty"); } if (ArrayUtils.isEmpty(orgCodes)) { throw new IllegalArgumentException( "Can not find budget performance with given organization code array is null"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException( "Can not find budget performance with given kmkCodeJs array is null"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; Map<String, BudgetPerformanceBean> mapBudgetPerformances = new HashMap<String, BudgetPerformanceBean>(); try { tx = session.beginTransaction(); StringBuilder sql = new StringBuilder( "SELECT KmkCodeJ as kmkCodeJ, SUM(YosanKingaku) as yosanKingaku, SUM(JisekiKingaku) as jisekiKingaku "); sql.append(" FROM " + TblConstants.TBL_BUDGET_PERFORMANCE); sql.append(" WHERE StrCode IN (:orgCodes) "); sql.append(" AND GetSudo = :month "); sql.append(" AND KmkCodeJ IN (:kmkCodeJs) "); sql.append(" AND DelKbn = 2 "); sql.append(" GROUP BY Getsudo, KmkCodeJ"); Query query = repository.getSQLQuery(session, sql.toString()); query.setParameterList("orgCodes", orgCodes); query.setString("month", month); query.setParameterList("kmkCodeJs", kmkCodeJs); query.setResultTransformer(Transformers.aliasToBean(BudgetPerformanceBean.class)); List<BudgetPerformanceBean> budgetPerformances = query.list(); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(budgetPerformances)) { throw new ObjectNotFoundException("No budget performance object found"); } for (BudgetPerformanceBean budgetPerformance : budgetPerformances) { mapBudgetPerformances.put(budgetPerformance.getKmkCodeJ(), budgetPerformance); } } catch (SQLGrammarException | GenericJDBCException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "Runtime exception occur when get a map of budget performance objects with given organization codes, kmkCodeJs and month", ex); } finally { HibernateSessionManager.closeSession(session); } return mapBudgetPerformances; } /** {@inheritDoc} */ @Override public Map<String, BudgetPerformanceBean> findByOrgCodesAndPeriodMonthAndKmkCodeJs(List<String> orgCodes, String startMonth, String endMonth, String... kmkCodeJs) throws ServiceException { if (CollectionUtils.isEmpty(orgCodes)) { throw new IllegalArgumentException( "Organization codes or current month in acton 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 month must not be null or empty"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException("KmkCodeJs must not be null"); } logger.info( "Get budgetperformance by organization codes:" + StringUtils.join(orgCodes, ",") + " startMonth " + startMonth + " endMonth " + endMonth + "kmkcodej " + StringUtils.join(kmkCodeJs, ",")); final Session session = HibernateSessionManager.getSession(); Transaction tx = null; Map<String, BudgetPerformanceBean> mapBudgetPerformances = new HashMap<String, BudgetPerformanceBean>(); try { tx = session.beginTransaction(); StringBuilder sql = new StringBuilder( "SELECT Getsudo as getSudo, KmkCodej as kmkCodeJ, SUM(YosanKingaku) as yosanKingaku,"); sql.append(" SUM(JisekiKingaku) as jisekiKingaku "); sql.append(" FROM " + TblConstants.TBL_BUDGET_PERFORMANCE); sql.append(" WHERE StrCode IN (:strCode) "); sql.append(" AND Getsudo >= :startMonth AND Getsudo <= :endMonth "); sql.append(" AND KmkCodej IN (:kmkCodeJs)"); sql.append(" AND DelKbn = " + Constants.STATUS_ACTIVE); sql.append(" GROUP BY Getsudo, KmkCodej "); sql.append(" ORDER BY Getsudo"); Query query = repository.getSQLQuery(session, sql.toString()); query.setParameterList("strCode", orgCodes); query.setParameter("startMonth", startMonth); query.setParameter("endMonth", endMonth); query.setParameterList("kmkCodeJs", kmkCodeJs); query.setResultTransformer(Transformers.aliasToBean(BudgetPerformanceBean.class)); List<BudgetPerformanceBean> budgetPerformances = query.list(); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(budgetPerformances)) { throw new ObjectNotFoundException("There is no the budget performance object"); } for (BudgetPerformanceBean budgetPerformance : budgetPerformances) { String key = budgetPerformance.getGetSudo() + budgetPerformance.getKmkCodeJ(); mapBudgetPerformances.put(key, budgetPerformance); } } catch (SQLGrammarException | GenericJDBCException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while get budget performance data by organization code " + orgCodes, ex); } finally { HibernateSessionManager.closeSession(session); } return mapBudgetPerformances; } /** {@inheritDoc} */ @Override public boolean checkAvailableByOrgCodesAndMonthlyAndKmkCodeJs(List<String> orgCodes, String monthly, String... kmkCodeJs) throws ServiceException { if (CollectionUtils.isEmpty(orgCodes)) { throw new IllegalArgumentException( "Organization codes or current month in acton must not be null or empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException("A month must not be null or empty"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException("KmkCodeJs must not be null"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; boolean isAvailable = Boolean.FALSE; try { tx = session.beginTransaction(); StringBuilder sql = new StringBuilder("SELECT count(strcode) "); sql.append(" FROM " + TblConstants.TBL_BUDGET_PERFORMANCE); sql.append(" WHERE strcode IN (:orgCodes) "); sql.append(" AND GetSudo = (:month) "); sql.append(" AND KmkCodeJ IN (:kmkCodeJs) "); sql.append(" AND DelKbn = 2 "); Query query = repository.getSQLQuery(session, sql.toString()); query.setParameterList("orgCodes", orgCodes); query.setParameter("month", monthly); query.setParameterList("kmkCodeJs", kmkCodeJs); 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 budgetperformance", ex); } finally { HibernateSessionManager.closeSession(session); } return isAvailable; } /** {@inheritDoc} */ @Override public boolean checkAvailableByOrgCodeAndMonthlyAndKmkCodeJs(String orgCode, String monthly, String... kmkCodeJs) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException( "Organization code or current month in acton must not be null or empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException("Month must not be null or empty"); } if (ArrayUtils.isEmpty(kmkCodeJs)) { throw new IllegalArgumentException("KmkCodeJs must not be null"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; boolean isAvailable = Boolean.FALSE; try { tx = session.beginTransaction(); StringBuilder sql = new StringBuilder("SELECT count(strcode) "); sql.append(" FROM " + TblConstants.TBL_BUDGET_PERFORMANCE); sql.append(" WHERE strcode = (:orgCodes) "); sql.append(" AND GetSudo = (:month) "); sql.append(" AND KmkCodeJ IN (:kmkCodeJs) "); sql.append(" AND DelKbn = 2 "); Query query = repository.getSQLQuery(session, sql.toString()); query.setParameter("orgCodes", orgCode); query.setParameter("month", monthly); query.setParameterList("kmkCodeJs", kmkCodeJs); 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 budgetperformance", ex); } finally { HibernateSessionManager.closeSession(session); } return isAvailable; } }