1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.googleapps;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.CharPool;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.xml.Document;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * <a href="GUserManagerImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class GUserManagerImpl extends GBaseManagerImpl implements GUserManager {
39  
40      public GUserManagerImpl(GoogleApps googleApps) {
41          super(googleApps);
42  
43          GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
44  
45          StringBundler sb = new StringBundler(4);
46  
47          sb.append(APPS_URL);
48          sb.append(StringPool.SLASH);
49          sb.append(gAuthenticator.getDomain());
50          sb.append("/user/2.0");
51  
52          userURL = sb.toString();
53      }
54  
55      public void addGUser(
56              long userId, String password, String firstName, String lastName)
57          throws GoogleAppsException {
58  
59          Document document = SAXReaderUtil.createDocument();
60  
61          Element atomEntryElement = addAtomEntry(document);
62  
63          addAtomCategory(atomEntryElement, "user");
64  
65          Element appsLoginElement = atomEntryElement.addElement(
66              "apps:login");
67  
68          appsLoginElement.addAttribute("password", password);
69          appsLoginElement.addAttribute("userName", String.valueOf(userId));
70  
71          Element appsNameElement = atomEntryElement.addElement("apps:name");
72  
73          appsNameElement.addAttribute("familyName", lastName);
74          appsNameElement.addAttribute("givenName", firstName);
75  
76          submitAdd(userURL, document);
77      }
78  
79      public void deleteGUser(long userId) throws GoogleAppsException {
80          submitDelete(getUserURL(userId));
81      }
82  
83      public GUser getGUser(long userId) throws GoogleAppsException {
84          Document document = getDocument(getUserURL(userId));
85  
86          if (hasError(document)) {
87              if (_log.isInfoEnabled()) {
88                  _log.info(getErrorMessage(document));
89              }
90  
91              return null;
92          }
93  
94          Element atomEntryElement = document.getRootElement();
95  
96          return getGUser(atomEntryElement);
97      }
98  
99      public GUser getGUser(String emailAddress) throws GoogleAppsException {
100         int pos = emailAddress.indexOf(CharPool.AT);
101 
102         if (pos == -1) {
103             return null;
104         }
105 
106         String nickname = emailAddress.substring(0, pos);
107 
108         if (Validator.isNumber(nickname)) {
109             long userId = GetterUtil.getLong(nickname);
110 
111             return getGUser(userId);
112         }
113         else {
114             try {
115                 User user = UserLocalServiceUtil.getUserByEmailAddress(
116                     getCompanyId(), emailAddress);
117 
118                 return getGUser(user.getUserId());
119             }
120             catch (Exception e) {
121             }
122 
123             GNicknameManager gNicknameManager =
124                 googleApps.getGNicknameManager();
125 
126             GNickname gNickname = gNicknameManager.getGNickname(nickname);
127 
128             if (gNickname != null) {
129                 return getGUser(gNickname.getUserId());
130             }
131 
132             return null;
133         }
134     }
135 
136     public List<GUser> getGUsers() throws GoogleAppsException {
137         List<GUser> gUsers = new ArrayList<GUser>();
138 
139         getGUsers(gUsers, userURL);
140 
141         return gUsers;
142     }
143 
144     public void updateActive(long userId, boolean active)
145         throws GoogleAppsException {
146 
147         Document document = getDocument(getUserURL(userId));
148 
149         if (hasError(document)) {
150             if (_log.isInfoEnabled()) {
151                 _log.info(getErrorMessage(document));
152             }
153 
154             return;
155         }
156 
157         Element atomEntryElement = document.getRootElement();
158 
159         Element appsLoginElement = atomEntryElement.element(
160             getAppsQName("login"));
161 
162         appsLoginElement.addAttribute("suspended", String.valueOf(!active));
163 
164         submitUpdate(getUserURL(userId), document);
165     }
166 
167     public void updatePassword(long userId, String password)
168         throws GoogleAppsException {
169 
170         Document document = getDocument(getUserURL(userId));
171 
172         if (hasError(document)) {
173             if (_log.isInfoEnabled()) {
174                 _log.info(getErrorMessage(document));
175             }
176 
177             return;
178         }
179 
180         Element atomEntryElement = document.getRootElement();
181 
182         Element appsLoginElement = atomEntryElement.element(
183             getAppsQName("login"));
184 
185         appsLoginElement.addAttribute("password", password);
186 
187         submitUpdate(getUserURL(userId), document);
188     }
189 
190     protected GUser getGUser(Element atomEntryElement) {
191         GUser gUser = new GUser();
192 
193         Element appsLoginElement = atomEntryElement.element(
194             getAppsQName("login"));
195         Element appsNameElement = atomEntryElement.element(
196             getAppsQName("name"));
197 
198         boolean active = !GetterUtil.getBoolean(
199             appsLoginElement.attributeValue("suspended"));
200 
201         gUser.setActive(active);
202 
203         boolean administrator = GetterUtil.getBoolean(
204             appsLoginElement.attributeValue("admin"));
205 
206         gUser.setAdministrator(administrator);
207 
208         boolean agreedToTermsOfUse = GetterUtil.getBoolean(
209             appsLoginElement.attributeValue("agreedToTerms"));
210 
211         gUser.setAgreedToTermsOfUse(agreedToTermsOfUse);
212 
213         String firstName = appsNameElement.attributeValue("givenName");
214 
215         gUser.setFirstName(firstName);
216 
217         String lastName = appsNameElement.attributeValue("familyName");
218 
219         gUser.setLastName(lastName);
220 
221         long userId = GetterUtil.getLong(
222             appsLoginElement.attributeValue("userName"));
223 
224         gUser.setUserId(userId);
225 
226         return gUser;
227     }
228 
229     protected void getGUsers(final List<GUser> gUsers, String url)
230         throws GoogleAppsException {
231 
232         Document document = getDocument(url);
233 
234         Element atomFeedElement = document.getRootElement();
235 
236         List<Element> atomEntryElements = atomFeedElement.elements(
237             getAtomQName("entry"));
238 
239         for (Element atomEntryElement : atomEntryElements) {
240             GUser gUser = getGUser(atomEntryElement);
241 
242             gUsers.add(gUser);
243         }
244 
245         new GetNextItems(url, atomFeedElement) {
246 
247             public void getNextItems(String nextURL)
248                 throws GoogleAppsException {
249 
250                 getGUsers(gUsers, nextURL);
251             }
252 
253         };
254     }
255 
256     protected String getUserURL(long userId) {
257         return userURL.concat(StringPool.SLASH).concat(String.valueOf(userId));
258     }
259 
260     protected String userURL;
261 
262     private static Log _log = LogFactoryUtil.getLog(GUserManagerImpl.class);
263 
264 }