001    /**
002     * Copyright (c) 2000-2012 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.portalsettings.action;
016    
017    import com.liferay.portal.ImageTypeException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadException;
020    import com.liferay.portal.kernel.upload.UploadPortletRequest;
021    import com.liferay.portal.kernel.util.StreamUtil;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.CompanyServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    
030    import java.io.InputStream;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class EditCompanyLogoAction extends PortletAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            updateLogo(actionRequest);
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof PrincipalException) {
060                                    SessionErrors.add(actionRequest, e.getClass().getName());
061    
062                                    setForward(actionRequest, "portlet.portal_settings.error");
063                            }
064                            else if (e instanceof ImageTypeException ||
065                                             e instanceof UploadException) {
066    
067                                    SessionErrors.add(actionRequest, e.getClass().getName());
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            @Override
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.portal_settings.edit_company_logo"));
083            }
084    
085            protected void updateLogo(ActionRequest actionRequest) throws Exception {
086                    UploadPortletRequest uploadPortletRequest =
087                            PortalUtil.getUploadPortletRequest(actionRequest);
088    
089                    long companyId = PortalUtil.getCompanyId(actionRequest);
090    
091                    InputStream inputStream = null;
092    
093                    try {
094                             inputStream = uploadPortletRequest.getFileAsStream("fileName");
095    
096                            if (inputStream == null) {
097                                    throw new UploadException();
098                            }
099    
100                            ThemeDisplay themeDisplay =
101                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
102    
103                            Company company = CompanyServiceUtil.updateLogo(
104                                    companyId, inputStream);
105    
106                            themeDisplay.setCompany(company);
107                    }
108                    finally {
109                            StreamUtil.cleanUp(inputStream);
110                    }
111            }
112    
113    }