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