001
014
015 package com.liferay.portlet.softwarecatalog.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.security.auth.PrincipalException;
021 import com.liferay.portal.struts.PortletAction;
022 import com.liferay.portlet.softwarecatalog.LicenseNameException;
023 import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
024 import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
025
026 import javax.portlet.ActionRequest;
027 import javax.portlet.ActionResponse;
028 import javax.portlet.PortletConfig;
029 import javax.portlet.RenderRequest;
030 import javax.portlet.RenderResponse;
031
032 import org.apache.struts.action.ActionForm;
033 import org.apache.struts.action.ActionForward;
034 import org.apache.struts.action.ActionMapping;
035
036
039 public class EditLicenseAction extends PortletAction {
040
041 @Override
042 public void processAction(
043 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044 ActionRequest actionRequest, ActionResponse actionResponse)
045 throws Exception {
046
047 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
048
049 try {
050 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
051 updateLicense(actionRequest);
052 }
053 else if (cmd.equals(Constants.DELETE)) {
054 deleteLicense(actionRequest);
055 }
056
057 sendRedirect(actionRequest, actionResponse);
058 }
059 catch (Exception e) {
060 if (e instanceof NoSuchLicenseException ||
061 e instanceof PrincipalException) {
062
063 SessionErrors.add(actionRequest, e.getClass().getName());
064
065 setForward(actionRequest, "portlet.software_catalog.error");
066 }
067 else if (e instanceof LicenseNameException) {
068
069 SessionErrors.add(actionRequest, e.getClass().getName());
070 }
071 else {
072 throw e;
073 }
074 }
075 }
076
077 @Override
078 public ActionForward render(
079 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
080 RenderRequest renderRequest, RenderResponse renderResponse)
081 throws Exception {
082
083 try {
084 ActionUtil.getLicense(renderRequest);
085 }
086 catch (Exception e) {
087 if (e instanceof NoSuchLicenseException ||
088 e instanceof PrincipalException) {
089
090 SessionErrors.add(renderRequest, e.getClass().getName());
091
092 return mapping.findForward("portlet.software_catalog.error");
093 }
094 else {
095 throw e;
096 }
097 }
098
099 return mapping.findForward(
100 getForward(renderRequest, "portlet.software_catalog.edit_license"));
101 }
102
103 protected void deleteLicense(ActionRequest actionRequest) throws Exception {
104 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
105
106 SCLicenseServiceUtil.deleteLicense(licenseId);
107 }
108
109 protected void updateLicense(ActionRequest actionRequest) throws Exception {
110 long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
111
112 String name = ParamUtil.getString(actionRequest, "name");
113 String url = ParamUtil.getString(actionRequest, "url");
114 boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
115 boolean active = ParamUtil.getBoolean(actionRequest, "active");
116 boolean recommended = ParamUtil.getBoolean(
117 actionRequest, "recommended");
118
119 if (licenseId <= 0) {
120
121
122
123 SCLicenseServiceUtil.addLicense(
124 name, url, openSource, active, recommended);
125 }
126 else {
127
128
129
130 SCLicenseServiceUtil.updateLicense(
131 licenseId, name, url, openSource, active, recommended);
132 }
133 }
134
135 }