1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.mail.util;
24  
25  import com.liferay.mail.model.Filter;
26  import com.liferay.portal.googleapps.GoogleApps;
27  import com.liferay.portal.googleapps.GoogleAppsFactory;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.ContactConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  
35  import java.util.List;
36  
37  /**
38   * <a href="GoogleHook.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class GoogleHook implements Hook {
44  
45      public void addForward(
46          long companyId, long userId, List<Filter> filters,
47          List<String> emailAddresses, boolean leaveCopy) {
48      }
49  
50      public void addUser(
51          long companyId, long userId, String password, String firstName,
52          String middleName, String lastName, String emailAddress) {
53  
54          try {
55              String nickname = _getNickname(emailAddress);
56  
57              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
58  
59              googleApps.addUser(userId, password, firstName, lastName);
60              googleApps.addNickname(userId, nickname);
61              googleApps.addSendAs(
62                  userId,
63                  ContactConstants.getFullName(firstName, middleName, lastName),
64                  emailAddress);
65          }
66          catch (Exception e) {
67              _log.error(e, e);
68          }
69      }
70  
71      public void addVacationMessage(
72          long companyId, long userId, String emailAddress,
73          String vacationMessage) {
74      }
75  
76      public void deleteEmailAddress(long companyId, long userId) {
77          try {
78              User user = UserLocalServiceUtil.getUserById(userId);
79  
80              String nickname = _getNickname(user.getEmailAddress());
81  
82              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
83  
84              googleApps.deleteNickname(nickname);
85          }
86          catch (Exception e) {
87              _log.error(e, e);
88          }
89      }
90  
91      public void deleteUser(long companyId, long userId) {
92          try {
93              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
94  
95              googleApps.deleteUser(userId);
96          }
97          catch (Exception e) {
98              _log.error(e, e);
99          }
100     }
101 
102     public void updateBlocked(
103         long companyId, long userId, List<String> blocked) {
104     }
105 
106     public void updateEmailAddress(
107         long companyId, long userId, String emailAddress) {
108 
109         try {
110             User user = UserLocalServiceUtil.getUserById(userId);
111 
112             deleteEmailAddress(companyId, userId);
113 
114             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
115 
116             googleApps.addNickname(userId, emailAddress);
117             googleApps.addSendAs(userId, user.getFullName(), emailAddress);
118         }
119         catch (Exception e) {
120             _log.error(e, e);
121         }
122     }
123 
124     public void updatePassword(long companyId, long userId, String password) {
125         try {
126             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
127 
128             googleApps.updatePassword(userId, password);
129         }
130         catch (Exception e) {
131             _log.error(e, e);
132         }
133     }
134 
135     private String _getNickname(String emailAddress) {
136         int pos = emailAddress.indexOf(StringPool.AT);
137 
138         return emailAddress.substring(0, pos);
139     }
140 
141     private static Log _log = LogFactoryUtil.getLog(GoogleHook.class);
142 
143 }