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.bookmarks.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.model.LayoutTypePortlet;
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.asset.AssetTagException;
028    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
029    import com.liferay.portlet.bookmarks.EntryURLException;
030    import com.liferay.portlet.bookmarks.NoSuchEntryException;
031    import com.liferay.portlet.bookmarks.NoSuchFolderException;
032    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
033    import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditEntryAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    updateEntry(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.DELETE)) {
062                                    deleteEntry(actionRequest);
063                            }
064    
065                            ThemeDisplay themeDisplay =
066                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
067    
068                            LayoutTypePortlet layoutTypePortlet =
069                                    themeDisplay.getLayoutTypePortlet();
070    
071                            if (layoutTypePortlet.hasPortletId(
072                                            portletConfig.getPortletName())) {
073    
074                                    sendRedirect(actionRequest, actionResponse);
075                            }
076                            else {
077                                    String redirect = ParamUtil.getString(
078                                            actionRequest, "redirect");
079    
080                                    actionResponse.sendRedirect(redirect);
081                            }
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchEntryException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(actionRequest, e.getClass().getName());
088    
089                                    setForward(actionRequest, "portlet.bookmarks.error");
090                            }
091                            else if (e instanceof EntryURLException ||
092                                             e instanceof NoSuchFolderException) {
093    
094                                    SessionErrors.add(actionRequest, e.getClass().getName());
095                            }
096                            else if (e instanceof AssetTagException) {
097                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103            }
104    
105            public ActionForward render(
106                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
107                            RenderRequest renderRequest, RenderResponse renderResponse)
108                    throws Exception {
109    
110                    try {
111                            ActionUtil.getEntry(renderRequest);
112                    }
113                    catch (Exception e) {
114                            if (e instanceof NoSuchEntryException ||
115                                    e instanceof PrincipalException) {
116    
117                                    SessionErrors.add(renderRequest, e.getClass().getName());
118    
119                                    return mapping.findForward("portlet.bookmarks.error");
120                            }
121                            else {
122                                    throw e;
123                            }
124                    }
125    
126                    return mapping.findForward(
127                            getForward(renderRequest, "portlet.bookmarks.edit_entry"));
128            }
129    
130            protected void deleteEntry(ActionRequest actionRequest) throws Exception {
131                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
132    
133                    BookmarksEntryServiceUtil.deleteEntry(entryId);
134            }
135    
136            protected void updateEntry(ActionRequest actionRequest) throws Exception {
137                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
138                            WebKeys.THEME_DISPLAY);
139    
140                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
141    
142                    long groupId = themeDisplay.getScopeGroupId();
143                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
144                    String name = ParamUtil.getString(actionRequest, "name");
145                    String url = ParamUtil.getString(actionRequest, "url");
146                    String comments = ParamUtil.getString(actionRequest, "comments");
147    
148                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
149                            BookmarksEntry.class.getName(), actionRequest);
150    
151                    if (entryId <= 0) {
152    
153                            // Add entry
154    
155                            BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
156                                    groupId, folderId, name, url, comments, serviceContext);
157    
158                            AssetPublisherUtil.addAndStoreSelection(
159                                    actionRequest, BookmarksEntry.class.getName(),
160                                    entry.getEntryId(), -1);
161                    }
162                    else {
163    
164                            // Update entry
165    
166                            BookmarksEntryServiceUtil.updateEntry(
167                                    entryId, groupId, folderId, name, url, comments,
168                                    serviceContext);
169                    }
170    
171                    AssetPublisherUtil.addRecentFolderId(
172                            actionRequest, BookmarksEntry.class.getName(), folderId);
173            }
174    
175    }