1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.service.CompanyServiceUtil;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.UploadRequestUtil;
30  import com.liferay.util.FileUtil;
31  import com.liferay.util.servlet.SessionErrors;
32  import com.liferay.util.servlet.UploadException;
33  import com.liferay.util.servlet.UploadPortletRequest;
34  
35  import java.io.File;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditCompanyLogoAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditCompanyLogoAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig config,
57              ActionRequest req, ActionResponse res)
58          throws Exception {
59  
60          try {
61              updateLogo(req);
62  
63              sendRedirect(req, res);
64          }
65          catch (Exception e) {
66              if (e instanceof PrincipalException) {
67                  SessionErrors.add(req, e.getClass().getName());
68  
69                  setForward(req, "portlet.enterprise_admin.error");
70              }
71              else if (e instanceof UploadException) {
72                  SessionErrors.add(req, e.getClass().getName());
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig config,
82              RenderRequest req, RenderResponse res)
83          throws Exception {
84  
85          return mapping.findForward(
86              getForward(req, "portlet.enterprise_admin.edit_company_logo"));
87      }
88  
89      protected void updateLogo(ActionRequest req) throws Exception {
90          UploadPortletRequest uploadReq =
91              UploadRequestUtil.getUploadPortletRequest(req);
92  
93          long companyId = PortalUtil.getCompanyId(req);
94  
95          File file = uploadReq.getFile("fileName");
96          byte[] bytes = FileUtil.getBytes(file);
97  
98          if ((bytes == null) || (bytes.length == 0)) {
99              throw new UploadException();
100         }
101 
102         CompanyServiceUtil.updateLogo(companyId, file);
103     }
104 
105 }