org.pdfgal.pdfgalweb.services.impl.ReindexServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.pdfgal.pdfgalweb.services.impl.ReindexServiceImpl.java

Source

/*
 * PDFGalWeb
 * Copyright (c) 2014, Alejandro Pernas Pan, All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
    
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

package org.pdfgal.pdfgalweb.services.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.pdfgal.pdfgal.model.PDFGalPageNumbering;
import org.pdfgal.pdfgal.model.enumerated.NumberingStyle;
import org.pdfgal.pdfgal.model.vo.PDFGalPageNumberingVO;
import org.pdfgal.pdfgal.pdfgal.PDFGal;
import org.pdfgal.pdfgalweb.forms.DownloadForm;
import org.pdfgal.pdfgalweb.services.ReindexService;
import org.pdfgal.pdfgalweb.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ReindexServiceImpl implements ReindexService {

    @Autowired
    private FileUtils fileUtils;

    @Autowired
    private PDFGal pdfGal;

    @Override
    public DownloadForm reindex(final MultipartFile file, final List<Integer> pagesList,
            final List<NumberingStyle> numberingStylesList, final HttpServletResponse response) throws Exception {

        DownloadForm result = new DownloadForm();

        if (file != null && CollectionUtils.isNotEmpty(pagesList) && CollectionUtils.isNotEmpty(numberingStylesList)
                && response != null) {

            final String originalName = file.getOriginalFilename();
            final String inputUri = this.fileUtils.saveFile(file);
            final String outputUri = this.fileUtils.getAutogeneratedName(originalName);

            // File is reindexed
            try {
                final List<PDFGalPageNumbering> pdfGalPageNumberingList = this
                        .createPdfGalPageNumberingList(pagesList, numberingStylesList);
                this.pdfGal.reIndexPageNumbers(inputUri, outputUri, pdfGalPageNumberingList);
            } catch (COSVisitorException | IOException e) {
                // Temporal files are deleted from system
                this.fileUtils.delete(inputUri);
                this.fileUtils.delete(outputUri);
                throw e;
            }

            // Temporal files are deleted from system
            this.fileUtils.delete(inputUri);

            result = new DownloadForm(outputUri, originalName);
        }

        return result;
    }

    private List<PDFGalPageNumbering> createPdfGalPageNumberingList(final List<Integer> pagesList,
            final List<NumberingStyle> numberingStylesList) {

        final List<PDFGalPageNumbering> result = new ArrayList<PDFGalPageNumbering>();

        if (!(CollectionUtils.isEmpty(pagesList) && CollectionUtils.isEmpty(numberingStylesList))) {

            if (CollectionUtils.isEmpty(pagesList) || CollectionUtils.isEmpty(numberingStylesList)
                    || (pagesList.size() != numberingStylesList.size())) {
                throw new IllegalArgumentException("Pages and numbering styles lists' size are different.");

            } else {
                for (int i = 0; i < pagesList.size(); i++) {
                    final Integer page = pagesList.get(i);
                    final NumberingStyle numberingStyle = numberingStylesList.get(i);
                    final PDFGalPageNumbering pdfGalPageNumbering = new PDFGalPageNumberingVO(page, numberingStyle);
                    result.add(pdfGalPageNumbering);
                }
            }
        }

        return result;
    }

}