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.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            public void onAfterCreate(Contact contact) throws ModelListenerException {
035                    try {
036                            exportToLDAP(contact);
037                    }
038                    catch (Exception e) {
039                            throw new ModelListenerException(e);
040                    }
041            }
042    
043            public void onAfterUpdate(Contact contact) throws ModelListenerException {
044                    try {
045                            exportToLDAP(contact);
046                    }
047                    catch (Exception e) {
048                            throw new ModelListenerException(e);
049                    }
050            }
051    
052            protected void exportToLDAP(Contact contact) throws Exception {
053                    if (LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
054                            return;
055                    }
056    
057                    ServiceContext serviceContext =
058                            ServiceContextThreadLocal.getServiceContext();
059    
060                    Map<String, Serializable> expandoBridgeAttributes = null;
061    
062                    if (serviceContext != null) {
063                            expandoBridgeAttributes =
064                                    serviceContext.getExpandoBridgeAttributes();
065                    }
066    
067                    PortalLDAPExporterUtil.exportToLDAP(contact, expandoBridgeAttributes);
068            }
069    
070    }