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.util.Constants;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Image;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.impl.ImageLocalUtil;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.util.UploadRequestUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.softwarecatalog.DuplicateProductEntryModuleIdException;
37  import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
38  import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
39  import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
40  import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
41  import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
42  import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
43  import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
44  import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
45  import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
46  import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
47  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
48  import com.liferay.util.FileUtil;
49  import com.liferay.util.servlet.SessionErrors;
50  import com.liferay.util.servlet.UploadPortletRequest;
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 config,
79              ActionRequest req, ActionResponse res)
80          throws Exception {
81  
82          String cmd = ParamUtil.getString(req, Constants.CMD);
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  updateProductEntry(req);
87              }
88              else if (cmd.equals(Constants.DELETE)) {
89                  deleteProductEntry(req);
90              }
91  
92              if (Validator.isNotNull(cmd)) {
93                  sendRedirect(req, res);
94              }
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchProductEntryException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(req, e.getClass().getName());
101 
102                 setForward(req, "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(req, e.getClass().getName());
114             }
115             else {
116                 throw e;
117             }
118         }
119     }
120 
121     public ActionForward render(
122             ActionMapping mapping, ActionForm form, PortletConfig config,
123             RenderRequest req, RenderResponse res)
124         throws Exception {
125 
126         try {
127             ActionUtil.getProductEntry(req);
128         }
129         catch (Exception e) {
130             if (e instanceof NoSuchProductEntryException ||
131                 e instanceof PrincipalException) {
132 
133                 SessionErrors.add(req, 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(
143             getForward(req, "portlet.software_catalog.edit_product_entry"));
144     }
145 
146     protected void deleteProductEntry(ActionRequest req) throws Exception {
147         long productEntryId = ParamUtil.getLong(req, "productEntryId");
148 
149         SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
150     }
151 
152     protected List<byte[]> getFullImages(UploadPortletRequest uploadReq)
153         throws Exception {
154 
155         return getImages(uploadReq, "fullImage");
156     }
157 
158     protected List<byte[]> getImages(
159             UploadPortletRequest uploadReq, String imagePrefix)
160         throws Exception {
161 
162         List<byte[]> images = new ArrayList<byte[]>();
163 
164         for (String name : getSortedParameterNames(uploadReq, imagePrefix)) {
165             int priority = GetterUtil.getInteger(
166                 name.substring(imagePrefix.length(), name.length()));
167 
168             File file = uploadReq.getFile(name);
169             byte[] bytes = FileUtil.getBytes(file);
170 
171             boolean preserveScreenshot = ParamUtil.getBoolean(
172                 uploadReq, "preserveScreenshot" + priority);
173 
174             if (preserveScreenshot) {
175                 SCProductScreenshot productScreenshot = getProductScreenshot(
176                     uploadReq, priority);
177 
178                 Image image = null;
179 
180                 if (imagePrefix.equals("fullImage")) {
181                     image = ImageLocalUtil.getImage(
182                         productScreenshot.getFullImageId());
183                 }
184                 else {
185                     image = ImageLocalUtil.getImage(
186                         productScreenshot.getThumbnailId());
187                 }
188 
189                 bytes = image.getTextObj();
190             }
191 
192             if ((bytes != null) && (bytes.length > 0)) {
193                 images.add(bytes);
194             }
195             else {
196                 throw new ProductEntryScreenshotsException();
197             }
198         }
199 
200         return images;
201     }
202 
203     protected SCProductScreenshot getProductScreenshot(
204             UploadPortletRequest uploadReq, int priority)
205         throws Exception {
206 
207         long productEntryId = ParamUtil.getLong(uploadReq, "productEntryId");
208 
209         try {
210             return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
211                 productEntryId, priority);
212         }
213         catch (Exception e) {
214             throw new ProductEntryScreenshotsException();
215         }
216     }
217 
218     protected List<String> getSortedParameterNames(
219             UploadPortletRequest uploadReq, String imagePrefix)
220         throws Exception {
221 
222         List<String> parameterNames = new ArrayList<String>();
223 
224         Enumeration<String> enu = uploadReq.getParameterNames();
225 
226         while (enu.hasMoreElements()) {
227             String name = enu.nextElement();
228 
229             if (name.startsWith(imagePrefix)) {
230                 parameterNames.add(name);
231             }
232         }
233 
234         Collections.sort(parameterNames);
235 
236         return parameterNames;
237     }
238 
239     protected List<byte[]> getThumbnails(UploadPortletRequest uploadReq)
240         throws Exception {
241 
242         return getImages(uploadReq, "thumbnail");
243     }
244 
245     protected void updateProductEntry(ActionRequest req) throws Exception {
246         UploadPortletRequest uploadReq =
247             UploadRequestUtil.getUploadPortletRequest(req);
248 
249         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
250 
251         long productEntryId = ParamUtil.getLong(req, "productEntryId");
252 
253         String name = ParamUtil.getString(req, "name");
254         String type = ParamUtil.getString(req, "type");
255         String tags = ParamUtil.getString(req, "tags");
256         String shortDescription = ParamUtil.getString(req, "shortDescription");
257         String longDescription = ParamUtil.getString(req, "longDescription");
258         String pageURL = ParamUtil.getString(req, "pageURL");
259         String author = ParamUtil.getString(req, "author");
260         String repoGroupId = ParamUtil.getString(req, "repoGroupId");
261         String repoArtifactId = ParamUtil.getString(req, "repoArtifactId");
262 
263         long[] licenseIds = ParamUtil.getLongValues(req, "licenses");
264 
265         List<byte[]> thumbnails = getThumbnails(uploadReq);
266         List<byte[]> fullImages = getFullImages(uploadReq);
267 
268         String[] communityPermissions = req.getParameterValues(
269             "communityPermissions");
270         String[] guestPermissions = req.getParameterValues(
271             "guestPermissions");
272 
273         if (productEntryId <= 0) {
274 
275             // Add product entry
276 
277             SCProductEntryServiceUtil.addProductEntry(
278                 layout.getPlid(), name, type, tags, shortDescription,
279                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
280                 licenseIds, thumbnails, fullImages, communityPermissions,
281                 guestPermissions);
282         }
283         else {
284 
285             // Update product entry
286 
287             SCProductEntryServiceUtil.updateProductEntry(
288                 productEntryId, name, type, tags, shortDescription,
289                 longDescription, pageURL, author, repoGroupId, repoArtifactId,
290                 licenseIds, thumbnails, fullImages);
291         }
292     }
293 
294 }