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