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.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.upload.UploadException;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StreamUtil;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutSetServiceUtil;
028    import com.liferay.portlet.documentlibrary.FileSizeException;
029    import com.liferay.portlet.documentlibrary.NoSuchFileException;
030    import com.liferay.portlet.portalsettings.action.EditCompanyLogoAction;
031    
032    import java.io.ByteArrayInputStream;
033    import java.io.InputStream;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.PortletRequest;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Julio Camarero
049     */
050    public class EditOrganizationLogoAction extends EditCompanyLogoAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    try {
059                            String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
060    
061                            if (cmd.equals(Constants.ADD_TEMP)) {
062                                    addTempImageFile(actionRequest);
063                            }
064                            else {
065                                    saveTempImageFile(actionRequest);
066    
067                                    sendRedirect(actionRequest, actionResponse);
068                            }
069                    }
070                    catch (Exception e) {
071                            if (e instanceof NoSuchOrganizationException ||
072                                    e instanceof PrincipalException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass());
075    
076                                    setForward(actionRequest, "portlet.users_admin.error");
077                            }
078                            else if (e instanceof FileSizeException ||
079                                             e instanceof ImageTypeException) {
080    
081                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
082    
083                                    jsonObject.putException(e);
084    
085                                    writeJSON(actionRequest, actionResponse, jsonObject);
086                            }
087                            else if (e instanceof NoSuchFileException ||
088                                             e instanceof UploadException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass());
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096            }
097    
098            @Override
099            public ActionForward render(
100                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101                            RenderRequest renderRequest, RenderResponse renderResponse)
102                    throws Exception {
103    
104                    return mapping.findForward(
105                            getForward(
106                                    renderRequest, "portlet.users_admin.edit_organization_logo"));
107            }
108    
109            @Override
110            protected String getTempImageFileName(PortletRequest portletRequest) {
111                    return ParamUtil.getString(portletRequest, "groupId");
112            }
113    
114            @Override
115            protected void saveTempImageFile(
116                            PortletRequest portletRequest, byte[] bytes)
117                    throws Exception {
118    
119                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
120    
121                    InputStream inputStream = null;
122    
123                    try {
124                            inputStream = new ByteArrayInputStream(bytes);
125    
126                            LayoutSetServiceUtil.updateLogo(
127                                    groupId, true, true, inputStream, false);
128    
129                            inputStream.reset();
130    
131                            LayoutSetServiceUtil.updateLogo(
132                                    groupId, false, true, inputStream, false);
133                    }
134                    finally {
135                            StreamUtil.cleanUp(inputStream);
136                    }
137            }
138    
139    }