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.portlet.LiferayWindowState;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.upload.UploadPortletRequest;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.ServiceContext;
35  import com.liferay.portal.service.ServiceContextFactory;
36  import com.liferay.portal.struts.PortletAction;
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                  sendRedirect(actionRequest, actionResponse);
83              }
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         if (renderRequest.getWindowState().equals(LiferayWindowState.POP_UP)) {
133             forward = "portlet.image_gallery.edit_image_form";
134         }
135 
136         return mapping.findForward(getForward(renderRequest, forward));
137     }
138 
139     protected void deleteImage(ActionRequest actionRequest) throws Exception {
140         long imageId = ParamUtil.getLong(actionRequest, "imageId");
141 
142         IGImageServiceUtil.deleteImage(imageId);
143     }
144 
145     protected String getContentType(
146         UploadPortletRequest uploadRequest, File file) {
147 
148         String contentType = GetterUtil.getString(
149             uploadRequest.getContentType("file"));
150 
151         if (contentType.equals("application/octet-stream")) {
152             String ext = GetterUtil.getString(
153                 FileUtil.getExtension(file.getName())).toLowerCase();
154 
155             if (Validator.isNotNull(ext)) {
156                 if (ext.equals("jpg")) {
157                     ext = "jpeg";
158                 }
159                 else if (ext.equals("tif")) {
160                     ext = "tiff";
161                 }
162 
163                 contentType = "image/" + ext;
164             }
165         }
166 
167         return contentType;
168     }
169 
170     protected void updateImage(ActionRequest actionRequest) throws Exception {
171         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
172             actionRequest);
173 
174         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
175 
176         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
177         String name = ParamUtil.getString(uploadRequest, "name");
178         String fileName = uploadRequest.getFileName("file");
179         String description = ParamUtil.getString(
180             uploadRequest, "description", fileName);
181 
182         File file = uploadRequest.getFile("file");
183         String contentType = getContentType(uploadRequest, file);
184 
185         if (contentType.equals("application/octet-stream")) {
186             String ext = GetterUtil.getString(
187                 FileUtil.getExtension(file.getName())).toLowerCase();
188 
189             if (Validator.isNotNull(ext)) {
190                 if (ext.equals("jpg")) {
191                     ext = "jpeg";
192                 }
193                 else if (ext.equals("tif")) {
194                     ext = "tiff";
195                 }
196 
197                 contentType = "image/" + ext;
198             }
199         }
200 
201         ServiceContext serviceContext = ServiceContextFactory.getInstance(
202             IGImage.class.getName(), actionRequest);
203 
204         if (imageId <= 0) {
205 
206             // Add image
207 
208             if (Validator.isNull(name)) {
209                 name = fileName;
210             }
211 
212             IGImage image = IGImageServiceUtil.addImage(
213                 folderId, name, description, file, contentType, serviceContext);
214 
215             AssetPublisherUtil.addAndStoreSelection(
216                 actionRequest, IGImage.class.getName(), image.getImageId(), -1);
217         }
218         else {
219 
220             // Update image
221 
222             if (Validator.isNull(fileName)) {
223                 file = null;
224             }
225 
226             IGImageServiceUtil.updateImage(
227                 imageId, folderId, name, description, file, contentType,
228                 serviceContext);
229         }
230 
231         AssetPublisherUtil.addRecentFolderId(
232             actionRequest, IGImage.class.getName(), folderId);
233     }
234 
235 }