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