1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.MimeTypesUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.service.ServiceContextFactory;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
32  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
33  import com.liferay.portlet.imagegallery.ImageNameException;
34  import com.liferay.portlet.imagegallery.ImageSizeException;
35  import com.liferay.portlet.imagegallery.NoSuchFolderException;
36  import com.liferay.portlet.imagegallery.NoSuchImageException;
37  import com.liferay.portlet.imagegallery.model.IGImage;
38  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
39  import com.liferay.portlet.tags.TagsEntryException;
40  
41  import java.io.File;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   */
58  public class EditImageAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateImage(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteImage(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchImageException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.image_gallery.error");
84              }
85              else if (e instanceof DuplicateImageNameException ||
86                       e instanceof ImageNameException ||
87                       e instanceof ImageSizeException ||
88                       e instanceof NoSuchFolderException) {
89  
90                  SessionErrors.add(actionRequest, e.getClass().getName());
91              }
92              else if (e instanceof TagsEntryException) {
93                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
94              }
95              else {
96                  throw e;
97              }
98          }
99      }
100 
101     public ActionForward render(
102             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103             RenderRequest renderRequest, RenderResponse renderResponse)
104         throws Exception {
105 
106         try {
107             ActionUtil.getImage(renderRequest);
108         }
109         catch (Exception e) {
110             if (e instanceof NoSuchImageException ||
111                 e instanceof PrincipalException) {
112 
113                 SessionErrors.add(renderRequest, e.getClass().getName());
114 
115                 return mapping.findForward("portlet.image_gallery.error");
116             }
117             else {
118                 throw e;
119             }
120         }
121 
122         String forward = "portlet.image_gallery.edit_image";
123 
124         return mapping.findForward(getForward(renderRequest, forward));
125     }
126 
127     protected void deleteImage(ActionRequest actionRequest) throws Exception {
128         long imageId = ParamUtil.getLong(actionRequest, "imageId");
129 
130         IGImageServiceUtil.deleteImage(imageId);
131     }
132 
133     protected String getContentType(
134         UploadPortletRequest uploadRequest, File file) {
135 
136         String contentType = GetterUtil.getString(
137             uploadRequest.getContentType("file"));
138 
139         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
140             String ext = GetterUtil.getString(
141                 FileUtil.getExtension(file.getName())).toLowerCase();
142 
143             if (Validator.isNotNull(ext)) {
144                 contentType = MimeTypesUtil.getContentType(ext);
145             }
146         }
147 
148         return contentType;
149     }
150 
151     protected void updateImage(ActionRequest actionRequest) throws Exception {
152         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
153             actionRequest);
154 
155         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
156 
157         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
158         String name = ParamUtil.getString(uploadRequest, "name");
159         String fileName = uploadRequest.getFileName("file");
160         String description = ParamUtil.getString(
161             uploadRequest, "description", fileName);
162 
163         File file = uploadRequest.getFile("file");
164         String contentType = getContentType(uploadRequest, file);
165 
166         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
167             String ext = GetterUtil.getString(
168                 FileUtil.getExtension(file.getName())).toLowerCase();
169 
170             if (Validator.isNotNull(ext)) {
171                 contentType = MimeTypesUtil.getContentType(ext);
172             }
173         }
174 
175         ServiceContext serviceContext = ServiceContextFactory.getInstance(
176             IGImage.class.getName(), actionRequest);
177 
178         if (imageId <= 0) {
179 
180             // Add image
181 
182             if (Validator.isNull(name)) {
183                 name = fileName;
184             }
185 
186             IGImage image = IGImageServiceUtil.addImage(
187                 folderId, name, description, file, contentType, serviceContext);
188 
189             AssetPublisherUtil.addAndStoreSelection(
190                 actionRequest, IGImage.class.getName(), image.getImageId(), -1);
191         }
192         else {
193 
194             // Update image
195 
196             if (Validator.isNull(fileName)) {
197                 file = null;
198             }
199 
200             IGImageServiceUtil.updateImage(
201                 imageId, folderId, name, description, file, contentType,
202                 serviceContext);
203         }
204 
205         AssetPublisherUtil.addRecentFolderId(
206             actionRequest, IGImage.class.getName(), folderId);
207     }
208 
209 }