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.history;
35
36 import fr.paris.lutece.plugins.identitystore.business.contract.AttributeRight;
37 import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
38 import fr.paris.lutece.plugins.identitystore.business.identity.Identity;
39 import fr.paris.lutece.plugins.identitystore.business.identity.IdentityAttributeHome;
40 import fr.paris.lutece.plugins.identitystore.business.identity.IdentityHome;
41 import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractNotFoundException;
42 import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractService;
43 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.AttributeChange;
44 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.AttributeHistory;
45 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChange;
46 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChangeType;
47 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistory;
48 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistorySearchRequest;
49 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistorySearchResponse;
50 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
51 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.ResponseStatusFactory;
52 import fr.paris.lutece.plugins.identitystore.web.exception.IdentityNotFoundException;
53 import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
54 import org.apache.commons.lang3.tuple.Pair;
55
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Objects;
59 import java.util.Set;
60 import java.util.stream.Collectors;
61
62 public class IdentityHistoryService
63 {
64 private static IdentityHistoryService _instance;
65
66 public static IdentityHistoryService instance( )
67 {
68 if ( _instance == null )
69 {
70 _instance = new IdentityHistoryService( );
71 }
72 return _instance;
73 }
74
75 public IdentityHistory get( final String customerId, final String clientCode ) throws IdentityStoreException
76 {
77 final Identity identity = IdentityHome.findByCustomerId( customerId );
78 if ( identity == null )
79 {
80 throw new IdentityNotFoundException( "CustomerId = " + customerId );
81 }
82 final ServiceContract serviceContract = ServiceContractService.instance( ).getActiveServiceContract( clientCode );
83 if ( serviceContract == null )
84 {
85 throw new ServiceContractNotFoundException( "Client App Code = " + clientCode );
86 }
87 final Set<String> readableAttributeKeys = serviceContract.getAttributeRights( ).stream( ).filter( AttributeRight::isReadable )
88 .map( ar -> ar.getAttributeKey( ).getKeyName( ) ).collect( Collectors.toSet( ) );
89
90 final List<IdentityChange> identityChangeList = IdentityHome.findHistoryByCustomerId( customerId );
91 if ( !serviceContract.getAuthorizedAgentHistoryRead( ) )
92 {
93 identityChangeList.removeIf( identityChange -> Objects.equals( identityChange.getChangeType( ), IdentityChangeType.READ ) );
94 }
95 final List<AttributeChange> attributeChangeList = IdentityAttributeHome.getAttributeChangeHistory( identity.getId( ) );
96
97 return toHistory( customerId, identityChangeList, attributeChangeList, readableAttributeKeys );
98 }
99
100 public IdentityHistorySearchResponse search( final IdentityHistorySearchRequest request, final String clientCode ) throws IdentityStoreException
101 {
102
103 final IdentityHistorySearchResponse response = new IdentityHistorySearchResponse( );
104 final ServiceContract serviceContract = ServiceContractService.instance( ).getActiveServiceContract( clientCode );
105 if ( serviceContract == null )
106 {
107 throw new ServiceContractNotFoundException( "Client App Code = " + clientCode );
108 }
109 final Set<String> readableAttributeKeys = serviceContract.getAttributeRights( ).stream( ).filter( AttributeRight::isReadable )
110 .map( ar -> ar.getAttributeKey( ).getKeyName( ) ).collect( Collectors.toSet( ) );
111
112 final List<IdentityChange> identityChangeList = IdentityHome.findHistoryBySearchParameters( request.getCustomerId( ), request.getClientCode( ),
113 request.getAuthorName( ), request.getIdentityChangeType( ), request.getChangeStatus( ), null, null, request.getMetadata( ), request.getNbDaysFrom( ),
114 Pair.of( request.getModificationDateIntervalStart( ), request.getModificationDateIntervalEnd( ) ) );
115 if ( !serviceContract.getAuthorizedAgentHistoryRead( ) )
116 {
117 identityChangeList.removeIf( identityChange -> Objects.equals( identityChange.getChangeType( ), IdentityChangeType.READ ) );
118 }
119 final Map<String, List<IdentityChange>> identityChangeMap = identityChangeList.stream( )
120 .collect( Collectors.groupingBy( IdentityChange::getCustomerId ) );
121
122
123 for ( final String customerId : identityChangeMap.keySet( ) )
124 {
125 final List<AttributeChange> attributeChangeList = IdentityAttributeHome.getAttributeChangeHistory( customerId );
126 response.getHistories( ).add( this.toHistory( customerId, identityChangeMap.get( customerId ), attributeChangeList, readableAttributeKeys ) );
127 }
128
129 if ( response.getHistories( ).isEmpty( ) )
130 {
131 response.setStatus( ResponseStatusFactory.noResult( ).setMessageKey( Constants.PROPERTY_REST_ERROR_NO_HISTORY_FOUND ) );
132 }
133 else
134 {
135 response.setStatus( ResponseStatusFactory.ok( ).setMessageKey( Constants.PROPERTY_REST_INFO_SUCCESSFUL_OPERATION ) );
136 }
137
138 return response;
139 }
140
141 private IdentityHistory toHistory( final String customerId, final List<IdentityChange> identityChangeList, final List<AttributeChange> attributeChangeList,
142 final Set<String> readableAttributeKeys )
143 {
144 final IdentityHistory history = new IdentityHistory( );
145 history.setCustomerId( customerId );
146 history.getIdentityChanges( ).addAll( identityChangeList );
147 attributeChangeList.stream( ).filter( ac -> readableAttributeKeys.contains( ac.getAttributeKey( ) ) )
148 .collect( Collectors.groupingBy( AttributeChange::getAttributeKey ) ).forEach( ( key, attributeChanges ) -> {
149 final AttributeHistory attributeHistory = new AttributeHistory( );
150 attributeHistory.setAttributeKey( key );
151 attributeHistory.setAttributeChanges( attributeChanges );
152 history.getAttributeHistories( ).add( attributeHistory );
153 } );
154 return history;
155 }
156 }