com.liferay.document.library.web.internal.portlet.action.EditFileShortcutMVCActionCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.document.library.web.internal.portlet.action.EditFileShortcutMVCActionCommand.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.document.library.web.internal.portlet.action;

import com.liferay.document.library.kernel.exception.FileShortcutPermissionException;
import com.liferay.document.library.kernel.exception.NoSuchFileEntryException;
import com.liferay.document.library.kernel.exception.NoSuchFileShortcutException;
import com.liferay.document.library.kernel.model.DLFileShortcutConstants;
import com.liferay.document.library.kernel.service.DLAppService;
import com.liferay.document.library.kernel.service.DLTrashService;
import com.liferay.document.library.web.constants.DLPortletKeys;
import com.liferay.portal.kernel.model.TrashedModel;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.repository.model.FileShortcut;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextFactory;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
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 Brian Wing Shun Chan
 * @author Levente Hudk
 */
@Component(property = { "javax.portlet.name=" + DLPortletKeys.DOCUMENT_LIBRARY,
        "javax.portlet.name=" + DLPortletKeys.DOCUMENT_LIBRARY_ADMIN,
        "javax.portlet.name=" + DLPortletKeys.MEDIA_GALLERY_DISPLAY,
        "mvc.command.name=/document_library/edit_file_shortcut" }, service = MVCActionCommand.class)
public class EditFileShortcutMVCActionCommand extends BaseMVCActionCommand {

    protected void deleteFileShortcut(ActionRequest actionRequest, boolean moveToTrash) throws Exception {

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

        if (moveToTrash) {
            FileShortcut fileShortcut = _dlTrashService.moveFileShortcutToTrash(fileShortcutId);

            if (fileShortcut.getModel() instanceof TrashedModel) {
                TrashUtil.addTrashSessionMessages(actionRequest, (TrashedModel) fileShortcut.getModel());
            }

            hideDefaultSuccessMessage(actionRequest);
        } else {
            _dlAppService.deleteFileShortcut(fileShortcutId);
        }
    }

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

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

        try {
            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
                updateFileShortcut(actionRequest);
            } else if (cmd.equals(Constants.DELETE)) {
                deleteFileShortcut(actionRequest, false);
            } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
                deleteFileShortcut(actionRequest, true);
            }
        } catch (Exception e) {
            if (e instanceof NoSuchFileShortcutException || e instanceof PrincipalException) {

                SessionErrors.add(actionRequest, e.getClass());

                actionResponse.setRenderParameter("mvcPath", "/document_library/error.jsp");
            } else if (e instanceof FileShortcutPermissionException || e instanceof NoSuchFileEntryException) {

                SessionErrors.add(actionRequest, e.getClass());
            } else {
                throw e;
            }
        }
    }

    @Reference(unbind = "-")
    protected void setDLAppService(DLAppService dlAppService) {
        _dlAppService = dlAppService;
    }

    @Reference(unbind = "-")
    protected void setDLTrashService(DLTrashService dlTrashService) {
        _dlTrashService = dlTrashService;
    }

    protected void updateFileShortcut(ActionRequest actionRequest) throws Exception {

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

        long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
        long folderId = ParamUtil.getLong(actionRequest, "folderId");
        long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileShortcutConstants.getClassName(),
                actionRequest);

        if (fileShortcutId <= 0) {

            // Add file shortcut

            _dlAppService.addFileShortcut(repositoryId, folderId, toFileEntryId, serviceContext);
        } else {

            // Update file shortcut

            _dlAppService.updateFileShortcut(fileShortcutId, folderId, toFileEntryId, serviceContext);
        }
    }

    private DLAppService _dlAppService;
    private DLTrashService _dlTrashService;

}