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