/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.bookmarks.web.internal.portlet.action; import com.liferay.bookmarks.constants.BookmarksPortletKeys; import com.liferay.bookmarks.exception.FolderNameException; import com.liferay.bookmarks.exception.NoSuchFolderException; import com.liferay.bookmarks.model.BookmarksFolder; import com.liferay.bookmarks.service.BookmarksFolderService; import com.liferay.portal.kernel.model.TrashedModel; import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand; import com.liferay.portal.kernel.security.auth.PrincipalException; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextFactory; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.Constants; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.trash.kernel.service.TrashEntryService; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Brian Wing Shun Chan */ @Component( immediate = true, property = { "javax.portlet.name=" + BookmarksPortletKeys.BOOKMARKS, "javax.portlet.name=" + BookmarksPortletKeys.BOOKMARKS_ADMIN, "mvc.command.name=/bookmarks/edit_folder" }, service = MVCActionCommand.class ) public class EditFolderMVCActionCommand extends BaseMVCActionCommand { protected void deleteFolders( ActionRequest actionRequest, boolean moveToTrash) throws Exception { long[] deleteFolderIds = null; long folderId = ParamUtil.getLong(actionRequest, "folderId"); if (folderId > 0) { deleteFolderIds = new long[] {folderId}; } else { deleteFolderIds = StringUtil.split( ParamUtil.getString(actionRequest, "folderIds"), 0L); } List<TrashedModel> trashedModels = new ArrayList<>(); for (long deleteFolderId : deleteFolderIds) { if (moveToTrash) { BookmarksFolder folder = _bookmarksFolderService.moveFolderToTrash(deleteFolderId); trashedModels.add(folder); } else { _bookmarksFolderService.deleteFolder(deleteFolderId); } } if (moveToTrash && !trashedModels.isEmpty()) { Map<String, Object> data = new HashMap<>(); data.put("trashedModels", trashedModels); addDeleteSuccessData(actionRequest, data); } } @Override protected void doProcessAction( ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { String cmd = ParamUtil.getString(actionRequest, Constants.CMD); try { if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) { updateFolder(actionRequest); } else if (cmd.equals(Constants.DELETE)) { deleteFolders(actionRequest, false); } else if (cmd.equals(Constants.MOVE_TO_TRASH)) { deleteFolders(actionRequest, true); } else if (cmd.equals(Constants.RESTORE)) { restoreTrashEntries(actionRequest); } else if (cmd.equals(Constants.SUBSCRIBE)) { subscribeFolder(actionRequest); } else if (cmd.equals(Constants.UNSUBSCRIBE)) { unsubscribeFolder(actionRequest); } } catch (Exception e) { if (e instanceof NoSuchFolderException || e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass()); actionResponse.setRenderParameter( "mvcPath", "/bookmarks/error.jsp"); } else if (e instanceof FolderNameException) { SessionErrors.add(actionRequest, e.getClass()); } else { throw e; } } } protected void restoreTrashEntries(ActionRequest actionRequest) throws Exception { long[] restoreTrashEntryIds = StringUtil.split( ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L); for (long restoreTrashEntryId : restoreTrashEntryIds) { _trashEntryService.restoreEntry(restoreTrashEntryId); } } @Reference(unbind = "-") protected void setBookmarksFolderService( BookmarksFolderService bookmarksFolderService) { _bookmarksFolderService = bookmarksFolderService; } @Reference(unbind = "-") protected void setTrashEntryService(TrashEntryService trashEntryService) { _trashEntryService = trashEntryService; } protected void subscribeFolder(ActionRequest actionRequest) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); long folderId = ParamUtil.getLong(actionRequest, "folderId"); _bookmarksFolderService.subscribeFolder( themeDisplay.getScopeGroupId(), folderId); } protected void unsubscribeFolder(ActionRequest actionRequest) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); long folderId = ParamUtil.getLong(actionRequest, "folderId"); _bookmarksFolderService.unsubscribeFolder( themeDisplay.getScopeGroupId(), folderId); } protected void updateFolder(ActionRequest actionRequest) throws Exception { long folderId = ParamUtil.getLong(actionRequest, "folderId"); long parentFolderId = ParamUtil.getLong( actionRequest, "parentFolderId"); String name = ParamUtil.getString(actionRequest, "name"); String description = ParamUtil.getString(actionRequest, "description"); boolean mergeWithParentFolder = ParamUtil.getBoolean( actionRequest, "mergeWithParentFolder"); ServiceContext serviceContext = ServiceContextFactory.getInstance( BookmarksFolder.class.getName(), actionRequest); if (folderId <= 0) { _bookmarksFolderService.addFolder( parentFolderId, name, description, serviceContext); } else if (mergeWithParentFolder) { _bookmarksFolderService.mergeFolders(folderId, parentFolderId); } else { _bookmarksFolderService.updateFolder( folderId, parentFolderId, name, description, serviceContext); } } private BookmarksFolderService _bookmarksFolderService; private TrashEntryService _trashEntryService; }