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

Java tutorial

Introduction

Here is the source code for com.nec.harvest.service.impl.CashServiceImpl.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.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;

import com.nec.core.exception.ObjectNotFoundException;
import com.nec.crud.hibernate.HibernateSessionManager;
import com.nec.harvest.bean.mapping.CashBean;
import com.nec.harvest.bean.mapping.json.JSONCash;
import com.nec.harvest.constant.SqlConstants;
import com.nec.harvest.exception.ServiceException;
import com.nec.harvest.helper.ProductHelper;
import com.nec.harvest.model.User;
import com.nec.harvest.model.internal.Version;
import com.nec.harvest.repository.CashRepository;
import com.nec.harvest.service.CashService;

/**
 * {@link CashService}
 * 
 * @author vuta
 *
 */
public class CashServiceImpl implements CashService {

    private CashRepository repository;

    public CashServiceImpl(CashRepository cashRepository) {
        this.repository = cashRepository;
    }

    /** {@inheritDoc} */
    @Override
    public boolean updateCashByOrgCodeAndDate(Session session, User user, JSONCash jSONCash, String monthly)
            throws ServiceException {
        if (user == null) {
            throw new IllegalArgumentException("user object must not be null or empty");
        }
        if (jSONCash == null) {
            throw new IllegalArgumentException("jSONCash must not be null or empty");
        }
        if (monthly == null) {
            throw new IllegalArgumentException("monthly must not be null or empty");
        }

        boolean isUpdated = Boolean.FALSE;
        try {
            Query query = repository.getNamedQuery(session, SqlConstants.SQL_UPDATE_CASH_BY_ORGANIZATION_AND_MONTH);
            query.setDouble("syokenHanbai", jSONCash.getSyokenHanbai());
            query.setDouble("prepaidHanbai", jSONCash.getPrepaidHanbai());
            query.setDouble("sonotaAzukari", jSONCash.getSonotaAzukari());
            query.setDouble("urikakeKaisyu", jSONCash.getUrikakeKaisyu());
            query.setDouble("kabusoku", jSONCash.getKabusoku());
            query.setDouble("soukingaku", jSONCash.getSoukingaku());
            query.setString("rem", jSONCash.getRem());

            // Logged-in user's code
            String userCode = user.getUsrCode();
            query.setString("tanCode", userCode);
            query.setString("APInf2", userCode);
            query.setString("stfCodeU", userCode);

            String version = null;
            try {
                Version productVersion = ProductHelper.getProductInfor();
                if (productVersion != null) {
                    version = productVersion.getProjectVersion();
                }
            } catch (IOException ex) {
                // TODO
            }
            query.setString("prdNoU", version);
            query.setInteger("updNo", jSONCash.getUpdNo() + 1);
            query.setString("strCode", user.getOrganization().getStrCode());
            query.setString("getSudo", monthly);
            query.setDate("srDate", jSONCash.getSrDate());

            // Updating the record by using SQL for given changed information
            int numberOfRecordUpdated = query.executeUpdate();
            isUpdated = numberOfRecordUpdated > 0;
        } catch (HibernateException ex) {
            throw new ServiceException("An exception occured while update cash data ", ex);
        }
        return isUpdated;
    }

    /** {@inheritDoc} */
    @Override
    @SuppressWarnings("unchecked")
    public List<CashBean> findByOrgCodeAndMonth(String orgCode, String monthly) throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Organization's code must not be null or empty");
        }
        if (StringUtils.isEmpty(monthly)) {
            throw new IllegalArgumentException("Monthly must not be null or empty");
        }

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

        List<CashBean> cashes = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            Query query = repository.getSQLQuery(session, SqlConstants.SQL_FIND_CASH_BY_ORGANIZATION_AND_MONTH);
            query.setParameter("strCode", orgCode);
            query.setParameter("getSudo", monthly);
            query.setResultTransformer(Transformers.aliasToBean(CashBean.class));

            cashes = query.list();
            // Release transaction
            tx.commit();
            if (CollectionUtils.isEmpty(cashes)) {
                throw new ObjectNotFoundException(
                        "Could not find any object in monthly " + monthly + " for the organization " + orgCode);
            }
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while get cash data ", ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return cashes;
    }

    /** {@inheritDoc} */
    @Override
    public int findUpdNoByOrgCodeAndMonthAndSrDate(String orgCode, String monthly, Date srDate)
            throws ServiceException {
        if (StringUtils.isEmpty(orgCode)) {
            throw new IllegalArgumentException("Organization's code must not be null or empty");
        }
        if (StringUtils.isEmpty(monthly)) {
            throw new IllegalArgumentException("Monthly must not be null or empty");
        }
        if (srDate == null) {
            throw new IllegalArgumentException("SrDate must not be null");
        }

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

        int currentUpdNo = 0;
        try {
            tx = session.beginTransaction();
            final String SQL_FIND_UPDNO_BY_ORGANIZATION_AND_MONTH = "SELECT MAX(c.updNo) from Cash AS c WHERE c.pk.organization.strCode = :strCode AND c.pk.getSudo = :getSudo AND c.pk.srDate = :srDate";
            Query query = repository.getQuery(session, SQL_FIND_UPDNO_BY_ORGANIZATION_AND_MONTH);
            query.setParameter("strCode", orgCode);
            query.setParameter("getSudo", monthly);
            query.setParameter("srDate", srDate);

            Object updNo = query.uniqueResult();
            if (updNo != null) {
                currentUpdNo = Integer.parseInt(updNo.toString());
            }
            tx.commit();
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException("An exception occured while get max updNo for the given organization "
                    + orgCode + " strCode and date" + srDate, ex);
        } finally {
            HibernateSessionManager.closeSession(session);
        }
        return currentUpdNo;
    }

}