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.io.IOException; import java.util.ArrayList; import java.util.List; 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.crud.hibernate.HibernateSessionManager; import com.nec.harvest.bean.mapping.ActualViewBean; import com.nec.harvest.bean.mapping.json.JSONActualViewBean; import com.nec.harvest.constant.Constants; import com.nec.harvest.constant.SqlConstants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.helper.ProductHelper; import com.nec.harvest.model.User; import com.nec.harvest.repository.MonthlySalesRepository; import com.nec.harvest.service.MonthlySalesService; import com.nec.harvest.userdetails.AuthenticatedUserDetails; /** * {@link MonthlySalesService} * * @author vuta * */ public class MonthlySalesServiceImpl implements MonthlySalesService { private static final Logger logger = LoggerFactory.getLogger(MonthlySalesServiceImpl.class); private MonthlySalesRepository repository; public MonthlySalesServiceImpl(MonthlySalesRepository monthlySalesRepository) { this.repository = monthlySalesRepository; } /** {@inheritDoc} */ @Override public int findByOrgCodeAndMonth(String strCode, String month, Integer... ugKbns) throws ServiceException { if (StringUtils.isEmpty(strCode)) { throw new IllegalArgumentException("Organization's code must not be null or empty"); } if (StringUtils.isEmpty(month)) { throw new IllegalArgumentException("Month must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; Long result = 0L; try { tx = session.beginTransaction(); Integer ugKbn = ugKbns.length > 0 ? ugKbns[0] : 2; Query namedQuery = repository.getNamedQuery(session, SqlConstants.SQL_FIND_MONTHLY_SALES_BY_ORGANIZATION_AND_MONTH); namedQuery.setString("strCode", strCode); namedQuery.setString("getSudo", month); namedQuery.setInteger("ugKbn", ugKbn); result = (Long) namedQuery.uniqueResult(); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An exception occured while count sales change for organization " + strCode + " month " + month, ex); } finally { HibernateSessionManager.closeSession(session); } return result.intValue(); } /** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public List<ActualViewBean> findDataBeansByOrgCodeAndMonthly(String orgCode, String monthly, String ugKbn) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException("Orginazation's code must not be null or empty"); } if (StringUtils.isEmpty(monthly)) { throw new IllegalArgumentException("Month must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<ActualViewBean> jisekiBeans = new ArrayList<ActualViewBean>(); try { tx = session.beginTransaction(); Query query = repository.getQuery(session, " SELECT " + " a.pk.category.ctgCode as ctgCode, a.pk.category.ctgNameR as ctgNameR, a.kingaku as kingaku, a.updNo as updNo, a.pk.ugKbn as ugKbn " + " FROM MonthlySales a " + " WHERE a.delKbn = :delKbn AND a.pk.organization.strCode = :strCode AND a.pk.getSudo = :getSudo AND a.pk.ugKbn = :ugKbn "); query.setParameter("strCode", orgCode); query.setParameter("getSudo", monthly); query.setParameter("ugKbn", ugKbn); query.setParameter("delKbn", Constants.STATUS_ACTIVE); query.setResultTransformer(Transformers.aliasToBean(ActualViewBean.class)); jisekiBeans = query.list(); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An error occurred while finding the monthly Sales Data by organization " + orgCode + " and monthly " + monthly); } finally { HibernateSessionManager.closeSession(session); } return jisekiBeans; } /** {@inheritDoc} */ @Override public boolean updateSalesData(Session session, String orgCode, String monthly, JSONActualViewBean[] beans) throws ServiceException { if (beans == null) { throw new IllegalArgumentException("Sales beans to update must not be null"); } User user = AuthenticatedUserDetails.getUserPrincipal(); if (user == null) { throw new IllegalArgumentException( "Sorry, you don't have a permission to use this. Please login with right permission"); } try { String sqlUpdate = " UPDATE at031 a SET a.`Kingaku` = :kingaku, a.`TanCode` = :tanCode, a.`UpdNo` = :updNo + 1, " + " a.`APInf2`= :aPInf2, a.`StfCodeU` = :stfCodeU, a.`PrdNoU` = :prdNoU, a.`TimeU` = NOW() " + " WHERE a.`Getsudo` = :getSudo AND a.`StrCode` = :strCode AND a.`CtgCode` = :ctgCode AND a.`UgKbn` = :ugKbn AND a.`UpdNo` = :updNo "; String usrCode = user.getUsrCode(); String version = ""; try { version = ProductHelper.getProductInfor().getVersion(); } catch (IOException ex) { logger.warn(ex.getMessage()); } for (JSONActualViewBean bean : beans) { Query query = repository.getSQLQuery(session, sqlUpdate).setParameter("kingaku", bean.getKingaku()) .setParameter("tanCode", usrCode).setParameter("updNo", bean.getUpdNo()) .setParameter("aPInf2", usrCode).setParameter("stfCodeU", usrCode) .setParameter("prdNoU", version).setParameter("getSudo", monthly) .setParameter("strCode", orgCode).setParameter("ctgCode", bean.getCtgCode()) .setParameter("ugKbn", bean.getUgKbn()); if (query.executeUpdate() <= 0) { logger.warn("can't update because updNo out of date at sales monthly record with: strCode = " + orgCode + " and getSudo = " + monthly); return false; } } return true; } catch (SQLGrammarException | GenericJDBCException ex) { throw new ServiceException("An error occurred while update sales data", ex); } } }