com.solidmaps.webapp.dao.LicensePFDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.solidmaps.webapp.dao.LicensePFDAO.java

Source

package com.solidmaps.webapp.dao;

import java.util.Calendar;
import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.solidmaps.webapp.entity.LicensePFEntity;
import com.solidmaps.webapp.entity.ProductOfficialEntity;

@Component
public class LicensePFDAO extends AbstractDAO<LicensePFEntity> {

    @Override
    public Class<LicensePFEntity> getType() {
        return LicensePFEntity.class;
    }

    public List<LicensePFEntity> findByCompany(Integer idCompany) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
        sb.append("order by e.dateInsert desc ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("idCompany", idCompany);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        return list;
    }

    /**
     * Pesquisa se o Produto informado possui uma Licensa valida no Periodo
     * 
     * @param idCompany
     * @param idProductOfficial
     * @return
     */
    public ProductOfficialEntity findByProductOfficial(Integer idLicense, String codNcm) {

        StringBuilder sb = new StringBuilder();
        sb.append("select p ");
        sb.append("from LicensePFEntity e join e.listProducts p ");
        sb.append("where e.idLicense = :idLicense ");
        sb.append("and p.codNcm =:codNcm ");
        sb.append("order by e.dateExpiration "); // deve-se ordenar dessa forma

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("codNcm", codNcm);
        query.setParameter("idLicense", idLicense);

        @SuppressWarnings("unchecked")
        List<ProductOfficialEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    public LicensePFEntity findByCompanyExpirationDate(Integer idCompany, Calendar dateExpiration) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
        sb.append("and e.isActive =:active ");
        sb.append("and e.dateExpiration >= :dateExpiration ");
        sb.append("order by dateExpiration desc ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("idCompany", idCompany);
        query.setParameter("dateExpiration", dateExpiration);
        query.setParameter("active", Boolean.TRUE);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        // TODO Refatorar essa Gambi aqui. Jogar pra uma Service

        // Se no possui Licena vigente, verifica se a ltima est protocolada
        LicensePFEntity lastProtocoled = this.findByLastProtocoled(idCompany);
        if (lastProtocoled != null && StringUtils.isNotBlank(lastProtocoled.getProtocolRenovation())) {
            return lastProtocoled;
        }

        return null;
    }

    /**
     * Obtem a Ultima licena cadastrada
     * 
     * @param idCompany
     * @return
     */
    public LicensePFEntity findByLastProtocoled(Integer idCompany) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
        sb.append("and e.isActive =:active ");
        sb.append("order by dateExpiration desc ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("idCompany", idCompany);
        query.setParameter("active", Boolean.TRUE);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Procura a licensa sem Considerar a Regra de Protocolo. 
     * Essa consulta eh usada na validao do cadsatro de Licenas.
     * 
     * @param idCompany
     * @param dateExpiration
     * @return
     */
    public LicensePFEntity findByCompanyExpirationDateNoProtocol(Integer idCompany, Calendar dateExpiration) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
        sb.append("and e.isActive =:active ");
        sb.append("and e.dateExpiration >= :dateExpiration ");
        sb.append("order by dateExpiration ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("idCompany", idCompany);
        query.setParameter("dateExpiration", dateExpiration);
        query.setParameter("active", Boolean.TRUE);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * A licensa atual  a com Data de Expirao mais atual
     * 
     * @param idCompany
     * @return
     */
    public LicensePFEntity findLastLicense(Integer idCompany) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
        sb.append("and e.isActive =:active ");
        sb.append("order by dateExpiration desc ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("idCompany", idCompany);
        query.setParameter("active", Boolean.TRUE);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Obtem todas as Licensas que existem na Data Atual (new Date())
     * 
     * @return
     */
    public LicensePFEntity findCurrent() {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicensePFEntity e ");
        sb.append("where e.isActive =:active ");
        sb.append("and ( e.dateExpiration >= :dateExpiration or e.protocolRenovation != null ) ");
        sb.append("order by e.dateExpiration desc ");

        Query query = super.getEm().createQuery(sb.toString());

        query.setParameter("dateExpiration", Calendar.getInstance());
        query.setParameter("active", Boolean.TRUE);

        @SuppressWarnings("unchecked")
        List<LicensePFEntity> list = query.getResultList();

        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;

    }

}