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.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 actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    try {
057                            updateLogo(actionRequest);
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof ImageTypeException ||
063                                    e instanceof NoSuchOrganizationException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(actionRequest, "portlet.users_admin.error");
069                            }
070                            else if (e instanceof UploadException) {
071                                    SessionErrors.add(actionRequest, e.getClass());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping actionMapping, ActionForm actionForm,
082                            PortletConfig portletConfig, RenderRequest renderRequest,
083                            RenderResponse renderResponse)
084                    throws Exception {
085    
086                    return actionMapping.findForward(
087                            getForward(
088                                    renderRequest, "portlet.users_admin.edit_organization_logo"));
089            }
090    
091            protected void updateLogo(ActionRequest actionRequest) throws Exception {
092                    UploadPortletRequest uploadPortletRequest =
093                            PortalUtil.getUploadPortletRequest(actionRequest);
094    
095                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
096    
097                    InputStream inputStream = null;
098    
099                    try {
100                            File file = uploadPortletRequest.getFile("fileName");
101    
102                            inputStream = new ByteArrayFileInputStream(file, 1024);
103    
104                            inputStream.mark(0);
105    
106                            LayoutSetServiceUtil.updateLogo(
107                                    groupId, true, true, inputStream, false);
108    
109                            inputStream.reset();
110    
111                            LayoutSetServiceUtil.updateLogo(
112                                    groupId, false, true, inputStream, false);
113                    }
114                    finally {
115                            StreamUtil.cleanUp(inputStream);
116                    }
117            }
118    
119    }