Java tutorial
package com.solidmaps.webapp.service; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.solidmaps.webapp.dao.LicensePFDAO; import com.solidmaps.webapp.dao.ProductOfficialDAO; import com.solidmaps.webapp.entity.CorporationEntity; import com.solidmaps.webapp.entity.LicensePFEntity; import com.solidmaps.webapp.entity.ProductOfficialEntity; import com.solidmaps.webapp.vo.LicenseValidationVO; @Service public class LicensePFServiceImpl implements LicensePFService { private static final Logger logger = Logger.getLogger(LicensePFServiceImpl.class); @Autowired private LicensePFDAO licenseDAO; @Autowired private InvoiceService invoiceService; @Autowired private ProductOfficialDAO productOfficialDAO; @Override public void delete(Integer id) { LicensePFEntity license = licenseDAO.findById(id); if (license != null) { license.setIsActive(false); licenseDAO.save(license); } invoiceService.revalitateInvoices(license.getCompany()); } @Override public LicensePFEntity insert(LicensePFEntity entity, List<Integer> listaProducts) { entity.setDateInsert(Calendar.getInstance()); entity.setDateUpdate(Calendar.getInstance()); entity.setIsActive(true); entity.setListProducts(new ArrayList<ProductOfficialEntity>()); for (Integer idProduct : listaProducts) { ProductOfficialEntity product = new ProductOfficialEntity(); product.setIdProduct(idProduct); entity.getListProducts().add(product); } entity = licenseDAO.save(entity); invoiceService.revalitateInvoices(entity.getCompany()); logger.info("[license pf insert] logged user: " + entity.getUserInsert().getEmail() + " license: " + entity.getNumCertified()); return entity; } @Override public LicensePFEntity update(LicensePFEntity entity, List<Integer> listaProducts) { LicensePFEntity updateLicense = licenseDAO.findById(entity.getIdLicense()); updateLicense.setDateUpdate(Calendar.getInstance()); updateLicense.setNumCertified(entity.getNumCertified()); updateLicense.setListProducts(new ArrayList<ProductOfficialEntity>()); updateLicense.setCompany(entity.getCompany()); updateLicense.setCodNacional(entity.getCodNacional()); updateLicense.setCodRegister(entity.getCodRegister()); updateLicense.setUserInsert(entity.getUserInsert()); updateLicense.setUserResponsable(entity.getUserResponsable()); updateLicense.setProtocolRenovation(entity.getProtocolRenovation()); updateLicense.setDateExpiration(entity.getDateExpiration()); for (Integer idProduct : listaProducts) { ProductOfficialEntity product = new ProductOfficialEntity(); product.setIdProduct(idProduct); updateLicense.getListProducts().add(product); } licenseDAO.merge(updateLicense); invoiceService.revalitateInvoices(entity.getCompany()); logger.info("[license pf update] logged user: " + entity.getUserInsert().getEmail() + " license: " + entity.getIdLicense()); return entity; } @Override public List<LicensePFEntity> findAll(CorporationEntity corporation) { return licenseDAO.findActivesByCorporation(corporation.getIdCorporation()); } @Override public LicensePFEntity findById(Integer id) { return licenseDAO.findById(id); } @Override public LicenseValidationVO validate(LicensePFEntity entity, List<Integer> listaProducts) { LicenseValidationVO licenseValidationVO = this.validateProducts(entity, listaProducts); if (StringUtils.isBlank(entity.getNumCertified())) { licenseValidationVO.setErrorMsg("Número de Certificado Inválido."); } else { /* Desativar essa regra por enquanto LicensePFEntity listLicense = licenseDAO.findByCompanyExpirationDateNoProtocol( entity.getCompany().getIdCompany(), entity.getDateExpiration()); if (entity.getIdLicense() == null && listLicense != null) { licenseValidationVO.setErrorMsg("Já existe um certificado para esta vigencia"); } */ } return licenseValidationVO; } /** * Valida se h Produtos a mais ou a menos em relao a ultima Licensa * * @param entity * @param listaProducts */ private LicenseValidationVO validateProducts(LicensePFEntity entity, List<Integer> listaProducts) { LicensePFEntity license = licenseDAO.findLastLicense(entity.getCompany().getIdCompany()); LicenseValidationVO licenseValidation = new LicenseValidationVO(); if (license == null) { return licenseValidation; } // Cria a lista de Produtos que esto faltando em relao a Licensa anterior for (ProductOfficialEntity product : license.getListProducts()) { if (!listaProducts.contains(product.getIdProduct())) { licenseValidation.getListMissedProducts().add(product); } } // Cria a lista de Produtos foram inseridos a mais na licensa // TODO ser que eh o melhor jeito de fazer essa merda?? for (Integer idProduct : listaProducts) { boolean exists = false; for (ProductOfficialEntity product : license.getListProducts()) { if (idProduct.equals(product.getIdProduct())) { exists = true; break; } } if (!exists) { licenseValidation.getListNewProducts().add(productOfficialDAO.findById(idProduct)); } } return licenseValidation; } }