001    /**
002     * Copyright (c) 2000-present 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.admin.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.Contact;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.model.UserGroupRole;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.service.UserServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.Calendar;
029    import java.util.List;
030    
031    import javax.portlet.ActionRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class AdminUtil {
039    
040            public static String getUpdateUserPassword(
041                    ActionRequest actionRequest, long userId) {
042    
043                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
044                            actionRequest);
045    
046                    return getUpdateUserPassword(request, userId);
047            }
048    
049            public static String getUpdateUserPassword(
050                    HttpServletRequest request, long userId) {
051    
052                    String password = PortalUtil.getUserPassword(request);
053    
054                    if (userId != PortalUtil.getUserId(request)) {
055                            password = StringPool.BLANK;
056                    }
057    
058                    if (password == null) {
059                            password = StringPool.BLANK;
060                    }
061    
062                    return password;
063            }
064    
065            public static User updateUser(
066                            ActionRequest actionRequest, long userId, String screenName,
067                            String emailAddress, long facebookId, String openId,
068                            String languageId, String timeZoneId, String greeting,
069                            String comments, String smsSn, String aimSn, String facebookSn,
070                            String icqSn, String jabberSn, String mySpaceSn, String skypeSn,
071                            String twitterSn, String ymSn)
072                    throws PortalException {
073    
074                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
075                            actionRequest);
076    
077                    return updateUser(
078                            request, userId, screenName, emailAddress, facebookId, openId,
079                            languageId, timeZoneId, greeting, comments, smsSn, aimSn,
080                            facebookSn, icqSn, jabberSn, mySpaceSn, skypeSn, twitterSn, ymSn);
081            }
082    
083            public static User updateUser(
084                            HttpServletRequest request, long userId, String screenName,
085                            String emailAddress, long facebookId, String openId,
086                            String languageId, String timeZoneId, String greeting,
087                            String comments, String smsSn, String aimSn, String facebookSn,
088                            String icqSn, String jabberSn, String mySpaceSn, String skypeSn,
089                            String twitterSn, String ymSn)
090                    throws PortalException {
091    
092                    String password = getUpdateUserPassword(request, userId);
093    
094                    User user = UserLocalServiceUtil.getUserById(userId);
095    
096                    Contact contact = user.getContact();
097    
098                    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
099    
100                    birthdayCal.setTime(contact.getBirthday());
101    
102                    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
103                    int birthdayDay = birthdayCal.get(Calendar.DATE);
104                    int birthdayYear = birthdayCal.get(Calendar.YEAR);
105    
106                    long[] groupIds = null;
107                    long[] organizationIds = null;
108                    long[] roleIds = null;
109                    List<UserGroupRole> userGroupRoles = null;
110                    long[] userGroupIds = null;
111                    ServiceContext serviceContext = new ServiceContext();
112    
113                    return UserServiceUtil.updateUser(
114                            userId, password, StringPool.BLANK, StringPool.BLANK,
115                            user.isPasswordReset(), user.getReminderQueryQuestion(),
116                            user.getReminderQueryAnswer(), screenName, emailAddress, facebookId,
117                            openId, languageId, timeZoneId, greeting, comments,
118                            contact.getFirstName(), contact.getMiddleName(),
119                            contact.getLastName(), contact.getPrefixId(), contact.getSuffixId(),
120                            contact.isMale(), birthdayMonth, birthdayDay, birthdayYear, smsSn,
121                            aimSn, facebookSn, icqSn, jabberSn, mySpaceSn, skypeSn, twitterSn,
122                            ymSn, contact.getJobTitle(), groupIds, organizationIds, roleIds,
123                            userGroupRoles, userGroupIds, serviceContext);
124            }
125    
126    }