com.ust.coppel.incentivos.repository.impl.ConnectionRepositoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ust.coppel.incentivos.repository.impl.ConnectionRepositoryImpl.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 com.ust.coppel.incentivos.repository.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.model.admin.CatConexiones;
import com.model.admin.ConnectionVO;
import com.model.admin.CtlAmbientes;
import com.ust.coppel.incentivos.repository.ConnectionRepository;

/**
 *
 * @author U39735
 */
@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ConnectionRepositoryImpl implements ConnectionRepository {

    private SessionFactory sessionFactory;

    @Transactional
    @Override
    public List<CatConexiones> findAll() {
        Session session = sessionFactory.getCurrentSession();
        List list = session.createQuery("from CatConexiones").list();
        return list;
    }

    @Transactional
    @Override
    public List<CtlAmbientes> findAmbientes() {
        Session session = sessionFactory.getCurrentSession();
        List list = session.createQuery("from CtlAmbientes").list();
        return list;
    }

    @Transactional
    @Override
    public void add(CatConexiones connection) {

        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(CatConexiones.class)
                .setProjection(Projections.max("iduConexion"));
        Integer max = ((Integer) criteria.uniqueResult());
        if (max != null)
            max += 1;
        else
            max = 1;
        connection.setIduConexion(max);
        session.save(connection);

    }

    @Transactional
    @Override
    public void update(CatConexiones connection) {

        Session session = sessionFactory.getCurrentSession();
        session.update(connection);

    }

    @Transactional
    @Override
    public void remove(CatConexiones connection) {
        Session session = sessionFactory.getCurrentSession();

        session.delete(connection);
    }

    @Transactional
    @Override
    public CatConexiones find(Integer Id) {
        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(CatConexiones.class);

        criteria.add(Restrictions.eq("iduConexion", Id));
        CatConexiones aux = null;
        for (Object aux1 : criteria.list()) {
            aux = (CatConexiones) aux1;
        }
        return aux;
    }

    @Transactional
    @Override
    public CtlAmbientes findAmbiente(Integer Id) {
        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(CtlAmbientes.class);

        criteria.add(Restrictions.eq("iduAmbiente", Id));
        CtlAmbientes aux = null;
        for (Object aux1 : criteria.list()) {
            aux = (CtlAmbientes) aux1;
        }
        return aux;
    }

    @Override
    public boolean testConnection(ConnectionVO connection) {

        Connection conn = null;

        String url = "jdbc:sqlserver://" + connection.getNomServer() + ":" + connection.getNumPuerto()
                + ";databaseName=" + connection.getNomBaseDatos();

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            conn = DriverManager.getConnection(url, connection.getNomUsr(), connection.getNomPwd());
        } catch (ClassNotFoundException | SQLException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(ConnectionRepositoryImpl.class.getName()).log(Level.SEVERE, null, ex);
            conn = null;
        }
        return conn != null;
    }

}