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