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.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.nec.crud.hibernate.HibernateSessionManager; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.InventoryCategory; import com.nec.harvest.repository.InventoryCategoryRepository; import com.nec.harvest.service.InventoryCategoryService; /** * {@link InventoryCategoryService} * * @author sondn * */ public class InventoryCategoryServiceImpl implements InventoryCategoryService { private InventoryCategoryRepository categoryRepository; public InventoryCategoryServiceImpl(InventoryCategoryRepository categoryRepository) { this.categoryRepository = categoryRepository; } /** {@inheritDoc} */ @Override public InventoryCategory findByCategoryCode(String categoryId) throws ServiceException { if (StringUtils.isEmpty(categoryId)) { throw new ServiceException("Id must not be null"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; InventoryCategory category = null; try { tx = session.beginTransaction(); category = categoryRepository.get(session, categoryId); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("Could not find any inventory category maches with " + categoryId, ex); } finally { HibernateSessionManager.closeSession(session); } return category; } /** {@inheritDoc} */ @Override public List<InventoryCategory> findByCategoryCodes(String... categories) throws ServiceException { if (ArrayUtils.isEmpty(categories)) { throw new ServiceException("Ids must not be null"); } List<InventoryCategory> results = new ArrayList<>(); for (String categoryCode : categories) { InventoryCategory category = findByCategoryCode(categoryCode); if (category != null) { results.add(category); } } return results; } }