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