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().getName());
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().getName());
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().getName());
106    
107                                    return mapping.findForward("portlet.software_catalog.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return mapping.findForward(getForward(
115                            renderRequest, "portlet.software_catalog.edit_product_version"));
116            }
117    
118            protected void deleteProductVersion(ActionRequest actionRequest)
119                    throws Exception {
120    
121                    long productVersionId = ParamUtil.getLong(
122                            actionRequest, "productVersionId");
123    
124                    SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
125            }
126    
127            protected void updateProductVersion(ActionRequest actionRequest)
128                    throws Exception {
129    
130                    long productVersionId = ParamUtil.getLong(
131                            actionRequest, "productVersionId");
132    
133                    long productEntryId = ParamUtil.getLong(
134                            actionRequest, "productEntryId");
135                    String version = ParamUtil.getString(actionRequest, "version");
136                    String changeLog = ParamUtil.getString(actionRequest, "changeLog");
137                    String downloadPageURL = ParamUtil.getString(
138                            actionRequest, "downloadPageURL");
139                    String directDownloadURL = ParamUtil.getString(
140                            actionRequest, "directDownloadURL");
141                    boolean testDirectDownloadURL = ParamUtil.getBoolean(
142                            actionRequest, "testDirectDownloadURL");
143                    boolean repoStoreArtifact = ParamUtil.getBoolean(
144                            actionRequest, "repoStoreArtifact");
145    
146                    long[] frameworkVersionIds = ParamUtil.getLongValues(
147                            actionRequest, "frameworkVersions");
148    
149                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
150                            SCProductVersion.class.getName(), actionRequest);
151    
152                    if (productVersionId <= 0) {
153    
154                            // Add product version
155    
156                            SCProductVersionServiceUtil.addProductVersion(
157                                    productEntryId, version, changeLog, downloadPageURL,
158                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
159                                    frameworkVersionIds, serviceContext);
160                    }
161                    else {
162    
163                            // Update product version
164    
165                            SCProductVersionServiceUtil.updateProductVersion(
166                                    productVersionId, version, changeLog, downloadPageURL,
167                                    directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
168                                    frameworkVersionIds);
169                    }
170            }
171    
172    }