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