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.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 actionMapping, ActionForm actionForm,
055                            PortletConfig portletConfig, ActionRequest actionRequest,
056                            ActionResponse actionResponse)
057                    throws Exception {
058    
059                    try {
060                            String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                            if (cmd.equals(Constants.ADD_TEMP)) {
063                                    addTempImageFile(actionRequest);
064                            }
065                            else {
066                                    saveTempImageFile(actionRequest);
067    
068                                    sendRedirect(actionRequest, actionResponse);
069                            }
070                    }
071                    catch (Exception e) {
072                            if (e instanceof NoSuchOrganizationException ||
073                                    e instanceof PrincipalException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass());
076    
077                                    setForward(actionRequest, "portlet.users_admin.error");
078                            }
079                            else if (e instanceof FileSizeException ||
080                                             e instanceof ImageTypeException) {
081    
082                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
083    
084                                    jsonObject.putException(e);
085    
086                                    writeJSON(actionRequest, actionResponse, jsonObject);
087                            }
088                            else if (e instanceof NoSuchFileException ||
089                                             e instanceof UploadException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097            }
098    
099            @Override
100            public ActionForward render(
101                            ActionMapping actionMapping, ActionForm actionForm,
102                            PortletConfig portletConfig, RenderRequest renderRequest,
103                            RenderResponse renderResponse)
104                    throws Exception {
105    
106                    return actionMapping.findForward(
107                            getForward(
108                                    renderRequest, "portlet.users_admin.edit_organization_logo"));
109            }
110    
111            @Override
112            protected String getTempImageFileName(PortletRequest portletRequest) {
113                    return ParamUtil.getString(portletRequest, "groupId");
114            }
115    
116            @Override
117            protected void saveTempImageFile(
118                            PortletRequest portletRequest, byte[] bytes)
119                    throws Exception {
120    
121                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
122    
123                    InputStream inputStream = null;
124    
125                    try {
126                            inputStream = new ByteArrayInputStream(bytes);
127    
128                            LayoutSetServiceUtil.updateLogo(
129                                    groupId, true, true, inputStream, false);
130    
131                            inputStream.reset();
132    
133                            LayoutSetServiceUtil.updateLogo(
134                                    groupId, false, true, inputStream, false);
135                    }
136                    finally {
137                            StreamUtil.cleanUp(inputStream);
138                    }
139            }
140    
141    }