/* * 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 { private static ImportFileService importFileService; private PaginationElts 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 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 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 datas = ImportData.importDataFromCsvFile(datIm); if (im.getType().equals(TypeFichier.class)) { AbstractDAO dao = new AbstractDAO<>(TypeFichier.class); datas.forEach(d -> { TypeFichier t = (TypeFichier) d; Optional opt = TypeFichierDAO.findByCode(t); if(!opt.isPresent()) dao.add(t); }); } } } //{}