View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.identitystore.modules.indexer.service;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
39  import fr.paris.lutece.plugins.grubusiness.business.indexing.IIndexingService;
40  import fr.paris.lutece.plugins.grubusiness.business.indexing.IndexingException;
41  import fr.paris.lutece.plugins.identitystore.business.Identity;
42  import fr.paris.lutece.plugins.identitystore.business.IdentityAttribute;
43  import fr.paris.lutece.plugins.identitystore.business.IdentityConstants;
44  import fr.paris.lutece.plugins.identitystore.service.IdentityChange;
45  import fr.paris.lutece.plugins.identitystore.service.IdentityChangeType;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  /**
51   * This class represents represents a service to index identities
52   */
53  public class IdentityIndexerService implements IIdentityIndexerService
54  {
55      private static final String ATTRIBUTE_IDENTITY_USER_GENDER = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_GENDER );
56      private static final String ATTRIBUTE_IDENTITY_USER_NAME_GIVEN = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_NAME_GIVEN );
57      private static final String ATTRIBUTE_IDENTITY_USER_NAME_PREFERRED_NAME = AppPropertiesService
58              .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_PREFERRED_NAME );
59      private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_ONLINE_EMAIL = AppPropertiesService
60              .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL );
61      private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER = AppPropertiesService
62              .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER );
63      private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_MOBILE_NUMBER = AppPropertiesService
64              .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER );
65      private static final String ATTRIBUTE_IDENTITY_USER_BDATE = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_BDATE );
66      private static final String ATTRIBUTE_IDENTITY_USER_NAME_FAMILY_NAME = AppPropertiesService
67              .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_FAMILY_NAME );
68  
69      private IIndexingService<Customer> _customerIndexService;
70  
71      /**
72       * Sets the customer index service to use
73       * 
74       * @param customerIndexService
75       *            the customer index service
76       */
77      public void setCustomerIndexService( IIndexingService<Customer> customerIndexService )
78      {
79          _customerIndexService = customerIndexService;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void index( IdentityChange identityChange ) throws IndexingException
87      {
88          if ( ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.CREATE.getValue( ) )
89                  || ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.UPDATE.getValue( ) ) )
90          {
91              index( identityChange.getIdentity( ) );
92          }
93          else
94              if ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.DELETE.getValue( ) )
95              {
96                  deleteIndex( identityChange.getIdentity( ) );
97              }
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public void index( List<IdentityChange> listIdentityChange ) throws IndexingException
105     {
106         List<Identity> listIdentity = new ArrayList<Identity>( );
107         for ( IdentityChange identityChange : listIdentityChange )
108         {
109             if ( ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.CREATE.getValue( ) )
110                     || ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.UPDATE.getValue( ) ) )
111             {
112                 listIdentity.add( identityChange.getIdentity( ) );
113             }
114             else
115                 if ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.DELETE.getValue( ) )
116                 {
117                     deleteIndex( identityChange.getIdentity( ) );
118                 }
119         }
120 
121         indexIdentities( listIdentity );
122     }
123 
124     /**
125      * Indexes the specified identity
126      *
127      * @param identity
128      *            the identity to index
129      * @throws IndexingException
130      *             if there is an exception during the indexing
131      * */
132     private void index( Identity identity ) throws IndexingException
133     {
134         Customer customer = buildCustomer( identity );
135 
136         _customerIndexService.index( customer );
137     }
138 
139     /**
140      * Indexes of list of identities
141      * 
142      * @param listIdentity
143      *            the list of identities to index
144      * @throws IndexingException
145      *             if there is an exception during the indexing
146      */
147     private void indexIdentities( List<Identity> listIdentity ) throws IndexingException
148     {
149         List<Customer> listCustomer = new ArrayList<Customer>( );
150         for ( Identity identity : listIdentity )
151         {
152             Customer customer = buildCustomer( identity );
153             listCustomer.add( customer );
154         }
155         _customerIndexService.indexList( listCustomer );
156     }
157 
158     /**
159      * Deletes the index for the specified identity
160      *
161      * @param identity
162      *            the identity
163      * @throws IndexingException
164      *             indexing exception
165      *
166      */
167     private void deleteIndex( Identity identity ) throws IndexingException
168     {
169         Customer customer = buildCustomer( identity );
170 
171         _customerIndexService.deleteIndex( customer );
172     }
173 
174     /**
175      * Build an identity to a customer.
176      *
177      * @param identity
178      *            the identity
179      * @return the customer
180      */
181     private Customer buildCustomer( Identity identity )
182     {
183         Customer customer = new Customer( );
184 
185         customer.setId( identity.getCustomerId( ) );
186         customer.setConnectionId( identity.getConnectionId( ) );
187 
188         if ( identity.getAttributes( ) != null )
189         {
190 
191             for ( IdentityAttribute attribute : identity.getAttributes( ).values( ) )
192             {
193                 String strKeyName = attribute.getAttributeKey( ).getKeyName( );
194 
195                 if ( ATTRIBUTE_IDENTITY_USER_GENDER.equals( strKeyName ) )
196                 {
197                     String strGender = getAttributeValue( attribute );
198                     customer.setIdTitle( StringUtils.isBlank( strGender ) || !StringUtils.isNumeric( strGender ) ? 0 : Integer.parseInt( strGender ) );
199                     continue;
200                 }
201 
202                 if ( ATTRIBUTE_IDENTITY_USER_NAME_GIVEN.equals( strKeyName ) )
203                 {
204                     customer.setFirstname( getAttributeValue( attribute ) );
205                     continue;
206                 }
207 
208                 if ( ATTRIBUTE_IDENTITY_USER_NAME_PREFERRED_NAME.equals( strKeyName ) )
209                 {
210                     customer.setLastname( getAttributeValue( attribute ) );
211                     continue;
212                 }
213 
214                 if ( ATTRIBUTE_IDENTITY_USER_NAME_FAMILY_NAME.equals( strKeyName ) )
215                 {
216                     customer.setFamilyname( getAttributeValue( attribute ) );
217                     continue;
218                 }
219 
220                 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_ONLINE_EMAIL.equals( strKeyName ) )
221                 {
222                     customer.setEmail( getAttributeValue( attribute ) );
223                     continue;
224                 }
225 
226                 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER.equals( strKeyName ) )
227                 {
228                     customer.setFixedPhoneNumber( getAttributeValue( attribute ) );
229                     continue;
230                 }
231 
232                 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_MOBILE_NUMBER.equals( strKeyName ) )
233                 {
234                     customer.setMobilePhone( getAttributeValue( attribute ) );
235                     continue;
236                 }
237 
238                 if ( ATTRIBUTE_IDENTITY_USER_BDATE.equals( strKeyName ) )
239                 {
240                     customer.setBirthDate( getAttributeValue( attribute ) );
241                     continue;
242                 }
243 
244                 customer.addAttributes( strKeyName, getAttributeValue( attribute ) );
245             }
246 
247         }
248 
249         return customer;
250     }
251 
252     /**
253      * Gets the attribute value from the identityAttribute
254      * 
255      * @param identityAttribute
256      *            the identityAttribute
257      * @return {@code null} if the identityAttribute does not exist, the identityAttribute value otherwise
258      */
259     private String getAttributeValue( IdentityAttribute identityAttribute )
260     {
261         return ( identityAttribute.getValue( ) == null ) ? StringUtils.EMPTY : identityAttribute.getValue( );
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     public void deleteAllIndexes( ) throws IndexingException
269     {
270         _customerIndexService.deleteAllIndexes( );
271     }
272 }