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.util.Constants;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.UploadRequestUtil;
34  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
35  import com.liferay.portlet.imagegallery.ImageNameException;
36  import com.liferay.portlet.imagegallery.ImageSizeException;
37  import com.liferay.portlet.imagegallery.NoSuchFolderException;
38  import com.liferay.portlet.imagegallery.NoSuchImageException;
39  import com.liferay.portlet.imagegallery.model.IGImage;
40  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
41  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
42  import com.liferay.portlet.tags.TagsEntryException;
43  import com.liferay.util.FileUtil;
44  import com.liferay.util.servlet.SessionErrors;
45  import com.liferay.util.servlet.UploadPortletRequest;
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 config,
69              ActionRequest req, ActionResponse res)
70          throws Exception {
71  
72          String cmd = ParamUtil.getString(req, Constants.CMD);
73  
74          try {
75              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
76                  updateImage(req);
77              }
78              else if (cmd.equals(Constants.DELETE)) {
79                  deleteImage(req);
80  
81                  sendRedirect(req, res);
82              }
83          }
84          catch (Exception e) {
85              if (e instanceof NoSuchImageException ||
86                  e instanceof PrincipalException) {
87  
88                  SessionErrors.add(req, e.getClass().getName());
89  
90                  setForward(req, "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(req, e.getClass().getName());
98              }
99              else if (e instanceof TagsEntryException) {
100                 SessionErrors.add(req, e.getClass().getName(), e);
101             }
102             else {
103                 throw e;
104             }
105         }
106     }
107 
108     public ActionForward render(
109             ActionMapping mapping, ActionForm form, PortletConfig config,
110             RenderRequest req, RenderResponse res)
111         throws Exception {
112 
113         try {
114             ActionUtil.getImage(req);
115         }
116         catch (Exception e) {
117             if (e instanceof NoSuchImageException ||
118                 e instanceof PrincipalException) {
119 
120                 SessionErrors.add(req, 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 (req.getWindowState().equals(LiferayWindowState.POP_UP)) {
132             forward = "portlet.image_gallery.edit_image_form";
133         }
134 
135         return mapping.findForward(getForward(req, forward));
136     }
137 
138     protected void deleteImage(ActionRequest req) throws Exception {
139         long imageId = ParamUtil.getLong(req, "imageId");
140 
141         IGImageServiceUtil.deleteImage(imageId);
142     }
143 
144     protected String getContentType(UploadPortletRequest uploadReq, File file) {
145         String contentType = GetterUtil.getString(
146             uploadReq.getContentType("file"));
147 
148         if (contentType.equals("application/octet-stream")) {
149             String ext = GetterUtil.getString(
150                 FileUtil.getExtension(file.getName())).toLowerCase();
151 
152             if (Validator.isNotNull(ext)) {
153                 if (ext.equals("jpg")) {
154                     ext = "jpeg";
155                 }
156                 else if (ext.equals("tif")) {
157                     ext = "tiff";
158                 }
159 
160                 contentType = "image/" + ext;
161             }
162         }
163 
164         return contentType;
165     }
166 
167     protected void updateImage(ActionRequest req) throws Exception {
168         UploadPortletRequest uploadReq =
169             UploadRequestUtil.getUploadPortletRequest(req);
170 
171         long imageId = ParamUtil.getLong(uploadReq, "imageId");
172 
173         long folderId = ParamUtil.getLong(uploadReq, "folderId");
174         String name = ParamUtil.getString(uploadReq, "name");
175         String fileName = uploadReq.getFileName("file");
176         String description = ParamUtil.getString(
177             uploadReq, "description", fileName);
178 
179         File file = uploadReq.getFile("file");
180         String contentType = getContentType(uploadReq, file);
181 
182         if (contentType.equals("application/octet-stream")) {
183             String ext = GetterUtil.getString(
184                 FileUtil.getExtension(file.getName())).toLowerCase();
185 
186             if (Validator.isNotNull(ext)) {
187                 if (ext.equals("jpg")) {
188                     ext = "jpeg";
189                 }
190                 else if (ext.equals("tif")) {
191                     ext = "tiff";
192                 }
193 
194                 contentType = "image/" + ext;
195             }
196         }
197 
198         String[] tagsEntries = StringUtil.split(
199             ParamUtil.getString(uploadReq, "tagsEntries"));
200 
201         String[] communityPermissions = req.getParameterValues(
202             "communityPermissions");
203         String[] guestPermissions = req.getParameterValues(
204             "guestPermissions");
205 
206         if (imageId <= 0) {
207 
208             // Add image
209 
210             if (Validator.isNull(name)) {
211                 name = fileName;
212             }
213 
214             IGImage image = IGImageServiceUtil.addImage(
215                 folderId, name, description, file, contentType, tagsEntries,
216                 communityPermissions, guestPermissions);
217 
218             AssetPublisherUtil.addAndStoreSelection(
219                 req, IGImage.class.getName(), image.getImageId(), -1);
220         }
221         else {
222 
223             // Update image
224 
225             if (Validator.isNull(fileName)) {
226                 file = null;
227             }
228 
229             IGImageServiceUtil.updateImage(
230                 imageId, folderId, name, description, file, contentType,
231                 tagsEntries);
232         }
233 
234         AssetPublisherUtil.addRecentFolderId(
235             req, IGImage.class.getName(), folderId);
236     }
237 
238 }