1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61 private ElasticSearchIdentityService( IIdentitySearcher _identitySearcher )
62 {
63 this._identitySearcher = _identitySearcher;
64 }
65
66
67
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 }