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.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 mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    updateProductVersion(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.DELETE)) {
062                                    deleteProductVersion(actionRequest);
063                            }
064    
065                            sendRedirect(actionRequest, actionResponse);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchProductVersionException ||
069                                    e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass());
072    
073                                    setForward(actionRequest, "portlet.software_catalog.error");
074                            }
075                            else if (e instanceof
076                                                    DuplicateProductVersionDirectDownloadURLException ||
077                                             e instanceof ProductVersionChangeLogException ||
078                                             e instanceof ProductVersionDownloadURLException ||
079                                             e instanceof ProductVersionFrameworkVersionException ||
080                                             e instanceof ProductVersionNameException ||
081                                             e instanceof
082                                                    UnavailableProductVersionDirectDownloadURLException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass());
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
095                            RenderRequest renderRequest, RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            ActionUtil.getProductVersion(renderRequest);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof NoSuchProductVersionException ||
103                                    e instanceof PrincipalException) {
104    
105                                    SessionErrors.add(renderRequest, e.getClass());
106    
107                                    return mapping.findForward("portlet.software_catalog.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return mapping.findForward(
115                            getForward(
116                                    renderRequest,
117                                    "portlet.software_catalog.edit_product_version"));
118            }
119    
120            protected void deleteProductVersion(ActionRequest actionRequest)
121                    throws Exception {
122    
123                    long productVersionId = ParamUtil.getLong(
124                            actionRequest, "productVersionId");
125    
126                    SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
127            }
128    
129            protected void updateProductVersion(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    long productVersionId = ParamUtil.getLong(
133                            actionRequest, "productVersionId");
134    
135                    long productEntryId = ParamUtil.getLong(
136                            actionRequest, "productEntryId");
137                    String version = ParamUtil.getString(actionRequest, "version");
138                    String changeLog = ParamUtil.getString(actionRequest, "changeLog");
139                    String downloadPageURL = ParamUtil.getString(
140                            actionRequest, "downloadPageURL");
141                    String directDownloadURL = ParamUtil.getString(
142                            actionRequest, "directDownloadURL");
143                    boolean testDirectDownloadURL = ParamUtil.getBoolean(
144                            actionRequest, "testDirectDownloadURL");
145                    boolean repoStoreArtifact = ParamUtil.getBoolean(
146                            actionRequest, "repoStoreArtifact");
147    
148                    long[] frameworkVersionIds = ParamUtil.getLongValues(
149                            actionRequest, "frameworkVersions");
150    
151                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
152                            SCProductVersion.class.getName(), actionRequest);
153    
154                    if (productVersionId <= 0) {
155    
156                            // Add product version
157    
158                            SCProductVersionServiceUtil.addProductVersion(
159                                    productEntryId, version, changeLog, downloadPageURL,
160                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
161                                    frameworkVersionIds, serviceContext);
162                    }
163                    else {
164    
165                            // Update product version
166    
167                            SCProductVersionServiceUtil.updateProductVersion(
168                                    productVersionId, version, changeLog, downloadPageURL,
169                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
170                                    frameworkVersionIds);
171                    }
172            }
173    
174    }