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.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.CompanyServiceUtil;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.util.servlet.UploadException;
026    
027    import java.io.File;
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            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    try {
050                            updateLogo(actionRequest);
051    
052                            sendRedirect(actionRequest, actionResponse);
053                    }
054                    catch (Exception e) {
055                            if (e instanceof PrincipalException) {
056                                    SessionErrors.add(actionRequest, e.getClass().getName());
057    
058                                    setForward(actionRequest, "portlet.enterprise_admin.error");
059                            }
060                            else if (e instanceof ImageTypeException ||
061                                             e instanceof UploadException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass().getName());
064                            }
065                            else {
066                                    throw e;
067                            }
068                    }
069            }
070    
071            public ActionForward render(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            RenderRequest renderRequest, RenderResponse renderResponse)
074                    throws Exception {
075    
076                    return mapping.findForward(getForward(
077                            renderRequest, "portlet.enterprise_admin.edit_company_logo"));
078            }
079    
080            protected void updateLogo(ActionRequest actionRequest) throws Exception {
081                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
082                            actionRequest);
083    
084                    long companyId = PortalUtil.getCompanyId(actionRequest);
085    
086                    File file = uploadRequest.getFile("fileName");
087                    byte[] bytes = FileUtil.getBytes(file);
088    
089                    if ((bytes == null) || (bytes.length == 0)) {
090                            throw new UploadException();
091                    }
092    
093                    CompanyServiceUtil.updateLogo(companyId, file);
094            }
095    
096    }