001    /**
002     * Copyright (c) 2000-present 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.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.TrashedModel;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.asset.AssetCategoryException;
033    import com.liferay.portlet.asset.AssetTagException;
034    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
035    import com.liferay.portlet.bookmarks.EntryURLException;
036    import com.liferay.portlet.bookmarks.NoSuchEntryException;
037    import com.liferay.portlet.bookmarks.NoSuchFolderException;
038    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
039    import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
040    import com.liferay.portlet.trash.util.TrashUtil;
041    
042    import java.util.ArrayList;
043    import java.util.List;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.RenderRequest;
049    import javax.portlet.RenderResponse;
050    import javax.portlet.WindowState;
051    
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Levente Hud??k
059     */
060    public class EditEntryAction extends PortletAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping actionMapping, ActionForm actionForm,
065                            PortletConfig portletConfig, ActionRequest actionRequest,
066                            ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    try {
072                            BookmarksEntry entry = null;
073    
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    entry = updateEntry(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deleteEntry(actionRequest, false);
079                            }
080                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
081                                    deleteEntry(actionRequest, true);
082                            }
083                            else if (cmd.equals(Constants.SUBSCRIBE)) {
084                                    subscribeEntry(actionRequest);
085                            }
086                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
087                                    unsubscribeEntry(actionRequest);
088                            }
089    
090                            WindowState windowState = actionRequest.getWindowState();
091    
092                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
093                                    sendRedirect(actionRequest, actionResponse);
094                            }
095                            else {
096                                    String redirect = PortalUtil.escapeRedirect(
097                                            ParamUtil.getString(actionRequest, "redirect"));
098    
099                                    if (Validator.isNotNull(redirect)) {
100                                            if (cmd.equals(Constants.ADD) && (entry != null)) {
101                                                    String portletId = HttpUtil.getParameter(
102                                                            redirect, "p_p_id", false);
103    
104                                                    String namespace = PortalUtil.getPortletNamespace(
105                                                            portletId);
106    
107                                                    redirect = HttpUtil.addParameter(
108                                                            redirect, namespace + "className",
109                                                            BookmarksEntry.class.getName());
110                                                    redirect = HttpUtil.addParameter(
111                                                            redirect, namespace + "classPK",
112                                                            entry.getEntryId());
113                                            }
114    
115                                            actionResponse.sendRedirect(redirect);
116                                    }
117                            }
118                    }
119                    catch (Exception e) {
120                            if (e instanceof NoSuchEntryException ||
121                                    e instanceof PrincipalException) {
122    
123                                    SessionErrors.add(actionRequest, e.getClass());
124    
125                                    setForward(actionRequest, "portlet.bookmarks.error");
126                            }
127                            else if (e instanceof EntryURLException ||
128                                             e instanceof NoSuchFolderException) {
129    
130                                    SessionErrors.add(actionRequest, e.getClass());
131                            }
132                            else if (e instanceof AssetCategoryException ||
133                                             e instanceof AssetTagException) {
134    
135                                    SessionErrors.add(actionRequest, e.getClass(), e);
136                            }
137                            else {
138                                    throw e;
139                            }
140                    }
141            }
142    
143            @Override
144            public ActionForward render(
145                            ActionMapping actionMapping, ActionForm actionForm,
146                            PortletConfig portletConfig, RenderRequest renderRequest,
147                            RenderResponse renderResponse)
148                    throws Exception {
149    
150                    try {
151                            ActionUtil.getEntry(renderRequest);
152                    }
153                    catch (Exception e) {
154                            if (e instanceof NoSuchEntryException ||
155                                    e instanceof PrincipalException) {
156    
157                                    SessionErrors.add(renderRequest, e.getClass());
158    
159                                    return actionMapping.findForward("portlet.bookmarks.error");
160                            }
161                            else {
162                                    throw e;
163                            }
164                    }
165    
166                    return actionMapping.findForward(
167                            getForward(renderRequest, "portlet.bookmarks.edit_entry"));
168            }
169    
170            protected void deleteEntry(ActionRequest actionRequest, boolean moveToTrash)
171                    throws Exception {
172    
173                    long[] deleteEntryIds = null;
174    
175                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
176    
177                    if (entryId > 0) {
178                            deleteEntryIds = new long[] {entryId};
179                    }
180                    else {
181                            deleteEntryIds = StringUtil.split(
182                                    ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
183                    }
184    
185                    List<TrashedModel> trashedModels = new ArrayList<TrashedModel>();
186    
187                    for (long deleteEntryId : deleteEntryIds) {
188                            if (moveToTrash) {
189                                    BookmarksEntry entry =
190                                            BookmarksEntryServiceUtil.moveEntryToTrash(deleteEntryId);
191    
192                                    trashedModels.add(entry);
193                            }
194                            else {
195                                    BookmarksEntryServiceUtil.deleteEntry(deleteEntryId);
196                            }
197                    }
198    
199                    if (moveToTrash && !trashedModels.isEmpty()) {
200                            TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
201    
202                            hideDefaultSuccessMessage(actionRequest);
203                    }
204            }
205    
206            protected void subscribeEntry(ActionRequest actionRequest)
207                    throws Exception {
208    
209                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
210    
211                    BookmarksEntryServiceUtil.subscribeEntry(entryId);
212            }
213    
214            protected void unsubscribeEntry(ActionRequest actionRequest)
215                    throws Exception {
216    
217                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
218    
219                    BookmarksEntryServiceUtil.unsubscribeEntry(entryId);
220            }
221    
222            protected BookmarksEntry updateEntry(ActionRequest actionRequest)
223                    throws Exception {
224    
225                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
226                            WebKeys.THEME_DISPLAY);
227    
228                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
229    
230                    long groupId = themeDisplay.getScopeGroupId();
231                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
232                    String name = ParamUtil.getString(actionRequest, "name");
233                    String url = ParamUtil.getString(actionRequest, "url");
234                    String description = ParamUtil.getString(actionRequest, "description");
235    
236                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
237                            BookmarksEntry.class.getName(), actionRequest);
238    
239                    BookmarksEntry entry = null;
240    
241                    if (entryId <= 0) {
242    
243                            // Add entry
244    
245                            entry = BookmarksEntryServiceUtil.addEntry(
246                                    groupId, folderId, name, url, description, serviceContext);
247    
248                            AssetPublisherUtil.addAndStoreSelection(
249                                    actionRequest, BookmarksEntry.class.getName(),
250                                    entry.getEntryId(), -1);
251                    }
252                    else {
253    
254                            // Update entry
255    
256                            entry = BookmarksEntryServiceUtil.updateEntry(
257                                    entryId, groupId, folderId, name, url, description,
258                                    serviceContext);
259                    }
260    
261                    AssetPublisherUtil.addRecentFolderId(
262                            actionRequest, BookmarksEntry.class.getName(), folderId);
263    
264                    return entry;
265            }
266    
267    }