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.repository.model.FileShortcut;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.model.TrashedModel;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
027    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
028    import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
029    import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
030    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
031    import com.liferay.portlet.trash.util.TrashUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Levente Hud??k
046     */
047    public class EditFileShortcutAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateFileShortcut(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteFileShortcut(actionRequest, false);
064                            }
065                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
066                                    deleteFileShortcut(actionRequest, true);
067                            }
068    
069                            sendRedirect(actionRequest, actionResponse);
070                    }
071                    catch (Exception e) {
072                            if (e instanceof NoSuchFileShortcutException ||
073                                    e instanceof PrincipalException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass());
076    
077                                    setForward(actionRequest, "portlet.document_library.error");
078                            }
079                            else if (e instanceof FileShortcutPermissionException ||
080                                             e instanceof NoSuchFileEntryException) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass());
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, RenderRequest renderRequest,
094                            RenderResponse renderResponse)
095                    throws Exception {
096    
097                    try {
098                            ActionUtil.getFileShortcut(renderRequest);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchFileShortcutException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(renderRequest, e.getClass());
105    
106                                    return actionMapping.findForward(
107                                            "portlet.document_library.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return actionMapping.findForward(
115                            getForward(
116                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
117            }
118    
119            protected void deleteFileShortcut(
120                            ActionRequest actionRequest, boolean moveToTrash)
121                    throws Exception {
122    
123                    long fileShortcutId = ParamUtil.getLong(
124                            actionRequest, "fileShortcutId");
125    
126                    if (moveToTrash) {
127                            FileShortcut fileShortcut =
128                                    DLAppServiceUtil.moveFileShortcutToTrash(fileShortcutId);
129    
130                            if (fileShortcut.getModel() instanceof TrashedModel) {
131                                    TrashUtil.addTrashSessionMessages(
132                                            actionRequest, (TrashedModel)fileShortcut.getModel());
133                            }
134    
135                            hideDefaultSuccessMessage(actionRequest);
136                    }
137                    else {
138                            DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
139                    }
140            }
141    
142            protected void updateFileShortcut(ActionRequest actionRequest)
143                    throws Exception {
144    
145                    long fileShortcutId = ParamUtil.getLong(
146                            actionRequest, "fileShortcutId");
147    
148                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
149                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
150                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
151    
152                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
153                            DLFileShortcutConstants.getClassName(), actionRequest);
154    
155                    if (fileShortcutId <= 0) {
156    
157                            // Add file shortcut
158    
159                            DLAppServiceUtil.addFileShortcut(
160                                    repositoryId, folderId, toFileEntryId, serviceContext);
161                    }
162                    else {
163    
164                            // Update file shortcut
165    
166                            DLAppServiceUtil.updateFileShortcut(
167                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
168                    }
169            }
170    
171    }