001    /**
002     * Copyright (c) 2000-2013 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.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
025    import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
026    import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
027    import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
028    import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
029    import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
030    import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
031    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
032    import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Jorge Ferrer
046     */
047    public class EditProductVersionAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateProductVersion(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteProductVersion(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchProductVersionException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.software_catalog.error");
075                            }
076                            else if (e instanceof
077                                                    DuplicateProductVersionDirectDownloadURLException ||
078                                             e instanceof ProductVersionChangeLogException ||
079                                             e instanceof ProductVersionDownloadURLException ||
080                                             e instanceof ProductVersionFrameworkVersionException ||
081                                             e instanceof ProductVersionNameException ||
082                                             e instanceof
083                                                    UnavailableProductVersionDirectDownloadURLException) {
084    
085                                    SessionErrors.add(actionRequest, e.getClass());
086                            }
087                            else {
088                                    throw e;
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping actionMapping, ActionForm actionForm,
096                            PortletConfig portletConfig, RenderRequest renderRequest,
097                            RenderResponse renderResponse)
098                    throws Exception {
099    
100                    try {
101                            ActionUtil.getProductVersion(renderRequest);
102                    }
103                    catch (Exception e) {
104                            if (e instanceof NoSuchProductVersionException ||
105                                    e instanceof PrincipalException) {
106    
107                                    SessionErrors.add(renderRequest, e.getClass());
108    
109                                    return actionMapping.findForward(
110                                            "portlet.software_catalog.error");
111                            }
112                            else {
113                                    throw e;
114                            }
115                    }
116    
117                    return actionMapping.findForward(
118                            getForward(
119                                    renderRequest,
120                                    "portlet.software_catalog.edit_product_version"));
121            }
122    
123            protected void deleteProductVersion(ActionRequest actionRequest)
124                    throws Exception {
125    
126                    long productVersionId = ParamUtil.getLong(
127                            actionRequest, "productVersionId");
128    
129                    SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
130            }
131    
132            protected void updateProductVersion(ActionRequest actionRequest)
133                    throws Exception {
134    
135                    long productVersionId = ParamUtil.getLong(
136                            actionRequest, "productVersionId");
137    
138                    long productEntryId = ParamUtil.getLong(
139                            actionRequest, "productEntryId");
140                    String version = ParamUtil.getString(actionRequest, "version");
141                    String changeLog = ParamUtil.getString(actionRequest, "changeLog");
142                    String downloadPageURL = ParamUtil.getString(
143                            actionRequest, "downloadPageURL");
144                    String directDownloadURL = ParamUtil.getString(
145                            actionRequest, "directDownloadURL");
146                    boolean testDirectDownloadURL = ParamUtil.getBoolean(
147                            actionRequest, "testDirectDownloadURL");
148                    boolean repoStoreArtifact = ParamUtil.getBoolean(
149                            actionRequest, "repoStoreArtifact");
150    
151                    long[] frameworkVersionIds = ParamUtil.getLongValues(
152                            actionRequest, "frameworkVersions");
153    
154                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
155                            SCProductVersion.class.getName(), actionRequest);
156    
157                    if (productVersionId <= 0) {
158    
159                            // Add product version
160    
161                            SCProductVersionServiceUtil.addProductVersion(
162                                    productEntryId, version, changeLog, downloadPageURL,
163                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
164                                    frameworkVersionIds, serviceContext);
165                    }
166                    else {
167    
168                            // Update product version
169    
170                            SCProductVersionServiceUtil.updateProductVersion(
171                                    productVersionId, version, changeLog, downloadPageURL,
172                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
173                                    frameworkVersionIds);
174                    }
175            }
176    
177    }