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

Java tutorial

Introduction

Here is the source code for com.solidmaps.webapp.dao.LicenseEXDAO.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.LicenseEXEntity;

@Component
public class LicenseEXDAO extends AbstractDAO<LicenseEXEntity> {

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

    public LicenseEXEntity findByCompanyExpirationDate(Integer idCompany, Calendar dateExpiration) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicenseEXEntity 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<LicenseEXEntity> 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
        LicenseEXEntity lastProtocoled = this.findByLastProtocoled(idCompany);
        if (lastProtocoled != null && StringUtils.isNotBlank(lastProtocoled.getProtocolRenovation())) {
            return lastProtocoled;
        }

        return null;

    }

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

        StringBuilder sb = new StringBuilder();
        sb.append("from LicenseEXEntity 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<LicenseEXEntity> 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 LicenseEXEntity findLastInvoice(Integer idCompany) {

        StringBuilder sb = new StringBuilder();
        sb.append("from LicenseEXEntity 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<LicenseEXEntity> list = query.getResultList();

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

        return null;
    }

    /**
     * Consulta a Licensa que est vigente no Periodo informado.
     * O correto  essa consulta retornar apenas um resultado, j que no 
     *  possivel ter mais de uma licena na mesma data de Expirao.
     * 
     * @param idCompany
     * @param referenceDate
     * @return
     */
    public LicenseEXEntity findByCompanyDateReference(Integer idCompany, Calendar initialReferenceDate,
            Calendar finalReferenceDate) {

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

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

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

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

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

        return null;
    }

}