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.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.service.CompanyServiceUtil;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.util.servlet.UploadException;
25  
26  import java.io.File;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.ActionResponse;
30  import javax.portlet.PortletConfig;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  
38  /**
39   * <a href="EditCompanyLogoAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class EditCompanyLogoAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          try {
51              updateLogo(actionRequest);
52  
53              sendRedirect(actionRequest, actionResponse);
54          }
55          catch (Exception e) {
56              if (e instanceof PrincipalException) {
57                  SessionErrors.add(actionRequest, e.getClass().getName());
58  
59                  setForward(actionRequest, "portlet.enterprise_admin.error");
60              }
61              else if (e instanceof UploadException) {
62                  SessionErrors.add(actionRequest, e.getClass().getName());
63              }
64              else {
65                  throw e;
66              }
67          }
68      }
69  
70      public ActionForward render(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              RenderRequest renderRequest, RenderResponse renderResponse)
73          throws Exception {
74  
75          return mapping.findForward(getForward(
76              renderRequest, "portlet.enterprise_admin.edit_company_logo"));
77      }
78  
79      protected void updateLogo(ActionRequest actionRequest) throws Exception {
80          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
81              actionRequest);
82  
83          long companyId = PortalUtil.getCompanyId(actionRequest);
84  
85          File file = uploadRequest.getFile("fileName");
86          byte[] bytes = FileUtil.getBytes(file);
87  
88          if ((bytes == null) || (bytes.length == 0)) {
89              throw new UploadException();
90          }
91  
92          CompanyServiceUtil.updateLogo(companyId, file);
93      }
94  
95  }