Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
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
package com.megatim.fdxconsultation.core.impl.dataproductionworker;
 
import com.megatim.fdxcommons.core.impl.tools.CommonUtilities;
import com.megatim.fdxcommons.model.camel.CamelRouteConfiguration;
import com.megatim.fdxcommons.model.dataproduction.CommonDataProduction;
import com.megatim.fdxcommons.tools.context.AppContext;
import com.megatim.fdxcommons.tools.database.connection.DBConnection;
import com.megatim.fdxconsultation.core.ifaces.dataproduction.metadata.ProductionMetaDataManager;
import com.megatim.fdxconsultation.core.ifaces.helper.TableInsert;
import com.megatim.fdxconsultation.core.ifaces.integration.ColumnDefinitionManager;
import com.megatim.fdxconsultation.core.impl.camel.helper.FileInsertionProcess;
import com.megatim.fdxconsultation.core.impl.sockets.DataProductionSessionHandler;
import com.megatim.fdxconsultation.model.dataproduction.DataProduction;
import com.megatim.fdxconsultation.model.mappers.MapStructMapper;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
 
/**
 *
 * @author Gabuntu
 */
class FileDataProductionTask implements DataProductionTask {
 
    private final DataProduction dataProduction;
    private final DataSource dataSource;
    private final ColumnDefinitionManager columnDefinitionManager;
    private final DataProductionSessionHandler dataProductionSessionHandler;
    private final TableInsert tableInsert;
    private final MapStructMapper mapper;
    private final ProductionMetaDataManager metaDataManager;
 
    public FileDataProductionTask(DataProduction dataProduction,
            DataSource dataSource,
            ColumnDefinitionManager columnDefinitionManager,
            DataProductionSessionHandler dataProductionSessionHandler,
            TableInsert tableInsert,
            MapStructMapper mapper,
            ProductionMetaDataManager metaDataManager) {
        this.dataProduction = dataProduction;
        this.dataSource = dataSource;
        this.columnDefinitionManager = columnDefinitionManager;
        this.dataProductionSessionHandler = dataProductionSessionHandler;
        this.tableInsert = tableInsert;
        this.mapper = mapper;
        this.metaDataManager = metaDataManager;
    }
 
    @Override
    public void processProduction() {
        System.out.println("-------------------------before FileInsertionProcess-------------------");
        CommonDataProduction cd = mapper.dataProductionToCommonDataProduction(dataProduction);
        cd.setId(dataProduction.getId());
 
        try {
            File dataFile = dataFile(dataProduction);
 
            if (dataFile.exists()) {
                Charset charset = CommonUtilities.getCharset(dataFile);
 
                try ( Connection connection = new DBConnection(dataSource).connection()) {
                    connection.setAutoCommit(false);
                    new FileInsertionProcess(columnDefinitionManager,
                            cd,
                            connection,
                            dataFile,
                            charset,
                            tableInsert,
                            metaDataManager
                    ).process();
                    connection.commit();
                }
                System.out.println("-------------------after FileInsertionProcess---------------------");
                dataProductionSessionHandler.publishMessage(dataProduction);
                dataFile.delete();
                metaDataManager.deleteById(cd.getId());
            }
        } catch (Exception ex) {
            Logger.getLogger(FileDataProductionTask.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private File dataFile(CommonDataProduction dataProduction) throws IOException {
        CamelRouteConfiguration camelRouteConfiguration = new CamelRouteConfiguration(AppContext.PROPERTY_FILE_PATH);
        String dataDir = camelRouteConfiguration.getLocalConfiguration().consultationDestinationDir();
 
        return new File(dataDir, dataProduction.getIntegrationFileName());
    }
}