001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, ActionRequest actionRequest,
051                            ActionResponse actionResponse)
052                    throws Exception {
053    
054                    try {
055                            updateLogo(actionRequest);
056    
057                            sendRedirect(actionRequest, actionResponse);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof PrincipalException) {
061                                    SessionErrors.add(actionRequest, e.getClass());
062    
063                                    setForward(actionRequest, "portlet.portal_settings.error");
064                            }
065                            else if (e instanceof ImageTypeException ||
066                                             e instanceof UploadException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass());
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            @Override
077            public ActionForward render(
078                            ActionMapping actionMapping, ActionForm actionForm,
079                            PortletConfig portletConfig, RenderRequest renderRequest,
080                            RenderResponse renderResponse)
081                    throws Exception {
082    
083                    return actionMapping.findForward(
084                            getForward(
085                                    renderRequest, "portlet.portal_settings.edit_company_logo"));
086            }
087    
088            protected void updateLogo(ActionRequest actionRequest) throws Exception {
089                    UploadPortletRequest uploadPortletRequest =
090                            PortalUtil.getUploadPortletRequest(actionRequest);
091    
092                    long companyId = PortalUtil.getCompanyId(actionRequest);
093    
094                    InputStream inputStream = null;
095    
096                    try {
097                            inputStream = uploadPortletRequest.getFileAsStream("fileName");
098    
099                            if (inputStream == null) {
100                                    throw new UploadException();
101                            }
102    
103                            ThemeDisplay themeDisplay =
104                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
105    
106                            Company company = CompanyServiceUtil.updateLogo(
107                                    companyId, inputStream);
108    
109                            themeDisplay.setCompany(company);
110                    }
111                    finally {
112                            StreamUtil.cleanUp(inputStream);
113                    }
114            }
115    
116    }