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.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
31  import com.liferay.portlet.imagegallery.ImageNameException;
32  import com.liferay.portlet.imagegallery.ImageSizeException;
33  import com.liferay.portlet.imagegallery.NoSuchFolderException;
34  import com.liferay.portlet.imagegallery.NoSuchImageException;
35  import com.liferay.portlet.imagegallery.model.IGImage;
36  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
37  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
38  import com.liferay.portlet.tags.TagsEntryException;
39  
40  import java.io.File;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   */
57  public class EditImageAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
68                  updateImage(actionRequest);
69              }
70              else if (cmd.equals(Constants.DELETE)) {
71                  deleteImage(actionRequest);
72              }
73  
74              sendRedirect(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchImageException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.image_gallery.error");
83              }
84              else if (e instanceof DuplicateImageNameException ||
85                       e instanceof ImageNameException ||
86                       e instanceof ImageSizeException ||
87                       e instanceof NoSuchFolderException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90              }
91              else if (e instanceof TagsEntryException) {
92                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
93              }
94              else {
95                  throw e;
96              }
97          }
98      }
99  
100     public ActionForward render(
101             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102             RenderRequest renderRequest, RenderResponse renderResponse)
103         throws Exception {
104 
105         try {
106             ActionUtil.getImage(renderRequest);
107         }
108         catch (Exception e) {
109             if (e instanceof NoSuchImageException ||
110                 e instanceof PrincipalException) {
111 
112                 SessionErrors.add(renderRequest, e.getClass().getName());
113 
114                 return mapping.findForward("portlet.image_gallery.error");
115             }
116             else {
117                 throw e;
118             }
119         }
120 
121         String forward = "portlet.image_gallery.edit_image";
122 
123         return mapping.findForward(getForward(renderRequest, forward));
124     }
125 
126     protected void deleteImage(ActionRequest actionRequest) throws Exception {
127         long imageId = ParamUtil.getLong(actionRequest, "imageId");
128 
129         IGImageServiceUtil.deleteImage(imageId);
130     }
131 
132     protected String getContentType(
133         UploadPortletRequest uploadRequest, File file) {
134 
135         String contentType = GetterUtil.getString(
136             uploadRequest.getContentType("file"));
137 
138         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
139             String ext = GetterUtil.getString(
140                 FileUtil.getExtension(file.getName())).toLowerCase();
141 
142             if (Validator.isNotNull(ext)) {
143                 contentType = MimeTypesUtil.getContentType(ext);
144             }
145         }
146 
147         return contentType;
148     }
149 
150     protected void updateImage(ActionRequest actionRequest) throws Exception {
151         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
152             actionRequest);
153 
154         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
155 
156         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
157         String name = ParamUtil.getString(uploadRequest, "name");
158         String fileName = uploadRequest.getFileName("file");
159         String description = ParamUtil.getString(
160             uploadRequest, "description", fileName);
161 
162         File file = uploadRequest.getFile("file");
163         String contentType = getContentType(uploadRequest, file);
164 
165         if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
166             String ext = GetterUtil.getString(
167                 FileUtil.getExtension(file.getName())).toLowerCase();
168 
169             if (Validator.isNotNull(ext)) {
170                 contentType = MimeTypesUtil.getContentType(ext);
171             }
172         }
173 
174         String[] tagsEntries = StringUtil.split(
175             ParamUtil.getString(uploadRequest, "tagsEntries"));
176 
177         String[] communityPermissions = PortalUtil.getCommunityPermissions(
178             actionRequest);
179         String[] guestPermissions = PortalUtil.getGuestPermissions(
180             actionRequest);
181 
182         if (imageId <= 0) {
183 
184             // Add image
185 
186             if (Validator.isNull(name)) {
187                 name = fileName;
188             }
189 
190             IGImage image = IGImageServiceUtil.addImage(
191                 folderId, name, description, file, contentType, tagsEntries,
192                 communityPermissions, guestPermissions);
193 
194             AssetPublisherUtil.addAndStoreSelection(
195                 actionRequest, IGImage.class.getName(), image.getImageId(), -1);
196         }
197         else {
198 
199             // Update image
200 
201             if (Validator.isNull(fileName)) {
202                 file = null;
203             }
204 
205             IGImageServiceUtil.updateImage(
206                 imageId, folderId, name, description, file, contentType,
207                 tagsEntries);
208         }
209 
210         AssetPublisherUtil.addRecentFolderId(
211             actionRequest, IGImage.class.getName(), folderId);
212     }
213 
214 }