Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
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
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.fdxconvert.service;
 
import com.megatimfx.common.service.GenericCrudService;
import com.megatim.fdxconvert.dao.AbstractDAO;
import com.megatim.fdxconvert.dao.TypeFichierDAO;
import com.megatim.fdxconvert.pojo.ImportFile;
import com.megatim.fdxconvert.model.TypeFichier;
import com.megatim.fdxconvert.pojo.DataToImport;
import com.megatim.fdxconvert.util.ImportData;
import com.megatim.fdxconvert.service.pojo.PaginationElts;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
/**
 * structLigne : id, code,format_date, position, taille, type_donnee,
 * validateur_fichier_id ValidateurFichier : id, nombre_position,
 * type_fichier_code
 *
 * @author STEPHANIE
 */
public class ImportFileService implements GenericCrudService<ImportFile> {
 
    private static ImportFileService importFileService;
    private PaginationElts<TypeFichier> pagination;
 
    private ImportFileService() {
        
    }
    
    public static synchronized ImportFileService getInstance() {
        if (importFileService == null) {
            importFileService = new ImportFileService();
        }
        return importFileService;
    }
 
    @Override
    public ImportFile add(ImportFile t) throws Exception {
        importDataFromFile(t);
        return t;
    }
 
    @Override
    public ImportFile edit(ImportFile t) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    @Override
    public List<ImportFile> getAll() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    @Override
    public void delete(ImportFile t) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
 
    private void importDataFromFile(ImportFile im) throws Exception{
        List<String> fields = new ArrayList<>();
 
        if (im != null && im.getType() != null) {
            if (im.getType().equals(TypeFichier.class)) {
                fields.add("code");
                fields.add("libelle");
                fields.add("codeParticipant");
            }
        }
        DataToImport datIm = new DataToImport(new File(im.getFilePath()), im.getDelimiteurLigne().getCode(), im.getDelimiteurColonne().getCode(), im.getType(), fields);
        List<Object> datas = ImportData.importDataFromCsvFile(datIm);
        if (im.getType().equals(TypeFichier.class)) {
            AbstractDAO<TypeFichier> dao = new AbstractDAO<>(TypeFichier.class);
            
            datas.forEach(d -> {
                TypeFichier t = (TypeFichier) d;
                Optional<TypeFichier> opt = TypeFichierDAO.findByCode(t);
                if(!opt.isPresent())
                     dao.add(t);
            });
        }
    }
}
//{}