1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.ImageTypeException;
18  import com.liferay.portal.NoSuchOrganizationException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.upload.UploadPortletRequest;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.LayoutSetServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.util.servlet.UploadException;
28  
29  import java.io.File;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="EditOrganizationLogoAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Julio Camarero
46   */
47  public class EditOrganizationLogoAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          try {
55              updateLogo(actionRequest);
56  
57              sendRedirect(actionRequest, actionResponse);
58          }
59          catch (Exception e) {
60              if (e instanceof ImageTypeException ||
61                  e instanceof NoSuchOrganizationException ||
62                  e instanceof PrincipalException) {
63  
64                  SessionErrors.add(actionRequest, e.getClass().getName());
65  
66                  setForward(actionRequest, "portlet.enterprise_admin.error");
67              }
68              else if (e instanceof UploadException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          return mapping.findForward(getForward(
84              renderRequest, "portlet.enterprise_admin.edit_organization_logo"));
85      }
86  
87      protected void updateLogo(ActionRequest actionRequest) throws Exception {
88          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
89              actionRequest);
90  
91          long groupId = ParamUtil.getLong(uploadRequest, "groupId");
92  
93          File file = uploadRequest.getFile("fileName");
94          byte[] bytes = FileUtil.getBytes(file);
95  
96          if ((bytes == null) || (bytes.length == 0)) {
97              throw new UploadException();
98          }
99  
100         LayoutSetServiceUtil.updateLogo(groupId, true, true, file);
101         LayoutSetServiceUtil.updateLogo(groupId, false, true, file);
102     }
103 
104 }