001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
020    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextThreadLocal;
023    
024    import java.io.Serializable;
025    
026    import java.util.Map;
027    
028    /**
029     * @author Scott Lee
030     * @author Brian Wing Shun Chan
031     * @author Raymond Aug??
032     */
033    public class ContactListener extends BaseModelListener<Contact> {
034    
035            @Override
036            public void onAfterCreate(Contact contact) {
037                    try {
038                            exportToLDAP(contact);
039                    }
040                    catch (Exception e) {
041                            _log.error(
042                                    "Unable to export contact with user ID " + contact.getUserId() +
043                                            " to LDAP on after create",
044                                    e);
045                    }
046            }
047    
048            @Override
049            public void onAfterUpdate(Contact contact) {
050                    try {
051                            exportToLDAP(contact);
052                    }
053                    catch (Exception e) {
054                            _log.error(
055                                    "Unable to export contact with user ID " + contact.getUserId() +
056                                            " to LDAP on after update",
057                                    e);
058                    }
059            }
060    
061            protected void exportToLDAP(Contact contact) throws Exception {
062                    if (LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
063                            return;
064                    }
065    
066                    ServiceContext serviceContext =
067                            ServiceContextThreadLocal.getServiceContext();
068    
069                    Map<String, Serializable> expandoBridgeAttributes = null;
070    
071                    if (serviceContext != null) {
072                            expandoBridgeAttributes =
073                                    serviceContext.getExpandoBridgeAttributes();
074                    }
075    
076                    PortalLDAPExporterUtil.exportToLDAP(contact, expandoBridgeAttributes);
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(ContactListener.class);
080    
081    }