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