001    /**
002     * Copyright (c) 2000-2011 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.ModelListenerException;
018    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
019    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portal.service.ServiceContextThreadLocal;
022    
023    import java.io.Serializable;
024    
025    import java.util.Map;
026    
027    /**
028     * @author Scott Lee
029     * @author Brian Wing Shun Chan
030     * @author Raymond Augé
031     */
032    public class ContactListener extends BaseModelListener<Contact> {
033    
034            @Override
035            public void onAfterCreate(Contact contact) throws ModelListenerException {
036                    try {
037                            exportToLDAP(contact);
038                    }
039                    catch (Exception e) {
040                            throw new ModelListenerException(e);
041                    }
042            }
043    
044            @Override
045            public void onAfterUpdate(Contact contact) throws ModelListenerException {
046                    try {
047                            exportToLDAP(contact);
048                    }
049                    catch (Exception e) {
050                            throw new ModelListenerException(e);
051                    }
052            }
053    
054            protected void exportToLDAP(Contact contact) throws Exception {
055                    if (LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
056                            return;
057                    }
058    
059                    ServiceContext serviceContext =
060                            ServiceContextThreadLocal.getServiceContext();
061    
062                    Map<String, Serializable> expandoBridgeAttributes = null;
063    
064                    if (serviceContext != null) {
065                            expandoBridgeAttributes =
066                                    serviceContext.getExpandoBridgeAttributes();
067                    }
068    
069                    PortalLDAPExporterUtil.exportToLDAP(contact, expandoBridgeAttributes);
070            }
071    
072    }