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.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    }