diff --git a/src/main/java/it/cnr/isti/epasmed/sync/SyncService.java b/src/main/java/it/cnr/isti/epasmed/sync/SyncService.java index 763d7fc..90d48e0 100644 --- a/src/main/java/it/cnr/isti/epasmed/sync/SyncService.java +++ b/src/main/java/it/cnr/isti/epasmed/sync/SyncService.java @@ -1,6 +1,8 @@ package it.cnr.isti.epasmed.sync; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; +import java.util.Date; import java.util.GregorianCalendar; import java.util.List; @@ -10,20 +12,32 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import it.cnr.isti.epasmed.domain.TabsSI; +import it.cnr.isti.epasmed.epas.dto.EPASAffiliationsDTO; +import it.cnr.isti.epasmed.epas.dto.EPASGroupsDTO; import it.cnr.isti.epasmed.epas.dto.EPASPersonsDTO; +import it.cnr.isti.epasmed.epas.mapper.EPASAffiliationsMapper; +import it.cnr.isti.epasmed.epas.mapper.EPASGroupsMapper; import it.cnr.isti.epasmed.epas.mapper.EPASPersonsMapper; +import it.cnr.isti.epasmed.epas.model.EPASAffiliations; +import it.cnr.isti.epasmed.epas.model.EPASGroups; import it.cnr.isti.epasmed.epas.model.EPASPersons; import it.cnr.isti.epasmed.epas.model.EPASTimeCards; import it.cnr.isti.epasmed.epas.model.EPASValidates; +import it.cnr.isti.epasmed.epas.service.EPASAffiliationsService; +import it.cnr.isti.epasmed.epas.service.EPASGroupsService; import it.cnr.isti.epasmed.epas.service.EPASPersonsService; import it.cnr.isti.epasmed.epas.service.EPASTimeCardsService; import it.cnr.isti.epasmed.epas.service.EPASValidatesService; import it.cnr.isti.epasmed.service.TabsSIService; import it.cnr.isti.epasmed.sistemainformativo.model.SIAnagrafico; import it.cnr.isti.epasmed.sistemainformativo.model.SIEmail; +import it.cnr.isti.epasmed.sistemainformativo.model.SIGruppi; +import it.cnr.isti.epasmed.sistemainformativo.model.SIGruppiPers; import it.cnr.isti.epasmed.sistemainformativo.model.SITelefoni; import it.cnr.isti.epasmed.sistemainformativo.service.SIAnagraficoService; import it.cnr.isti.epasmed.sistemainformativo.service.SIEmailService; +import it.cnr.isti.epasmed.sistemainformativo.service.SIGruppiPersService; +import it.cnr.isti.epasmed.sistemainformativo.service.SIGruppiService; import it.cnr.isti.epasmed.sistemainformativo.service.SIMasterLogService; import it.cnr.isti.epasmed.sistemainformativo.service.SITelefoniService; @@ -31,7 +45,7 @@ import it.cnr.isti.epasmed.sistemainformativo.service.SITelefoniService; public class SyncService { private static final String ISTI_OFFICE_ID = "1"; private static final String ISTI_OFFICE_CODEID = "225200"; - + private static final String SI_FLAG_DEL_TRUE = "1"; private static final String SI_RECAPITO_TELEFONICO_UFFICIO = "Ufficio"; @SuppressWarnings("unused") @@ -39,7 +53,7 @@ public class SyncService { private static final String SI_TIPO_EMAIL_CNR = "C.N.R."; private final Logger log = LoggerFactory.getLogger(SyncService.class); - + private final SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); @Autowired TabsSIService tabsSIService; @@ -51,13 +65,20 @@ public class SyncService { SITelefoniService siTelefoniService; @Autowired SIEmailService siEmailService; + @Autowired + SIGruppiService siGruppiService; + @Autowired + SIGruppiPersService siGruppiPersService; @Autowired EPASPersonsService epasPersonsService; @Autowired EPASPersonsMapper epasPersonsMapper; @Autowired - + EPASGroupsService epasGroupsService; + @Autowired + EPASAffiliationsService epasAffiliationsService; + EPASTimeCardsService epasTimeCardsService; @Autowired EPASValidatesService epasValidatesService; @@ -67,8 +88,8 @@ public class SyncService { private boolean btelefoni; private boolean bgruppi; private boolean bgruppo_pers; - //private boolean bposizioni; - //private boolean bproroghe; + // private boolean bposizioni; + // private boolean bproroghe; private boolean borario; private boolean bcartellini; private boolean bcartellini_rendicontazioni; @@ -82,8 +103,8 @@ public class SyncService { btelefoni = false; bgruppi = false; bgruppo_pers = false; - //bposizioni = false; - //bproroghe = false; + // bposizioni = false; + // bproroghe = false; List tabsSI = tabsSIService.getAllTabsSI(); siMasterLogService.startFluxReads(); @@ -91,7 +112,7 @@ public class SyncService { siMasterLogService.closeFluxReads(readTabs()); } - + public void executeWrites() throws Exception { borario = false; bcartellini = false; @@ -101,15 +122,15 @@ public class SyncService { baspettative = false; List tabsSI = tabsSIService.getAllTabsSI(); - Long idFlux=siMasterLogService.startFluxWrites(); - writeData(idFlux,tabsSI); - siMasterLogService.closeFluxWrites(idFlux,writeTabs()); + Long idFlux = siMasterLogService.startFluxWrites(); + writeData(idFlux, tabsSI); + siMasterLogService.closeFluxWrites(idFlux, writeTabs()); } private void readData(List tabsSI) { - //TabsSI posizioniTab = null; - //TabsSI prorogheTab = null; + // TabsSI posizioniTab = null; + // TabsSI prorogheTab = null; for (TabsSI tab : tabsSI) { log.info("TabSI: {}", tab); if (tab.getOperazioni() != null && !tab.getOperazioni().isEmpty() @@ -133,21 +154,21 @@ public class SyncService { case "gruppo_pers": syncGruppoPers(tab); break; - //case "posizioni": - // posizioniTab = tab; - // break; - //case "proroghe": - // prorogheTab = tab; - // break; + // case "posizioni": + // posizioniTab = tab; + // break; + // case "proroghe": + // prorogheTab = tab; + // break; default: break; } } } - //if (posizioniTab != null || prorogheTab != null) { - // syncPosizioniAndProroghe(posizioniTab, prorogheTab); - //} + // if (posizioniTab != null || prorogheTab != null) { + // syncPosizioniAndProroghe(posizioniTab, prorogheTab); + // } } private void syncAnagrafico(TabsSI tab) { @@ -219,13 +240,13 @@ public class SyncService { EPASPersonsDTO epasPersonsDTO = epasPersonsMapper .epasPersonsToEPASPersonsDTO(epasPerson); switch (emailTipo) { - //case SI_TIPO_EMAIL_ISTITUZIONALE: - // epasPersonsDTO.setEmail(se.getEmail()); - // epasPersonsService.updateByFiscalCode(epasPersonsDTO.getFiscalCode(), - // epasPersonsDTO); - // log.info("EPAS Updated Person: {}", epasPersonsDTO); - // - // break; + // case SI_TIPO_EMAIL_ISTITUZIONALE: + // epasPersonsDTO.setEmail(se.getEmail()); + // epasPersonsService.updateByFiscalCode(epasPersonsDTO.getFiscalCode(), + // epasPersonsDTO); + // log.info("EPAS Updated Person: {}", epasPersonsDTO); + // + // break; case SI_TIPO_EMAIL_CNR: epasPersonsDTO.setEmail(se.getEmail()); epasPersonsDTO.setEppn(se.getEmail()); @@ -301,19 +322,221 @@ public class SyncService { } private void syncGruppi(TabsSI tab) { - // TODO Auto-generated method stub + if (tab.getIdFlusso() == null || tab.getIdFlusso().longValue() == 0) { + log.info("Invalid Id Flusso for tab: {}", tab); + return; + } + long maxIdFlusso = 0; + + List sigList = siGruppiService.readNewFlux(tab.getIdFlusso()); + for (SIGruppi sig : sigList) { + if (sig.getId_flusso() != null && sig.getId_flusso().longValue() > maxIdFlusso) { + maxIdFlusso = sig.getId_flusso(); + } + if (sig.getId() != null && sig.getId() != 0) { + if (sig.getFlag_del() != null && !sig.getFlag_del().isEmpty()) { + if (sig.getFlag_del().compareTo(SI_FLAG_DEL_TRUE) != 0) { + List epasGroups = epasGroupsService.getList(ISTI_OFFICE_ID); + if (epasGroups != null) { + EPASGroups groupPresent = null; + for (EPASGroups g : epasGroups) { + if (Integer.valueOf(g.getExternalId()).compareTo(sig.getId()) == 0) { + groupPresent = g; + break; + } + } + + if (groupPresent == null) { + EPASGroupsDTO epasGroupsDTO = new EPASGroupsDTO(sig.getDescrizione(), null, + String.valueOf(sig.getId()), null, null, sig.getSigla(), ISTI_OFFICE_ID); + EPASGroups epasGroup = epasGroupsService.create(epasGroupsDTO); + log.info("EPAS Created Group: {}", epasGroup); + } else { + EPASGroupsMapper epasGroupsMapper = new EPASGroupsMapper(); + EPASGroupsDTO epasGroupsDTO = epasGroupsMapper.epasGroupsToEPASGroupsDTO(groupPresent); + epasGroupsDTO.setDescription(sig.getDescrizione()); + epasGroupsDTO.setName(sig.getSigla()); + epasGroupsService.updateById(groupPresent.getId(), epasGroupsDTO); + log.info("EPAS Updated Group: {}", epasGroupsDTO); + } + } else { + EPASGroupsDTO epasGroupsDTO = new EPASGroupsDTO(sig.getDescrizione(), null, + String.valueOf(sig.getId()), null, null, sig.getSigla(), ISTI_OFFICE_ID); + EPASGroups epasGroup = epasGroupsService.create(epasGroupsDTO); + log.info("EPAS Created Group: {}", epasGroup); + } + } else { + List epasGroups = epasGroupsService.getList(ISTI_OFFICE_ID); + if (epasGroups != null) { + EPASGroups groupPresent = null; + for (EPASGroups g : epasGroups) { + if (Integer.valueOf(g.getExternalId()).compareTo(sig.getId()) == 0) { + groupPresent = g; + break; + } + } + + if (groupPresent != null) { + Date endDate = new Date(); + if (sig.getData_mod() != null) { + endDate.setTime(sig.getData_mod().getTime()); + groupPresent.setEndDate(sdfDate.format(endDate)); + EPASGroupsMapper epasGroupsMapper = new EPASGroupsMapper(); + EPASGroupsDTO epasGroupsDTO = epasGroupsMapper + .epasGroupsToEPASGroupsDTO(groupPresent); + epasGroupsService.updateById(groupPresent.getId(), epasGroupsDTO); + log.info("EPAS Delete Group: {}", epasGroupsDTO); + } + } + } + } + } + } + + } + + if (maxIdFlusso > 0) { + bgruppi = true; + tab.setIdFlusso(maxIdFlusso); + tab.setLastUpdate(LocalDateTime.now()); + tabsSIService.updateTabsSI(tab); + } } private void syncGruppoPers(TabsSI tab) { - // TODO Auto-generated method stub + if (tab.getIdFlusso() == null || tab.getIdFlusso().longValue() == 0) { + log.info("Invalid Id Flusso for tab: {}", tab); + return; + } + long maxIdFlusso = 0; + List sigList = siGruppiPersService.readNewFlux(tab.getIdFlusso()); + for (SIGruppiPers sigp : sigList) { + if (sigp.getId_flusso() != null && sigp.getId_flusso().longValue() > maxIdFlusso) { + maxIdFlusso = sigp.getId_flusso(); + } + if (sigp.getId() != null && sigp.getId() != 0) { + if (sigp.getFlag_del() != null && !sigp.getFlag_del().isEmpty()) { + if (sigp.getFlag_del().compareTo(SI_FLAG_DEL_TRUE) != 0) { + List epasAffiliations = epasAffiliationsService + .getByPersonFiscalcode(sigp.getCf()); + if (epasAffiliations != null) { + EPASAffiliations affPresent = null; + for (EPASAffiliations aff : epasAffiliations) { + if (Integer.valueOf(aff.getExternalId()).compareTo(sigp.getId()) == 0) { + affPresent = aff; + break; + } + } + if (affPresent != null) { + List epasGroups = epasGroupsService.getList(ISTI_OFFICE_ID); + EPASGroups groupPresent = null; + if (epasGroups != null) { + for (EPASGroups g : epasGroups) { + if (Integer.valueOf(g.getExternalId()).compareTo(sigp.getId()) == 0) { + groupPresent = g; + break; + } + } + if (groupPresent != null) { + EPASPersons epasPerson = epasPersonsService.getByFiscalCode(sigp.getCf()); + if (epasPerson != null) { + EPASAffiliationsDTO epasAffiliationsDTO = new EPASAffiliationsDTO( + sdfDate.format(sigp.getDal()), sdfDate.format(sigp.getAl()), + groupPresent.getId(), null, String.valueOf(sigp.getPercentuale()), + epasPerson.getId(), String.valueOf(sigp.getId())); + epasAffiliationsService.updateById(affPresent.getId(), epasAffiliationsDTO); + log.info("EPAS Updated Affilation: {}", epasAffiliationsDTO); + + } + + } + } + } else { + List epasGroups = epasGroupsService.getList(ISTI_OFFICE_ID); + EPASGroups groupPresent = null; + if (epasGroups != null) { + for (EPASGroups g : epasGroups) { + if (Integer.valueOf(g.getExternalId()).compareTo(sigp.getId()) == 0) { + groupPresent = g; + break; + } + } + if (groupPresent != null) { + EPASPersons epasPerson = epasPersonsService.getByFiscalCode(sigp.getCf()); + if (epasPerson != null) { + EPASAffiliationsDTO epasAffiliationsDTO = new EPASAffiliationsDTO( + sdfDate.format(sigp.getDal()), sdfDate.format(sigp.getAl()), + groupPresent.getId(), null, String.valueOf(sigp.getPercentuale()), + epasPerson.getId(), String.valueOf(sigp.getId())); + EPASAffiliations epasAffiliationNew = epasAffiliationsService + .create(epasAffiliationsDTO); + log.info("EPAS Created Affilation: {}", epasAffiliationNew); + + } + + } + } + } + } else { + List epasGroups = epasGroupsService.getList(ISTI_OFFICE_ID); + EPASGroups groupPresent = null; + if (epasGroups != null) { + for (EPASGroups g : epasGroups) { + if (Integer.valueOf(g.getExternalId()).compareTo(sigp.getId()) == 0) { + groupPresent = g; + break; + } + } + if (groupPresent != null) { + EPASPersons epasPerson = epasPersonsService.getByFiscalCode(sigp.getCf()); + if (epasPerson != null) { + EPASAffiliationsDTO epasAffiliationsDTO = new EPASAffiliationsDTO( + sdfDate.format(sigp.getDal()), sdfDate.format(sigp.getAl()), + groupPresent.getId(), null, String.valueOf(sigp.getPercentuale()), + epasPerson.getId(), String.valueOf(sigp.getId())); + EPASAffiliations epasAffiliationNew = epasAffiliationsService + .create(epasAffiliationsDTO); + log.info("EPAS Created Affilation: {}", epasAffiliationNew); + + } + + } + } + } + } else { + List epasAffiliations = epasAffiliationsService + .getByPersonFiscalcode(sigp.getCf()); + if (epasAffiliations != null) { + EPASAffiliations affPresent = null; + for (EPASAffiliations aff : epasAffiliations) { + if (Integer.valueOf(aff.getExternalId()).compareTo(sigp.getId()) == 0) { + affPresent = aff; + break; + } + } + if (affPresent != null) { + EPASAffiliationsMapper epasAffiliationsMapper=new EPASAffiliationsMapper(); + EPASAffiliationsDTO epasAffiliationsDTO=epasAffiliationsMapper.epasAffiliationsToEPASAffiliationsDTO(affPresent); + epasAffiliationsDTO.setEndDate(sdfDate.format(sigp.getAl())); + epasAffiliationsService.updateById(affPresent.getId(), epasAffiliationsDTO); + log.info("EPAS Delete Affilation: {}", epasAffiliationsDTO); + } + } + } + } + } + } + + if (maxIdFlusso > 0) { + bgruppo_pers = true; + tab.setIdFlusso(maxIdFlusso); + tab.setLastUpdate(LocalDateTime.now()); + tabsSIService.updateTabsSI(tab); + } } - //private void syncPosizioniAndProroghe(TabsSI posizioniTab, TabsSI prorogheTab) { - // - //} - private String readTabs() { String readTabs = ""; if (banagrafico) { @@ -354,27 +577,21 @@ public class SyncService { } } - /*if (bposizioni) { - if (tabelle_lette == null || tabelle_lette.isEmpty()) { - tabelle_lette = "posizioni"; - } else { - tabelle_lette = tabelle_lette + ",posizioni"; - } - } - - if (bproroghe) { - if (tabelle_lette == null || tabelle_lette.isEmpty()) { - tabelle_lette = "proroghe"; - } else { - tabelle_lette = tabelle_lette + ",proroghe"; - } - }*/ + /* + * if (bposizioni) { if (tabelle_lette == null || tabelle_lette.isEmpty()) { + * tabelle_lette = "posizioni"; } else { tabelle_lette = tabelle_lette + + * ",posizioni"; } } + * + * if (bproroghe) { if (tabelle_lette == null || tabelle_lette.isEmpty()) { + * tabelle_lette = "proroghe"; } else { tabelle_lette = tabelle_lette + + * ",proroghe"; } } + */ return readTabs; } - + private String writeTabs() { - + String writeTabs = ""; if (borario) { if (writeTabs == null || writeTabs.isEmpty()) { @@ -420,14 +637,10 @@ public class SyncService { } return writeTabs; } - - private void writeData(Long idFlux, List tabsSI) { - TabsSI caretelliniRendicontazioniTab = null; - TabsSI cartelliniTab = null; - - + //TabsSI caretelliniRendicontazioniTab = null; + //TabsSI cartelliniTab = null; for (TabsSI tab : tabsSI) { log.info("TabSI: {}", tab); @@ -436,29 +649,29 @@ public class SyncService { if (tab.getNome() == null || tab.getNome().isEmpty()) { continue; } - + switch (tab.getNome()) { - //case "orario": - // syncOrario(idFlux,tab); - // break; - //case "pers_orario": - // syncPersOrario(idFlux,tab); - // break; + // case "orario": + // syncOrario(idFlux,tab); + // break; + // case "pers_orario": + // syncPersOrario(idFlux,tab); + // break; case "cartellini": break; case "cartellini_rendicontazioni": - syncCartelliniRendicontazioni(idFlux,tab); - break; - //case "lavoro_fuori_sede": - // syncLavoroFuoriSede(idFlux,tab); - // break; - //case "aspettative": - // syncAspettative(idFlux,tab); - // break; + syncCartelliniRendicontazioni(idFlux, tab); + break; + // case "lavoro_fuori_sede": + // syncLavoroFuoriSede(idFlux,tab); + // break; + // case "aspettative": + // syncAspettative(idFlux,tab); + // break; default: break; } - + } } } @@ -470,30 +683,27 @@ public class SyncService { } long maxIdFlusso = 0; - GregorianCalendar lastMonthSent=new GregorianCalendar(2021,11,1); - GregorianCalendar nextMonthToSent=new GregorianCalendar(); + GregorianCalendar lastMonthSent = new GregorianCalendar(2021, 11, 1); + GregorianCalendar nextMonthToSent = new GregorianCalendar(); nextMonthToSent.setTime(lastMonthSent.getTime()); nextMonthToSent.add(GregorianCalendar.MONTH, 1); - String y=String.valueOf(nextMonthToSent.get(GregorianCalendar.YEAR)); - String m=String.valueOf(nextMonthToSent.get(GregorianCalendar.YEAR)); - EPASValidates epasValidates=epasValidatesService.getValidatesByOfficeCodeId(ISTI_OFFICE_CODEID, y, - m); - - if(!epasValidates.getAllCertificationsValidated()) { - log.info("No new month closed on EPAS: [year={}, month={}]", y,m); + String y = String.valueOf(nextMonthToSent.get(GregorianCalendar.YEAR)); + String m = String.valueOf(nextMonthToSent.get(GregorianCalendar.YEAR)); + EPASValidates epasValidates = epasValidatesService.getValidatesByOfficeCodeId(ISTI_OFFICE_CODEID, y, m); + + if (!epasValidates.getAllCertificationsValidated()) { + log.info("No new month closed on EPAS: [year={}, month={}]", y, m); return; } - - //TODO - log.info("Certifications Validated: [year={}, month={}]",y,m); - for(EPASPersons person:epasValidates.getValidatedPersons()) { - log.info("Writing TimeCard for Person: {}",person); - EPASTimeCards epasTimeCards=epasTimeCardsService.getTimeCardByPersonEmail(person.getEmail(), y, m); - + + // TODO + log.info("Certifications Validated: [year={}, month={}]", y, m); + for (EPASPersons person : epasValidates.getValidatedPersons()) { + log.info("Writing TimeCard for Person: {}", person); + EPASTimeCards epasTimeCards = epasTimeCardsService.getTimeCardByPersonEmail(person.getEmail(), y, m); + } - - - + } }