001    /**
002     * Copyright (c) 2000-2013 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.upload.UploadPortletRequest;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.MimeTypesUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Image;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.ImageLocalServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.ServiceContextFactory;
031    import com.liferay.portal.struts.PortletAction;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portlet.softwarecatalog.DuplicateProductEntryModuleIdException;
034    import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
035    import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
036    import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
037    import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
038    import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
039    import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
040    import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
041    import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
042    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
043    import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
044    import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
045    import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
046    
047    import java.io.InputStream;
048    
049    import java.util.ArrayList;
050    import java.util.Enumeration;
051    import java.util.List;
052    
053    import javax.portlet.ActionRequest;
054    import javax.portlet.ActionResponse;
055    import javax.portlet.PortletConfig;
056    import javax.portlet.RenderRequest;
057    import javax.portlet.RenderResponse;
058    
059    import org.apache.struts.action.ActionForm;
060    import org.apache.struts.action.ActionForward;
061    import org.apache.struts.action.ActionMapping;
062    
063    /**
064     * @author Jorge Ferrer
065     */
066    public class EditProductEntryAction extends PortletAction {
067    
068            @Override
069            public void processAction(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            PortletConfig portletConfig, ActionRequest actionRequest,
072                            ActionResponse actionResponse)
073                    throws Exception {
074    
075                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
076    
077                    try {
078                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
079                                    updateProductEntry(actionRequest);
080                            }
081                            else if (cmd.equals(Constants.DELETE)) {
082                                    deleteProductEntry(actionRequest);
083                            }
084    
085                            if (Validator.isNotNull(cmd)) {
086                                    sendRedirect(actionRequest, actionResponse);
087                            }
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchProductEntryException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(actionRequest, e.getClass());
094    
095                                    setForward(actionRequest, "portlet.software_catalog.error");
096                            }
097                            else if (e instanceof DuplicateProductEntryModuleIdException ||
098                                             e instanceof ProductEntryAuthorException ||
099                                             e instanceof ProductEntryNameException ||
100                                             e instanceof ProductEntryLicenseException ||
101                                             e instanceof ProductEntryPageURLException ||
102                                             e instanceof ProductEntryScreenshotsException ||
103                                             e instanceof ProductEntryShortDescriptionException ||
104                                             e instanceof ProductEntryTypeException) {
105    
106                                    SessionErrors.add(actionRequest, e.getClass());
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112            }
113    
114            @Override
115            public ActionForward render(
116                            ActionMapping actionMapping, ActionForm actionForm,
117                            PortletConfig portletConfig, RenderRequest renderRequest,
118                            RenderResponse renderResponse)
119                    throws Exception {
120    
121                    try {
122                            ActionUtil.getProductEntry(renderRequest);
123                    }
124                    catch (Exception e) {
125                            if (e instanceof NoSuchProductEntryException ||
126                                    e instanceof PrincipalException) {
127    
128                                    SessionErrors.add(renderRequest, e.getClass());
129    
130                                    return actionMapping.findForward(
131                                            "portlet.software_catalog.error");
132                            }
133                            else {
134                                    throw e;
135                            }
136                    }
137    
138                    return actionMapping.findForward(
139                            getForward(
140                                    renderRequest, "portlet.software_catalog.edit_product_entry"));
141            }
142    
143            protected void deleteProductEntry(ActionRequest actionRequest)
144                    throws Exception {
145    
146                    long productEntryId = ParamUtil.getLong(
147                            actionRequest, "productEntryId");
148    
149                    SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
150            }
151    
152            protected List<byte[]> getFullImages(
153                            UploadPortletRequest uploadPortletRequest)
154                    throws Exception {
155    
156                    return getImages(uploadPortletRequest, "fullImage");
157            }
158    
159            protected List<byte[]> getImages(
160                            UploadPortletRequest uploadPortletRequest, String imagePrefix)
161                    throws Exception {
162    
163                    List<byte[]> images = new ArrayList<byte[]>();
164    
165                    for (String name :
166                                    getSortedParameterNames(uploadPortletRequest, imagePrefix)) {
167    
168                            String contentType = uploadPortletRequest.getContentType(name);
169    
170                            if (!MimeTypesUtil.isWebImage(contentType)) {
171                                    throw new ProductEntryScreenshotsException();
172                            }
173    
174                            int priority = GetterUtil.getInteger(
175                                    name.substring(imagePrefix.length()));
176    
177                            boolean preserveScreenshot = ParamUtil.getBoolean(
178                                    uploadPortletRequest, "preserveScreenshot" + priority);
179    
180                            byte[] bytes = null;
181    
182                            if (preserveScreenshot) {
183                                    SCProductScreenshot productScreenshot = getProductScreenshot(
184                                            uploadPortletRequest, priority);
185    
186                                    Image image = null;
187    
188                                    if (imagePrefix.equals("fullImage")) {
189                                            image = ImageLocalServiceUtil.getImage(
190                                                    productScreenshot.getFullImageId());
191                                    }
192                                    else {
193                                            image = ImageLocalServiceUtil.getImage(
194                                                    productScreenshot.getThumbnailId());
195                                    }
196    
197                                    bytes = image.getTextObj();
198                            }
199                            else {
200                                    InputStream inputStream = uploadPortletRequest.getFileAsStream(
201                                            name);
202    
203                                    if (inputStream != null) {
204                                            bytes = FileUtil.getBytes(inputStream);
205                                    }
206                            }
207    
208                            if ((bytes != null) && (bytes.length > 0)) {
209                                    images.add(bytes);
210                            }
211                            else {
212                                    throw new ProductEntryScreenshotsException();
213                            }
214                    }
215    
216                    return images;
217            }
218    
219            protected SCProductScreenshot getProductScreenshot(
220                            UploadPortletRequest uploadPortletRequest, int priority)
221                    throws Exception {
222    
223                    long productEntryId = ParamUtil.getLong(
224                            uploadPortletRequest, "productEntryId");
225    
226                    try {
227                            return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
228                                    productEntryId, priority);
229                    }
230                    catch (Exception e) {
231                            throw new ProductEntryScreenshotsException();
232                    }
233            }
234    
235            protected List<String> getSortedParameterNames(
236                            UploadPortletRequest uploadPortletRequest, String imagePrefix)
237                    throws Exception {
238    
239                    List<String> parameterNames = new ArrayList<String>();
240    
241                    Enumeration<String> enu = uploadPortletRequest.getParameterNames();
242    
243                    while (enu.hasMoreElements()) {
244                            String name = enu.nextElement();
245    
246                            if (name.startsWith(imagePrefix)) {
247                                    parameterNames.add(name);
248                            }
249                    }
250    
251                    return ListUtil.sort(parameterNames);
252            }
253    
254            protected List<byte[]> getThumbnails(
255                            UploadPortletRequest uploadPortletRequest)
256                    throws Exception {
257    
258                    return getImages(uploadPortletRequest, "thumbnail");
259            }
260    
261            protected void updateProductEntry(ActionRequest actionRequest)
262                    throws Exception {
263    
264                    UploadPortletRequest uploadPortletRequest =
265                            PortalUtil.getUploadPortletRequest(actionRequest);
266    
267                    long productEntryId = ParamUtil.getLong(
268                            actionRequest, "productEntryId");
269    
270                    String name = ParamUtil.getString(actionRequest, "name");
271                    String type = ParamUtil.getString(actionRequest, "type");
272                    String tags = ParamUtil.getString(actionRequest, "tags");
273                    String shortDescription = ParamUtil.getString(
274                            actionRequest, "shortDescription");
275                    String longDescription = ParamUtil.getString(
276                            actionRequest, "longDescription");
277                    String pageURL = ParamUtil.getString(actionRequest, "pageURL");
278                    String author = ParamUtil.getString(actionRequest, "author");
279                    String repoGroupId = ParamUtil.getString(actionRequest, "repoGroupId");
280                    String repoArtifactId = ParamUtil.getString(
281                            actionRequest, "repoArtifactId");
282    
283                    long[] licenseIds = ParamUtil.getLongValues(actionRequest, "licenses");
284    
285                    List<byte[]> thumbnails = getThumbnails(uploadPortletRequest);
286                    List<byte[]> fullImages = getFullImages(uploadPortletRequest);
287    
288                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
289                            SCProductEntry.class.getName(), actionRequest);
290    
291                    if (productEntryId <= 0) {
292    
293                            // Add product entry
294    
295                            SCProductEntryServiceUtil.addProductEntry(
296                                    name, type, tags, shortDescription, longDescription, pageURL,
297                                    author, repoGroupId, repoArtifactId, licenseIds, thumbnails,
298                                    fullImages, serviceContext);
299                    }
300                    else {
301    
302                            // Update product entry
303    
304                            SCProductEntryServiceUtil.updateProductEntry(
305                                    productEntryId, name, type, tags, shortDescription,
306                                    longDescription, pageURL, author, repoGroupId, repoArtifactId,
307                                    licenseIds, thumbnails, fullImages);
308                    }
309            }
310    
311    }