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 org.apache.commons.lang.StringUtils; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.nec.crud.hibernate.HibernateSessionManager; import com.nec.harvest.constant.SqlConstants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.repository.MonthlyPurchasesRepository; import com.nec.harvest.service.MonthlyPurchaseService; /** * {@link MonthlyPurchaseService} * * @author hungpd * */ public class MonthlyPurchaseServiceImpl implements MonthlyPurchaseService { private MonthlyPurchasesRepository repository; public MonthlyPurchaseServiceImpl(MonthlyPurchasesRepository monthlyPurchaseRepository) { this.repository = monthlyPurchaseRepository; } /** {@inheritDoc} */ @Override public int findByOrgCodeAndMonth(String strCode, String month) 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; int recordsNo = 0; try { tx = session.beginTransaction(); Query namedQuery = repository.getNamedQuery(session, SqlConstants.SQL_FIND_MONTHLY_PURCHASE_BY_ORGANIZATION_AND_MONTH); namedQuery.setString("strCode", strCode); namedQuery.setString("getSudo", month); Long result = (Long) namedQuery.uniqueResult(); recordsNo = result.intValue(); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An exception occured while count purchase change for organization " + strCode + " month " + month, ex); } finally { HibernateSessionManager.closeSession(session); } return recordsNo; } }