001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.imagegallery.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.upload.UploadPortletRequest;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.MimeTypesUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.asset.AssetTagException;
034    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
035    import com.liferay.portlet.imagegallery.DuplicateImageNameException;
036    import com.liferay.portlet.imagegallery.ImageNameException;
037    import com.liferay.portlet.imagegallery.ImageSizeException;
038    import com.liferay.portlet.imagegallery.NoSuchFolderException;
039    import com.liferay.portlet.imagegallery.NoSuchImageException;
040    import com.liferay.portlet.imagegallery.model.IGImage;
041    import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
042    
043    import java.io.File;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.RenderRequest;
049    import javax.portlet.RenderResponse;
050    
051    import org.apache.struts.action.ActionForm;
052    import org.apache.struts.action.ActionForward;
053    import org.apache.struts.action.ActionMapping;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     */
058    public class EditImageAction extends PortletAction {
059    
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateImage(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteImage(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof NoSuchImageException ||
079                                    e instanceof PrincipalException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass().getName());
082    
083                                    setForward(actionRequest, "portlet.image_gallery.error");
084                            }
085                            else if (e instanceof DuplicateImageNameException ||
086                                             e instanceof ImageNameException ||
087                                             e instanceof ImageSizeException ||
088                                             e instanceof NoSuchFolderException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass().getName());
091                            }
092                            else if (e instanceof AssetTagException) {
093                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099            }
100    
101            public ActionForward render(
102                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getImage(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchImageException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass().getName());
114    
115                                    return mapping.findForward("portlet.image_gallery.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    String forward = "portlet.image_gallery.edit_image";
123    
124                    return mapping.findForward(getForward(renderRequest, forward));
125            }
126    
127            protected void deleteImage(ActionRequest actionRequest) throws Exception {
128                    long imageId = ParamUtil.getLong(actionRequest, "imageId");
129    
130                    IGImageServiceUtil.deleteImage(imageId);
131            }
132    
133            protected String getContentType(
134                    UploadPortletRequest uploadRequest, File file) {
135    
136                    String contentType = GetterUtil.getString(
137                            uploadRequest.getContentType("file"));
138    
139                    if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
140                            String ext = GetterUtil.getString(
141                                    FileUtil.getExtension(file.getName())).toLowerCase();
142    
143                            if (Validator.isNotNull(ext)) {
144                                    contentType = MimeTypesUtil.getContentType(ext);
145                            }
146                    }
147    
148                    return contentType;
149            }
150    
151            protected void updateImage(ActionRequest actionRequest) throws Exception {
152                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
153                            actionRequest);
154    
155                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156                            WebKeys.THEME_DISPLAY);
157    
158                    long imageId = ParamUtil.getLong(uploadRequest, "imageId");
159    
160                    long groupId = themeDisplay.getScopeGroupId();
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(ContentTypes.APPLICATION_OCTET_STREAM)) {
171                            String ext = GetterUtil.getString(
172                                    FileUtil.getExtension(file.getName())).toLowerCase();
173    
174                            if (Validator.isNotNull(ext)) {
175                                    contentType = MimeTypesUtil.getContentType(ext);
176                            }
177                    }
178    
179                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
180                            IGImage.class.getName(), actionRequest);
181    
182                    if (imageId <= 0) {
183    
184                            // Add image
185    
186                            if (Validator.isNull(name)) {
187                                    name = fileName;
188                            }
189    
190                            IGImage image = IGImageServiceUtil.addImage(
191                                    groupId, folderId, name, description, file, contentType,
192                                    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, groupId, folderId, name, description, file,
207                                    contentType, serviceContext);
208                    }
209    
210                    AssetPublisherUtil.addRecentFolderId(
211                            actionRequest, IGImage.class.getName(), folderId);
212            }
213    
214    }