Java tutorial
/** * Copyright (c) 2000-2012 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.portlet.bookmarks.action; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.Constants; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.service.ServiceContextFactory; import com.liferay.portal.struts.PortletAction; import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil; import com.liferay.portlet.bookmarks.FolderNameException; import com.liferay.portlet.bookmarks.NoSuchFolderException; import com.liferay.portlet.bookmarks.model.BookmarksEntry; import com.liferay.portlet.bookmarks.model.BookmarksFolder; import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author Brian Wing Shun Chan */ public class EditFolderAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, 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)) { deleteFolder(actionRequest); } sendRedirect(actionRequest, actionResponse); } catch (Exception e) { if (e instanceof NoSuchFolderException || e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.bookmarks.error"); } else if (e instanceof FolderNameException) { SessionErrors.add(actionRequest, e.getClass().getName()); } else { throw e; } } } @Override public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { try { ActionUtil.getFolder(renderRequest); } catch (Exception e) { if (e instanceof NoSuchFolderException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass().getName()); return mapping.findForward("portlet.bookmarks.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.bookmarks.edit_folder")); } protected void deleteFolder(ActionRequest actionRequest) throws Exception { long folderId = ParamUtil.getLong(actionRequest, "folderId"); BookmarksFolderServiceUtil.deleteFolder(folderId); AssetPublisherUtil.removeRecentFolderId(actionRequest, BookmarksEntry.class.getName(), 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) { // Add folder BookmarksFolderServiceUtil.addFolder(parentFolderId, name, description, serviceContext); } else { // Update folder BookmarksFolderServiceUtil.updateFolder(folderId, parentFolderId, name, description, mergeWithParentFolder, serviceContext); } } }