1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.admin.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Contact;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.UserGroupRole;
32  import com.liferay.portal.service.ServiceContext;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import java.util.Calendar;
38  import java.util.List;
39  
40  import javax.portlet.ActionRequest;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  /**
45   * <a href="AdminUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class AdminUtil {
51  
52      public static String getUpdateUserPassword(
53          HttpServletRequest request, long userId) {
54  
55          String password = PortalUtil.getUserPassword(request);
56  
57          if (userId != PortalUtil.getUserId(request)) {
58              password = StringPool.BLANK;
59          }
60  
61          return password;
62      }
63  
64      public static String getUpdateUserPassword(
65          ActionRequest actionRequest, long userId) {
66  
67          HttpServletRequest request = PortalUtil.getHttpServletRequest(
68              actionRequest);
69  
70          return getUpdateUserPassword(request, userId);
71      }
72  
73      public static User updateUser(
74              HttpServletRequest request, long userId, String screenName,
75              String emailAddress, String openId, String languageId,
76              String timeZoneId, String greeting, String comments, String smsSn,
77              String aimSn, String facebookSn, String icqSn, String jabberSn,
78              String msnSn, String mySpaceSn, String skypeSn, String twitterSn,
79              String ymSn)
80          throws PortalException, SystemException {
81  
82          String password = getUpdateUserPassword(request, userId);
83  
84          User user = UserLocalServiceUtil.getUserById(userId);
85  
86          Contact contact = user.getContact();
87  
88          Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
89  
90          birthdayCal.setTime(contact.getBirthday());
91  
92          int birthdayMonth = birthdayCal.get(Calendar.MONTH);
93          int birthdayDay = birthdayCal.get(Calendar.DATE);
94          int birthdayYear = birthdayCal.get(Calendar.YEAR);
95  
96          long[] groupIds = null;
97          long[] organizationIds = null;
98          long[] roleIds = null;
99          List<UserGroupRole> userGroupRoles = null;
100         long[] userGroupIds = null;
101         ServiceContext serviceContext = new ServiceContext();
102 
103         return UserServiceUtil.updateUser(
104             userId, password, StringPool.BLANK, StringPool.BLANK,
105             user.isPasswordReset(), user.getReminderQueryQuestion(),
106             user.getReminderQueryAnswer(), screenName, emailAddress, openId,
107             languageId, timeZoneId, greeting, comments, contact.getFirstName(),
108             contact.getMiddleName(), contact.getLastName(),
109             contact.getPrefixId(), contact.getSuffixId(), contact.isMale(),
110             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
111             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
112             contact.getJobTitle(), groupIds, organizationIds, roleIds,
113             userGroupRoles, userGroupIds, serviceContext);
114     }
115 
116     public static User updateUser(
117             ActionRequest actionRequest, long userId, String screenName,
118             String emailAddress, String openId, String languageId,
119             String timeZoneId, String greeting, String comments, String smsSn,
120             String aimSn, String facebookSn, String icqSn, String jabberSn,
121             String msnSn, String mySpaceSn, String skypeSn, String twitterSn,
122             String ymSn)
123         throws PortalException, SystemException {
124 
125         HttpServletRequest request = PortalUtil.getHttpServletRequest(
126             actionRequest);
127 
128         return updateUser(
129             request, userId, screenName, emailAddress, openId, languageId,
130             timeZoneId, greeting, comments, smsSn, aimSn, facebookSn, icqSn,
131             jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn);
132     }
133 
134 }