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.GoogleAppsException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class GNicknameManagerImpl
036            extends GBaseManagerImpl implements GNicknameManager {
037    
038            public GNicknameManagerImpl(GoogleApps googleApps) {
039                    super(googleApps);
040    
041                    GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
042    
043                    StringBundler sb = new StringBundler(4);
044    
045                    sb.append(APPS_URL);
046                    sb.append(StringPool.SLASH);
047                    sb.append(gAuthenticator.getDomain());
048                    sb.append("/nickname/2.0");
049    
050                    nicknameURL = sb.toString();
051            }
052    
053            public void addGNickname(long userId, String nickname)
054                    throws GoogleAppsException {
055    
056                    Document document = SAXReaderUtil.createDocument();
057    
058                    Element atomEntryElement = addAtomEntry(document);
059    
060                    addAtomCategory(atomEntryElement, "nickname");
061    
062                    Element appsLoginElement = atomEntryElement.addElement("apps:login");
063    
064                    appsLoginElement.addAttribute("userName", String.valueOf(userId));
065    
066                    Element appsNicknameElement = atomEntryElement.addElement(
067                            "apps:nickname");
068    
069                    appsNicknameElement.addAttribute("name", nickname);
070    
071                    submitAdd(nicknameURL, document);
072            }
073    
074            public void deleteGNickname(String nickname) throws GoogleAppsException {
075                    submitDelete(getNicknameURL(nickname));
076            }
077    
078            public GNickname getGNickname(String nickname) throws GoogleAppsException {
079                    Document document = getDocument(getNicknameURL(nickname));
080    
081                    if (hasError(document)) {
082                            if (_log.isInfoEnabled()) {
083                                    _log.info(getErrorMessage(document));
084                            }
085    
086                            return null;
087                    }
088    
089                    Element atomEntryElement = document.getRootElement();
090    
091                    return getGNickname(atomEntryElement);
092            }
093    
094            public List<GNickname> getGNicknames() throws GoogleAppsException {
095                    List<GNickname> gNicknames = new ArrayList<GNickname>();
096    
097                    getGNicknames(gNicknames, nicknameURL);
098    
099                    return gNicknames;
100            }
101    
102            protected GNickname getGNickname(Element atomEntryElement) {
103                    GNickname gNickname = new GNickname();
104    
105                    Element appsLoginElement = atomEntryElement.element(
106                            getAppsQName("login"));
107                    Element appsNicknameElement = atomEntryElement.element(
108                            getAppsQName("nickname"));
109    
110                    String nickname = appsNicknameElement.attributeValue("name");
111    
112                    gNickname.setNickname(nickname);
113    
114                    long userId = GetterUtil.getLong(
115                            appsLoginElement.attributeValue("userName"));
116    
117                    gNickname.setUserId(userId);
118    
119                    return gNickname;
120            }
121    
122            protected void getGNicknames(final List<GNickname> gNicknames, String url)
123                    throws GoogleAppsException {
124    
125                    Document document = getDocument(url);
126    
127                    Element atomFeedElement = document.getRootElement();
128    
129                    List<Element> atomEntryElements = atomFeedElement.elements(
130                            getAtomQName("entry"));
131    
132                    for (Element atomEntryElement : atomEntryElements) {
133                            GNickname gNickname = getGNickname(atomEntryElement);
134    
135                            gNicknames.add(gNickname);
136                    }
137    
138                    new GetNextItems(url, atomFeedElement) {
139    
140                            public void getNextItems(String nextURL)
141                                    throws GoogleAppsException {
142    
143                                    getGNicknames(gNicknames, nextURL);
144                            }
145    
146                    };
147            }
148    
149            protected String getNicknameURL(String nickname) {
150                    return nicknameURL.concat(StringPool.SLASH).concat(nickname);
151            }
152    
153            protected String nicknameURL;
154    
155            private static Log _log = LogFactoryUtil.getLog(GNicknameManagerImpl.class);
156    
157    }