package com.megatim.apifdxweb.service.impl.camel.processors;
|
|
import com.megatim.apifdxweb.core.ifaces.dataproduction.DataProductionManager;
|
import com.megatim.apifdxweb.core.ifaces.helper.FileDataReader;
|
import com.megatim.apifdxweb.core.ifaces.integration.ColumnDefinitionManager;
|
import com.megatim.apifdxweb.core.ifaces.referentiel.TypeFichierManager;
|
import com.megatim.apifdxweb.core.ifaces.referentiel.natureproduction.NatureProductionFichierManager;
|
import com.megatim.fdxcommons.model.searchentities.ColumnDefinitionSearch;
|
import com.megatim.apifdxweb.service.impl.camel.exceptions.BadFileNamePatterException;
|
import com.megatim.apifdxweb.service.impl.camel.exceptions.TimestampException;
|
import com.megatim.fdxcommons.model.dataproduction.DataProduction;
|
import com.megatim.fdxcommons.tools.exceptions.FileTypeNotExistException;
|
import com.megatim.fdxcommons.model.integration.ColumnDefinition;
|
import com.megatim.fdxcommons.model.referentiel.TypeFichier;
|
import com.megatim.fdxcommons.model.referentiel.natureproduction.NatureProduction;
|
import com.megatim.fdxcommons.tools.exceptions.CommonRessourceNotFoundException;
|
import com.megatim.fdxcommons.tools.integration.ApiIntegrationData;
|
import java.io.File;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import javax.enterprise.context.Dependent;
|
import javax.inject.Inject;
|
import org.apache.camel.Exchange;
|
import org.apache.camel.Processor;
|
|
/**
|
*
|
* @author lenovo
|
*/
|
@Dependent
|
public class FichierIntegrationProcessor implements Processor {
|
|
@Inject
|
private TypeFichierManager typeFichierManager;
|
|
@Inject
|
private ColumnDefinitionManager columnDefinitionManager;
|
|
@Inject
|
private FileDataReader fileDataReader;
|
|
@Inject
|
private DataProductionManager dataProductionManager;
|
|
@Inject
|
private NatureProductionFichierManager natureProductionFichierManager;
|
|
@Override
|
public void process(Exchange exchange) throws Exception {
|
System.out.println("***********************in FichierIntegrationProcessor");
|
File file = exchange.getIn().getBody(File.class);
|
final String fileName = file.getName();
|
|
if (fileName.length() < 12) {
|
throw new BadFileNamePatterException(fileName);
|
}
|
|
String referentielVersion = fileName.substring(0, 6);
|
String codeTypeFichier = fileName.substring(6, 12);
|
String date = fileName.substring(12, 26);
|
LocalDateTime fileDate = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
|
|
stopIfTypeFichierNotExists(codeTypeFichier, referentielVersion);
|
stopIfFileTimestampBeforeLastPoduction(referentielVersion, codeTypeFichier, fileName, fileDate);
|
|
List<LinkedHashMap<String, Object>> data = fileDataReader.getDatas(referentielVersion, codeTypeFichier, file);
|
ApiIntegrationData integrationData = new ApiIntegrationData(data, codeTypeFichier, referentielVersion, fileName, fileDate);
|
|
exchange.getIn().setBody(integrationData);
|
}
|
|
private void stopIfTypeFichierNotExists(String codeTypeFichier, String referentielVersion) throws FileTypeNotExistException {
|
TypeFichier typeFich = typeFichierManager.getById(codeTypeFichier);
|
|
if (typeFich == null) {
|
throw new CommonRessourceNotFoundException("TypeFichier '" + codeTypeFichier + "' inexistant");
|
}
|
System.out.println("******************************");
|
System.out.println("codeTypeFichier : " + codeTypeFichier + ", referentielVersion : " + referentielVersion);
|
ColumnDefinitionSearch searchEntity = new ColumnDefinitionSearch();
|
searchEntity.setCodeTypeFichier(codeTypeFichier);
|
searchEntity.setReferentielVersion(referentielVersion);
|
|
System.out.println("before call to getAll");
|
List<ColumnDefinition> colDefs = columnDefinitionManager.getAll(searchEntity);
|
System.out.println("after call to getAll");
|
System.out.println("******************************");
|
|
if (colDefs == null || colDefs.isEmpty()) {
|
throw new CommonRessourceNotFoundException("La structure du typeFichier '" + codeTypeFichier + "' est introuvable");
|
}
|
}
|
|
private void stopIfFileTimestampBeforeLastPoduction(String referentielVersion, String codeTypeFichier, String fileName, LocalDateTime dateTime) throws TimestampException {
|
NatureProduction natureProd = natureProductionFichierManager.getNatureProduction(codeTypeFichier, referentielVersion);
|
|
if (natureProd.equals(NatureProduction.REFERENTIELLE)) {
|
DataProduction lastDataProduction = dataProductionManager.lastProduction(referentielVersion, codeTypeFichier);
|
|
if (lastDataProduction != null
|
&& (dateTime.equals(lastDataProduction.getFileDate()) || dateTime.isBefore(lastDataProduction.getFileDate()))) {
|
throw new TimestampException(fileName);
|
}
|
}
|
}
|
}
|