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.imagegallery.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.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.service.ServiceContextFactory;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.util.ContentTypeUtil;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
39  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
40  import com.liferay.portlet.imagegallery.ImageNameException;
41  import com.liferay.portlet.imagegallery.ImageSizeException;
42  import com.liferay.portlet.imagegallery.NoSuchFolderException;
43  import com.liferay.portlet.imagegallery.NoSuchImageException;
44  import com.liferay.portlet.imagegallery.model.IGImage;
45  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
46  import com.liferay.portlet.tags.TagsEntryException;
47  
48  import java.io.File;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.ActionResponse;
52  import javax.portlet.PortletConfig;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class EditImageAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
74  
75          try {
76              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
77                  updateImage(actionRequest);
78              }
79              else if (cmd.equals(Constants.DELETE)) {
80                  deleteImage(actionRequest);
81              }
82  
83              sendRedirect(actionRequest, actionResponse);
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchImageException ||
87                  e instanceof PrincipalException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90  
91                  setForward(actionRequest, "portlet.image_gallery.error");
92              }
93              else if (e instanceof DuplicateImageNameException ||
94                       e instanceof ImageNameException ||
95                       e instanceof ImageSizeException ||
96                       e instanceof NoSuchFolderException) {
97  
98                  SessionErrors.add(actionRequest, e.getClass().getName());
99              }
100             else if (e instanceof TagsEntryException) {
101                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
102             }
103             else {
104                 throw e;
105             }
106         }
107     }
108 
109     public ActionForward render(
110             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111             RenderRequest renderRequest, RenderResponse renderResponse)
112         throws Exception {
113 
114         try {
115             ActionUtil.getImage(renderRequest);
116         }
117         catch (Exception e) {
118             if (e instanceof NoSuchImageException ||
119                 e instanceof PrincipalException) {
120 
121                 SessionErrors.add(renderRequest, e.getClass().getName());
122 
123                 return mapping.findForward("portlet.image_gallery.error");
124             }
125             else {
126                 throw e;
127             }
128         }
129 
130         String forward = "portlet.image_gallery.edit_image";
131 
132         return mapping.findForward(getForward(renderRequest, forward));
133     }
134 
135     protected void deleteImage(ActionRequest actionRequest) throws Exception {
136         long imageId = ParamUtil.getLong(actionRequest, "imageId");
137 
138         IGImageServiceUtil.deleteImage(imageId);
139     }
140 
141     protected String getContentType(
142         UploadPortletRequest uploadRequest, File file) {
143 
144         String contentType = GetterUtil.getString(
145             uploadRequest.getContentType("file"));
146 
147         if (contentType.equals("application/octet-stream")) {
148             String ext = GetterUtil.getString(
149                 FileUtil.getExtension(file.getName())).toLowerCase();
150 
151             if (Validator.isNotNull(ext)) {
152                 contentType = ContentTypeUtil.getContentType(ext);
153             }
154         }
155 
156         return contentType;
157     }
158 
159     protected void updateImage(ActionRequest actionRequest) throws Exception {
160         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
161             actionRequest);
162 
163         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
164 
165         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
166         String name = ParamUtil.getString(uploadRequest, "name");
167         String fileName = uploadRequest.getFileName("file");
168         String description = ParamUtil.getString(
169             uploadRequest, "description", fileName);
170 
171         File file = uploadRequest.getFile("file");
172         String contentType = getContentType(uploadRequest, file);
173 
174         if (contentType.equals("application/octet-stream")) {
175             String ext = GetterUtil.getString(
176                 FileUtil.getExtension(file.getName())).toLowerCase();
177 
178             if (Validator.isNotNull(ext)) {
179                 contentType = ContentTypeUtil.getContentType(ext);
180             }
181         }
182 
183         ServiceContext serviceContext = ServiceContextFactory.getInstance(
184             IGImage.class.getName(), actionRequest);
185 
186         if (imageId <= 0) {
187 
188             // Add image
189 
190             if (Validator.isNull(name)) {
191                 name = fileName;
192             }
193 
194             IGImage image = IGImageServiceUtil.addImage(
195                 folderId, name, description, file, contentType, serviceContext);
196 
197             AssetPublisherUtil.addAndStoreSelection(
198                 actionRequest, IGImage.class.getName(), image.getImageId(), -1);
199         }
200         else {
201 
202             // Update image
203 
204             if (Validator.isNull(fileName)) {
205                 file = null;
206             }
207 
208             IGImageServiceUtil.updateImage(
209                 imageId, folderId, name, description, file, contentType,
210                 serviceContext);
211         }
212 
213         AssetPublisherUtil.addRecentFolderId(
214             actionRequest, IGImage.class.getName(), folderId);
215     }
216 
217 }