1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.imagegallery.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.MimeTypesUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.service.ServiceContextFactory;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.asset.AssetTagException;
34  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
35  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
36  import com.liferay.portlet.imagegallery.ImageNameException;
37  import com.liferay.portlet.imagegallery.ImageSizeException;
38  import com.liferay.portlet.imagegallery.NoSuchFolderException;
39  import com.liferay.portlet.imagegallery.NoSuchImageException;
40  import com.liferay.portlet.imagegallery.model.IGImage;
41  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
42  
43  import java.io.File;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   */
60  public class EditImageAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
68  
69          try {
70              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
71                  updateImage(actionRequest);
72              }
73              else if (cmd.equals(Constants.DELETE)) {
74                  deleteImage(actionRequest);
75              }
76  
77              sendRedirect(actionRequest, actionResponse);
78          }
79          catch (Exception e) {
80              if (e instanceof NoSuchImageException ||
81                  e instanceof PrincipalException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84  
85                  setForward(actionRequest, "portlet.image_gallery.error");
86              }
87              else if (e instanceof DuplicateImageNameException ||
88                       e instanceof ImageNameException ||
89                       e instanceof ImageSizeException ||
90                       e instanceof NoSuchFolderException) {
91  
92                  SessionErrors.add(actionRequest, e.getClass().getName());
93              }
94              else if (e instanceof AssetTagException) {
95                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         try {
109             ActionUtil.getImage(renderRequest);
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchImageException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(renderRequest, e.getClass().getName());
116 
117                 return mapping.findForward("portlet.image_gallery.error");
118             }
119             else {
120                 throw e;
121             }
122         }
123 
124         String forward = "portlet.image_gallery.edit_image";
125 
126         return mapping.findForward(getForward(renderRequest, forward));
127     }
128 
129     protected void deleteImage(ActionRequest actionRequest) throws Exception {
130         long imageId = ParamUtil.getLong(actionRequest, "imageId");
131 
132         IGImageServiceUtil.deleteImage(imageId);
133     }
134 
135     protected String getContentType(
136         UploadPortletRequest uploadRequest, File file) {
137 
138         String contentType = GetterUtil.getString(
139             uploadRequest.getContentType("file"));
140 
141         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
142             String ext = GetterUtil.getString(
143                 FileUtil.getExtension(file.getName())).toLowerCase();
144 
145             if (Validator.isNotNull(ext)) {
146                 contentType = MimeTypesUtil.getContentType(ext);
147             }
148         }
149 
150         return contentType;
151     }
152 
153     protected void updateImage(ActionRequest actionRequest) throws Exception {
154         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
155             actionRequest);
156 
157         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
158             WebKeys.THEME_DISPLAY);
159 
160         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
161 
162         long groupId = themeDisplay.getScopeGroupId();
163         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
164         String name = ParamUtil.getString(uploadRequest, "name");
165         String fileName = uploadRequest.getFileName("file");
166         String description = ParamUtil.getString(
167             uploadRequest, "description", fileName);
168 
169         File file = uploadRequest.getFile("file");
170         String contentType = getContentType(uploadRequest, file);
171 
172         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
173             String ext = GetterUtil.getString(
174                 FileUtil.getExtension(file.getName())).toLowerCase();
175 
176             if (Validator.isNotNull(ext)) {
177                 contentType = MimeTypesUtil.getContentType(ext);
178             }
179         }
180 
181         ServiceContext serviceContext = ServiceContextFactory.getInstance(
182             IGImage.class.getName(), actionRequest);
183 
184         if (imageId <= 0) {
185 
186             // Add image
187 
188             if (Validator.isNull(name)) {
189                 name = fileName;
190             }
191 
192             IGImage image = IGImageServiceUtil.addImage(
193                 groupId, folderId, name, description, file, contentType,
194                 serviceContext);
195 
196             AssetPublisherUtil.addAndStoreSelection(
197                 actionRequest, IGImage.class.getName(), image.getImageId(), -1);
198         }
199         else {
200 
201             // Update image
202 
203             if (Validator.isNull(fileName)) {
204                 file = null;
205             }
206 
207             IGImageServiceUtil.updateImage(
208                 imageId, groupId, folderId, name, description, file,
209                 contentType, serviceContext);
210         }
211 
212         AssetPublisherUtil.addRecentFolderId(
213             actionRequest, IGImage.class.getName(), folderId);
214     }
215 
216 }