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.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    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class EditFileShortcutAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
057                                    updateFileShortcut(actionRequest);
058                            }
059                            else if (cmd.equals(Constants.DELETE)) {
060                                    deleteFileShortcut(actionRequest);
061                            }
062    
063                            sendRedirect(actionRequest, actionResponse);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof NoSuchFileShortcutException ||
067                                    e instanceof PrincipalException) {
068    
069                                    SessionErrors.add(actionRequest, e.getClass());
070    
071                                    setForward(actionRequest, "portlet.document_library.error");
072                            }
073                            else if (e instanceof FileShortcutPermissionException ||
074                                             e instanceof NoSuchFileEntryException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass());
077                            }
078                            else {
079                                    throw e;
080                            }
081                    }
082            }
083    
084            @Override
085            public ActionForward render(
086                            ActionMapping actionMapping, ActionForm actionForm,
087                            PortletConfig portletConfig, RenderRequest renderRequest,
088                            RenderResponse renderResponse)
089                    throws Exception {
090    
091                    try {
092                            ActionUtil.getFileShortcut(renderRequest);
093                    }
094                    catch (Exception e) {
095                            if (e instanceof NoSuchFileShortcutException ||
096                                    e instanceof PrincipalException) {
097    
098                                    SessionErrors.add(renderRequest, e.getClass());
099    
100                                    return actionMapping.findForward(
101                                            "portlet.document_library.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107    
108                    return actionMapping.findForward(
109                            getForward(
110                                    renderRequest, "portlet.document_library.edit_file_shortcut"));
111            }
112    
113            protected void deleteFileShortcut(ActionRequest actionRequest)
114                    throws Exception {
115    
116                    long fileShortcutId = ParamUtil.getLong(
117                            actionRequest, "fileShortcutId");
118    
119                    DLAppServiceUtil.deleteFileShortcut(fileShortcutId);
120            }
121    
122            protected void updateFileShortcut(ActionRequest actionRequest)
123                    throws Exception {
124    
125                    long fileShortcutId = ParamUtil.getLong(
126                            actionRequest, "fileShortcutId");
127    
128                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
129                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
130                    long toFileEntryId = ParamUtil.getLong(actionRequest, "toFileEntryId");
131    
132                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
133                            DLFileShortcut.class.getName(), actionRequest);
134    
135                    if (fileShortcutId <= 0) {
136    
137                            // Add file shortcut
138    
139                            DLFileShortcut fileShortcut = DLAppServiceUtil.addFileShortcut(
140                                    repositoryId, folderId, toFileEntryId, serviceContext);
141    
142                            AssetPublisherUtil.addAndStoreSelection(
143                                    actionRequest, DLFileShortcut.class.getName(),
144                                    fileShortcut.getFileShortcutId(), -1);
145                    }
146                    else {
147    
148                            // Update file shortcut
149    
150                            DLAppServiceUtil.updateFileShortcut(
151                                    fileShortcutId, folderId, toFileEntryId, serviceContext);
152    
153                            AssetPublisherUtil.addRecentFolderId(
154                                    actionRequest, DLFileShortcut.class.getName(), folderId);
155                    }
156            }
157    
158    }