1   /**
2    * Copyright (c) 2000-2009 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.ListUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Image;
34  import com.liferay.portal.model.Layout;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.service.ImageLocalServiceUtil;
37  import com.liferay.portal.struts.PortletAction;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.softwarecatalog.DuplicateProductEntryModuleIdException;
41  import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
42  import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
43  import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
44  import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
45  import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
46  import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
47  import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
48  import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
49  import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
50  import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
51  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
52  
53  import java.io.File;
54  
55  import java.util.ArrayList;
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  public class EditProductEntryAction extends PortletAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
82  
83          try {
84              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
85                  updateProductEntry(actionRequest);
86              }
87              else if (cmd.equals(Constants.DELETE)) {
88                  deleteProductEntry(actionRequest);
89              }
90  
91              if (Validator.isNotNull(cmd)) {
92                  sendRedirect(actionRequest, actionResponse);
93              }
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchProductEntryException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(actionRequest, e.getClass().getName());
100 
101                 setForward(actionRequest, "portlet.software_catalog.error");
102             }
103             else if (e instanceof DuplicateProductEntryModuleIdException ||
104                      e instanceof ProductEntryAuthorException ||
105                      e instanceof ProductEntryNameException ||
106                      e instanceof ProductEntryLicenseException ||
107                      e instanceof ProductEntryPageURLException ||
108                      e instanceof ProductEntryScreenshotsException ||
109                      e instanceof ProductEntryShortDescriptionException ||
110                      e instanceof ProductEntryTypeException) {
111 
112                 SessionErrors.add(actionRequest, e.getClass().getName());
113             }
114             else {
115                 throw e;
116             }
117         }
118     }
119 
120     public ActionForward render(
121             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
122             RenderRequest renderRequest, RenderResponse renderResponse)
123         throws Exception {
124 
125         try {
126             ActionUtil.getProductEntry(renderRequest);
127         }
128         catch (Exception e) {
129             if (e instanceof NoSuchProductEntryException ||
130                 e instanceof PrincipalException) {
131 
132                 SessionErrors.add(renderRequest, e.getClass().getName());
133 
134                 return mapping.findForward("portlet.software_catalog.error");
135             }
136             else {
137                 throw e;
138             }
139         }
140 
141         return mapping.findForward(getForward(
142             renderRequest, "portlet.software_catalog.edit_product_entry"));
143     }
144 
145     protected void deleteProductEntry(ActionRequest actionRequest)
146         throws Exception {
147 
148         long productEntryId = ParamUtil.getLong(
149             actionRequest, "productEntryId");
150 
151         SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
152     }
153 
154     protected List<byte[]> getFullImages(UploadPortletRequest uploadRequest)
155         throws Exception {
156 
157         return getImages(uploadRequest, "fullImage");
158     }
159 
160     protected List<byte[]> getImages(
161             UploadPortletRequest uploadRequest, String imagePrefix)
162         throws Exception {
163 
164         List<byte[]> images = new ArrayList<byte[]>();
165 
166         for (String name :
167                 getSortedParameterNames(uploadRequest, imagePrefix)) {
168 
169             int priority = GetterUtil.getInteger(
170                 name.substring(imagePrefix.length(), name.length()));
171 
172             File file = uploadRequest.getFile(name);
173             byte[] bytes = FileUtil.getBytes(file);
174 
175             boolean preserveScreenshot = ParamUtil.getBoolean(
176                 uploadRequest, "preserveScreenshot" + priority);
177 
178             if (preserveScreenshot) {
179                 SCProductScreenshot productScreenshot = getProductScreenshot(
180                     uploadRequest, priority);
181 
182                 Image image = null;
183 
184                 if (imagePrefix.equals("fullImage")) {
185                     image = ImageLocalServiceUtil.getImage(
186                         productScreenshot.getFullImageId());
187                 }
188                 else {
189                     image = ImageLocalServiceUtil.getImage(
190                         productScreenshot.getThumbnailId());
191                 }
192 
193                 bytes = image.getTextObj();
194             }
195 
196             if ((bytes != null) && (bytes.length > 0)) {
197                 images.add(bytes);
198             }
199             else {
200                 throw new ProductEntryScreenshotsException();
201             }
202         }
203 
204         return images;
205     }
206 
207     protected SCProductScreenshot getProductScreenshot(
208             UploadPortletRequest uploadRequest, int priority)
209         throws Exception {
210 
211         long productEntryId = ParamUtil.getLong(
212             uploadRequest, "productEntryId");
213 
214         try {
215             return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
216                 productEntryId, priority);
217         }
218         catch (Exception e) {
219             throw new ProductEntryScreenshotsException();
220         }
221     }
222 
223     protected List<String> getSortedParameterNames(
224             UploadPortletRequest uploadRequest, String imagePrefix)
225         throws Exception {
226 
227         List<String> parameterNames = new ArrayList<String>();
228 
229         Enumeration<String> enu = uploadRequest.getParameterNames();
230 
231         while (enu.hasMoreElements()) {
232             String name = enu.nextElement();
233 
234             if (name.startsWith(imagePrefix)) {
235                 parameterNames.add(name);
236             }
237         }
238 
239         return ListUtil.sort(parameterNames);
240     }
241 
242     protected List<byte[]> getThumbnails(UploadPortletRequest uploadRequest)
243         throws Exception {
244 
245         return getImages(uploadRequest, "thumbnail");
246     }
247 
248     protected void updateProductEntry(ActionRequest actionRequest)
249         throws Exception {
250 
251         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
252             actionRequest);
253 
254         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
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(uploadRequest);
275         List<byte[]> fullImages = getFullImages(uploadRequest);
276 
277         String[] communityPermissions = PortalUtil.getCommunityPermissions(
278             actionRequest);
279         String[] guestPermissions = PortalUtil.getGuestPermissions(
280             actionRequest);
281 
282         if (productEntryId <= 0) {
283 
284             // Add product entry
285 
286             SCProductEntryServiceUtil.addProductEntry(
287                 layout.getPlid(), name, type, tags, shortDescription,
288                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
289                 licenseIds, thumbnails, fullImages, communityPermissions,
290                 guestPermissions);
291         }
292         else {
293 
294             // Update product entry
295 
296             SCProductEntryServiceUtil.updateProductEntry(
297                 productEntryId, name, type, tags, shortDescription,
298                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
299                 licenseIds, thumbnails, fullImages);
300         }
301     }
302 
303 }