com.nec.harvest.service.impl.VendorServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.service.impl.VendorServiceImpl.java

Source

/*
 * 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 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.Vendor;
import com.nec.harvest.repository.VendorRepository;
import com.nec.harvest.service.VendorService;

/**
 * {@link VendorService}
 * 
 * @author hungpd
 * 
 */
public class VendorServiceImpl implements VendorService {

    private VendorRepository vendorRepository;

    public VendorServiceImpl(VendorRepository vendorRepository) {
        this.vendorRepository = vendorRepository;
    }

    /**{@inheritDoc}*/
    @Override
    public Vendor findBySrsCode(String srsCode) throws ServiceException {
        if (StringUtils.isEmpty(srsCode)) {
            throw new IllegalArgumentException("Vendor classification (srsCode) must not be null or empty");
        }

        final Session session = HibernateSessionManager.getSession();
        Transaction tx = null;

        Vendor vendor = null;
        try {
            tx = session.beginTransaction();
            Criterion criterion = Restrictions.conjunction().add(Restrictions.eq("srsCode", srsCode))
                    .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE));
            Criteria criteria = vendorRepository.getCriteria(session, Vendor.class);
            criteria.add(criterion);
            List<Vendor> vendors = vendorRepository.findByCriteria(criteria);
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(vendors)) {
                throw new ObjectNotFoundException(
                        "Could not find the Vendor for Vendor classification (srsCode) " + srsCode);
            }

            if (vendors.size() > 1) {
                throw new TooManyObjectsException(
                        "Too many objects are matched with Vendor classification (srsCode) " + srsCode);
            }
            vendor = vendors.get(0);
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while finding a Vendor that matches with "
                    + "Vendor classification " + srsCode, ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return vendor;
    }

}