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.portal.googleapps;
016    
017    import com.liferay.portal.kernel.googleapps.GGroup;
018    import com.liferay.portal.kernel.googleapps.GGroupManager;
019    import com.liferay.portal.kernel.googleapps.GGroupMember;
020    import com.liferay.portal.kernel.googleapps.GGroupOwner;
021    import com.liferay.portal.kernel.googleapps.GUser;
022    import com.liferay.portal.kernel.googleapps.GUserManager;
023    import com.liferay.portal.kernel.googleapps.GoogleAppsException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.xml.Attribute;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.kernel.xml.SAXReaderUtil;
033    
034    import java.util.ArrayList;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class GGroupManagerImpl
041            extends GBaseManagerImpl implements GGroupManager {
042    
043            public GGroupManagerImpl(GoogleApps googleApps) {
044                    super(googleApps);
045    
046                    GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
047    
048                    groupURL = APPS_URL.concat("/group/2.0/").concat(
049                            gAuthenticator.getDomain());
050            }
051    
052            @Override
053            public void addGGroupMember(
054                            String groupEmailAddress, String memberEmailAddress)
055                    throws GoogleAppsException {
056    
057                    Document document = SAXReaderUtil.createDocument();
058    
059                    Element atomEntryElement = addAtomEntry(document);
060    
061                    addAppsProperty(atomEntryElement, "memberId", memberEmailAddress);
062    
063                    StringBundler sb = new StringBundler(4);
064    
065                    sb.append(groupURL);
066                    sb.append(StringPool.SLASH);
067                    sb.append(groupEmailAddress);
068                    sb.append("/member");
069    
070                    submitAdd(sb.toString(), document);
071            }
072    
073            @Override
074            public void addGGroupOwner(
075                            String groupEmailAddress, String ownerEmailAddress)
076                    throws GoogleAppsException {
077    
078                    Document document = SAXReaderUtil.createDocument();
079    
080                    Element atomEntryElement = addAtomEntry(document);
081    
082                    addAppsProperty(atomEntryElement, "email", ownerEmailAddress);
083    
084                    StringBundler sb = new StringBundler(4);
085    
086                    sb.append(groupURL);
087                    sb.append(StringPool.SLASH);
088                    sb.append(groupEmailAddress);
089                    sb.append("/owner");
090    
091                    submitAdd(sb.toString(), document);
092            }
093    
094            @Override
095            public void deleteGGroup(String emailAddress) throws GoogleAppsException {
096                    submitDelete(getGroupURL(emailAddress));
097            }
098    
099            @Override
100            public void deleteGGroupMember(
101                            String groupEmailAddress, String memberEmailAddress)
102                    throws GoogleAppsException {
103    
104                    StringBundler sb = new StringBundler(5);
105    
106                    sb.append(groupURL);
107                    sb.append(StringPool.SLASH);
108                    sb.append(groupEmailAddress);
109                    sb.append("/member/");
110                    sb.append(memberEmailAddress);
111    
112                    submitDelete(sb.toString());
113            }
114    
115            @Override
116            public void deleteGGroupOwner(
117                            String groupEmailAddress, String ownerEmailAddress)
118                    throws GoogleAppsException {
119    
120                    StringBundler sb = new StringBundler(5);
121    
122                    sb.append(groupURL);
123                    sb.append(StringPool.SLASH);
124                    sb.append(groupEmailAddress);
125                    sb.append("/owner/");
126                    sb.append(ownerEmailAddress);
127    
128                    submitDelete(sb.toString());
129            }
130    
131            @Override
132            public GGroup getGGroup(String emailAddress) throws GoogleAppsException {
133                    Document document = getDocument(getGroupURL(emailAddress));
134    
135                    if (hasError(document)) {
136                            if (_log.isInfoEnabled()) {
137                                    _log.info(getErrorMessage(document));
138                            }
139    
140                            return null;
141                    }
142    
143                    Element atomEntryElement = document.getRootElement();
144    
145                    return getGGroup(atomEntryElement);
146            }
147    
148            @Override
149            public GGroupMember getGGroupMember(
150                            String groupEmailAddress, String memberEmailAddress)
151                    throws GoogleAppsException {
152    
153                    StringBundler sb = new StringBundler(5);
154    
155                    sb.append(groupURL);
156                    sb.append(StringPool.SLASH);
157                    sb.append(groupEmailAddress);
158                    sb.append("/member/");
159                    sb.append(memberEmailAddress);
160    
161                    Document document = getDocument(sb.toString());
162    
163                    if (hasError(document)) {
164                            if (_log.isInfoEnabled()) {
165                                    _log.info(getErrorMessage(document));
166                            }
167    
168                            return null;
169                    }
170    
171                    Element atomEntryElement = document.getRootElement();
172    
173                    return getGGroupMember(atomEntryElement);
174            }
175    
176            @Override
177            public List<GGroupMember> getGGroupMembers(String emailAddress)
178                    throws GoogleAppsException {
179    
180                    List<GGroupMember> gGroupMembers = new ArrayList<GGroupMember>();
181    
182                    StringBundler sb = new StringBundler(4);
183    
184                    sb.append(groupURL);
185                    sb.append(StringPool.SLASH);
186                    sb.append(emailAddress);
187                    sb.append("/member");
188    
189                    getGGroupMembers(gGroupMembers, sb.toString());
190    
191                    return gGroupMembers;
192            }
193    
194            @Override
195            public GGroupOwner getGGroupOwner(
196                            String groupEmailAddress, String ownerEmailAddress)
197                    throws GoogleAppsException {
198    
199                    StringBundler sb = new StringBundler(5);
200    
201                    sb.append(groupURL);
202                    sb.append(StringPool.SLASH);
203                    sb.append(groupEmailAddress);
204                    sb.append("/owner/");
205                    sb.append(ownerEmailAddress);
206    
207                    Document document = getDocument(sb.toString());
208    
209                    if (hasError(document)) {
210                            if (_log.isInfoEnabled()) {
211                                    _log.info(getErrorMessage(document));
212                            }
213    
214                            return null;
215                    }
216    
217                    Element atomEntryElement = document.getRootElement();
218    
219                    return getGGroupOwner(atomEntryElement);
220            }
221    
222            @Override
223            public List<GGroupOwner> getGGroupOwners(String emailAddress)
224                    throws GoogleAppsException {
225    
226                    List<GGroupOwner> gGroupOwners = new ArrayList<GGroupOwner>();
227    
228                    StringBundler sb = new StringBundler(4);
229    
230                    sb.append(groupURL);
231                    sb.append(StringPool.SLASH);
232                    sb.append(emailAddress);
233                    sb.append("/owner");
234    
235                    getGGroupOwners(gGroupOwners, sb.toString());
236    
237                    return gGroupOwners;
238            }
239    
240            @Override
241            public List<GGroup> getGGroups() throws GoogleAppsException {
242                    List<GGroup> gGroups = new ArrayList<GGroup>();
243    
244                    getGGroups(gGroups, groupURL);
245    
246                    return gGroups;
247            }
248    
249            @Override
250            public List<GGroup> getGGroups(long userId, boolean directOnly)
251                    throws GoogleAppsException {
252    
253                    List<GGroup> gGroups = new ArrayList<GGroup>();
254    
255                    StringBundler sb = new StringBundler(4);
256    
257                    sb.append(groupURL);
258                    sb.append("?member=");
259                    sb.append(userId);
260                    sb.append("&directOnly=");
261                    sb.append(directOnly);
262    
263                    getGGroups(gGroups, sb.toString());
264    
265                    return gGroups;
266            }
267    
268            @Override
269            public void updateDescription(String emailAddress, String description)
270                    throws GoogleAppsException {
271    
272                    Document document = getDocument(getGroupURL(emailAddress));
273    
274                    if (hasError(document)) {
275                            if (_log.isInfoEnabled()) {
276                                    _log.info(getErrorMessage(document));
277                            }
278    
279                            return;
280                    }
281    
282                    Element atomEntryElement = document.getRootElement();
283    
284                    List<Element> appsPropertyElements = atomEntryElement.elements(
285                            getAppsQName("property"));
286    
287                    for (Element appsPropertyElement : appsPropertyElements) {
288                            String name = appsPropertyElement.attributeValue("name");
289    
290                            if (name.equals("description")) {
291                                    Attribute valueAttribute = appsPropertyElement.attribute(
292                                            "value");
293    
294                                    valueAttribute.setValue(description);
295                            }
296                    }
297    
298                    submitUpdate(getGroupURL(emailAddress), document);
299            }
300    
301            protected GGroup getGGroup(Element atomEntryElement) {
302                    GGroup gGroup = new GGroup();
303    
304                    List<Element> appsPropertyElements = atomEntryElement.elements(
305                            getAppsQName("property"));
306    
307                    for (Element appsPropertyElement : appsPropertyElements) {
308                            String name = appsPropertyElement.attributeValue("name");
309                            String value = appsPropertyElement.attributeValue("value");
310    
311                            if (name.equals("description")) {
312                                    gGroup.setDescription(value);
313                            }
314                            else if (name.equals("emailPermission")) {
315                                    gGroup.setEmailPermission(value);
316                            }
317                            else if (name.equals("groupId")) {
318                                    gGroup.setEmailAddress(value);
319                            }
320                            else if (name.equals("groupName")) {
321                                    gGroup.setName(value);
322                            }
323                            else if (name.equals("permissionPreset")) {
324                                    gGroup.setPermissionPreset(value);
325                            }
326                    }
327    
328                    return gGroup;
329            }
330    
331            protected GGroupMember getGGroupMember(Element atomEntryElement)
332                    throws GoogleAppsException {
333    
334                    GGroupMember gGroupMember = new GGroupMember();
335    
336                    List<Element> appsPropertyElements = atomEntryElement.elements(
337                            getAppsQName("property"));
338    
339                    for (Element appsPropertyElement : appsPropertyElements) {
340                            String name = appsPropertyElement.attributeValue("name");
341                            String value = appsPropertyElement.attributeValue("value");
342    
343                            if (name.equals("directMember")) {
344                                    gGroupMember.setDirect(GetterUtil.getBoolean(value));
345                            }
346                            else if (name.equals("memberId")) {
347                                    gGroupMember.setEmailAddress(value);
348                            }
349                            else if (name.equals("memberType")) {
350                                    gGroupMember.setType(value);
351                            }
352                    }
353    
354                    String type = gGroupMember.getType();
355    
356                    if (type.equals("Group")) {
357                            GGroup gGroup = getGGroup(gGroupMember.getEmailAddress());
358    
359                            gGroupMember.setGGroup(gGroup);
360                    }
361                    else if (type.equals("User")) {
362                            GUserManager gUserManager = googleApps.getGUserManager();
363    
364                            GUser gUser = gUserManager.getGUser(gGroupMember.getEmailAddress());
365    
366                            gGroupMember.setGUser(gUser);
367                    }
368    
369                    return gGroupMember;
370            }
371    
372            protected void getGGroupMembers(
373                            final List<GGroupMember> gGroupMembers, String url)
374                    throws GoogleAppsException {
375    
376                    Document document = getDocument(url);
377    
378                    Element atomFeedElement = document.getRootElement();
379    
380                    List<Element> atomEntryElements = atomFeedElement.elements(
381                            getAtomQName("entry"));
382    
383                    for (Element atomEntryElement : atomEntryElements) {
384                            GGroupMember gGroupMember = getGGroupMember(atomEntryElement);
385    
386                            gGroupMembers.add(gGroupMember);
387                    }
388    
389                    new GetNextItems(url, atomFeedElement) {
390    
391                            @Override
392                            public void getNextItems(String nextURL)
393                                    throws GoogleAppsException {
394    
395                                    getGGroupMembers(gGroupMembers, nextURL);
396                            }
397    
398                    };
399            }
400    
401            protected GGroupOwner getGGroupOwner(Element atomEntryElement)
402                    throws GoogleAppsException {
403    
404                    GGroupOwner gGroupOwner = new GGroupOwner();
405    
406                    List<Element> appsPropertyElements = atomEntryElement.elements(
407                            getAppsQName("property"));
408    
409                    for (Element appsPropertyElement : appsPropertyElements) {
410                            String name = appsPropertyElement.attributeValue("name");
411                            String value = appsPropertyElement.attributeValue("value");
412    
413                            if (name.equals("email")) {
414                                    gGroupOwner.setEmailAddress(value);
415                            }
416                            else if (name.equals("type")) {
417                                    gGroupOwner.setType(value);
418                            }
419                    }
420    
421                    String type = gGroupOwner.getType();
422    
423                    if (type.equals("Group")) {
424                            GGroup gGroup = getGGroup(gGroupOwner.getEmailAddress());
425    
426                            gGroupOwner.setGGroup(gGroup);
427                    }
428                    else if (type.equals("User")) {
429                            GUserManager gUserManager = googleApps.getGUserManager();
430    
431                            GUser gUser = gUserManager.getGUser(gGroupOwner.getEmailAddress());
432    
433                            gGroupOwner.setGUser(gUser);
434                    }
435    
436                    return gGroupOwner;
437            }
438    
439            protected void getGGroupOwners(
440                            final List<GGroupOwner> gGroupOwners, String url)
441                    throws GoogleAppsException {
442    
443                    Document document = getDocument(url);
444    
445                    Element atomFeedElement = document.getRootElement();
446    
447                    List<Element> atomEntryElements = atomFeedElement.elements(
448                            getAtomQName("entry"));
449    
450                    for (Element atomEntryElement : atomEntryElements) {
451                            GGroupOwner gGroupOwner = getGGroupOwner(atomEntryElement);
452    
453                            gGroupOwners.add(gGroupOwner);
454                    }
455    
456                    new GetNextItems(url, atomFeedElement) {
457    
458                            @Override
459                            public void getNextItems(String nextURL)
460                                    throws GoogleAppsException {
461    
462                                    getGGroupOwners(gGroupOwners, nextURL);
463                            }
464    
465                    };
466            }
467    
468            protected void getGGroups(final List<GGroup> gGroups, String url)
469                    throws GoogleAppsException {
470    
471                    Document document = getDocument(url);
472    
473                    Element atomFeedElement = document.getRootElement();
474    
475                    List<Element> atomEntryElements = atomFeedElement.elements(
476                            getAtomQName("entry"));
477    
478                    for (Element atomEntryElement : atomEntryElements) {
479                            GGroup gGroup = getGGroup(atomEntryElement);
480    
481                            gGroups.add(gGroup);
482                    }
483    
484                    new GetNextItems(url, atomFeedElement) {
485    
486                            @Override
487                            public void getNextItems(String nextURL)
488                                    throws GoogleAppsException {
489    
490                                    getGGroups(gGroups, nextURL);
491                            }
492    
493                    };
494            }
495    
496            protected String getGroupURL(String emailAddress) {
497                    return groupURL.concat(StringPool.SLASH).concat(emailAddress);
498            }
499    
500            protected String groupURL;
501    
502            private static Log _log = LogFactoryUtil.getLog(GGroupManagerImpl.class);
503    
504    }