1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.NoSuchUserException;
26  import com.liferay.portal.UserPortraitException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.upload.UploadPortletRequest;
31  import com.liferay.portal.kernel.util.FileUtil;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.util.portlet.PortletRequestUtil;
38  import com.liferay.util.servlet.UploadException;
39  
40  import java.io.File;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditUserPortraitAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   */
57  public class EditUserPortraitAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              updatePortrait(actionRequest);
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchUserException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.enterprise_admin.error");
76              }
77              else if (e instanceof UploadException ||
78                       e instanceof UserPortraitException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws Exception {
92  
93          return mapping.findForward(getForward(
94              renderRequest, "portlet.enterprise_admin.edit_user_portrait"));
95      }
96  
97      protected void updatePortrait(ActionRequest actionRequest)
98          throws Exception {
99  
100         if (_log.isDebugEnabled()) {
101             PortletRequestUtil.testMultipartWithCommonsFileUpload(
102                 actionRequest);
103         }
104 
105         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
106             actionRequest);
107 
108         User user = PortalUtil.getSelectedUser(uploadRequest);
109 
110         File file = uploadRequest.getFile("fileName");
111         byte[] bytes = FileUtil.getBytes(file);
112 
113         if ((bytes == null) || (bytes.length == 0)) {
114             throw new UploadException();
115         }
116 
117         UserServiceUtil.updatePortrait(user.getUserId(), bytes);
118     }
119 
120     private static Log _log =
121         LogFactoryUtil.getLog(EditUserPortraitAction.class);
122 
123 }