001
014
015 package com.liferay.portlet.bookmarks.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.security.auth.PrincipalException;
021 import com.liferay.portal.service.ServiceContext;
022 import com.liferay.portal.service.ServiceContextFactory;
023 import com.liferay.portal.struts.PortletAction;
024 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
025 import com.liferay.portlet.bookmarks.FolderNameException;
026 import com.liferay.portlet.bookmarks.NoSuchFolderException;
027 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
028 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
029 import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
030
031 import javax.portlet.ActionRequest;
032 import javax.portlet.ActionResponse;
033 import javax.portlet.PortletConfig;
034 import javax.portlet.RenderRequest;
035 import javax.portlet.RenderResponse;
036
037 import org.apache.struts.action.ActionForm;
038 import org.apache.struts.action.ActionForward;
039 import org.apache.struts.action.ActionMapping;
040
041
044 public class EditFolderAction extends PortletAction {
045
046 @Override
047 public void processAction(
048 ActionMapping actionMapping, ActionForm actionForm,
049 PortletConfig portletConfig, ActionRequest actionRequest,
050 ActionResponse actionResponse)
051 throws Exception {
052
053 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054
055 try {
056 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
057 updateFolder(actionRequest);
058 }
059 else if (cmd.equals(Constants.DELETE)) {
060 deleteFolder(actionRequest);
061 }
062
063 sendRedirect(actionRequest, actionResponse);
064 }
065 catch (Exception e) {
066 if (e instanceof NoSuchFolderException ||
067 e instanceof PrincipalException) {
068
069 SessionErrors.add(actionRequest, e.getClass());
070
071 setForward(actionRequest, "portlet.bookmarks.error");
072 }
073 else if (e instanceof FolderNameException) {
074 SessionErrors.add(actionRequest, e.getClass());
075 }
076 else {
077 throw e;
078 }
079 }
080 }
081
082 @Override
083 public ActionForward render(
084 ActionMapping actionMapping, ActionForm actionForm,
085 PortletConfig portletConfig, RenderRequest renderRequest,
086 RenderResponse renderResponse)
087 throws Exception {
088
089 try {
090 ActionUtil.getFolder(renderRequest);
091 }
092 catch (Exception e) {
093 if (e instanceof NoSuchFolderException ||
094 e instanceof PrincipalException) {
095
096 SessionErrors.add(renderRequest, e.getClass());
097
098 return actionMapping.findForward("portlet.bookmarks.error");
099 }
100 else {
101 throw e;
102 }
103 }
104
105 return actionMapping.findForward(
106 getForward(renderRequest, "portlet.bookmarks.edit_folder"));
107 }
108
109 protected void deleteFolder(ActionRequest actionRequest) throws Exception {
110 long folderId = ParamUtil.getLong(actionRequest, "folderId");
111
112 BookmarksFolderServiceUtil.deleteFolder(folderId);
113
114 AssetPublisherUtil.removeRecentFolderId(
115 actionRequest, BookmarksEntry.class.getName(), folderId);
116 }
117
118 protected void updateFolder(ActionRequest actionRequest) throws Exception {
119 long folderId = ParamUtil.getLong(actionRequest, "folderId");
120
121 long parentFolderId = ParamUtil.getLong(
122 actionRequest, "parentFolderId");
123 String name = ParamUtil.getString(actionRequest, "name");
124 String description = ParamUtil.getString(actionRequest, "description");
125
126 boolean mergeWithParentFolder = ParamUtil.getBoolean(
127 actionRequest, "mergeWithParentFolder");
128
129 ServiceContext serviceContext = ServiceContextFactory.getInstance(
130 BookmarksFolder.class.getName(), actionRequest);
131
132 if (folderId <= 0) {
133
134
135
136 BookmarksFolderServiceUtil.addFolder(
137 parentFolderId, name, description, serviceContext);
138 }
139 else {
140
141
142
143 BookmarksFolderServiceUtil.updateFolder(
144 folderId, parentFolderId, name, description,
145 mergeWithParentFolder, serviceContext);
146 }
147 }
148
149 }