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.cache;
35
36 import fr.paris.lutece.plugins.identitystore.business.application.ClientApplication;
37 import fr.paris.lutece.plugins.identitystore.business.application.ClientApplicationHome;
38 import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
39 import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContractHome;
40 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
41 import fr.paris.lutece.plugins.identitystore.web.exception.ClientAuthorizationException;
42 import fr.paris.lutece.plugins.identitystore.web.exception.ResourceNotFoundException;
43 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
44 import fr.paris.lutece.portal.service.util.AppLogService;
45 import org.apache.commons.collections.CollectionUtils;
46
47 import java.util.List;
48 import java.util.Objects;
49
50 public class ActiveServiceContractCache extends AbstractCacheableService
51 {
52
53 public static final String SERVICE_NAME = "ActiveServiceContractCache";
54
55 public ActiveServiceContractCache( )
56 {
57 this.initCache( );
58 }
59
60 public void refresh( )
61 {
62 AppLogService.debug( "Init service contract cache" );
63 this.resetCache( );
64 final List<ClientApplication> clientApplications = ClientApplicationHome.selectApplicationList( );
65 clientApplications.forEach( clientApplication -> {
66 try
67 {
68 final ServiceContract activeServiceContract = this.getActiveServiceContractFromDatabase( clientApplication.getClientCode( ) );
69 this.put( clientApplication.getClientCode( ), activeServiceContract );
70 }
71 catch( final ClientAuthorizationException e )
72 {
73 AppLogService.debug( e.getMessage( ) );
74 }
75 } );
76 }
77
78 public void put( final String clientCode, final ServiceContract serviceContract )
79 {
80 if ( this.getKeys( ).contains( clientCode ) )
81 {
82 this.removeKey( clientCode );
83 }
84 this.putInCache( clientCode, serviceContract );
85 AppLogService.debug( "An active service contract has been added for client application with code : " + clientCode );
86 }
87
88
89
90
91
92
93 public void deleteById( final Integer id )
94 {
95 this.getKeys( ).forEach( key -> {
96 try
97 {
98 ServiceContract contract = this.get( key );
99 if ( Objects.equals( contract.getId( ), id ) )
100 {
101 this.removeKey( key );
102 }
103 }
104 catch( final ClientAuthorizationException e )
105 {
106 AppLogService.error( "Cannot delete service contract with id" + id + " : {}", e );
107 }
108 } );
109 }
110
111 public ServiceContract get( final String clientCode ) throws ClientAuthorizationException
112 {
113 ServiceContractfr/paris/lutece/plugins/identitystore/business/contract/ServiceContract.html#ServiceContract">ServiceContract serviceContract = (ServiceContract) this.getFromCache( clientCode );
114 if ( serviceContract == null )
115 {
116 serviceContract = this.getActiveServiceContractFromDatabase( clientCode );
117 this.put( clientCode, serviceContract );
118 }
119 return serviceContract;
120 }
121
122 private ServiceContract getActiveServiceContractFromDatabase( final String clientCode ) throws ClientAuthorizationException
123 {
124 final List<ServiceContract> serviceContracts = ClientApplicationHome.selectActiveServiceContract( clientCode );
125 if ( CollectionUtils.isEmpty( serviceContracts ) )
126 {
127 throw new ClientAuthorizationException( "No contract service found for client application with code " + clientCode,
128 Constants.PROPERTY_REST_ERROR_SERVICE_CONTRACT_NOT_FOUND );
129 }
130 else
131 if ( CollectionUtils.size( serviceContracts ) > 1 )
132 {
133 throw new ClientAuthorizationException(
134 "There is more than one active service contract for the application with code " + clientCode + ". There must be only one",
135 Constants.PROPERTY_REST_ERROR_MULTIPLE_ACTIVE_SERVICE_CONTRACTS );
136 }
137
138 final ServiceContract serviceContract = serviceContracts.get( 0 );
139 serviceContract.setAttributeRights( ServiceContractHome.selectApplicationRights( serviceContract ) );
140 serviceContract.setAttributeCertifications( ServiceContractHome.selectAttributeCertifications( serviceContract ) );
141 serviceContract.setAttributeRequirements( ServiceContractHome.selectAttributeRequirements( serviceContract ) );
142
143 return serviceContract;
144 }
145
146 @Override
147 public String getName( )
148 {
149 return SERVICE_NAME;
150 }
151 }