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