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
49 public class EditLicenseAction extends PortletAction {
50
51 public void processAction(
52 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53 ActionRequest actionRequest, ActionResponse actionResponse)
54 throws Exception {
55
56 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57
58 try {
59 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
60 updateLicense(actionRequest);
61 }
62 else if (cmd.equals(Constants.DELETE)) {
63 deleteLicense(actionRequest);
64 }
65
66 sendRedirect(actionRequest, actionResponse);
67 }
68 catch (Exception e) {
69 if (e instanceof NoSuchLicenseException ||
70 e instanceof PrincipalException) {
71
72 SessionErrors.add(actionRequest, e.getClass().getName());
73
74 setForward(actionRequest, "portlet.software_catalog.error");
75 }
76 else if (e instanceof LicenseNameException) {
77
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.getLicense(renderRequest);
93 }
94 catch (Exception e) {
95 if (e instanceof NoSuchLicenseException ||
96 e instanceof PrincipalException) {
97
98 SessionErrors.add(renderRequest, e.getClass().getName());
99
100 return mapping.findForward("portlet.software_catalog.error");
101 }
102 else {
103 throw e;
104 }
105 }
106
107 return mapping.findForward(
108 getForward(renderRequest, "portlet.software_catalog.edit_license"));
109 }
110
111 protected void deleteLicense(ActionRequest actionRequest) throws Exception {
112 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
113
114 SCLicenseServiceUtil.deleteLicense(licenseId);
115 }
116
117 protected void updateLicense(ActionRequest actionRequest) throws Exception {
118 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
119
120 String name = ParamUtil.getString(actionRequest, "name");
121 String url = ParamUtil.getString(actionRequest, "url");
122 boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
123 boolean active = ParamUtil.getBoolean(actionRequest, "active");
124 boolean recommended = ParamUtil.getBoolean(
125 actionRequest, "recommended");
126
127 if (licenseId <= 0) {
128
129
131 SCLicenseServiceUtil.addLicense(
132 name, url, openSource, active, recommended);
133 }
134 else {
135
136
138 SCLicenseServiceUtil.updateLicense(
139 licenseId, name, url, openSource, active, recommended);
140 }
141 }
142
143 }