1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.FileUtil;
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.util.servlet.UploadException;
30  
31  import java.io.File;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditCompanyLogoAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class EditCompanyLogoAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          try {
57              updateLogo(actionRequest);
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof PrincipalException) {
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(actionRequest, "portlet.enterprise_admin.error");
66              }
67              else if (e instanceof UploadException) {
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_company_logo"));
83      }
84  
85      protected void updateLogo(ActionRequest actionRequest) throws Exception {
86          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
87              actionRequest);
88  
89          long companyId = PortalUtil.getCompanyId(actionRequest);
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          CompanyServiceUtil.updateLogo(companyId, file);
99      }
100 
101 }