Java tutorial
package com.solidmaps.webapp.xml; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.solidmaps.webapp.dao.GenerateMapsDAO; import com.solidmaps.webapp.dao.InventoryDAO; import com.solidmaps.webapp.dao.LicensePFDAO; import com.solidmaps.webapp.dao.MapProductInventoryFederalDAO; import com.solidmaps.webapp.entity.CompanyEntity; import com.solidmaps.webapp.entity.InventoryEntity; import com.solidmaps.webapp.entity.InvoiceProductEntity; import com.solidmaps.webapp.entity.LicensePFEntity; import com.solidmaps.webapp.entity.MapProductEntity; import com.solidmaps.webapp.entity.MapProductInventoryEntity; import com.solidmaps.webapp.entity.ProductEntity; import com.solidmaps.webapp.entity.ProductOfficialEntity; import com.solidmaps.webapp.entity.UserEntity; import com.solidmaps.webapp.enuns.MapTypeEnum; import com.solidmaps.webapp.report.MapTxtFederalGeneratorService; import com.solidmaps.webapp.service.CompanyService; import com.solidmaps.webapp.service.MailService; import com.solidmaps.webapp.service.MapProductService; import com.solidmaps.webapp.utils.DateUtils; import com.solidmaps.webapp.utils.NumberUtils; /** * Responsavel por gerar o XML que sera enviado para a policia federal * * @author brunorocca * */ @Service public class GenerateFederalMapsServiceImpl implements GenerateFederalMapsService { private static final Logger logger = Logger.getLogger(GenerateFederalMapsServiceImpl.class); @Autowired private GenerateMapsDAO generateMapsDAO; @Autowired private CompanyService companyService; @Autowired private LicensePFDAO licensePFDAO; @Autowired private InventoryDAO inventoryDAO; @Autowired private MapProductService mapProductService; @Autowired private MailService mailService; @Autowired private MapTxtFederalGeneratorService generateFederalTxtService; @Autowired private MapProductInventoryFederalDAO mapProductInventoryDAO; @Value("${path.map.file.path}") private String FILE_PATH; @Override public MapProductEntity generate(Integer idCompany, String monthYear, UserEntity userInsert) throws Exception { logger.info("Gerando o Mapa do Exercito. Empresa: " + idCompany + " MesAno: " + monthYear); // Cria o TXT MapProductEntity mapProduct = this.findAndCreate(idCompany, monthYear, userInsert); // Envia um Email mailService.sendMapGeneraterEmail(mapProduct); return mapProduct; } /** * Efetua a busca dos Dados e Gera o Arquivo XML * * @param idCompany * @param monthYear * @param userInsert * @return * @throws Exception */ private MapProductEntity findAndCreate(Integer idCompany, String monthYear, UserEntity userInsert) throws Exception { StringBuilder sb = new StringBuilder(); Calendar initialReferenceDate = DateUtils.finalMonthYear(monthYear); CompanyEntity company = companyService.findById(idCompany); LicensePFEntity license = licensePFDAO.findByCompanyExpirationDate(idCompany, initialReferenceDate); // Buscas as Informaes List<InvoiceProductEntity> listMoves = this.createMovesProduts(license, monthYear); List<InvoiceProductEntity> listImportMoves = this.createMovesImportProduts(license, company, monthYear); List<InventoryEntity> listInventory = this.createProductsInventory(license, company, monthYear); // Gera o arquivo texto sb.append(this.createCompany(license, company, monthYear)); sb.append(this.createPeriod(license, company, monthYear)); sb.append(generateFederalTxtService.createMovements(listMoves)); sb.append(generateFederalTxtService.createImportMovements(license, listImportMoves)); sb.append(generateFederalTxtService.createInventory(listInventory)); // Cria fisicamente o Mapa MapProductEntity mapProduct = this.generateAndSaveFile(sb.toString(), company, monthYear, userInsert); // Salva os dados dos Mapa this.saveProductMoves(mapProduct, listMoves); this.saveProductMoves(mapProduct, listImportMoves); this.saveProductInventory(mapProduct, listInventory); return mapProduct; } private String createPeriod(LicensePFEntity license, CompanyEntity companyEntity, String mouthYear) { return generateFederalTxtService.createMonth(companyEntity, mouthYear); } private String createCompany(LicensePFEntity license, CompanyEntity companyEntity, String mouthYear) { return generateFederalTxtService.createCompany(license); } private List<InventoryEntity> createProductsInventory(LicensePFEntity license, CompanyEntity company, String mouthYear) { Calendar initialReferenceDate = DateUtils.createInitialMonthYear(mouthYear); Calendar finalReferenceDate = DateUtils.finalMonthYear(mouthYear); List<InventoryEntity> listInventory = inventoryDAO.findByReferenceDate(company.getIdCompany(), license, initialReferenceDate, finalReferenceDate); List<InventoryEntity> listInventorySum = this.sumInventory(listInventory); this.addInventoryZero(license, listInventorySum, initialReferenceDate); return listInventorySum; } /** * Os produtos da Licena que no tem Inventrio deve aparecer no mapa zerados * @return */ private void addInventoryZero(LicensePFEntity license, List<InventoryEntity> listInventorySum, Calendar initialReferenceDate) { List<ProductOfficialEntity> listProducts = license.getListProducts(); List<InventoryEntity> listInventory = new ArrayList<InventoryEntity>(); for (ProductOfficialEntity licenseProduct : listProducts) { boolean haveInventory = false; for (InventoryEntity inventory : listInventorySum) { if (inventory.getProduct().getProductOfficial().getCodNcm().equals(licenseProduct.getCodNcm())) { haveInventory = true; break; } } // Se no tem inventrio, ento cria um zerado if (!haveInventory) { InventoryEntity inventoryZero = new InventoryEntity(); inventoryZero.setDateCreate(initialReferenceDate); inventoryZero.setCompany(license.getCompany()); inventoryZero.setProduct(new ProductEntity()); inventoryZero.getProduct().setProductOfficial(licenseProduct); inventoryZero.setTypeQtdProduct("KG"); listInventory.add(inventoryZero); } } listInventorySum.addAll(listInventory); } private MapProductEntity generateAndSaveFile(String map, CompanyEntity company, String monthYear, UserEntity userInsert) throws Exception { StringBuilder fileName = new StringBuilder(); fileName.append("M").append(DateUtils.getYear(monthYear)) .append(DateUtils.getMonthName(monthYear).substring(0, 3).toUpperCase()) .append(NumberUtils.clear(company.getCnpj())).append(".txt"); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(FILE_PATH + fileName.toString()), "UTF-8"); out.write(map); out.close(); MapProductEntity mapProduct = this.saveMapProducts(fileName.toString(), company, monthYear, userInsert); return mapProduct; } private MapProductEntity saveMapProducts(String fileName, CompanyEntity company, String monthYear, UserEntity userInsert) { MapProductEntity mapProductEntity = new MapProductEntity(); mapProductEntity.setDateInsert(Calendar.getInstance()); mapProductEntity.setDateUpdate(Calendar.getInstance()); mapProductEntity.setIsActive(Boolean.TRUE); mapProductEntity.setMonthYear(DateUtils.createInitialMonthYear(monthYear)); mapProductEntity.setCompany(company); mapProductEntity.setFileName(fileName); mapProductEntity.setFilePath(FILE_PATH); mapProductEntity.setType(MapTypeEnum.POLICIA_FEDERAL.getType()); mapProductEntity.setName(MapTypeEnum.POLICIA_FEDERAL.getName()); mapProductEntity.setUserInsert(userInsert); mapProductEntity.setCorporation(company.getCorporation()); mapProductService.insert(mapProductEntity); return mapProductEntity; } private List<InvoiceProductEntity> createMovesProduts(LicensePFEntity license, String mouthYear) { return generateMapsDAO.findInvoicesFederal(license.getIdLicense(), DateUtils.createInitialMonthYear(mouthYear), DateUtils.finalMonthYear(mouthYear)); } private List<InvoiceProductEntity> createMovesImportProduts(LicensePFEntity license, CompanyEntity companyEntity, String mouthYear) { return generateMapsDAO.findImportExportMovesFederal(companyEntity.getIdCompany(), license.getIdLicense(), DateUtils.createInitialMonthYear(mouthYear), DateUtils.finalMonthYear(mouthYear)); } private void saveProductMoves(MapProductEntity mapProduct, List<InvoiceProductEntity> listInvoiceProducts) { mapProductService.saveMapProductInvoices(mapProduct, listInvoiceProducts); } private void saveProductInventory(MapProductEntity mapProduct, List<InventoryEntity> listInventoryProducts) throws Exception { for (InventoryEntity inventory : listInventoryProducts) { // Se o ID inventory for nullo, ento o Inventrio Zerado. if (inventory.getIdInventory() == null) { continue; } InventoryEntity newInventory = (InventoryEntity) BeanUtils.cloneBean(inventory); newInventory.setIdInventory(null); newInventory.setDateInsert(Calendar.getInstance()); newInventory.setIsActive(Boolean.FALSE); // no deve aparecer na listagem inventoryDAO.persist(newInventory); MapProductInventoryEntity mapProductMovesEntity = new MapProductInventoryEntity(); mapProductMovesEntity.setMapProduct(mapProduct); mapProductMovesEntity.setInventory(newInventory); mapProductInventoryDAO.save(mapProductMovesEntity); } } /** * Soma as Qnts do Inventario pelo Codigo Meyre * @return * */ private List<InventoryEntity> sumInventory(List<InventoryEntity> listInventory) { Map<String, InventoryEntity> mapInventory = new HashMap<String, InventoryEntity>(); for (InventoryEntity inventoryParam : listInventory) { InventoryEntity inventoryMap = mapInventory .get(inventoryParam.getProduct().getProductOfficial().getCodNcm()); if (inventoryMap != null) { inventoryMap.setQtdBuy(inventoryMap.getQtdBuy().add(inventoryParam.getQtdBuy())); inventoryMap.setQtdEvaporation( inventoryMap.getQtdEvaporation().add(inventoryParam.getQtdEvaporation())); inventoryMap.setQtdExport(inventoryMap.getQtdExport().add(inventoryParam.getQtdExport())); inventoryMap.setQtdImport(inventoryMap.getQtdImport().add(inventoryParam.getQtdImport())); inventoryMap.setQtdInventoryBefore( inventoryMap.getQtdInventoryBefore().add(inventoryParam.getQtdInventoryBefore())); inventoryMap.setQtdLost(inventoryMap.getQtdLost().add(inventoryParam.getQtdLost())); inventoryMap .setQtdProduction(inventoryMap.getQtdProduction().add(inventoryParam.getQtdProduction())); inventoryMap.setQtdRecycling(inventoryMap.getQtdRecycling().add(inventoryParam.getQtdRecycling())); inventoryMap.setQtdReuse(inventoryMap.getQtdReuse().add(inventoryParam.getQtdReuse())); inventoryMap.setQtdSell(inventoryMap.getQtdSell().add(inventoryParam.getQtdSell())); inventoryMap.setQtdTransformation( inventoryMap.getQtdTransformation().add(inventoryParam.getQtdTransformation())); inventoryMap.setQtdUtilization( inventoryMap.getQtdUtilization().add(inventoryParam.getQtdUtilization())); inventoryMap.setQtdVariousInput( inventoryMap.getQtdVariousInput().add(inventoryParam.getQtdVariousInput())); inventoryMap.setQtdVariousOutput( inventoryMap.getQtdVariousOutput().add(inventoryParam.getQtdVariousOutput())); inventoryMap.setQtdInventoryCurrent( inventoryMap.getQtdInventoryCurrent().add(inventoryParam.getQtdInventoryCurrent())); } else { mapInventory.put(inventoryParam.getProduct().getProductOfficial().getCodNcm(), inventoryParam); } } List<InventoryEntity> listInventoryNew = new ArrayList<InventoryEntity>(); listInventoryNew.addAll(mapInventory.values()); return listInventoryNew; } }