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  public class GoogleHook implements Hook {
43  
44      public void addForward(
45          long companyId, long userId, List<Filter> filters,
46          List<String> emailAddresses, boolean leaveCopy) {
47      }
48  
49      public void addUser(
50          long companyId, long userId, String password, String firstName,
51          String middleName, String lastName, String emailAddress) {
52  
53          try {
54              String nickname = _getNickname(emailAddress);
55  
56              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
57  
58              googleApps.addUser(userId, password, firstName, lastName);
59              googleApps.addNickname(userId, nickname);
60              googleApps.addSendAs(
61                  userId,
62                  ContactConstants.getFullName(firstName, middleName, lastName),
63                  emailAddress);
64          }
65          catch (Exception e) {
66              _log.error(e, e);
67          }
68      }
69  
70      public void addVacationMessage(
71          long companyId, long userId, String emailAddress,
72          String vacationMessage) {
73      }
74  
75      public void deleteEmailAddress(long companyId, long userId) {
76          try {
77              User user = UserLocalServiceUtil.getUserById(userId);
78  
79              String nickname = _getNickname(user.getEmailAddress());
80  
81              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
82  
83              googleApps.deleteNickname(nickname);
84          }
85          catch (Exception e) {
86              _log.error(e, e);
87          }
88      }
89  
90      public void deleteUser(long companyId, long userId) {
91          try {
92              GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
93  
94              googleApps.deleteUser(userId);
95          }
96          catch (Exception e) {
97              _log.error(e, e);
98          }
99      }
100 
101     public void updateBlocked(
102         long companyId, long userId, List<String> blocked) {
103     }
104 
105     public void updateEmailAddress(
106         long companyId, long userId, String emailAddress) {
107 
108         try {
109             User user = UserLocalServiceUtil.getUserById(userId);
110 
111             deleteEmailAddress(companyId, userId);
112 
113             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
114 
115             googleApps.addNickname(userId, emailAddress);
116             googleApps.addSendAs(userId, user.getFullName(), emailAddress);
117         }
118         catch (Exception e) {
119             _log.error(e, e);
120         }
121     }
122 
123     public void updatePassword(long companyId, long userId, String password) {
124         try {
125             GoogleApps googleApps = GoogleAppsFactory.getGoogleApps(companyId);
126 
127             googleApps.updatePassword(userId, password);
128         }
129         catch (Exception e) {
130             _log.error(e, e);
131         }
132     }
133 
134     private String _getNickname(String emailAddress) {
135         int pos = emailAddress.indexOf(StringPool.AT);
136 
137         return emailAddress.substring(0, pos);
138     }
139 
140     private static Log _log = LogFactoryUtil.getLog(GoogleHook.class);
141 
142 }