001    /**
002     * Copyright (c) 2000-2013 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.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
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.assetpublisher.util.AssetPublisherUtil;
027    import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
028    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
029    import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
030    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Levente Hudák
049     */
050    public class EditFileShortcutAction extends PortletAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062                                    updateFileShortcut(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.DELETE)) {
065                                    deleteFileShortcut(
066                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
067                            }
068                            else if (cmd.equals(Constants.MOVE)) {
069                                    moveFileShortcut(actionRequest, false);
070                            }
071                            else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
072                                    moveFileShortcut(actionRequest, true);
073                            }
074                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
075                                    deleteFileShortcut(
076                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
077                            }
078    
079                            sendRedirect(actionRequest, actionResponse);
080                    }
081                    catch (Exception e) {
082                            if (e instanceof NoSuchFileShortcutException ||
083                                    e instanceof PrincipalException) {
084    
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    setForward(actionRequest, "portlet.document_library.error");
088                            }
089                            else if (e instanceof FileShortcutPermissionException ||
090                                             e instanceof NoSuchFileEntryException) {
091    
092                                    SessionErrors.add(actionRequest, e.getClass());
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098            }
099    
100            @Override
101            public ActionForward render(
102                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getFileShortcut(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchFileShortcutException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass());
114    
115                                    return mapping.findForward("portlet.document_library.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    return mapping.findForward(
123                            getForward(
124                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
125            }
126    
127            protected void deleteFileShortcut(
128                            LiferayPortletConfig liferayPortletConfig,
129                            ActionRequest actionRequest, boolean moveToTrash)
130                    throws Exception {
131    
132                    long fileShortcutId = ParamUtil.getLong(
133                            actionRequest, "fileShortcutId");
134    
135                    if (moveToTrash) {
136                            DLAppServiceUtil.moveFileShortcutToTrash(fileShortcutId);
137    
138                            Map<String, String[]> data = new HashMap<String, String[]>();
139    
140                            data.put(
141                                    "restoreFileShortcutIds",
142                                    new String[] {String.valueOf(fileShortcutId)});
143    
144                            SessionMessages.add(
145                                    actionRequest,
146                                    liferayPortletConfig.getPortletId() +
147                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
148    
149                            SessionMessages.add(
150                                    actionRequest,
151                                    liferayPortletConfig.getPortletId() +
152                                            SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
153                    }
154                    else {
155                            DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
156                    }
157            }
158    
159            protected void moveFileShortcut(
160                            ActionRequest actionRequest, boolean moveFromTrash)
161                    throws Exception {
162    
163                    long fileShortcutId = ParamUtil.getLong(
164                            actionRequest, "fileShortcutId");
165    
166                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
167    
168                    DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
169                            fileShortcutId);
170    
171                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
172                            DLFileShortcut.class.getName(), actionRequest);
173    
174                    if (moveFromTrash) {
175                            DLAppServiceUtil.moveFileShortcutFromTrash(
176                                    fileShortcutId, newFolderId, serviceContext);
177                    }
178                    else {
179                            DLAppServiceUtil.updateFileShortcut(
180                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
181                                    serviceContext);
182                    }
183            }
184    
185            protected void updateFileShortcut(ActionRequest actionRequest)
186                    throws Exception {
187    
188                    long fileShortcutId = ParamUtil.getLong(
189                            actionRequest, "fileShortcutId");
190    
191                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
192                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
193                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
194    
195                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
196                            DLFileShortcut.class.getName(), actionRequest);
197    
198                    if (fileShortcutId <= 0) {
199    
200                            // Add file shortcut
201    
202                            DLFileShortcut fileShortcut = DLAppServiceUtil.addFileShortcut(
203                                    repositoryId, folderId, toFileEntryId, serviceContext);
204    
205                            AssetPublisherUtil.addAndStoreSelection(
206                                    actionRequest, DLFileShortcut.class.getName(),
207                                    fileShortcut.getFileShortcutId(), -1);
208                    }
209                    else {
210    
211                            // Update file shortcut
212    
213                            DLAppServiceUtil.updateFileShortcut(
214                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
215    
216                            AssetPublisherUtil.addRecentFolderId(
217                                    actionRequest, DLFileShortcut.class.getName(), folderId);
218                    }
219            }
220    
221    }