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