001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.security.auth.PrincipalException;
023    import com.liferay.portal.service.CompanyServiceUtil;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.io.InputStream;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class EditCompanyLogoAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    try {
051                            updateLogo(actionRequest);
052    
053                            sendRedirect(actionRequest, actionResponse);
054                    }
055                    catch (Exception e) {
056                            if (e instanceof PrincipalException) {
057                                    SessionErrors.add(actionRequest, e.getClass().getName());
058    
059                                    setForward(actionRequest, "portlet.portal_settings.error");
060                            }
061                            else if (e instanceof ImageTypeException ||
062                                             e instanceof UploadException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass().getName());
065                            }
066                            else {
067                                    throw e;
068                            }
069                    }
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            RenderRequest renderRequest, RenderResponse renderResponse)
076                    throws Exception {
077    
078                    return mapping.findForward(getForward(
079                            renderRequest, "portlet.portal_settings.edit_company_logo"));
080            }
081    
082            protected void updateLogo(ActionRequest actionRequest) throws Exception {
083                    UploadPortletRequest uploadPortletRequest =
084                            PortalUtil.getUploadPortletRequest(actionRequest);
085    
086                    long companyId = PortalUtil.getCompanyId(actionRequest);
087    
088                    InputStream inputStream = null;
089    
090                    try {
091                             inputStream = uploadPortletRequest.getFileAsStream("fileName");
092    
093                            if (inputStream == null) {
094                                    throw new UploadException();
095                            }
096    
097                            CompanyServiceUtil.updateLogo(companyId, inputStream);
098                    }
099                    finally {
100                            StreamUtil.cleanUp(inputStream);
101                    }
102            }
103    
104    }