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