Kenmegne
7 days ago 1bc8864f134272c4bf23a9b05831803a423a4771
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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);
            }
        }
    }
}