001    /**
002     * Copyright (c) 2000-2013 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.CharPool;
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            @Override
038            public void addForward(
039                    long companyId, long userId, List<Filter> filters,
040                    List<String> emailAddresses, boolean leaveCopy) {
041            }
042    
043            @Override
044            public void addUser(
045                    long companyId, long userId, String password, String firstName,
046                    String middleName, String lastName, String emailAddress) {
047    
048                    try {
049                            String nickname = _getNickname(emailAddress);
050    
051                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
052                                    companyId);
053    
054                            gUserManager.addGUser(userId, password, firstName, lastName);
055    
056                            GNicknameManager gNicknameManager =
057                                    GoogleAppsFactoryUtil.getGNicknameManager(companyId);
058    
059                            gNicknameManager.addGNickname(userId, nickname);
060    
061                            GEmailSettingsManager gEmailSettingsManager =
062                                    GoogleAppsFactoryUtil.getGEmailSettingsManager(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            @Override
078            public void addVacationMessage(
079                    long companyId, long userId, String emailAddress,
080                    String vacationMessage) {
081            }
082    
083            @Override
084            public void deleteEmailAddress(long companyId, long userId) {
085                    try {
086                            User user = UserLocalServiceUtil.getUserById(userId);
087    
088                            String nickname = _getNickname(user.getEmailAddress());
089    
090                            GNicknameManager gNicknameManager =
091                                    GoogleAppsFactoryUtil.getGNicknameManager(companyId);
092    
093                            gNicknameManager.deleteGNickname(nickname);
094                    }
095                    catch (Exception e) {
096                            _log.error(e, e);
097                    }
098            }
099    
100            @Override
101            public void deleteUser(long companyId, long userId) {
102                    try {
103                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
104                                    companyId);
105    
106                            gUserManager.deleteGUser(userId);
107                    }
108                    catch (Exception e) {
109                            _log.error(e, e);
110                    }
111            }
112    
113            @Override
114            public void updateBlocked(
115                    long companyId, long userId, List<String> blocked) {
116            }
117    
118            @Override
119            public void updateEmailAddress(
120                    long companyId, long userId, String emailAddress) {
121    
122                    try {
123                            User user = UserLocalServiceUtil.getUserById(userId);
124    
125                            deleteEmailAddress(companyId, userId);
126    
127                            GNicknameManager gNicknameManager =
128                                    GoogleAppsFactoryUtil.getGNicknameManager(companyId);
129    
130                            String nickname = _getNickname(emailAddress);
131    
132                            gNicknameManager.addGNickname(userId, nickname);
133    
134                            GEmailSettingsManager gEmailSettingsManager =
135                                    GoogleAppsFactoryUtil.getGEmailSettingsManager(companyId);
136    
137                            gEmailSettingsManager.addSendAs(
138                                    userId, user.getFullName(), emailAddress);
139                    }
140                    catch (Exception e) {
141                            _log.error(e, e);
142                    }
143            }
144    
145            @Override
146            public void updatePassword(long companyId, long userId, String password) {
147                    try {
148                            GUserManager gUserManager = GoogleAppsFactoryUtil.getGUserManager(
149                                    companyId);
150    
151                            gUserManager.updatePassword(userId, password);
152                    }
153                    catch (Exception e) {
154                            _log.error(e, e);
155                    }
156            }
157    
158            private String _getNickname(String emailAddress) {
159                    int pos = emailAddress.indexOf(CharPool.AT);
160    
161                    return emailAddress.substring(0, pos);
162            }
163    
164            private static Log _log = LogFactoryUtil.getLog(GoogleHook.class);
165    
166    }