com.iana.dver.dao.impl.DverDetailsDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.iana.dver.dao.impl.DverDetailsDaoImpl.java

Source

/*
 * Copyright (c) 2013 IANA. All Rights Reserved. THE AUTHOR MAKES NO
 * REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE
 * AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.iana.dver.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.iana.dver.dao.DverDetailsDao;
import com.iana.dver.domain.DverDetail;
import com.iana.dver.domain.DverFiles;
import com.iana.dver.domain.DverNotif;

/**
 * @author tgbaxi
 * @date 13-Sep-2013
 * 
 */
@Repository(value = "dverDetailsDao")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DverDetailsDaoImpl extends HibernateDaoSupport implements DverDetailsDao {

    private static final Logger logger = Logger.getLogger(DverDetailsDaoImpl.class);

    private final static String FIND_REJECTED_DVERS = "FROM DverDetail DD WHERE DD.mcFound = 'N' AND DD.iepFound = 'N' ORDER BY DD.dverDetailId DESC";

    private final static String FIND_SELECTED_REJECTED_DVERS = "FROM DverDetail DD WHERE DD.mcFound = 'N' AND DD.iepFound = 'N' AND DD.dverDetailId IN (:dverDetailIds) ORDER BY DD.dverDetailId DESC";

    private final static String COUNT_REJECTED_DVERS = "SELECT COUNT(DD.dverDetailId) FROM DverDetail DD WHERE DD.mcFound = 'N' AND DD.iepFound = 'N' ORDER BY DD.dverDetailId DESC";

    private final static String FIND_FAIL_NOTIFICATIONS = "FROM DverNotif DN WHERE DN.sent = 'N' ";

    private final static String COUNT_FAIL_NOTIFICATIONS = "SELECT COUNT(DN.dverNotifId) FROM DverNotif DN WHERE DN.sent = 'N' ";

    private final static String FIND_DVER_FILE_FROM_DVER_DETAIL_ID = "FROM DverFiles DF WHERE DF.dverDetail.dverDetailId = :dverDetailId";

    private final static String FIND_DVER_FILE_VO_FROM_DVER_DETAIL_ID = "SELECT DF.fileContent FROM DverFiles DF WHERE DF.dverDetail.dverDetailId IN (:dverDetailIds)";

    @Autowired
    public DverDetailsDaoImpl(@Qualifier(value = "sessionFactory") SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<DverDetail> findAllRejectedDvers(final int start, final int end) throws DataAccessException {

        logger.info("Find All Rejected Dvers....");
        List rejectedDvers = getHibernateTemplate().executeFind(new HibernateCallback<List<DverDetail>>() {
            @Override
            public List<DverDetail> doInHibernate(Session session) throws HibernateException, SQLException {
                org.hibernate.Query q = session.createQuery(FIND_REJECTED_DVERS);
                q.setFirstResult(start);
                q.setMaxResults(end);
                return q.list();
            }
        });

        return (!CollectionUtils.isEmpty(rejectedDvers)) ? rejectedDvers : new ArrayList<DverDetail>();
    }

    @Override
    public List<DverDetail> findAllRejectedDversForExcel() throws DataAccessException {

        logger.info("Find All Rejected Dvers for Excel report....");
        List rejectedDvers = getHibernateTemplate().executeFind(new HibernateCallback<List<DverDetail>>() {
            @Override
            public List<DverDetail> doInHibernate(Session session) throws HibernateException, SQLException {
                org.hibernate.Query q = session.createQuery(FIND_REJECTED_DVERS);
                return q.list();
            }
        });

        return (!CollectionUtils.isEmpty(rejectedDvers)) ? rejectedDvers : new ArrayList<DverDetail>();
    }

    @Override
    public List<DverDetail> findSelectedRejectedDversForExcel(final List<Integer> dverSelectedIdList)
            throws DataAccessException {

        logger.info("Find selected Rejected Dvers for Excel report....");
        List rejectedDvers = getHibernateTemplate().executeFind(new HibernateCallback<List<DverDetail>>() {
            @Override
            public List<DverDetail> doInHibernate(Session session) throws HibernateException, SQLException {
                Query q = session.createQuery(FIND_SELECTED_REJECTED_DVERS);
                q.setParameterList("dverDetailIds", dverSelectedIdList);
                return q.list();
            }
        });

        return (!CollectionUtils.isEmpty(rejectedDvers)) ? rejectedDvers : new ArrayList<DverDetail>();
    }

    @Override
    public int getAllRejectedDversCount() throws DataAccessException {
        logger.info("get All Rejected Dvers Count....");
        List result = getHibernateTemplate().find(COUNT_REJECTED_DVERS);
        return CollectionUtils.isEmpty(result) ? 0 : Integer.valueOf(result.get(0).toString());
    }

    @Override
    public DverDetail findDverDetailById(Integer dverDetailId) throws DataAccessException {
        logger.info("find DverDetail ById....");
        return getHibernateTemplate().get(DverDetail.class, dverDetailId);
    }

    @Override
    public void updateDverDetail(DverDetail dverDetail) throws DataAccessException {
        logger.info("Update DverDetail ....");
        getHibernateTemplate().saveOrUpdate(dverDetail);
    }

    @Override
    public List<DverNotif> findAllFailNotifications(final int start, final int end) throws DataAccessException {
        List failNotifs = getHibernateTemplate().executeFind(new HibernateCallback<List<DverNotif>>() {
            @Override
            public List<DverNotif> doInHibernate(Session session) throws HibernateException, SQLException {
                logger.info("find All Failed Notifications ....");
                org.hibernate.Query q = session.createQuery(FIND_FAIL_NOTIFICATIONS);
                q.setFirstResult(start);
                q.setMaxResults(end);
                return q.list();
            }
        });

        return (!CollectionUtils.isEmpty(failNotifs)) ? failNotifs : new ArrayList<DverNotif>();

    }

    @Override
    public int countAllFailNotifs() throws DataAccessException {
        logger.info("Count All Failed Notifications ....");
        List result = getHibernateTemplate().find(COUNT_FAIL_NOTIFICATIONS);
        return CollectionUtils.isEmpty(result) ? 0 : Integer.valueOf(result.get(0).toString());
    }

    @Override
    public DverFiles findDverFileByDverDetailId(Integer dverDetailsId) throws DataAccessException {
        logger.info("find DverFile By DverDetailId ....");
        List result = getHibernateTemplate().findByNamedParam(FIND_DVER_FILE_FROM_DVER_DETAIL_ID, "dverDetailId",
                dverDetailsId);
        return (!CollectionUtils.isEmpty(result)) ? (DverFiles) result.get(0) : null;
    }

    @Override
    public List<byte[]> getDverFilesFromDverDetailIds(final List<Integer> dverDetailsIds)
            throws DataAccessException {
        List files = getHibernateTemplate().executeFind(new HibernateCallback<List<byte[]>>() {
            @Override
            public List<byte[]> doInHibernate(Session session) throws HibernateException, SQLException {
                logger.info("get DverFiles From DverDetailIds ....");
                Query query = session.createQuery(FIND_DVER_FILE_VO_FROM_DVER_DETAIL_ID);
                query.setParameterList("dverDetailIds", dverDetailsIds);
                return query.list();
            }
        });
        return (!CollectionUtils.isEmpty(files)) ? files : new ArrayList<byte[]>();
    }

    @Override
    public List<DverDetail> filterRejectedDvers(Map<String, String> filtermap, int start, int end)
            throws DataAccessException {
        logger.info("filter Rejected DVER's By Scac Or Dot.....");
        Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
        String filterQuery = generateQuery(filtermap, Boolean.FALSE);
        logger.info("filter query for Rejected DVER's is :: " + filterQuery);
        Query filters = session.createQuery(filterQuery);
        filters.setFirstResult(start);
        filters.setMaxResults(end);
        List result = filters.list();
        session.close();
        return result != null ? result : new ArrayList<DverDetail>();
    }

    @Override
    public int filterRejectedDversCount(Map<String, String> filtermap) throws DataAccessException {
        Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
        String filterQuery = generateQuery(filtermap, Boolean.TRUE);
        logger.info("filter query for Count Rejected DVER's is :: " + filterQuery);
        Query filters = session.createQuery(filterQuery);
        List result = filters.list();
        session.close();
        return CollectionUtils.isEmpty(result) ? 0 : Integer.valueOf(result.get(0).toString());
    }

    private String generateQuery(Map<String, String> filtermap, Boolean isCntQuery) {
        StringBuilder filterQuery = new StringBuilder();
        if (isCntQuery) {
            filterQuery.append("SELECT COUNT(DD.dverDetailId) ");
        }
        filterQuery.append("FROM DverDetail DD WHERE DD.mcFound = 'N' AND DD.iepFound = 'N' ");

        if (filtermap.containsKey("mcDotFilter")) {
            filterQuery.append(" AND  TRIM(LEADING '0' FROM DD.mcdot) like '" + filtermap.get("mcDotFilter") + "'");
        }

        if (filtermap.containsKey("iepDotFilter")) {
            filterQuery
                    .append(" AND TRIM(LEADING '0' FROM DD.iepdot) like '" + filtermap.get("iepDotFilter") + "'");
        }

        if (filtermap.containsKey("reportNoFilter")) {
            filterQuery.append(" AND DD.reportNum like '" + filtermap.get("reportNoFilter") + "'");
        }

        filterQuery.append(" ORDER BY DD.dverDetailId DESC");
        return filterQuery.toString();
    }
}