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