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.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.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import com.nec.core.exception.ObjectNotFoundException; import com.nec.crud.hibernate.HibernateSessionManager; import com.nec.harvest.constant.Constants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.Account; import com.nec.harvest.repository.AccountRepository; import com.nec.harvest.service.AccountService; /** * {@link AccountService} * * @author QuanDK * */ public class AccountServiceImpl implements AccountService { private AccountRepository repository; public AccountServiceImpl(AccountRepository accountRepository) { this.repository = accountRepository; } /** {@inheritDoc} */ @Override public List<Account> findByIdoKbn(String idoKbn) throws ServiceException { if (StringUtils.isEmpty(idoKbn)) { throw new IllegalArgumentException("The Account's idoKbn to search must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<Account> accounts = new ArrayList<>(); try { tx = session.beginTransaction(); Criteria criteria = repository.getCriteria(session, Account.class) .setProjection(Projections.projectionList().add(Projections.property("kmkCode").as("kmkCode")) .add(Projections.property("kmkNameR").as("kmkNameR"))) .add(Restrictions.eq("idoKbn", idoKbn)).add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)) .addOrder(Order.asc("kmkCode")); accounts = repository.findByCriteria(criteria); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(accounts)) { throw new ObjectNotFoundException("Could not find any account with idoKbn 1"); } return accounts; } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An exception occured while finding account list with idoKbn 1", ex); } finally { HibernateSessionManager.closeSession(session); } } /** {@inheritDoc} */ @Override public List<Account> findByKoteiKbnAndDelKbn(String koteiKhKbn, String delKbn) throws ServiceException { if (StringUtils.isEmpty(koteiKhKbn)) { throw new IllegalArgumentException("The Account's koteiKhKbn to search must not be null or empty"); } if (StringUtils.isEmpty(delKbn)) { throw new IllegalArgumentException("The Account's delKbn to search must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<Account> accounts = new ArrayList<>(); try { tx = session.beginTransaction(); Criteria criteria = repository.getCriteria(session, Account.class) .setProjection(Projections.projectionList().add(Projections.property("kmkCode").as("kmkCode")) .add(Projections.property("kmkNameR").as("kmkNameR"))) .add(Restrictions.eq("koteiKhKbn", koteiKhKbn)).add(Restrictions.eq("delKbn", delKbn)) .addOrder(Order.asc("kmkCode")); accounts = repository.findByCriteria(criteria); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(accounts)) { throw new ObjectNotFoundException("Could not find any account with idoKbn = 1"); } } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An exception occured while finding account list with idoKbn = 1", ex); } finally { HibernateSessionManager.closeSession(session); } return accounts; } /** {@inheritDoc} */ @Override public List<Account> findByHelpKbn(String helpKbn) throws ServiceException { if (StringUtils.isEmpty(helpKbn)) { throw new IllegalArgumentException("The Account's idoKbn to search must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<Account> accounts = new ArrayList<>(); try { tx = session.beginTransaction(); Criteria criteria = repository.getCriteria(session, Account.class) .setProjection(Projections.projectionList().add(Projections.property("kmkCode").as("kmkCode")) .add(Projections.property("kmkNameR").as("kmkNameR"))) .add(Restrictions.eq("helpKbn", helpKbn)) .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)).addOrder(Order.asc("kmkCode")); accounts = repository.findByCriteria(criteria); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(accounts)) { throw new ObjectNotFoundException("Could not find any account with idoKbn = 1"); } } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An exception occured while finding account list with idoKbn = 1", ex); } finally { HibernateSessionManager.closeSession(session); } return accounts; } }