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 actionMapping, ActionForm actionForm,
055                            PortletConfig portletConfig, ActionRequest actionRequest,
056                            ActionResponse actionResponse)
057                    throws Exception {
058    
059                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
060    
061                    try {
062                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
063                                    updateFileShortcut(actionRequest);
064                            }
065                            else if (cmd.equals(Constants.DELETE)) {
066                                    deleteFileShortcut(
067                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
068                            }
069                            else if (cmd.equals(Constants.MOVE)) {
070                                    moveFileShortcut(actionRequest, false);
071                            }
072                            else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
073                                    moveFileShortcut(actionRequest, true);
074                            }
075                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
076                                    deleteFileShortcut(
077                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
078                            }
079    
080                            sendRedirect(actionRequest, actionResponse);
081                    }
082                    catch (Exception e) {
083                            if (e instanceof NoSuchFileShortcutException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass());
087    
088                                    setForward(actionRequest, "portlet.document_library.error");
089                            }
090                            else if (e instanceof FileShortcutPermissionException ||
091                                             e instanceof NoSuchFileEntryException) {
092    
093                                    SessionErrors.add(actionRequest, e.getClass());
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099            }
100    
101            @Override
102            public ActionForward render(
103                            ActionMapping actionMapping, ActionForm actionForm,
104                            PortletConfig portletConfig, RenderRequest renderRequest,
105                            RenderResponse renderResponse)
106                    throws Exception {
107    
108                    try {
109                            ActionUtil.getFileShortcut(renderRequest);
110                    }
111                    catch (Exception e) {
112                            if (e instanceof NoSuchFileShortcutException ||
113                                    e instanceof PrincipalException) {
114    
115                                    SessionErrors.add(renderRequest, e.getClass());
116    
117                                    return actionMapping.findForward(
118                                            "portlet.document_library.error");
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124    
125                    return actionMapping.findForward(
126                            getForward(
127                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
128            }
129    
130            protected void deleteFileShortcut(
131                            LiferayPortletConfig liferayPortletConfig,
132                            ActionRequest actionRequest, boolean moveToTrash)
133                    throws Exception {
134    
135                    long fileShortcutId = ParamUtil.getLong(
136                            actionRequest, "fileShortcutId");
137    
138                    if (moveToTrash) {
139                            DLFileShortcut fileShortcut =
140                                    DLAppServiceUtil.moveFileShortcutToTrash(fileShortcutId);
141    
142                            Map<String, String[]> data = new HashMap<String, String[]>();
143    
144                            data.put(
145                                    "deleteEntryClassName",
146                                    new String[] {DLFileShortcut.class.getName()});
147    
148                            if (fileShortcut != null) {
149                                    data.put(
150                                            "deleteEntryTitle",
151                                            new String[] {fileShortcut.getToTitle()});
152                            }
153    
154                            data.put(
155                                    "restoreFileShortcutIds",
156                                    new String[] {String.valueOf(fileShortcutId)});
157    
158                            SessionMessages.add(
159                                    actionRequest,
160                                    liferayPortletConfig.getPortletId() +
161                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
162    
163                            hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
164                    }
165                    else {
166                            DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
167                    }
168            }
169    
170            protected void moveFileShortcut(
171                            ActionRequest actionRequest, boolean moveFromTrash)
172                    throws Exception {
173    
174                    long fileShortcutId = ParamUtil.getLong(
175                            actionRequest, "fileShortcutId");
176    
177                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
178    
179                    DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
180                            fileShortcutId);
181    
182                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
183                            DLFileShortcut.class.getName(), actionRequest);
184    
185                    if (moveFromTrash) {
186                            DLAppServiceUtil.moveFileShortcutFromTrash(
187                                    fileShortcutId, newFolderId, serviceContext);
188                    }
189                    else {
190                            DLAppServiceUtil.updateFileShortcut(
191                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
192                                    serviceContext);
193                    }
194            }
195    
196            protected void updateFileShortcut(ActionRequest actionRequest)
197                    throws Exception {
198    
199                    long fileShortcutId = ParamUtil.getLong(
200                            actionRequest, "fileShortcutId");
201    
202                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
203                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
204                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
205    
206                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
207                            DLFileShortcut.class.getName(), actionRequest);
208    
209                    if (fileShortcutId <= 0) {
210    
211                            // Add file shortcut
212    
213                            DLFileShortcut fileShortcut = DLAppServiceUtil.addFileShortcut(
214                                    repositoryId, folderId, toFileEntryId, serviceContext);
215    
216                            AssetPublisherUtil.addAndStoreSelection(
217                                    actionRequest, DLFileShortcut.class.getName(),
218                                    fileShortcut.getFileShortcutId(), -1);
219                    }
220                    else {
221    
222                            // Update file shortcut
223    
224                            DLAppServiceUtil.updateFileShortcut(
225                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
226    
227                            AssetPublisherUtil.addRecentFolderId(
228                                    actionRequest, DLFileShortcut.class.getName(), folderId);
229                    }
230            }
231    
232    }