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.Date; import java.util.List; import org.apache.commons.collections.CollectionUtils; 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 com.nec.crud.hibernate.HibernateSessionManager; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.Stamp; import com.nec.harvest.repository.StampedRepository; import com.nec.harvest.service.StampedService; /** * {@link StampedService} * * @author huonghv * */ public class StampedServiceImpl implements StampedService { private StampedRepository repository; public StampedServiceImpl(StampedRepository stampedRepository) { this.repository = stampedRepository; } /** {@inheritDoc} */ @Override public double findStampedLackingByOrgCode(String orgCode) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException("User's organization code must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; double isStamped = 0; try { tx = session.beginTransaction(); Criterion criterion = Restrictions.and(Restrictions.eq("pk.organization.strCode", orgCode), Restrictions.eq("delKbn", "2")); Criteria criteria = repository.getCriteria(session, Stamp.class); criteria.add(criterion); List<Stamp> stamps = repository.findByCriteria(criteria); if (CollectionUtils.isNotEmpty(stamps)) { isStamped = stamps.get(0).getNinzu(); } tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An error occurred when getting all the number of stamped lacking " + "for the organization's code " + orgCode, ex); } finally { HibernateSessionManager.closeSession(session); } return isStamped; } /** {@inheritDoc} */ @Override public double findStampedLackingByOrgCodeAndBusinessDay(String orgCode, Date businessDay) throws ServiceException { if (StringUtils.isEmpty(orgCode)) { throw new IllegalArgumentException("Organization's code must not be null or empty"); } if (businessDay == null) { throw new IllegalArgumentException("Business day must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; double isStamped = 0; try { tx = session.beginTransaction(); String hql = "SELECT at021.Ninzu FROM at021 WHERE at021.StrCode = :StrCode " + " AND at021.SrDate = (SELECT at051.EigDateY FROM at051 WHERE at051.EigDate = :EigDate)" + " AND at021.DelKbn = 2 "; Query query = repository.getSQLQuery(session, hql); query.setString("StrCode", orgCode); query.setDate("EigDate", businessDay); Object result = query.uniqueResult(); if (result != null) { isStamped = Double.parseDouble(result.toString()); } tx.commit(); } catch (SQLGrammarException | GenericJDBCException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An error occurred when getting all the number of stamped lacking " + "for the organization's code " + orgCode + " of day " + businessDay, ex); } finally { HibernateSessionManager.closeSession(session); } return isStamped; } }