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.documentlibrary.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.kernel.util.StringUtil; 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.documentlibrary.DuplicateFileException; import com.liferay.portlet.documentlibrary.DuplicateFolderNameException; import com.liferay.portlet.documentlibrary.FolderNameException; import com.liferay.portlet.documentlibrary.NoSuchFolderException; import com.liferay.portlet.documentlibrary.model.DLFileEntry; import com.liferay.portlet.documentlibrary.model.DLFolder; import com.liferay.portlet.documentlibrary.model.DLFolderConstants; import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil; 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 * @author Alexander Chow * @author Sergio Gonzlez */ 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)) { deleteFolders(actionRequest); } else if (cmd.equals(Constants.MOVE)) { moveFolders(actionRequest); } else if (cmd.equals("updateWorkflowDefinitions")) { updateWorkflowDefinitions(actionRequest); } sendRedirect(actionRequest, actionResponse); } catch (Exception e) { if (e instanceof NoSuchFolderException || e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.document_library.error"); } else if (e instanceof DuplicateFileException || e instanceof DuplicateFolderNameException || 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.document_library.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.document_library.edit_folder")); } protected void deleteFolders(ActionRequest actionRequest) throws Exception { long folderId = ParamUtil.getLong(actionRequest, "folderId"); if (folderId > 0) { DLAppServiceUtil.deleteFolder(folderId); AssetPublisherUtil.removeRecentFolderId(actionRequest, DLFileEntry.class.getName(), folderId); } else { long[] folderIds = StringUtil.split(ParamUtil.getString(actionRequest, "folderIds"), 0L); for (long curFolderId : folderIds) { DLAppServiceUtil.deleteFolder(curFolderId); AssetPublisherUtil.removeRecentFolderId(actionRequest, DLFileEntry.class.getName(), curFolderId); } } } protected void moveFolders(ActionRequest actionRequest) throws Exception { long folderId = ParamUtil.getLong(actionRequest, "folderId"); long parentFolderId = ParamUtil.getLong(actionRequest, "parentFolderId"); ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest); if (folderId > 0) { DLAppServiceUtil.moveFolder(folderId, parentFolderId, serviceContext); } else { long[] folderIds = StringUtil.split(ParamUtil.getString(actionRequest, "folderIds"), 0L); for (long curFolderId : folderIds) { DLAppServiceUtil.moveFolder(curFolderId, parentFolderId, serviceContext); } } } protected void updateFolder(ActionRequest actionRequest) throws Exception { long folderId = ParamUtil.getLong(actionRequest, "folderId"); long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId"); long parentFolderId = ParamUtil.getLong(actionRequest, "parentFolderId"); String name = ParamUtil.getString(actionRequest, "name"); String description = ParamUtil.getString(actionRequest, "description"); ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFolder.class.getName(), actionRequest); if (folderId <= 0) { // Add folder DLAppServiceUtil.addFolder(repositoryId, parentFolderId, name, description, serviceContext); } else { // Update folder DLAppServiceUtil.updateFolder(folderId, name, description, serviceContext); } } protected void updateWorkflowDefinitions(ActionRequest actionRequest) throws Exception { ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest); DLAppServiceUtil.updateFolder(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null, serviceContext); } }