1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.mail.util;
16  
17  import com.liferay.mail.model.Filter;
18  import com.liferay.portal.googleapps.GoogleApps;
19  import com.liferay.portal.googleapps.GoogleAppsFactory;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.ContactConstants;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.UserLocalServiceUtil;
26  
27  import java.util.List;
28  
29  /**
30   * <a href="GoogleHook.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class GoogleHook implements Hook {
35  
36      public void addForward(
37          long companyId, long userId, List<Filter> filters,
38          List<String> emailAddresses, boolean leaveCopy) {
39      }
40  
41      public void addUser(
42          long companyId, long userId, String password, String firstName,
43          String middleName, String lastName, String emailAddress) {
44  
45          try {
46              String nickname = _getNickname(emailAddress);
47  
48              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
49  
50              googleApps.addUser(userId, password, firstName, lastName);
51              googleApps.addNickname(userId, nickname);
52              googleApps.addSendAs(
53                  userId,
54                  ContactConstants.getFullName(firstName, middleName, lastName),
55                  emailAddress);
56          }
57          catch (Exception e) {
58              _log.error(e, e);
59          }
60      }
61  
62      public void addVacationMessage(
63          long companyId, long userId, String emailAddress,
64          String vacationMessage) {
65      }
66  
67      public void deleteEmailAddress(long companyId, long userId) {
68          try {
69              User user = UserLocalServiceUtil.getUserById(userId);
70  
71              String nickname = _getNickname(user.getEmailAddress());
72  
73              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
74  
75              googleApps.deleteNickname(nickname);
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80      }
81  
82      public void deleteUser(long companyId, long userId) {
83          try {
84              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
85  
86              googleApps.deleteUser(userId);
87          }
88          catch (Exception e) {
89              _log.error(e, e);
90          }
91      }
92  
93      public void updateBlocked(
94          long companyId, long userId, List<String> blocked) {
95      }
96  
97      public void updateEmailAddress(
98          long companyId, long userId, String emailAddress) {
99  
100         try {
101             User user = UserLocalServiceUtil.getUserById(userId);
102 
103             deleteEmailAddress(companyId, userId);
104 
105             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
106 
107             googleApps.addNickname(userId, emailAddress);
108             googleApps.addSendAs(userId, user.getFullName(), emailAddress);
109         }
110         catch (Exception e) {
111             _log.error(e, e);
112         }
113     }
114 
115     public void updatePassword(long companyId, long userId, String password) {
116         try {
117             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
118 
119             googleApps.updatePassword(userId, password);
120         }
121         catch (Exception e) {
122             _log.error(e, e);
123         }
124     }
125 
126     private String _getNickname(String emailAddress) {
127         int pos = emailAddress.indexOf(StringPool.AT);
128 
129         return emailAddress.substring(0, pos);
130     }
131 
132     private static Log _log = LogFactoryUtil.getLog(GoogleHook.class);
133 
134 }