001    /**
002     * Copyright (c) 2000-2013 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                    ActionRequest actionRequest, long userId) {
043    
044                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
045                            actionRequest);
046    
047                    return getUpdateUserPassword(request, userId);
048            }
049    
050            public static String getUpdateUserPassword(
051                    HttpServletRequest request, long userId) {
052    
053                    String password = PortalUtil.getUserPassword(request);
054    
055                    if (userId != PortalUtil.getUserId(request)) {
056                            password = StringPool.BLANK;
057                    }
058    
059                    if (password == null) {
060                            password = StringPool.BLANK;
061                    }
062    
063                    return password;
064            }
065    
066            public static User updateUser(
067                            ActionRequest actionRequest, 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                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
076                            actionRequest);
077    
078                    return updateUser(
079                            request, userId, screenName, emailAddress, facebookId, openId,
080                            languageId, timeZoneId, greeting, comments, smsSn, aimSn,
081                            facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn,
082                            ymSn);
083            }
084    
085            public static User updateUser(
086                            HttpServletRequest request, long userId, String screenName,
087                            String emailAddress, long facebookId, String openId,
088                            String languageId, String timeZoneId, String greeting,
089                            String comments, String smsSn, String aimSn, String facebookSn,
090                            String icqSn, String jabberSn, String msnSn, String mySpaceSn,
091                            String skypeSn, String twitterSn, String ymSn)
092                    throws PortalException, SystemException {
093    
094                    String password = getUpdateUserPassword(request, userId);
095    
096                    User user = UserLocalServiceUtil.getUserById(userId);
097    
098                    Contact contact = user.getContact();
099    
100                    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
101    
102                    birthdayCal.setTime(contact.getBirthday());
103    
104                    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
105                    int birthdayDay = birthdayCal.get(Calendar.DATE);
106                    int birthdayYear = birthdayCal.get(Calendar.YEAR);
107    
108                    long[] groupIds = null;
109                    long[] organizationIds = null;
110                    long[] roleIds = null;
111                    List<UserGroupRole> userGroupRoles = null;
112                    long[] userGroupIds = null;
113                    ServiceContext serviceContext = new ServiceContext();
114    
115                    return UserServiceUtil.updateUser(
116                            userId, password, StringPool.BLANK, StringPool.BLANK,
117                            user.isPasswordReset(), user.getReminderQueryQuestion(),
118                            user.getReminderQueryAnswer(), screenName, emailAddress, facebookId,
119                            openId, languageId, timeZoneId, greeting, comments,
120                            contact.getFirstName(), contact.getMiddleName(),
121                            contact.getLastName(), contact.getPrefixId(), contact.getSuffixId(),
122                            contact.isMale(), birthdayMonth, birthdayDay, birthdayYear, smsSn,
123                            aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn,
124                            twitterSn, ymSn, contact.getJobTitle(), groupIds, organizationIds,
125                            roleIds, userGroupRoles, userGroupIds, serviceContext);
126            }
127    
128    }