Java tutorial
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.LicensePCEntity; @Component public class LicensePCDAO extends AbstractDAO<LicensePCEntity> { @Override public Class<LicensePCEntity> getType() { return LicensePCEntity.class; } public List<LicensePCEntity> findByCompany(Integer idCompany) { StringBuilder sb = new StringBuilder(); sb.append("from LicensePCEntity e where e.company.idCompany =:idCompany "); sb.append("and e.isActive =:active "); sb.append("order by dateInsert "); Query query = super.getEm().createQuery(sb.toString()); query.setParameter("idCompany", idCompany); query.setParameter("active", Boolean.TRUE); @SuppressWarnings("unchecked") List<LicensePCEntity> list = query.getResultList(); return list; } public LicensePCEntity findByCompanyExpirationDate(Integer idCompany, Calendar dateExpiration) { StringBuilder sb = new StringBuilder(); sb.append("from LicensePCEntity 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<LicensePCEntity> 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 LicensePCEntity lastProtocoled = this.findByLastProtocoled(idCompany); if (lastProtocoled != null && StringUtils.isNotBlank(lastProtocoled.getProtocolRenovation())) { return lastProtocoled; } return null; } /** * Obtem a Ultima licena cadastrada * * @param idCompany * @return */ public LicensePCEntity findByLastProtocoled(Integer idCompany) { StringBuilder sb = new StringBuilder(); sb.append("from LicensePCEntity 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<LicensePCEntity> list = query.getResultList(); if (list != null && !list.isEmpty()) { return list.get(0); } return null; } public LicensePCEntity findByNumCertified(String numCertified) { StringBuilder sb = new StringBuilder(); sb.append("from LicensePCEntity e where e.numCertified =:numCertified "); sb.append("and e.isActive =:active "); Query query = super.getEm().createQuery(sb.toString()); query.setParameter("numCertified", numCertified); query.setParameter("active", Boolean.TRUE); @SuppressWarnings("unchecked") List<LicensePCEntity> 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 LicensePCEntity findLastInvoice(Integer idCompany) { StringBuilder sb = new StringBuilder(); sb.append("from LicensePCEntity 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<LicensePCEntity> list = query.getResultList(); if (list != null && !list.isEmpty()) { return list.get(0); } return null; } }