001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.enterpriseadmin.action;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.UserPortraitSizeException;
019    import com.liferay.portal.UserPortraitTypeException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.upload.UploadPortletRequest;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.UserServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.util.portlet.PortletRequestUtil;
031    import com.liferay.util.servlet.UploadException;
032    
033    import java.io.File;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditUserPortraitAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    try {
056                            updatePortrait(actionRequest);
057    
058                            sendRedirect(actionRequest, actionResponse);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof NoSuchUserException ||
062                                    e instanceof PrincipalException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass().getName());
065    
066                                    setForward(actionRequest, "portlet.enterprise_admin.error");
067                            }
068                            else if (e instanceof UploadException ||
069                                             e instanceof UserPortraitSizeException ||
070                                             e instanceof UserPortraitTypeException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    return mapping.findForward(getForward(
086                            renderRequest, "portlet.enterprise_admin.edit_user_portrait"));
087            }
088    
089            protected void updatePortrait(ActionRequest actionRequest)
090                    throws Exception {
091    
092                    if (_log.isDebugEnabled()) {
093                            PortletRequestUtil.testMultipartWithCommonsFileUpload(
094                                    actionRequest);
095                    }
096    
097                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
098                            actionRequest);
099    
100                    User user = PortalUtil.getSelectedUser(uploadRequest);
101    
102                    File file = uploadRequest.getFile("fileName");
103                    byte[] bytes = FileUtil.getBytes(file);
104    
105                    if ((bytes == null) || (bytes.length == 0)) {
106                            throw new UploadException();
107                    }
108    
109                    UserServiceUtil.updatePortrait(user.getUserId(), bytes);
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    EditUserPortraitAction.class);
114    
115    }