View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of 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.service.indexer.search;
35  
36  import fr.paris.lutece.plugins.identitystore.service.contract.AttributeCertificationDefinitionService;
37  import fr.paris.lutece.plugins.identitystore.service.indexer.elastic.index.model.AttributeObject;
38  import fr.paris.lutece.plugins.identitystore.service.indexer.elastic.index.model.IdentityObject;
39  import fr.paris.lutece.plugins.identitystore.service.indexer.elastic.search.model.inner.response.Response;
40  import fr.paris.lutece.plugins.identitystore.service.indexer.elastic.search.service.IIdentitySearcher;
41  import fr.paris.lutece.plugins.identitystore.service.search.ISearchIdentityService;
42  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeDto;
43  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.ExpirationDefinition;
44  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.IdentityDto;
45  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.QualifiedIdentitySearchResult;
46  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchAttribute;
47  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
48  
49  import java.util.ArrayList;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.stream.Collectors;
53  
54  public class ElasticSearchIdentityService implements ISearchIdentityService
55  {
56      private final IIdentitySearcher _identitySearcher;
57  
58      /**
59       * private constructor
60       */
61      private ElasticSearchIdentityService( IIdentitySearcher _identitySearcher )
62      {
63          this._identitySearcher = _identitySearcher;
64      }
65  
66      /**
67       * {@inheritDoc }
68       */
69      @Override
70      public QualifiedIdentitySearchResult getQualifiedIdentities( final List<SearchAttribute> attributes,
71              final List<List<SearchAttribute>> specialTreatmentAttributes, final Integer nbEqualAttributes, final Integer nbMissingAttributes, final int max,
72              final boolean connected, final List<String> attributesFilter ) throws IdentityStoreException
73      {
74          final List<SearchAttribute> searchAttributes = this.computeOutputKeys( attributes );
75          final List<List<SearchAttribute>> specialAttributes = specialTreatmentAttributes == null ? null
76                  : specialTreatmentAttributes.stream( ).map( this::computeOutputKeys ).collect( Collectors.toList( ) );
77  
78          final Response search = _identitySearcher.multiSearch( searchAttributes, specialAttributes, nbEqualAttributes, nbMissingAttributes, max, connected,
79                  attributesFilter );
80  
81          return new QualifiedIdentitySearchResult( this.getEntities( search ), search.getMetadata( ) );
82      }
83  
84      @Override
85      public QualifiedIdentitySearchResult getQualifiedIdentities( List<SearchAttribute> attributes, int max, boolean connected,
86              final List<String> attributesFilter ) throws IdentityStoreException
87      {
88          final List<SearchAttribute> searchAttributes = this.computeOutputKeys( attributes );
89  
90          final Response search = _identitySearcher.search( searchAttributes, max, connected, attributesFilter );
91  
92          return new QualifiedIdentitySearchResult( this.getEntities( search ), search.getMetadata( ) );
93      }
94  
95      @Override
96      public QualifiedIdentitySearchResult getQualifiedIdentities( String customerId, final List<String> attributesFilter ) throws IdentityStoreException
97      {
98          final Response search = _identitySearcher.search( customerId, attributesFilter );
99  
100         return new QualifiedIdentitySearchResult( this.getEntities( search ), search.getMetadata( ) );
101     }
102 
103     @Override
104     public QualifiedIdentitySearchResult getQualifiedIdentities( List<String> customerIds, final List<String> attributesFilter ) throws IdentityStoreException
105     {
106         final Response search = _identitySearcher.search( customerIds, attributesFilter );
107 
108         return new QualifiedIdentitySearchResult( this.getEntities( search ), search.getMetadata( ) );
109     }
110 
111     private List<IdentityDto> getEntities( Response search )
112     {
113         final List<IdentityDto> identities = new ArrayList<>( );
114 
115         if ( search != null )
116         {
117             search.getResult( ).getHits( ).forEach( hit -> {
118                 identities.add( this.toQualifiedIdentity( hit.getSource( ) ) );
119             } );
120         }
121         return identities;
122     }
123 
124     private IdentityDto toQualifiedIdentity( final IdentityObject identityObject )
125     {
126         final IdentityDto identity = new IdentityDto( );
127         identity.setConnectionId( identityObject.getConnectionId( ) );
128         identity.setCustomerId( identityObject.getCustomerId( ) );
129         identity.setCreationDate( identityObject.getCreationDate( ) );
130         identity.setLastUpdateDate( identityObject.getLastUpdateDate( ) );
131         if( identityObject.getExpirationDate( ) != null )
132         {
133             identity.setExpiration( new ExpirationDefinition());
134             identity.getExpiration().setExpirationDate(identityObject.getExpirationDate());
135             identity.getExpiration().setDeleted(false);
136             identity.getExpiration().setDeleteDate(null);
137         }
138         identity.setMonParisActive( identityObject.isMonParisActive( ) );
139         for ( final Map.Entry<String, AttributeObject> entry : identityObject.getAttributes( ).entrySet( ) )
140         {
141             final String s = entry.getKey( );
142             AttributeObject attributeObject = entry.getValue( );
143             final AttributeDto attribute = new AttributeDto( );
144             attribute.setKey( s );
145             attribute.setValue( attributeObject.getValue( ) );
146             attribute.setType( attributeObject.getType( ) );
147             attribute.setCertifier( attributeObject.getCertifierCode( ) );
148             attribute.setCertificationDate( attributeObject.getCertificateDate( ) );
149             attribute.setCertificationLevel( AttributeCertificationDefinitionService.instance( ).getLevelAsInteger( attribute.getCertifier( ), s ) );
150             identity.getAttributes( ).add( attribute );
151         }
152         return identity;
153     }
154 }