1
22
23 package com.liferay.portlet.softwarecatalog.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.struts.PortletAction;
30 import com.liferay.portlet.softwarecatalog.LicenseNameException;
31 import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
32 import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
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 EditLicenseAction 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 updateLicense(actionRequest);
62 }
63 else if (cmd.equals(Constants.DELETE)) {
64 deleteLicense(actionRequest);
65 }
66
67 sendRedirect(actionRequest, actionResponse);
68 }
69 catch (Exception e) {
70 if (e instanceof NoSuchLicenseException ||
71 e instanceof PrincipalException) {
72
73 SessionErrors.add(actionRequest, e.getClass().getName());
74
75 setForward(actionRequest, "portlet.software_catalog.error");
76 }
77 else if (e instanceof LicenseNameException) {
78
79 SessionErrors.add(actionRequest, e.getClass().getName());
80 }
81 else {
82 throw e;
83 }
84 }
85 }
86
87 public ActionForward render(
88 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89 RenderRequest renderRequest, RenderResponse renderResponse)
90 throws Exception {
91
92 try {
93 ActionUtil.getLicense(renderRequest);
94 }
95 catch (Exception e) {
96 if (e instanceof NoSuchLicenseException ||
97 e instanceof PrincipalException) {
98
99 SessionErrors.add(renderRequest, e.getClass().getName());
100
101 return mapping.findForward("portlet.software_catalog.error");
102 }
103 else {
104 throw e;
105 }
106 }
107
108 return mapping.findForward(
109 getForward(renderRequest, "portlet.software_catalog.edit_license"));
110 }
111
112 protected void deleteLicense(ActionRequest actionRequest) throws Exception {
113 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
114
115 SCLicenseServiceUtil.deleteLicense(licenseId);
116 }
117
118 protected void updateLicense(ActionRequest actionRequest) throws Exception {
119 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
120
121 String name = ParamUtil.getString(actionRequest, "name");
122 String url = ParamUtil.getString(actionRequest, "url");
123 boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
124 boolean active = ParamUtil.getBoolean(actionRequest, "active");
125 boolean recommended = ParamUtil.getBoolean(
126 actionRequest, "recommended");
127
128 if (licenseId <= 0) {
129
130
132 SCLicenseServiceUtil.addLicense(
133 name, url, openSource, active, recommended);
134 }
135 else {
136
137
139 SCLicenseServiceUtil.updateLicense(
140 licenseId, name, url, openSource, active, recommended);
141 }
142 }
143
144 }