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