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