com.liferay.message.boards.web.internal.portlet.action.EditMessageAttachmentsMVCActionCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.message.boards.web.internal.portlet.action.EditMessageAttachmentsMVCActionCommand.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.message.boards.web.internal.portlet.action;

import com.liferay.message.boards.kernel.service.MBMessageLocalService;
import com.liferay.message.boards.kernel.service.MBMessageService;
import com.liferay.message.boards.web.constants.MBPortletKeys;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.taglib.util.RestoreEntryUtil;
import com.liferay.trash.kernel.service.TrashEntryService;
import com.liferay.trash.kernel.util.TrashUtil;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

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

/**
 * @author Eudaldo Alonso
 */
@Component(property = { "javax.portlet.name=" + MBPortletKeys.MESSAGE_BOARDS,
        "javax.portlet.name=" + MBPortletKeys.MESSAGE_BOARDS_ADMIN,
        "mvc.command.name=/message_boards/edit_message_attachments" }, service = MVCActionCommand.class)
public class EditMessageAttachmentsMVCActionCommand extends BaseMVCActionCommand {

    protected void deleteAttachment(ActionRequest actionRequest) throws PortalException {

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

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

        _mbMessageLocalService.deleteMessageAttachment(messageId, fileName);
    }

    @Override
    protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        try {
            if (cmd.equals(Constants.CHECK)) {
                JSONObject jsonObject = RestoreEntryUtil.checkEntry(actionRequest);

                JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);

                return;
            } else if (cmd.equals(Constants.DELETE)) {
                deleteAttachment(actionRequest);
            } else if (cmd.equals(Constants.EMPTY_TRASH)) {
                emptyTrash(actionRequest);
            } else if (cmd.equals(Constants.RENAME)) {
                restoreRename(actionRequest);
            } else if (cmd.equals(Constants.RESTORE)) {
                restoreEntries(actionRequest);
            } else if (cmd.equals(Constants.OVERRIDE)) {
                restoreOverride(actionRequest);
            }

            if (Validator.isNotNull(cmd)) {
                String redirect = ParamUtil.getString(actionRequest, "redirect");

                sendRedirect(actionRequest, actionResponse, redirect);
            }
        } catch (PrincipalException pe) {
            SessionErrors.add(actionRequest, pe.getClass());

            actionResponse.setRenderParameter("mvcPath", "/message_boards/error.jsp");
        }
    }

    protected void emptyTrash(ActionRequest actionRequest) throws Exception {
        long messageId = ParamUtil.getLong(actionRequest, "messageId");

        _mbMessageService.emptyMessageAttachments(messageId);
    }

    protected 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);
        }
    }

    protected void restoreOverride(ActionRequest actionRequest) throws Exception {

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

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

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

    protected 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 setMBMessageLocalService(MBMessageLocalService mbMessageLocalService) {

        _mbMessageLocalService = mbMessageLocalService;
    }

    @Reference(unbind = "-")
    protected void setMBMessageService(MBMessageService mbMessageService) {
        _mbMessageService = mbMessageService;
    }

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

    private MBMessageLocalService _mbMessageLocalService;
    private MBMessageService _mbMessageService;
    private TrashEntryService _trashEntryService;

}