001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.mail.util;
016    
017    import com.liferay.mail.model.Filter;
018    import com.liferay.portal.kernel.googleapps.GEmailSettingsManager;
019    import com.liferay.portal.kernel.googleapps.GNicknameManager;
020    import com.liferay.portal.kernel.googleapps.GUserManager;
021    import com.liferay.portal.kernel.googleapps.GoogleAppsFactoryUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.security.auth.FullNameGenerator;
027    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class GoogleHook implements Hook {
036    
037            public void addForward(
038                    long companyId, long userId, List<Filter> filters,
039                    List<String> emailAddresses, boolean leaveCopy) {
040            }
041    
042            public void addUser(
043                    long companyId, long userId, String password, String firstName,
044                    String middleName, String lastName, String emailAddress) {
045    
046                    try {
047                            String nickname = _getNickname(emailAddress);
048    
049                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
050                                    companyId);
051    
052                            gUserManager.addGUser(userId, password, firstName, lastName);
053    
054                            GNicknameManager gNicknameManager =
055                                    GoogleAppsFactoryUtil.getGNicknameManager(
056                                            companyId);
057    
058                            gNicknameManager.addGNickname(userId, nickname);
059    
060                            GEmailSettingsManager gEmailSettingsManager =
061                                    GoogleAppsFactoryUtil.getGEmailSettingsManager(
062                                            companyId);
063    
064                            FullNameGenerator fullNameGenerator =
065                                    FullNameGeneratorFactory.getInstance();
066    
067                            gEmailSettingsManager.addSendAs(
068                                    userId,
069                                    fullNameGenerator.getFullName(firstName, middleName, lastName),
070                                    emailAddress);
071                    }
072                    catch (Exception e) {
073                            _log.error(e, e);
074                    }
075            }
076    
077            public void addVacationMessage(
078                    long companyId, long userId, String emailAddress,
079                    String vacationMessage) {
080            }
081    
082            public void deleteEmailAddress(long companyId, long userId) {
083                    try {
084                            User user = UserLocalServiceUtil.getUserById(userId);
085    
086                            String nickname = _getNickname(user.getEmailAddress());
087    
088                            GNicknameManager gNicknameManager =
089                                    GoogleAppsFactoryUtil.getGNicknameManager(
090                                            companyId);
091    
092                            gNicknameManager.deleteGNickname(nickname);
093                    }
094                    catch (Exception e) {
095                            _log.error(e, e);
096                    }
097            }
098    
099            public void deleteUser(long companyId, long userId) {
100                    try {
101                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
102                                    companyId);
103    
104                            gUserManager.deleteGUser(userId);
105                    }
106                    catch (Exception e) {
107                            _log.error(e, e);
108                    }
109            }
110    
111            public void updateBlocked(
112                    long companyId, long userId, List<String> blocked) {
113            }
114    
115            public void updateEmailAddress(
116                    long companyId, long userId, String emailAddress) {
117    
118                    try {
119                            User user = UserLocalServiceUtil.getUserById(userId);
120    
121                            deleteEmailAddress(companyId, userId);
122    
123                            GNicknameManager gNicknameManager =
124                                    GoogleAppsFactoryUtil.getGNicknameManager(
125                                            companyId);
126    
127                            gNicknameManager.addGNickname(userId, emailAddress);
128    
129                            GEmailSettingsManager gEmailSettingsManager =
130                                    GoogleAppsFactoryUtil.getGEmailSettingsManager(
131                                            companyId);
132    
133                            gEmailSettingsManager.addSendAs(
134                                    userId, user.getFullName(), emailAddress);
135                    }
136                    catch (Exception e) {
137                            _log.error(e, e);
138                    }
139            }
140    
141            public void updatePassword(long companyId, long userId, String password) {
142                    try {
143                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
144                                    companyId);
145    
146                            gUserManager.updatePassword(userId, password);
147                    }
148                    catch (Exception e) {
149                            _log.error(e, e);
150                    }
151            }
152    
153            private String _getNickname(String emailAddress) {
154                    int pos = emailAddress.indexOf(StringPool.AT);
155    
156                    return emailAddress.substring(0, pos);
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(GoogleHook.class);
160    
161    }