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.List; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; 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.constant.Constants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.BusinessDay; import com.nec.harvest.repository.BusinessDayRepository; import com.nec.harvest.service.BusinessDayService; /** * {@link BusinessDayService} * * @author sondn * */ public class BusinessDayServiceImpl implements BusinessDayService { @SuppressWarnings("unused") private static final Logger logger = LoggerFactory.getLogger(BusinessDayServiceImpl.class); private BusinessDayRepository repository; public BusinessDayServiceImpl(BusinessDayRepository businessDayRepository) { this.repository = businessDayRepository; } @Override public BusinessDay findLatest() throws ServiceException { final Session session = HibernateSessionManager.getSession(); Transaction tx = null; BusinessDay businessDay = null; try { tx = session.beginTransaction(); List<BusinessDay> businessDates = repository.findAll(session); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(businessDates)) { throw new ObjectNotFoundException("There is no business day"); } if (businessDates.size() > 1) { throw new TooManyObjectsException("Found too many business day"); } businessDay = businessDates.get(0); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An error occurred when trying to find the latest business day", ex); } finally { HibernateSessionManager.closeSession(session); } return businessDay; } /** {@inheritDoc} */ @Override public BusinessDay findByEigyobiCode(String eigCode) throws ServiceException { if (StringUtils.isEmpty(eigCode)) { throw new IllegalArgumentException("Eigyobi code must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; BusinessDay businessDay = null; try { tx = session.beginTransaction(); Criterion criterion = Restrictions.and(Restrictions.eq("eigCode", eigCode), Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)); Criteria criteria = repository.getCriteria(session, BusinessDay.class); criteria.add(criterion); List<BusinessDay> businessDates = repository.findByCriteria(criteria); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(businessDates)) { throw new ObjectNotFoundException("There is no businessDay with eigCode of " + eigCode); } if (businessDates.size() > 1) { throw new TooManyObjectsException("Found too many business days are the same with " + eigCode); } businessDay = businessDates.get(0); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("Could not find any record for business day code " + eigCode, ex); } finally { HibernateSessionManager.closeSession(session); } return businessDay; } }