001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
025    import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
026    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
027    import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
028    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
029    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
030    import com.liferay.portlet.trash.util.TrashUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Levente Hud??k
045     */
046    public class EditFileShortcutAction extends PortletAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            PortletConfig portletConfig, ActionRequest actionRequest,
052                            ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    updateFileShortcut(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.DELETE)) {
062                                    deleteFileShortcut(actionRequest, false);
063                            }
064                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
065                                    deleteFileShortcut(actionRequest, true);
066                            }
067    
068                            sendRedirect(actionRequest, actionResponse);
069                    }
070                    catch (Exception e) {
071                            if (e instanceof NoSuchFileShortcutException ||
072                                    e instanceof PrincipalException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass());
075    
076                                    setForward(actionRequest, "portlet.document_library.error");
077                            }
078                            else if (e instanceof FileShortcutPermissionException ||
079                                             e instanceof NoSuchFileEntryException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass());
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping actionMapping, ActionForm actionForm,
092                            PortletConfig portletConfig, RenderRequest renderRequest,
093                            RenderResponse renderResponse)
094                    throws Exception {
095    
096                    try {
097                            ActionUtil.getFileShortcut(renderRequest);
098                    }
099                    catch (Exception e) {
100                            if (e instanceof NoSuchFileShortcutException ||
101                                    e instanceof PrincipalException) {
102    
103                                    SessionErrors.add(renderRequest, e.getClass());
104    
105                                    return actionMapping.findForward(
106                                            "portlet.document_library.error");
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112    
113                    return actionMapping.findForward(
114                            getForward(
115                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
116            }
117    
118            protected void deleteFileShortcut(
119                            ActionRequest actionRequest, boolean moveToTrash)
120                    throws Exception {
121    
122                    long fileShortcutId = ParamUtil.getLong(
123                            actionRequest, "fileShortcutId");
124    
125                    if (moveToTrash) {
126                            DLFileShortcut fileShortcut =
127                                    DLAppServiceUtil.moveFileShortcutToTrash(fileShortcutId);
128    
129                            TrashUtil.addTrashSessionMessages(actionRequest, fileShortcut);
130    
131                            hideDefaultSuccessMessage(actionRequest);
132                    }
133                    else {
134                            DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
135                    }
136            }
137    
138            protected void updateFileShortcut(ActionRequest actionRequest)
139                    throws Exception {
140    
141                    long fileShortcutId = ParamUtil.getLong(
142                            actionRequest, "fileShortcutId");
143    
144                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
145                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
146                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
147    
148                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
149                            DLFileShortcut.class.getName(), actionRequest);
150    
151                    if (fileShortcutId <= 0) {
152    
153                            // Add file shortcut
154    
155                            DLFileShortcut fileShortcut = DLAppServiceUtil.addFileShortcut(
156                                    repositoryId, folderId, toFileEntryId, serviceContext);
157    
158                            AssetPublisherUtil.addAndStoreSelection(
159                                    actionRequest, DLFileShortcut.class.getName(),
160                                    fileShortcut.getFileShortcutId(), -1);
161                    }
162                    else {
163    
164                            // Update file shortcut
165    
166                            DLAppServiceUtil.updateFileShortcut(
167                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
168    
169                            AssetPublisherUtil.addRecentFolderId(
170                                    actionRequest, DLFileShortcut.class.getName(), folderId);
171                    }
172            }
173    
174    }