org.jblupus.merger.dao.CrawlerTargetDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.jblupus.merger.dao.CrawlerTargetDAOImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jblupus.merger.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.Query;
import org.jblupus.crawler.CrawlerTarget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Repository
public class CrawlerTargetDAOImpl extends RepositoryDAOImpl<CrawlerTarget> implements CrawlerTargetDAO {
    private static final Logger LOG = Logger.getLogger(CrawlerTargetDAO.class.getName());
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    @Override
    public List<CrawlerTarget> listBySetNumberAndIsCompleted(int setNumber, boolean isCompleted) {
        Session session = null;
        List<CrawlerTarget> response = null;
        try {
            session = sessionFactory.openSession();
            String queryString = "SELECT c FROM CrawlerTarget c" + " WHERE c.setNumber = :setNumber"
                    + " AND c.isCompleted = :isCompleted";
            Query query = session.createQuery(queryString);
            query.setInteger("setNumber", setNumber);
            query.setBoolean("isCompleted", isCompleted);
            query.setMaxResults(4000);
            response = query.list();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return response != null && !response.isEmpty() ? response : null;
    }

    @Transactional
    @SuppressWarnings("unchecked")
    @Override
    public CrawlerTarget findOne(CrawlerTarget crawlerTarget) {
        Session session = null;
        List<CrawlerTarget> response = null;
        try {
            session = sessionFactory.openSession();
            String strQuery = "SELECT c" + " FROM CrawlerTarget c" + " WHERE c.userId = :userId";
            Query query = session.createQuery(strQuery);
            Long userId = (crawlerTarget.getUserId());
            //   query.setParameter("userId", userId);
            query.setLong("userId", userId);
            response = query.list();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return response != null && !response.isEmpty() ? response.get(0) : null;
    }

    @Override
    public void save(List<CrawlerTarget> crawlerTargets) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            for (CrawlerTarget crawlerTarget : crawlerTargets) {
                if (!exists(session, crawlerTarget.getUserId())) {
                    session.save(crawlerTarget);
                }
            }
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        } finally {
            if (session != null) {
                session.flush();
                session.close();
            }
        }
    }

    @Transactional
    @SuppressWarnings({ "unchecked" })
    private boolean exists(Session session, long userId) {
        try {
            String queryString = "SELECT c" + " FROM CrawlerTarget c" + " WHERE c.userId = :userId";
            Query query = session.createQuery(queryString);
            query.setMaxResults(1);
            query.setLong("userId", userId);
            List<CrawlerTarget> result = query.list();
            return !result.isEmpty();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CrawlerTarget> listBySetNumberAndIsCompleted(int setNumber, boolean isCompleted, int serverId) {
        Session session = null;
        List<CrawlerTarget> response = null;
        try {
            session = sessionFactory.openSession();
            String queryString = "SELECT c FROM CrawlerTarget c" + " WHERE c.setNumber = :setNumber"
                    + " AND c.isCompleted = :isCompleted" + " AND c.serverId = :serverId";
            Query query = session.createQuery(queryString);
            System.out.println(queryString);
            query.setInteger("setNumber", setNumber);
            query.setBoolean("isCompleted", isCompleted);
            query.setInteger("serverId", serverId);
            query.setMaxResults(4000);
            response = query.list();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return response != null && !response.isEmpty() ? response : null;
    }
}