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