001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.softwarecatalog.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.security.auth.PrincipalException;
026: import com.liferay.portal.struts.PortletAction;
027: import com.liferay.portlet.softwarecatalog.LicenseNameException;
028: import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
029: import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
030: import com.liferay.util.servlet.SessionErrors;
031:
032: import javax.portlet.ActionRequest;
033: import javax.portlet.ActionResponse;
034: import javax.portlet.PortletConfig;
035: import javax.portlet.RenderRequest;
036: import javax.portlet.RenderResponse;
037:
038: import org.apache.struts.action.ActionForm;
039: import org.apache.struts.action.ActionForward;
040: import org.apache.struts.action.ActionMapping;
041:
042: /**
043: * <a href="EditLicenseAction.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Jorge Ferrer
046: *
047: */
048: public class EditLicenseAction extends PortletAction {
049:
050: public void processAction(ActionMapping mapping, ActionForm form,
051: PortletConfig config, ActionRequest req, ActionResponse res)
052: throws Exception {
053:
054: String cmd = ParamUtil.getString(req, Constants.CMD);
055:
056: try {
057: if (cmd.equals(Constants.ADD)
058: || cmd.equals(Constants.UPDATE)) {
059: updateLicense(req);
060: } else if (cmd.equals(Constants.DELETE)) {
061: deleteLicense(req);
062: }
063:
064: sendRedirect(req, res);
065: } catch (Exception e) {
066: if (e instanceof NoSuchLicenseException
067: || e instanceof PrincipalException) {
068:
069: SessionErrors.add(req, e.getClass().getName());
070:
071: setForward(req, "portlet.software_catalog.error");
072: } else if (e instanceof LicenseNameException) {
073:
074: SessionErrors.add(req, e.getClass().getName());
075: } else {
076: throw e;
077: }
078: }
079: }
080:
081: public ActionForward render(ActionMapping mapping, ActionForm form,
082: PortletConfig config, RenderRequest req, RenderResponse res)
083: throws Exception {
084:
085: try {
086: ActionUtil.getLicense(req);
087: } catch (Exception e) {
088: if (e instanceof NoSuchLicenseException
089: || e instanceof PrincipalException) {
090:
091: SessionErrors.add(req, e.getClass().getName());
092:
093: return mapping
094: .findForward("portlet.software_catalog.error");
095: } else {
096: throw e;
097: }
098: }
099:
100: return mapping.findForward(getForward(req,
101: "portlet.software_catalog.edit_license"));
102: }
103:
104: protected void deleteLicense(ActionRequest req) throws Exception {
105: long licenseId = ParamUtil.getLong(req, "licenseId");
106:
107: SCLicenseServiceUtil.deleteLicense(licenseId);
108: }
109:
110: protected void updateLicense(ActionRequest req) throws Exception {
111: long licenseId = ParamUtil.getLong(req, "licenseId");
112:
113: String name = ParamUtil.getString(req, "name");
114: String url = ParamUtil.getString(req, "url");
115: boolean openSource = ParamUtil.getBoolean(req, "openSource");
116: boolean active = ParamUtil.getBoolean(req, "active");
117: boolean recommended = ParamUtil.getBoolean(req, "recommended");
118:
119: if (licenseId <= 0) {
120:
121: // Add license
122:
123: SCLicenseServiceUtil.addLicense(name, url, openSource,
124: active, recommended);
125: } else {
126:
127: // Update license
128:
129: SCLicenseServiceUtil.updateLicense(licenseId, name, url,
130: openSource, active, recommended);
131: }
132: }
133:
134: }
|