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