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.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         // TODO refactorer dans le cas d'une recherche par metadata la liste est beaucoup trop grande pour faire des appels successifs à la BDD.
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 }