001    /**
002     * Copyright (c) 2000-present 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.portlet.bridges.mvc.BaseMVCActionCommand;
018    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
019    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
020    import com.liferay.portal.kernel.upload.UploadPortletRequest;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.MimeTypesUtil;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.model.Image;
029    import com.liferay.portal.service.ImageLocalServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.PortletKeys;
034    import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
035    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
036    import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
037    import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
038    import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
039    
040    import java.io.InputStream;
041    
042    import java.util.ArrayList;
043    import java.util.Enumeration;
044    import java.util.List;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    
049    /**
050     * @author Jorge Ferrer
051     * @author Philip Jones
052     */
053    @OSGiBeanProperties(
054            property = {
055                    "javax.portlet.name=" + PortletKeys.SOFTWARE_CATALOG,
056                    "mvc.command.name=/software_catalog/edit_product_entry"
057            },
058            service = MVCActionCommand.class
059    )
060    public class EditProductEntryMVCActionCommand extends BaseMVCActionCommand {
061    
062            @Override
063            public void doProcessAction(
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
070                            updateProductEntry(actionRequest);
071                    }
072                    else if (cmd.equals(Constants.DELETE)) {
073                            deleteProductEntry(actionRequest);
074                    }
075    
076                    sendRedirect(actionRequest, actionResponse);
077            }
078    
079            protected void deleteProductEntry(ActionRequest actionRequest)
080                    throws Exception {
081    
082                    long productEntryId = ParamUtil.getLong(
083                            actionRequest, "productEntryId");
084    
085                    SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
086            }
087    
088            protected List<byte[]> getFullImages(
089                            UploadPortletRequest uploadPortletRequest)
090                    throws Exception {
091    
092                    return getImages(uploadPortletRequest, "fullImage");
093            }
094    
095            protected List<byte[]> getImages(
096                            UploadPortletRequest uploadPortletRequest, String imagePrefix)
097                    throws Exception {
098    
099                    List<byte[]> images = new ArrayList<>();
100    
101                    for (String name :
102                                    getSortedParameterNames(uploadPortletRequest, imagePrefix)) {
103    
104                            String contentType = uploadPortletRequest.getContentType(name);
105    
106                            if (!MimeTypesUtil.isWebImage(contentType)) {
107                                    throw new ProductEntryScreenshotsException();
108                            }
109    
110                            int priority = GetterUtil.getInteger(
111                                    name.substring(imagePrefix.length()));
112    
113                            boolean preserveScreenshot = ParamUtil.getBoolean(
114                                    uploadPortletRequest, "preserveScreenshot" + priority);
115    
116                            byte[] bytes = null;
117    
118                            if (preserveScreenshot) {
119                                    SCProductScreenshot productScreenshot = getProductScreenshot(
120                                            uploadPortletRequest, priority);
121    
122                                    Image image = null;
123    
124                                    if (imagePrefix.equals("fullImage")) {
125                                            image = ImageLocalServiceUtil.getImage(
126                                                    productScreenshot.getFullImageId());
127                                    }
128                                    else {
129                                            image = ImageLocalServiceUtil.getImage(
130                                                    productScreenshot.getThumbnailId());
131                                    }
132    
133                                    bytes = image.getTextObj();
134                            }
135                            else {
136                                    InputStream inputStream = uploadPortletRequest.getFileAsStream(
137                                            name);
138    
139                                    if (inputStream != null) {
140                                            bytes = FileUtil.getBytes(inputStream);
141                                    }
142                            }
143    
144                            if (ArrayUtil.isNotEmpty(bytes)) {
145                                    images.add(bytes);
146                            }
147                            else {
148                                    throw new ProductEntryScreenshotsException();
149                            }
150                    }
151    
152                    return images;
153            }
154    
155            protected SCProductScreenshot getProductScreenshot(
156                            UploadPortletRequest uploadPortletRequest, int priority)
157                    throws Exception {
158    
159                    long productEntryId = ParamUtil.getLong(
160                            uploadPortletRequest, "productEntryId");
161    
162                    try {
163                            return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
164                                    productEntryId, priority);
165                    }
166                    catch (Exception e) {
167                            throw new ProductEntryScreenshotsException();
168                    }
169            }
170    
171            protected List<String> getSortedParameterNames(
172                            UploadPortletRequest uploadPortletRequest, String imagePrefix)
173                    throws Exception {
174    
175                    List<String> parameterNames = new ArrayList<>();
176    
177                    Enumeration<String> enu = uploadPortletRequest.getParameterNames();
178    
179                    while (enu.hasMoreElements()) {
180                            String name = enu.nextElement();
181    
182                            if (name.startsWith(imagePrefix)) {
183                                    parameterNames.add(name);
184                            }
185                    }
186    
187                    return ListUtil.sort(parameterNames);
188            }
189    
190            protected List<byte[]> getThumbnails(
191                            UploadPortletRequest uploadPortletRequest)
192                    throws Exception {
193    
194                    return getImages(uploadPortletRequest, "thumbnail");
195            }
196    
197            protected void updateProductEntry(ActionRequest actionRequest)
198                    throws Exception {
199    
200                    UploadPortletRequest uploadPortletRequest =
201                            PortalUtil.getUploadPortletRequest(actionRequest);
202    
203                    long productEntryId = ParamUtil.getLong(
204                            actionRequest, "productEntryId");
205    
206                    String name = ParamUtil.getString(actionRequest, "name");
207                    String type = ParamUtil.getString(actionRequest, "type");
208                    String tags = ParamUtil.getString(actionRequest, "tags");
209                    String shortDescription = ParamUtil.getString(
210                            actionRequest, "shortDescription");
211                    String longDescription = ParamUtil.getString(
212                            actionRequest, "longDescription");
213                    String pageURL = ParamUtil.getString(actionRequest, "pageURL");
214                    String author = ParamUtil.getString(actionRequest, "author");
215                    String repoGroupId = ParamUtil.getString(actionRequest, "repoGroupId");
216                    String repoArtifactId = ParamUtil.getString(
217                            actionRequest, "repoArtifactId");
218    
219                    long[] licenseIds = ParamUtil.getLongValues(actionRequest, "licenses");
220    
221                    List<byte[]> thumbnails = getThumbnails(uploadPortletRequest);
222                    List<byte[]> fullImages = getFullImages(uploadPortletRequest);
223    
224                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
225                            SCProductEntry.class.getName(), actionRequest);
226    
227                    if (productEntryId <= 0) {
228    
229                            // Add product entry
230    
231                            SCProductEntryServiceUtil.addProductEntry(
232                                    name, type, tags, shortDescription, longDescription, pageURL,
233                                    author, repoGroupId, repoArtifactId, licenseIds, thumbnails,
234                                    fullImages, serviceContext);
235                    }
236                    else {
237    
238                            // Update product entry
239    
240                            SCProductEntryServiceUtil.updateProductEntry(
241                                    productEntryId, name, type, tags, shortDescription,
242                                    longDescription, pageURL, author, repoGroupId, repoArtifactId,
243                                    licenseIds, thumbnails, fullImages);
244                    }
245            }
246    
247    }