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