com.liferay.wiki.web.internal.WikiAttachmentsHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.wiki.web.internal.WikiAttachmentsHelper.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. 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 2.1 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.
 */

package com.liferay.wiki.web.internal;

import com.liferay.document.library.kernel.model.DLFileEntry;
import com.liferay.portal.kernel.model.TrashedModel;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.ObjectValuePair;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.StreamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.trash.kernel.service.TrashEntryService;
import com.liferay.trash.kernel.util.TrashUtil;
import com.liferay.wiki.service.WikiPageService;

import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Roberto Daz
 */
@Component(service = WikiAttachmentsHelper.class)
public class WikiAttachmentsHelper {

    public void addAttachments(ActionRequest actionRequest) throws Exception {
        UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

        long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
        String title = ParamUtil.getString(actionRequest, "title");

        int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");

        List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<>();

        try {
            if (numOfFiles == 0) {
                String fileName = uploadPortletRequest.getFileName("file");
                InputStream inputStream = uploadPortletRequest.getFileAsStream("file");

                if (inputStream != null) {
                    ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName,
                            inputStream);

                    inputStreamOVPs.add(inputStreamOVP);
                }
            } else {
                for (int i = 1; i <= numOfFiles; i++) {
                    String fileName = uploadPortletRequest.getFileName("file" + i);
                    InputStream inputStream = uploadPortletRequest.getFileAsStream("file" + i);

                    if (inputStream == null) {
                        continue;
                    }

                    ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName,
                            inputStream);

                    inputStreamOVPs.add(inputStreamOVP);
                }
            }

            _wikiPageService.addPageAttachments(nodeId, title, inputStreamOVPs);
        } finally {
            for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

                InputStream inputStream = inputStreamOVP.getValue();

                StreamUtil.cleanUp(inputStream);
            }
        }
    }

    public TrashedModel deleteAttachment(ActionRequest actionRequest, boolean moveToTrash) throws Exception {

        long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
        String title = ParamUtil.getString(actionRequest, "title");
        String attachment = ParamUtil.getString(actionRequest, "fileName");

        TrashedModel trashedModel = null;

        if (moveToTrash) {
            FileEntry fileEntry = _wikiPageService.movePageAttachmentToTrash(nodeId, title, attachment);

            if (fileEntry.getModel() instanceof DLFileEntry) {
                trashedModel = (DLFileEntry) fileEntry.getModel();
            }
        } else {
            _wikiPageService.deletePageAttachment(nodeId, title, attachment);
        }

        return trashedModel;
    }

    public void emptyTrash(ActionRequest actionRequest) throws Exception {
        long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
        String title = ParamUtil.getString(actionRequest, "title");

        _wikiPageService.deleteTrashPageAttachments(nodeId, title);
    }

    public void restoreEntries(ActionRequest actionRequest) throws Exception {
        long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");

        if (trashEntryId > 0) {
            _trashEntryService.restoreEntry(trashEntryId);

            return;
        }

        long[] restoreEntryIds = StringUtil.split(ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);

        for (long restoreEntryId : restoreEntryIds) {
            _trashEntryService.restoreEntry(restoreEntryId);
        }
    }

    public void restoreOverride(ActionRequest actionRequest) throws Exception {
        long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");

        long duplicateEntryId = ParamUtil.getLong(actionRequest, "duplicateEntryId");

        _trashEntryService.restoreEntry(trashEntryId, duplicateEntryId, null);
    }

    public void restoreRename(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");

        String newName = ParamUtil.getString(actionRequest, "newName");

        if (Validator.isNull(newName)) {
            String oldName = ParamUtil.getString(actionRequest, "oldName");

            newName = TrashUtil.getNewName(themeDisplay, null, 0, oldName);
        }

        _trashEntryService.restoreEntry(trashEntryId, 0, newName);
    }

    @Reference(unbind = "-")
    protected void setTrashEntryService(TrashEntryService trashEntryService) {
        _trashEntryService = trashEntryService;
    }

    @Reference(unbind = "-")
    protected void setWikiPageService(WikiPageService wikiPageService) {
        _wikiPageService = wikiPageService;
    }

    private TrashEntryService _trashEntryService;
    private WikiPageService _wikiPageService;

}