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.NoSuchUserException;
18  import com.liferay.portal.UserPortraitSizeException;
19  import com.liferay.portal.UserPortraitTypeException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.model.User;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.UserServiceUtil;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.util.portlet.PortletRequestUtil;
31  import com.liferay.util.servlet.UploadException;
32  
33  import java.io.File;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditUserPortraitAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class EditUserPortraitAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          try {
58              updatePortrait(actionRequest);
59  
60              sendRedirect(actionRequest, actionResponse);
61          }
62          catch (Exception e) {
63              if (e instanceof NoSuchUserException ||
64                  e instanceof PrincipalException) {
65  
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.enterprise_admin.error");
69              }
70              else if (e instanceof UploadException ||
71                       e instanceof UserPortraitSizeException ||
72                       e instanceof UserPortraitTypeException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75              }
76              else {
77                  throw e;
78              }
79          }
80      }
81  
82      public ActionForward render(
83              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
84              RenderRequest renderRequest, RenderResponse renderResponse)
85          throws Exception {
86  
87          return mapping.findForward(getForward(
88              renderRequest, "portlet.enterprise_admin.edit_user_portrait"));
89      }
90  
91      protected void updatePortrait(ActionRequest actionRequest)
92          throws Exception {
93  
94          if (_log.isDebugEnabled()) {
95              PortletRequestUtil.testMultipartWithCommonsFileUpload(
96                  actionRequest);
97          }
98  
99          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
100             actionRequest);
101 
102         User user = PortalUtil.getSelectedUser(uploadRequest);
103 
104         File file = uploadRequest.getFile("fileName");
105         byte[] bytes = FileUtil.getBytes(file);
106 
107         if ((bytes == null) || (bytes.length == 0)) {
108             throw new UploadException();
109         }
110 
111         UserServiceUtil.updatePortrait(user.getUserId(), bytes);
112     }
113 
114     private static Log _log = LogFactoryUtil.getLog(
115         EditUserPortraitAction.class);
116 
117 }