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.security.auth.PrincipalException;
29 import com.liferay.portal.service.ServiceContext;
30 import com.liferay.portal.service.ServiceContextFactory;
31 import com.liferay.portal.struts.PortletAction;
32 import com.liferay.portlet.documentlibrary.model.DLFolder;
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 long folderId = ParamUtil.getLong(actionRequest, "folderId");
125
126 long parentFolderId = ParamUtil.getLong(
127 actionRequest, "parentFolderId");
128 String name = ParamUtil.getString(actionRequest, "name");
129 String description = ParamUtil.getString(actionRequest, "description");
130
131 boolean mergeWithParentFolder = ParamUtil.getBoolean(
132 actionRequest, "mergeWithParentFolder");
133
134 ServiceContext serviceContext = ServiceContextFactory.getInstance(
135 DLFolder.class.getName(), actionRequest);
136
137 if (folderId <= 0) {
138
139
141 IGFolderServiceUtil.addFolder(
142 parentFolderId, name, description, serviceContext);
143 }
144 else {
145
146
148 IGFolderServiceUtil.updateFolder(
149 folderId, parentFolderId, name, description,
150 mergeWithParentFolder);
151 }
152 }
153
154 }