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.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
026    import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionServiceUtil;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    
031    /**
032     * @author Jorge Ferrer
033     * @author Philip Jones
034     */
035    @OSGiBeanProperties(
036            property = {
037                    "javax.portlet.name=" + PortletKeys.SOFTWARE_CATALOG,
038                    "mvc.command.name=/software_catalog/edit_framework_version"
039            },
040            service = MVCActionCommand.class
041    )
042    public class EditFrameworkVersionMVCActionCommand extends BaseMVCActionCommand {
043    
044            @Override
045            public void doProcessAction(
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
052                            updateFrameworkVersion(actionRequest);
053                    }
054                    else if (cmd.equals(Constants.DELETE)) {
055                            deleteFrameworkVersion(actionRequest);
056                    }
057    
058                    sendRedirect(actionRequest, actionResponse);
059            }
060    
061            protected void deleteFrameworkVersion(ActionRequest actionRequest)
062                    throws Exception {
063    
064                    long frameworkVersionId = ParamUtil.getLong(
065                            actionRequest, "frameworkVersionId");
066    
067                    SCFrameworkVersionServiceUtil.deleteFrameworkVersion(
068                            frameworkVersionId);
069            }
070    
071            protected void updateFrameworkVersion(ActionRequest actionRequest)
072                    throws Exception {
073    
074                    long frameworkVersionId = ParamUtil.getLong(
075                            actionRequest, "frameworkVersionId");
076    
077                    String name = ParamUtil.getString(actionRequest, "name");
078                    String url = ParamUtil.getString(actionRequest, "url");
079                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
080                    int priority = ParamUtil.getInteger(actionRequest, "priority");
081    
082                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
083                            SCFrameworkVersion.class.getName(), actionRequest);
084    
085                    if (frameworkVersionId <= 0) {
086    
087                            // Add framework version
088    
089                            SCFrameworkVersionServiceUtil.addFrameworkVersion(
090                                    name, url, active, priority, serviceContext);
091                    }
092                    else {
093    
094                            // Update framework version
095    
096                            SCFrameworkVersionServiceUtil.updateFrameworkVersion(
097                                    frameworkVersionId, name, url, active, priority);
098                    }
099            }
100    
101    }