nc.noumea.mairie.appock.util.StockSpreadsheetImporter.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.util.StockSpreadsheetImporter.java

Source

package nc.noumea.mairie.appock.util;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de
 *         Nouma
 * %%
 * Copyright (C) 2017 - 2018 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import nc.noumea.mairie.appock.entity.ArticleStock;
import nc.noumea.mairie.appock.entity.Service;
import nc.noumea.mairie.appock.entity.Stock;
import nc.noumea.mairie.appock.enums.TypeMouvementStock;
import nc.noumea.mairie.appock.exception.ImportExcelException;
import nc.noumea.mairie.appock.services.StockService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class StockSpreadsheetImporter {

    private static Logger log = LoggerFactory.getLogger(StockSpreadsheetImporter.class);

    private static String NOM_ONGLET_CLASSEUR = "Inventaire";

    public static List<String> importFromXls(Service service, StockService stockService, InputStream in)
            throws Exception {

        List<String> warnings = new ArrayList<>();
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(in);
            XSSFSheet worksheet = workbook.getSheet(NOM_ONGLET_CLASSEUR);

            if (worksheet == null) {
                throw new Exception("L'onglet '" + NOM_ONGLET_CLASSEUR + "' du classeur est introuvable");
            }

            for (int i = 1; i < worksheet.getLastRowNum() + 1; i++) {
                try {
                    traiterLigne(i, worksheet, service, stockService);
                } catch (ImportExcelException e) {
                    warnings.add(e.getMessage());
                }
            }
        } finally {
            in.close();
        }
        return warnings;
    }

    private static void traiterLigne(int ligne, XSSFSheet worksheet, Service service, StockService stockService)
            throws ImportExcelException {
        XSSFCell referenceCell = worksheet.getRow(ligne).getCell(1);
        if (StringUtils.isBlank(referenceCell.getRawValue())) {
            throw new ImportExcelException(ligne + 1, "La rfrence est introuvable");
        }
        String reference = referenceCell.getStringCellValue();

        XSSFCell stockReelCell = worksheet.getRow(ligne).getCell(4);
        if (StringUtils.isBlank(stockReelCell.getRawValue())) {
            throw new ImportExcelException(ligne + 1, reference, "La quantit relle n'est pas renseigne");
        }

        if (stockReelCell.getCellTypeEnum() != CellType.NUMERIC) {
            throw new ImportExcelException(ligne + 1, reference, "La cellule 'Stock rl' n'est pas un entier");
        }
        double value = stockReelCell.getNumericCellValue();
        int stockReel = (int) value;
        if (value != stockReel) {
            throw new ImportExcelException(ligne + 1, reference, "La cellule 'Stock rl' n'est pas un entier");
        }
        updateStock(service, reference, stockReel, stockService, ligne);
    }

    private static void updateStock(Service service, String reference, int stockReel, StockService stockService,
            int ligne) throws ImportExcelException {
        Stock stock = stockService.findOne(service.getStock().getId());
        Optional<ArticleStock> articleStockOptional = stock.getListeArticleStock().stream() //
                .filter(articleStock -> articleStock.getReferenceArticleStock().equals(reference)) //
                .findFirst();
        if (articleStockOptional.isPresent()) {
            int diffQuantite = stockReel - articleStockOptional.get().getQuantiteStock();
            if (diffQuantite == 0) {
                return;
            }
            TypeMouvementStock typeMouvementStock = diffQuantite < 0 ? TypeMouvementStock.SORTIE
                    : TypeMouvementStock.ENTREE;
            stockService.creeEntreeSortieEtMouvement(articleStockOptional.get(), Math.abs(diffQuantite),
                    typeMouvementStock, "Import inventaire");
            return;
        } else {
            throw new ImportExcelException(ligne + 1, reference,
                    "Aucun article n'a t trouv dans le stock pour cette rfrence");
        }
    }

}