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