1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.NoSuchOrganizationException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.upload.UploadPortletRequest;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.LayoutSetServiceUtil;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.util.servlet.UploadException;
27  
28  import java.io.File;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditOrganizationLogoAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Julio Camarero
45   */
46  public class EditOrganizationLogoAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          try {
54              updateLogo(actionRequest);
55  
56              sendRedirect(actionRequest, actionResponse);
57          }
58          catch (Exception e) {
59              if (e instanceof NoSuchOrganizationException ||
60                  e instanceof PrincipalException) {
61  
62                  SessionErrors.add(actionRequest, e.getClass().getName());
63  
64                  setForward(actionRequest, "portlet.enterprise_admin.error");
65              }
66              else if (e instanceof UploadException) {
67  
68                  SessionErrors.add(actionRequest, e.getClass().getName());
69              }
70              else {
71                  throw e;
72              }
73          }
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          return mapping.findForward(getForward(
82              renderRequest, "portlet.enterprise_admin.edit_organization_logo"));
83      }
84  
85      protected void updateLogo(ActionRequest actionRequest) throws Exception {
86          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
87              actionRequest);
88  
89          long groupId = ParamUtil.getLong(uploadRequest, "groupId");
90  
91          File file = uploadRequest.getFile("fileName");
92          byte[] bytes = FileUtil.getBytes(file);
93  
94          if ((bytes == null) || (bytes.length == 0)) {
95              throw new UploadException();
96          }
97  
98          LayoutSetServiceUtil.updateLogo(groupId, true, true, file);
99          LayoutSetServiceUtil.updateLogo(groupId, false, true, file);
100     }
101 
102 }