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