1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.imagegallery.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.Constants;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.ContentTypeUtil;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
36  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
37  import com.liferay.portlet.imagegallery.ImageNameException;
38  import com.liferay.portlet.imagegallery.ImageSizeException;
39  import com.liferay.portlet.imagegallery.NoSuchFolderException;
40  import com.liferay.portlet.imagegallery.NoSuchImageException;
41  import com.liferay.portlet.imagegallery.model.IGImage;
42  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
43  import com.liferay.portlet.tags.TagsEntryException;
44  
45  import java.io.File;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class EditImageAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  updateImage(actionRequest);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteImage(actionRequest);
78              }
79  
80              sendRedirect(actionRequest, actionResponse);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchImageException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  setForward(actionRequest, "portlet.image_gallery.error");
89              }
90              else if (e instanceof DuplicateImageNameException ||
91                       e instanceof ImageNameException ||
92                       e instanceof ImageSizeException ||
93                       e instanceof NoSuchFolderException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96              }
97              else if (e instanceof TagsEntryException) {
98                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
99              }
100             else {
101                 throw e;
102             }
103         }
104     }
105 
106     public ActionForward render(
107             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
108             RenderRequest renderRequest, RenderResponse renderResponse)
109         throws Exception {
110 
111         try {
112             ActionUtil.getImage(renderRequest);
113         }
114         catch (Exception e) {
115             if (e instanceof NoSuchImageException ||
116                 e instanceof PrincipalException) {
117 
118                 SessionErrors.add(renderRequest, 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         return mapping.findForward(getForward(renderRequest, forward));
130     }
131 
132     protected void deleteImage(ActionRequest actionRequest) throws Exception {
133         long imageId = ParamUtil.getLong(actionRequest, "imageId");
134 
135         IGImageServiceUtil.deleteImage(imageId);
136     }
137 
138     protected String getContentType(
139         UploadPortletRequest uploadRequest, File file) {
140 
141         String contentType = GetterUtil.getString(
142             uploadRequest.getContentType("file"));
143 
144         if (contentType.equals("application/octet-stream")) {
145             String ext = GetterUtil.getString(
146                 FileUtil.getExtension(file.getName())).toLowerCase();
147 
148             if (Validator.isNotNull(ext)) {
149                 contentType = ContentTypeUtil.getContentType(ext);
150             }
151         }
152 
153         return contentType;
154     }
155 
156     protected void updateImage(ActionRequest actionRequest) throws Exception {
157         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
158             actionRequest);
159 
160         long imageId = ParamUtil.getLong(uploadRequest, "imageId");
161 
162         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
163         String name = ParamUtil.getString(uploadRequest, "name");
164         String fileName = uploadRequest.getFileName("file");
165         String description = ParamUtil.getString(
166             uploadRequest, "description", fileName);
167 
168         File file = uploadRequest.getFile("file");
169         String contentType = getContentType(uploadRequest, file);
170 
171         if (contentType.equals("application/octet-stream")) {
172             String ext = GetterUtil.getString(
173                 FileUtil.getExtension(file.getName())).toLowerCase();
174 
175             if (Validator.isNotNull(ext)) {
176                 contentType = ContentTypeUtil.getContentType(ext);
177             }
178         }
179 
180         ServiceContext serviceContext = ServiceContextFactory.getInstance(
181             IGImage.class.getName(), actionRequest);
182 
183         if (imageId <= 0) {
184 
185             // Add image
186 
187             if (Validator.isNull(name)) {
188                 name = fileName;
189             }
190 
191             IGImage image = IGImageServiceUtil.addImage(
192                 folderId, name, description, file, contentType, serviceContext);
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                 serviceContext);
208         }
209 
210         AssetPublisherUtil.addRecentFolderId(
211             actionRequest, IGImage.class.getName(), folderId);
212     }
213 
214 }