001    /**
002     * Copyright (c) 2000-present 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.portlet.bridges.mvc.BaseMVCActionCommand;
018    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
019    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    @OSGiBeanProperties(
032            property = {
033                    "javax.portlet.name=" + PortletKeys.SOFTWARE_CATALOG,
034                    "mvc.command.name=/software_catalog/edit_license"
035            },
036            service = MVCActionCommand.class
037    )
038    public class EditLicenseMVCActionCommand extends BaseMVCActionCommand {
039    
040            @Override
041            public void doProcessAction(
042                            ActionRequest actionRequest, ActionResponse actionResponse)
043                    throws Exception {
044    
045                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
046    
047                    if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
048                            updateLicense(actionRequest);
049                    }
050                    else if (cmd.equals(Constants.DELETE)) {
051                            deleteLicense(actionRequest);
052                    }
053    
054                    sendRedirect(actionRequest, actionResponse);
055            }
056    
057            protected void deleteLicense(ActionRequest actionRequest) throws Exception {
058                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
059    
060                    SCLicenseServiceUtil.deleteLicense(licenseId);
061            }
062    
063            protected void updateLicense(ActionRequest actionRequest) throws Exception {
064                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
065    
066                    String name = ParamUtil.getString(actionRequest, "name");
067                    String url = ParamUtil.getString(actionRequest, "url");
068                    boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
069                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
070                    boolean recommended = ParamUtil.getBoolean(
071                            actionRequest, "recommended");
072    
073                    if (licenseId <= 0) {
074    
075                            // Add license
076    
077                            SCLicenseServiceUtil.addLicense(
078                                    name, url, openSource, active, recommended);
079                    }
080                    else {
081    
082                            // Update license
083    
084                            SCLicenseServiceUtil.updateLicense(
085                                    licenseId, name, url, openSource, active, recommended);
086                    }
087            }
088    
089    }