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.enterpriseadmin.action;
016    
017    import com.liferay.portal.ImageTypeException;
018    import com.liferay.portal.NoSuchOrganizationException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.upload.UploadPortletRequest;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.LayoutSetServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.util.servlet.UploadException;
028    
029    import java.io.File;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Julio Camarero
044     */
045    public class EditOrganizationLogoAction extends PortletAction {
046    
047            public void processAction(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    try {
053                            updateLogo(actionRequest);
054    
055                            sendRedirect(actionRequest, actionResponse);
056                    }
057                    catch (Exception e) {
058                            if (e instanceof ImageTypeException ||
059                                    e instanceof NoSuchOrganizationException ||
060                                    e instanceof PrincipalException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063    
064                                    setForward(actionRequest, "portlet.enterprise_admin.error");
065                            }
066                            else if (e instanceof UploadException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass().getName());
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            public ActionForward render(
077                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
078                            RenderRequest renderRequest, RenderResponse renderResponse)
079                    throws Exception {
080    
081                    return mapping.findForward(getForward(
082                            renderRequest, "portlet.enterprise_admin.edit_organization_logo"));
083            }
084    
085            protected void updateLogo(ActionRequest actionRequest) throws Exception {
086                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
087                            actionRequest);
088    
089                    long groupId = ParamUtil.getLong(uploadRequest, "groupId");
090    
091                    File file = uploadRequest.getFile("fileName");
092                    byte[] bytes = FileUtil.getBytes(file);
093    
094                    if ((bytes == null) || (bytes.length == 0)) {
095                            throw new UploadException();
096                    }
097    
098                    LayoutSetServiceUtil.updateLogo(groupId, true, true, file);
099                    LayoutSetServiceUtil.updateLogo(groupId, false, true, file);
100            }
101    
102    }