001    /**
002     * Copyright (c) 2000-2012 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.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());
066    
067                                    setForward(actionRequest, "portlet.users_admin.error");
068                            }
069                            else if (e instanceof UploadException) {
070                                    SessionErrors.add(actionRequest, e.getClass());
071                            }
072                            else {
073                                    throw e;
074                            }
075                    }
076            }
077    
078            @Override
079            public ActionForward render(
080                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
081                            RenderRequest renderRequest, RenderResponse renderResponse)
082                    throws Exception {
083    
084                    return mapping.findForward(getForward(
085                            renderRequest, "portlet.users_admin.edit_organization_logo"));
086            }
087    
088            protected void updateLogo(ActionRequest actionRequest) throws Exception {
089                    UploadPortletRequest uploadPortletRequest =
090                            PortalUtil.getUploadPortletRequest(actionRequest);
091    
092                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
093    
094                    InputStream inputStream = null;
095    
096                    try {
097                            File file = uploadPortletRequest.getFile("fileName");
098    
099                            inputStream = new ByteArrayFileInputStream(file, 1024);
100    
101                            inputStream.mark(0);
102    
103                            LayoutSetServiceUtil.updateLogo(
104                                    groupId, true, true, inputStream, false);
105    
106                            inputStream.reset();
107    
108                            LayoutSetServiceUtil.updateLogo(
109                                    groupId, false, true, inputStream, false);
110                    }
111                    finally {
112                            StreamUtil.cleanUp(inputStream);
113                    }
114            }
115    
116    }