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