001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
037     * @author Jorge Ferrer
038     */
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                            // Add license
122    
123                            SCLicenseServiceUtil.addLicense(
124                                    name, url, openSource, active, recommended);
125                    }
126                    else {
127    
128                            // Update license
129    
130                            SCLicenseServiceUtil.updateLicense(
131                                    licenseId, name, url, openSource, active, recommended);
132                    }
133            }
134    
135    }