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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.util.PortalUtil;
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(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(actionRequest, true);
076                            }
077    
078                            sendRedirect(actionRequest, actionResponse);
079                    }
080                    catch (Exception e) {
081                            if (e instanceof NoSuchFileShortcutException ||
082                                    e instanceof PrincipalException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass());
085    
086                                    setForward(actionRequest, "portlet.document_library.error");
087                            }
088                            else if (e instanceof FileShortcutPermissionException ||
089                                             e instanceof NoSuchFileEntryException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097            }
098    
099            @Override
100            public ActionForward render(
101                            ActionMapping actionMapping, ActionForm actionForm,
102                            PortletConfig portletConfig, RenderRequest renderRequest,
103                            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 actionMapping.findForward(
116                                            "portlet.document_library.error");
117                            }
118                            else {
119                                    throw e;
120                            }
121                    }
122    
123                    return actionMapping.findForward(
124                            getForward(
125                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
126            }
127    
128            protected void deleteFileShortcut(
129                            ActionRequest actionRequest, boolean moveToTrash)
130                    throws Exception {
131    
132                    long fileShortcutId = ParamUtil.getLong(
133                            actionRequest, "fileShortcutId");
134    
135                    if (moveToTrash) {
136                            DLFileShortcut fileShortcut =
137                                    DLAppServiceUtil.moveFileShortcutToTrash(fileShortcutId);
138    
139                            Map<String, String[]> data = new HashMap<String, String[]>();
140    
141                            data.put(
142                                    "deleteEntryClassName",
143                                    new String[] {DLFileShortcut.class.getName()});
144    
145                            if (fileShortcut != null) {
146                                    data.put(
147                                            "deleteEntryTitle",
148                                            new String[] {fileShortcut.getToTitle()});
149                            }
150    
151                            data.put(
152                                    "restoreFileShortcutIds",
153                                    new String[] {String.valueOf(fileShortcutId)});
154    
155                            SessionMessages.add(
156                                    actionRequest,
157                                    PortalUtil.getPortletId(actionRequest) +
158                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
159    
160                            hideDefaultSuccessMessage(actionRequest);
161                    }
162                    else {
163                            DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
164                    }
165            }
166    
167            protected void moveFileShortcut(
168                            ActionRequest actionRequest, boolean moveFromTrash)
169                    throws Exception {
170    
171                    long fileShortcutId = ParamUtil.getLong(
172                            actionRequest, "fileShortcutId");
173    
174                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
175    
176                    DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
177                            fileShortcutId);
178    
179                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
180                            DLFileShortcut.class.getName(), actionRequest);
181    
182                    if (moveFromTrash) {
183                            DLAppServiceUtil.moveFileShortcutFromTrash(
184                                    fileShortcutId, newFolderId, serviceContext);
185                    }
186                    else {
187                            DLAppServiceUtil.updateFileShortcut(
188                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
189                                    serviceContext);
190                    }
191            }
192    
193            protected void updateFileShortcut(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    long fileShortcutId = ParamUtil.getLong(
197                            actionRequest, "fileShortcutId");
198    
199                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
200                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
201                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
202    
203                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
204                            DLFileShortcut.class.getName(), actionRequest);
205    
206                    if (fileShortcutId <= 0) {
207    
208                            // Add file shortcut
209    
210                            DLFileShortcut fileShortcut = DLAppServiceUtil.addFileShortcut(
211                                    repositoryId, folderId, toFileEntryId, serviceContext);
212    
213                            AssetPublisherUtil.addAndStoreSelection(
214                                    actionRequest, DLFileShortcut.class.getName(),
215                                    fileShortcut.getFileShortcutId(), -1);
216                    }
217                    else {
218    
219                            // Update file shortcut
220    
221                            DLAppServiceUtil.updateFileShortcut(
222                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
223    
224                            AssetPublisherUtil.addRecentFolderId(
225                                    actionRequest, DLFileShortcut.class.getName(), folderId);
226                    }
227            }
228    
229    }