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 msnSn, String mySpaceSn,
071                            String skypeSn, 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, msnSn, mySpaceSn, skypeSn, twitterSn,
081                            ymSn);
082            }
083    
084            public static User updateUser(
085                            HttpServletRequest request, long userId, String screenName,
086                            String emailAddress, long facebookId, String openId,
087                            String languageId, String timeZoneId, String greeting,
088                            String comments, String smsSn, String aimSn, String facebookSn,
089                            String icqSn, String jabberSn, String msnSn, String mySpaceSn,
090                            String skypeSn, String twitterSn, String ymSn)
091                    throws PortalException {
092    
093                    String password = getUpdateUserPassword(request, userId);
094    
095                    User user = UserLocalServiceUtil.getUserById(userId);
096    
097                    Contact contact = user.getContact();
098    
099                    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
100    
101                    birthdayCal.setTime(contact.getBirthday());
102    
103                    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
104                    int birthdayDay = birthdayCal.get(Calendar.DATE);
105                    int birthdayYear = birthdayCal.get(Calendar.YEAR);
106    
107                    long[] groupIds = null;
108                    long[] organizationIds = null;
109                    long[] roleIds = null;
110                    List<UserGroupRole> userGroupRoles = null;
111                    long[] userGroupIds = null;
112                    ServiceContext serviceContext = new ServiceContext();
113    
114                    return UserServiceUtil.updateUser(
115                            userId, password, StringPool.BLANK, StringPool.BLANK,
116                            user.isPasswordReset(), user.getReminderQueryQuestion(),
117                            user.getReminderQueryAnswer(), screenName, emailAddress, facebookId,
118                            openId, languageId, timeZoneId, greeting, comments,
119                            contact.getFirstName(), contact.getMiddleName(),
120                            contact.getLastName(), contact.getPrefixId(), contact.getSuffixId(),
121                            contact.isMale(), birthdayMonth, birthdayDay, birthdayYear, smsSn,
122                            aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn,
123                            twitterSn, ymSn, contact.getJobTitle(), groupIds, organizationIds,
124                            roleIds, userGroupRoles, userGroupIds, serviceContext);
125            }
126    
127    }