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