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.GetterUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * <a href="GNicknameManagerImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class GNicknameManagerImpl
35      extends GBaseManagerImpl implements GNicknameManager {
36  
37      public GNicknameManagerImpl(GoogleApps googleApps) {
38          super(googleApps);
39  
40          GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
41  
42          StringBundler sb = new StringBundler(4);
43  
44          sb.append(APPS_URL);
45          sb.append(StringPool.SLASH);
46          sb.append(gAuthenticator.getDomain());
47          sb.append("/nickname/2.0");
48  
49          nicknameURL = sb.toString();
50      }
51  
52      public void addGNickname(long userId, String nickname)
53          throws GoogleAppsException {
54  
55          Document document = SAXReaderUtil.createDocument();
56  
57          Element atomEntryElement = addAtomEntry(document);
58  
59          addAtomCategory(atomEntryElement, "nickname");
60  
61          Element appsLoginElement = atomEntryElement.addElement("apps:login");
62  
63          appsLoginElement.addAttribute("userName", String.valueOf(userId));
64  
65          Element appsNicknameElement = atomEntryElement.addElement(
66              "apps:nickname");
67  
68          appsNicknameElement.addAttribute("name", nickname);
69  
70          submitAdd(nicknameURL, document);
71      }
72  
73      public void deleteGNickname(String nickname) throws GoogleAppsException {
74          submitDelete(getNicknameURL(nickname));
75      }
76  
77      public GNickname getGNickname(String nickname) throws GoogleAppsException {
78          Document document = getDocument(getNicknameURL(nickname));
79  
80          if (hasError(document)) {
81              if (_log.isInfoEnabled()) {
82                  _log.info(getErrorMessage(document));
83              }
84  
85              return null;
86          }
87  
88          Element atomEntryElement = document.getRootElement();
89  
90          return getGNickname(atomEntryElement);
91      }
92  
93      public List<GNickname> getGNicknames() throws GoogleAppsException {
94          List<GNickname> gNicknames = new ArrayList<GNickname>();
95  
96          getGNicknames(gNicknames, nicknameURL);
97  
98          return gNicknames;
99      }
100 
101     protected GNickname getGNickname(Element atomEntryElement) {
102         GNickname gNickname = new GNickname();
103 
104         Element appsLoginElement = atomEntryElement.element(
105             getAppsQName("login"));
106         Element appsNicknameElement = atomEntryElement.element(
107             getAppsQName("nickname"));
108 
109         String nickname = appsNicknameElement.attributeValue("name");
110 
111         gNickname.setNickname(nickname);
112 
113         long userId = GetterUtil.getLong(
114             appsLoginElement.attributeValue("userName"));
115 
116         gNickname.setUserId(userId);
117 
118         return gNickname;
119     }
120 
121     protected void getGNicknames(final List<GNickname> gNicknames, String url)
122         throws GoogleAppsException {
123 
124         Document document = getDocument(url);
125 
126         Element atomFeedElement = document.getRootElement();
127 
128         List<Element> atomEntryElements = atomFeedElement.elements(
129             getAtomQName("entry"));
130 
131         for (Element atomEntryElement : atomEntryElements) {
132             GNickname gNickname = getGNickname(atomEntryElement);
133 
134             gNicknames.add(gNickname);
135         }
136 
137         new GetNextItems(url, atomFeedElement) {
138 
139             public void getNextItems(String nextURL)
140                 throws GoogleAppsException {
141 
142                 getGNicknames(gNicknames, nextURL);
143             }
144 
145         };
146     }
147 
148     protected String getNicknameURL(String nickname) {
149         return nicknameURL.concat(StringPool.SLASH).concat(nickname);
150     }
151 
152     protected String nicknameURL;
153 
154     private static Log _log = LogFactoryUtil.getLog(GNicknameManagerImpl.class);
155 
156 }