1
22
23 package com.liferay.portlet.softwarecatalog.action;
24
25 import com.liferay.portal.kernel.util.Constants;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.security.auth.PrincipalException;
28 import com.liferay.portal.struts.PortletAction;
29 import com.liferay.portlet.softwarecatalog.LicenseNameException;
30 import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
31 import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
32 import com.liferay.util.servlet.SessionErrors;
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 config,
54 ActionRequest req, ActionResponse res)
55 throws Exception {
56
57 String cmd = ParamUtil.getString(req, Constants.CMD);
58
59 try {
60 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61 updateLicense(req);
62 }
63 else if (cmd.equals(Constants.DELETE)) {
64 deleteLicense(req);
65 }
66
67 sendRedirect(req, res);
68 }
69 catch (Exception e) {
70 if (e instanceof NoSuchLicenseException ||
71 e instanceof PrincipalException) {
72
73 SessionErrors.add(req, e.getClass().getName());
74
75 setForward(req, "portlet.software_catalog.error");
76 }
77 else if (e instanceof LicenseNameException) {
78
79 SessionErrors.add(req, e.getClass().getName());
80 }
81 else {
82 throw e;
83 }
84 }
85 }
86
87 public ActionForward render(
88 ActionMapping mapping, ActionForm form, PortletConfig config,
89 RenderRequest req, RenderResponse res)
90 throws Exception {
91
92 try {
93 ActionUtil.getLicense(req);
94 }
95 catch (Exception e) {
96 if (e instanceof NoSuchLicenseException ||
97 e instanceof PrincipalException) {
98
99 SessionErrors.add(req, 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(req, "portlet.software_catalog.edit_license"));
110 }
111
112 protected void deleteLicense(ActionRequest req) throws Exception {
113 long licenseId = ParamUtil.getLong(req, "licenseId");
114
115 SCLicenseServiceUtil.deleteLicense(licenseId);
116 }
117
118 protected void updateLicense(ActionRequest req) throws Exception {
119 long licenseId = ParamUtil.getLong(req, "licenseId");
120
121 String name = ParamUtil.getString(req, "name");
122 String url = ParamUtil.getString(req, "url");
123 boolean openSource = ParamUtil.getBoolean(req, "openSource");
124 boolean active = ParamUtil.getBoolean(req, "active");
125 boolean recommended = ParamUtil.getBoolean(req, "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 }