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.plugin.Version;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
023    import com.liferay.portlet.softwarecatalog.model.SCLicense;
024    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
025    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
026    import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionServiceUtil;
027    import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
028    import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
029    import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
030    
031    import javax.portlet.PortletRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Jorge Ferrer
037     */
038    public class ActionUtil {
039    
040            public static void getFrameworkVersion(HttpServletRequest request)
041                    throws Exception {
042    
043                    long frameworkVersionId = ParamUtil.getLong(
044                            request, "frameworkVersionId");
045    
046                    SCFrameworkVersion frameworkVersion = null;
047    
048                    if (frameworkVersionId > 0) {
049                            frameworkVersion =
050                                    SCFrameworkVersionServiceUtil.getFrameworkVersion(
051                                            frameworkVersionId);
052                    }
053    
054                    request.setAttribute(
055                            WebKeys.SOFTWARE_CATALOG_FRAMEWORK_VERSION, frameworkVersion);
056            }
057    
058            public static void getFrameworkVersion(PortletRequest portletRequest)
059                    throws Exception {
060    
061                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
062                            portletRequest);
063    
064                    getFrameworkVersion(request);
065            }
066    
067            public static void getLicense(HttpServletRequest request) throws Exception {
068                    long licenseId = ParamUtil.getLong(request, "licenseId");
069    
070                    SCLicense license = null;
071    
072                    if (licenseId > 0) {
073                            license = SCLicenseServiceUtil.getLicense(licenseId);
074                    }
075    
076                    request.setAttribute(WebKeys.SOFTWARE_CATALOG_LICENSE, license);
077            }
078    
079            public static void getLicense(PortletRequest portletRequest)
080                    throws Exception {
081    
082                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
083                            portletRequest);
084    
085                    getLicense(request);
086            }
087    
088            public static void getProductEntry(HttpServletRequest request)
089                    throws Exception {
090    
091                    long productEntryId = ParamUtil.getLong(request, "productEntryId");
092    
093                    SCProductEntry productEntry = null;
094    
095                    if (productEntryId > 0) {
096                            productEntry = SCProductEntryServiceUtil.getProductEntry(
097                                    productEntryId);
098                    }
099    
100                    request.setAttribute(
101                            WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
102            }
103    
104            public static void getProductEntry(PortletRequest portletRequest)
105                    throws Exception {
106    
107                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
108                            portletRequest);
109    
110                    getProductEntry(request);
111            }
112    
113            public static void getProductVersion(HttpServletRequest request)
114                    throws Exception {
115    
116                    long productVersionId = ParamUtil.getLong(request, "productVersionId");
117                    long copyProductVersionId = ParamUtil.getLong(
118                            request, "copyProductVersionId");
119    
120                    SCProductVersion productVersion = null;
121                    SCProductEntry productEntry = null;
122    
123                    if (productVersionId > 0) {
124                            productVersion = SCProductVersionServiceUtil.getProductVersion(
125                                    productVersionId);
126    
127                            productEntry = SCProductEntryServiceUtil.getProductEntry(
128                                    productVersion.getProductEntryId());
129    
130                            request.setAttribute(
131                                    WebKeys.SOFTWARE_CATALOG_PRODUCT_VERSION, productVersion);
132    
133                            request.setAttribute(
134                                    WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
135                    }
136                    else if (copyProductVersionId > 0) {
137                            productVersion = SCProductVersionServiceUtil.getProductVersion(
138                                    copyProductVersionId);
139    
140                            productEntry = SCProductEntryServiceUtil.getProductEntry(
141                                    productVersion.getProductEntryId());
142    
143                            String oldVersion = productVersion.getVersion();
144    
145                            Version version = Version.getInstance(oldVersion);
146    
147                            version = Version.incrementBuildNumber(version);
148    
149                            String newVersion = version.toString();
150    
151                            productVersion.setVersion(newVersion);
152    
153                            String directDownloadURL = productVersion.getDirectDownloadURL();
154    
155                            directDownloadURL = StringUtil.replace(
156                                    directDownloadURL, oldVersion, newVersion);
157    
158                            productVersion.setDirectDownloadURL(directDownloadURL);
159    
160                            request.setAttribute(
161                                    WebKeys.SOFTWARE_CATALOG_PRODUCT_VERSION, productVersion);
162    
163                            request.setAttribute(
164                                    WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
165                    }
166                    else {
167                            getProductEntry(request);
168                    }
169            }
170    
171            public static void getProductVersion(PortletRequest portletRequest)
172                    throws Exception {
173    
174                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
175                            portletRequest);
176    
177                    getProductVersion(request);
178            }
179    
180    }