001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.googleapps;
016    
017    import com.liferay.portal.kernel.googleapps.GNickname;
018    import com.liferay.portal.kernel.googleapps.GNicknameManager;
019    import com.liferay.portal.kernel.googleapps.GUser;
020    import com.liferay.portal.kernel.googleapps.GUserManager;
021    import com.liferay.portal.kernel.googleapps.GoogleAppsException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.CharPool;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.xml.Document;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.kernel.xml.SAXReaderUtil;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.service.UserLocalServiceUtil;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class GUserManagerImpl extends GBaseManagerImpl implements GUserManager {
042    
043            public GUserManagerImpl(GoogleApps googleApps) {
044                    super(googleApps);
045    
046                    GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
047    
048                    StringBundler sb = new StringBundler(4);
049    
050                    sb.append(APPS_URL);
051                    sb.append(StringPool.SLASH);
052                    sb.append(gAuthenticator.getDomain());
053                    sb.append("/user/2.0");
054    
055                    userURL = sb.toString();
056            }
057    
058            public void addGUser(
059                            long userId, String password, String firstName, String lastName)
060                    throws GoogleAppsException {
061    
062                    Document document = SAXReaderUtil.createDocument();
063    
064                    Element atomEntryElement = addAtomEntry(document);
065    
066                    addAtomCategory(atomEntryElement, "user");
067    
068                    Element appsLoginElement = atomEntryElement.addElement(
069                            "apps:login");
070    
071                    appsLoginElement.addAttribute("password", password);
072                    appsLoginElement.addAttribute("userName", String.valueOf(userId));
073    
074                    Element appsNameElement = atomEntryElement.addElement("apps:name");
075    
076                    appsNameElement.addAttribute("familyName", lastName);
077                    appsNameElement.addAttribute("givenName", firstName);
078    
079                    submitAdd(userURL, document);
080            }
081    
082            public void deleteGUser(long userId) throws GoogleAppsException {
083                    submitDelete(getUserURL(userId));
084            }
085    
086            public GUser getGUser(long userId) throws GoogleAppsException {
087                    Document document = getDocument(getUserURL(userId));
088    
089                    if (hasError(document)) {
090                            if (_log.isInfoEnabled()) {
091                                    _log.info(getErrorMessage(document));
092                            }
093    
094                            return null;
095                    }
096    
097                    Element atomEntryElement = document.getRootElement();
098    
099                    return getGUser(atomEntryElement);
100            }
101    
102            public GUser getGUser(String emailAddress) throws GoogleAppsException {
103                    int pos = emailAddress.indexOf(CharPool.AT);
104    
105                    if (pos == -1) {
106                            return null;
107                    }
108    
109                    String nickname = emailAddress.substring(0, pos);
110    
111                    if (Validator.isNumber(nickname)) {
112                            long userId = GetterUtil.getLong(nickname);
113    
114                            return getGUser(userId);
115                    }
116                    else {
117                            try {
118                                    User user = UserLocalServiceUtil.getUserByEmailAddress(
119                                            getCompanyId(), emailAddress);
120    
121                                    return getGUser(user.getUserId());
122                            }
123                            catch (Exception e) {
124                            }
125    
126                            GNicknameManager gNicknameManager =
127                                    googleApps.getGNicknameManager();
128    
129                            GNickname gNickname = gNicknameManager.getGNickname(nickname);
130    
131                            if (gNickname != null) {
132                                    return getGUser(gNickname.getUserId());
133                            }
134    
135                            return null;
136                    }
137            }
138    
139            public List<GUser> getGUsers() throws GoogleAppsException {
140                    List<GUser> gUsers = new ArrayList<GUser>();
141    
142                    getGUsers(gUsers, userURL);
143    
144                    return gUsers;
145            }
146    
147            public void updateActive(long userId, boolean active)
148                    throws GoogleAppsException {
149    
150                    Document document = getDocument(getUserURL(userId));
151    
152                    if (hasError(document)) {
153                            if (_log.isInfoEnabled()) {
154                                    _log.info(getErrorMessage(document));
155                            }
156    
157                            return;
158                    }
159    
160                    Element atomEntryElement = document.getRootElement();
161    
162                    Element appsLoginElement = atomEntryElement.element(
163                            getAppsQName("login"));
164    
165                    appsLoginElement.addAttribute("suspended", String.valueOf(!active));
166    
167                    submitUpdate(getUserURL(userId), document);
168            }
169    
170            public void updatePassword(long userId, String password)
171                    throws GoogleAppsException {
172    
173                    Document document = getDocument(getUserURL(userId));
174    
175                    if (hasError(document)) {
176                            if (_log.isInfoEnabled()) {
177                                    _log.info(getErrorMessage(document));
178                            }
179    
180                            return;
181                    }
182    
183                    Element atomEntryElement = document.getRootElement();
184    
185                    Element appsLoginElement = atomEntryElement.element(
186                            getAppsQName("login"));
187    
188                    appsLoginElement.addAttribute("password", password);
189    
190                    submitUpdate(getUserURL(userId), document);
191            }
192    
193            protected GUser getGUser(Element atomEntryElement) {
194                    GUser gUser = new GUser();
195    
196                    Element appsLoginElement = atomEntryElement.element(
197                            getAppsQName("login"));
198                    Element appsNameElement = atomEntryElement.element(
199                            getAppsQName("name"));
200    
201                    boolean active = !GetterUtil.getBoolean(
202                            appsLoginElement.attributeValue("suspended"));
203    
204                    gUser.setActive(active);
205    
206                    boolean administrator = GetterUtil.getBoolean(
207                            appsLoginElement.attributeValue("admin"));
208    
209                    gUser.setAdministrator(administrator);
210    
211                    boolean agreedToTermsOfUse = GetterUtil.getBoolean(
212                            appsLoginElement.attributeValue("agreedToTerms"));
213    
214                    gUser.setAgreedToTermsOfUse(agreedToTermsOfUse);
215    
216                    String firstName = appsNameElement.attributeValue("givenName");
217    
218                    gUser.setFirstName(firstName);
219    
220                    String lastName = appsNameElement.attributeValue("familyName");
221    
222                    gUser.setLastName(lastName);
223    
224                    long userId = GetterUtil.getLong(
225                            appsLoginElement.attributeValue("userName"));
226    
227                    gUser.setUserId(userId);
228    
229                    return gUser;
230            }
231    
232            protected void getGUsers(final List<GUser> gUsers, String url)
233                    throws GoogleAppsException {
234    
235                    Document document = getDocument(url);
236    
237                    Element atomFeedElement = document.getRootElement();
238    
239                    List<Element> atomEntryElements = atomFeedElement.elements(
240                            getAtomQName("entry"));
241    
242                    for (Element atomEntryElement : atomEntryElements) {
243                            GUser gUser = getGUser(atomEntryElement);
244    
245                            gUsers.add(gUser);
246                    }
247    
248                    new GetNextItems(url, atomFeedElement) {
249    
250                            public void getNextItems(String nextURL)
251                                    throws GoogleAppsException {
252    
253                                    getGUsers(gUsers, nextURL);
254                            }
255    
256                    };
257            }
258    
259            protected String getUserURL(long userId) {
260                    return userURL.concat(StringPool.SLASH).concat(String.valueOf(userId));
261            }
262    
263            protected String userURL;
264    
265            private static Log _log = LogFactoryUtil.getLog(GUserManagerImpl.class);
266    
267    }