1
19
20 package com.liferay.portlet.bookmarks.action;
21
22 import com.liferay.portal.kernel.servlet.SessionErrors;
23 import com.liferay.portal.kernel.util.Constants;
24 import com.liferay.portal.kernel.util.ParamUtil;
25 import com.liferay.portal.security.auth.PrincipalException;
26 import com.liferay.portal.service.ServiceContext;
27 import com.liferay.portal.service.ServiceContextFactory;
28 import com.liferay.portal.struts.PortletAction;
29 import com.liferay.portlet.bookmarks.FolderNameException;
30 import com.liferay.portlet.bookmarks.NoSuchFolderException;
31 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
32 import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
33
34 import javax.portlet.ActionRequest;
35 import javax.portlet.ActionResponse;
36 import javax.portlet.PortletConfig;
37 import javax.portlet.RenderRequest;
38 import javax.portlet.RenderResponse;
39
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.action.ActionForward;
42 import org.apache.struts.action.ActionMapping;
43
44
50 public class EditFolderAction extends PortletAction {
51
52 public void processAction(
53 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54 ActionRequest actionRequest, ActionResponse actionResponse)
55 throws Exception {
56
57 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58
59 try {
60 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61 updateFolder(actionRequest);
62 }
63 else if (cmd.equals(Constants.DELETE)) {
64 deleteFolder(actionRequest);
65 }
66
67 sendRedirect(actionRequest, actionResponse);
68 }
69 catch (Exception e) {
70 if (e instanceof NoSuchFolderException ||
71 e instanceof PrincipalException) {
72
73 SessionErrors.add(actionRequest, e.getClass().getName());
74
75 setForward(actionRequest, "portlet.bookmarks.error");
76 }
77 else if (e instanceof FolderNameException) {
78 SessionErrors.add(actionRequest, e.getClass().getName());
79 }
80 else {
81 throw e;
82 }
83 }
84 }
85
86 public ActionForward render(
87 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88 RenderRequest renderRequest, RenderResponse renderResponse)
89 throws Exception {
90
91 try {
92 ActionUtil.getFolder(renderRequest);
93 }
94 catch (Exception e) {
95 if (e instanceof NoSuchFolderException ||
96 e instanceof PrincipalException) {
97
98 SessionErrors.add(renderRequest, e.getClass().getName());
99
100 return mapping.findForward("portlet.bookmarks.error");
101 }
102 else {
103 throw e;
104 }
105 }
106
107 return mapping.findForward(
108 getForward(renderRequest, "portlet.bookmarks.edit_folder"));
109 }
110
111 protected void deleteFolder(ActionRequest actionRequest) throws Exception {
112 long folderId = ParamUtil.getLong(actionRequest, "folderId");
113
114 BookmarksFolderServiceUtil.deleteFolder(folderId);
115 }
116
117 protected void updateFolder(ActionRequest actionRequest) throws Exception {
118 long folderId = ParamUtil.getLong(actionRequest, "folderId");
119
120 long parentFolderId = ParamUtil.getLong(
121 actionRequest, "parentFolderId");
122 String name = ParamUtil.getString(actionRequest, "name");
123 String description = ParamUtil.getString(actionRequest, "description");
124
125 boolean mergeWithParentFolder = ParamUtil.getBoolean(
126 actionRequest, "mergeWithParentFolder");
127
128 ServiceContext serviceContext = ServiceContextFactory.getInstance(
129 BookmarksFolder.class.getName(), actionRequest);
130
131 if (folderId <= 0) {
132
133
135 BookmarksFolderServiceUtil.addFolder(
136 parentFolderId, name, description, serviceContext);
137 }
138 else {
139
140
142 BookmarksFolderServiceUtil.updateFolder(
143 folderId, parentFolderId, name, description,
144 mergeWithParentFolder, serviceContext);
145 }
146 }
147
148 }