1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.softwarecatalog.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.upload.UploadPortletRequest;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Image;
33  import com.liferay.portal.model.Layout;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.service.ImageLocalServiceUtil;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.softwarecatalog.DuplicateProductEntryModuleIdException;
40  import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
41  import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
42  import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
43  import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
44  import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
45  import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
46  import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
47  import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
48  import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
49  import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
50  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
51  
52  import java.io.File;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.Enumeration;
57  import java.util.List;
58  
59  import javax.portlet.ActionRequest;
60  import javax.portlet.ActionResponse;
61  import javax.portlet.PortletConfig;
62  import javax.portlet.RenderRequest;
63  import javax.portlet.RenderResponse;
64  
65  import org.apache.struts.action.ActionForm;
66  import org.apache.struts.action.ActionForward;
67  import org.apache.struts.action.ActionMapping;
68  
69  /**
70   * <a href="EditProductEntryAction.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Jorge Ferrer
73   *
74   */
75  public class EditProductEntryAction extends PortletAction {
76  
77      public void processAction(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              ActionRequest actionRequest, ActionResponse actionResponse)
80          throws Exception {
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  updateProductEntry(actionRequest);
87              }
88              else if (cmd.equals(Constants.DELETE)) {
89                  deleteProductEntry(actionRequest);
90              }
91  
92              if (Validator.isNotNull(cmd)) {
93                  sendRedirect(actionRequest, actionResponse);
94              }
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchProductEntryException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(actionRequest, e.getClass().getName());
101 
102                 setForward(actionRequest, "portlet.software_catalog.error");
103             }
104             else if (e instanceof DuplicateProductEntryModuleIdException ||
105                      e instanceof ProductEntryAuthorException ||
106                      e instanceof ProductEntryNameException ||
107                      e instanceof ProductEntryLicenseException ||
108                      e instanceof ProductEntryPageURLException ||
109                      e instanceof ProductEntryScreenshotsException ||
110                      e instanceof ProductEntryShortDescriptionException ||
111                      e instanceof ProductEntryTypeException) {
112 
113                 SessionErrors.add(actionRequest, e.getClass().getName());
114             }
115             else {
116                 throw e;
117             }
118         }
119     }
120 
121     public ActionForward render(
122             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
123             RenderRequest renderRequest, RenderResponse renderResponse)
124         throws Exception {
125 
126         try {
127             ActionUtil.getProductEntry(renderRequest);
128         }
129         catch (Exception e) {
130             if (e instanceof NoSuchProductEntryException ||
131                 e instanceof PrincipalException) {
132 
133                 SessionErrors.add(renderRequest, e.getClass().getName());
134 
135                 return mapping.findForward("portlet.software_catalog.error");
136             }
137             else {
138                 throw e;
139             }
140         }
141 
142         return mapping.findForward(getForward(
143             renderRequest, "portlet.software_catalog.edit_product_entry"));
144     }
145 
146     protected void deleteProductEntry(ActionRequest actionRequest)
147         throws Exception {
148 
149         long productEntryId = ParamUtil.getLong(
150             actionRequest, "productEntryId");
151 
152         SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
153     }
154 
155     protected List<byte[]> getFullImages(UploadPortletRequest uploadRequest)
156         throws Exception {
157 
158         return getImages(uploadRequest, "fullImage");
159     }
160 
161     protected List<byte[]> getImages(
162             UploadPortletRequest uploadRequest, String imagePrefix)
163         throws Exception {
164 
165         List<byte[]> images = new ArrayList<byte[]>();
166 
167         for (String name :
168                 getSortedParameterNames(uploadRequest, imagePrefix)) {
169 
170             int priority = GetterUtil.getInteger(
171                 name.substring(imagePrefix.length(), name.length()));
172 
173             File file = uploadRequest.getFile(name);
174             byte[] bytes = FileUtil.getBytes(file);
175 
176             boolean preserveScreenshot = ParamUtil.getBoolean(
177                 uploadRequest, "preserveScreenshot" + priority);
178 
179             if (preserveScreenshot) {
180                 SCProductScreenshot productScreenshot = getProductScreenshot(
181                     uploadRequest, 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 
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 uploadRequest, int priority)
210         throws Exception {
211 
212         long productEntryId = ParamUtil.getLong(
213             uploadRequest, "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 uploadRequest, String imagePrefix)
226         throws Exception {
227 
228         List<String> parameterNames = new ArrayList<String>();
229 
230         Enumeration<String> enu = uploadRequest.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         Collections.sort(parameterNames);
241 
242         return parameterNames;
243     }
244 
245     protected List<byte[]> getThumbnails(UploadPortletRequest uploadRequest)
246         throws Exception {
247 
248         return getImages(uploadRequest, "thumbnail");
249     }
250 
251     protected void updateProductEntry(ActionRequest actionRequest)
252         throws Exception {
253 
254         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
255             actionRequest);
256 
257         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
258 
259         long productEntryId = ParamUtil.getLong(
260             actionRequest, "productEntryId");
261 
262         String name = ParamUtil.getString(actionRequest, "name");
263         String type = ParamUtil.getString(actionRequest, "type");
264         String tags = ParamUtil.getString(actionRequest, "tags");
265         String shortDescription = ParamUtil.getString(
266             actionRequest, "shortDescription");
267         String longDescription = ParamUtil.getString(
268             actionRequest, "longDescription");
269         String pageURL = ParamUtil.getString(actionRequest, "pageURL");
270         String author = ParamUtil.getString(actionRequest, "author");
271         String repoGroupId = ParamUtil.getString(actionRequest, "repoGroupId");
272         String repoArtifactId = ParamUtil.getString(
273             actionRequest, "repoArtifactId");
274 
275         long[] licenseIds = ParamUtil.getLongValues(actionRequest, "licenses");
276 
277         List<byte[]> thumbnails = getThumbnails(uploadRequest);
278         List<byte[]> fullImages = getFullImages(uploadRequest);
279 
280         String[] communityPermissions = actionRequest.getParameterValues(
281             "communityPermissions");
282         String[] guestPermissions = actionRequest.getParameterValues(
283             "guestPermissions");
284 
285         if (productEntryId <= 0) {
286 
287             // Add product entry
288 
289             SCProductEntryServiceUtil.addProductEntry(
290                 layout.getPlid(), name, type, tags, shortDescription,
291                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
292                 licenseIds, thumbnails, fullImages, communityPermissions,
293                 guestPermissions);
294         }
295         else {
296 
297             // Update product entry
298 
299             SCProductEntryServiceUtil.updateProductEntry(
300                 productEntryId, name, type, tags, shortDescription,
301                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
302                 licenseIds, thumbnails, fullImages);
303         }
304     }
305 
306 }