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

Java tutorial

Introduction

Here is the source code for org.pdfgal.pdfgalweb.services.impl.BookmarkServiceImpl.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.commons.lang3.StringUtils;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.pdfgal.pdfgal.model.PDFGalBookmark;
import org.pdfgal.pdfgal.model.vo.PDFGalBookmarkVO;
import org.pdfgal.pdfgal.pdfgal.PDFGal;
import org.pdfgal.pdfgalweb.forms.DownloadForm;
import org.pdfgal.pdfgalweb.services.BookmarkService;
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 BookmarkServiceImpl implements BookmarkService {

    @Autowired
    private FileUtils fileUtils;

    @Autowired
    private PDFGal pdfGal;

    @Override
    public DownloadForm addBookmarks(final MultipartFile file, final String title, final List<Integer> pagesList,
            final List<String> textsList, final HttpServletResponse response) throws Exception {

        DownloadForm result = new DownloadForm();

        if (file != null && StringUtils.isNotBlank(title) && CollectionUtils.isNotEmpty(pagesList)
                && CollectionUtils.isNotEmpty(textsList) && response != null) {

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

            // File is bookmarked
            try {
                final List<PDFGalBookmark> pdfGalBookmarksList = createPDFGalBookmarksList(pagesList, textsList);
                this.pdfGal.addBookmarks(inputUri, outputUri, title, pdfGalBookmarksList);
            } 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;
    }

    /**
     * Creates a {@link PDFGalBookmark} list from a list of pages and texts
     * @param pagesList
     * @param textsList
     * @return
     */
    private List<PDFGalBookmark> createPDFGalBookmarksList(final List<Integer> pagesList,
            final List<String> textsList) {

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

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

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

            } else {
                for (int i = 0; i < pagesList.size(); i++) {
                    final Integer page = pagesList.get(i);
                    final String text = textsList.get(i);
                    final PDFGalBookmark bookmark = new PDFGalBookmarkVO(page, text);
                    result.add(bookmark);
                }
            }
        }

        return result;
    }
}