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