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.v3.web.request.contract;
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.AttributeCertification;
39 import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
40 import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContractHome;
41 import fr.paris.lutece.plugins.identitystore.business.referentiel.RefAttributeCertificationProcessus;
42 import fr.paris.lutece.plugins.identitystore.service.contract.AttributeCertificationDefinitionService;
43 import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractService;
44 import fr.paris.lutece.plugins.identitystore.v3.web.rs.AbstractIdentityStoreRequest;
45 import fr.paris.lutece.plugins.identitystore.v3.web.rs.DtoConverter;
46 import fr.paris.lutece.plugins.identitystore.v3.web.rs.IdentityRequestValidator;
47 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.contract.ServiceContractChangeResponse;
48 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.contract.ServiceContractDto;
49 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
50 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.ResponseStatusFactory;
51 import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
52
53 import java.util.Optional;
54
55
56
57
58 public class ServiceContractUpdateRequest extends AbstractIdentityStoreRequest
59 {
60 private final ServiceContractDto _serviceContractDto;
61 private final Integer _serviceContractId;
62
63
64
65
66
67
68
69
70
71
72
73 public ServiceContractUpdateRequest( ServiceContractDto serviceContractDto, String strClientCode, Integer serviceContractId, String authorName,
74 String authorType ) throws IdentityStoreException
75 {
76 super( strClientCode, authorName, authorType );
77 this._serviceContractDto = serviceContractDto;
78 this._serviceContractId = serviceContractId;
79 }
80
81 @Override
82 protected void validateSpecificRequest( ) throws IdentityStoreException
83 {
84 IdentityRequestValidator.instance( ).checkServiceContract( _serviceContractDto );
85 IdentityRequestValidator.instance( ).checkContractId( _serviceContractId );
86 }
87
88 @Override
89 public ServiceContractChangeResponse doSpecificRequest( ) throws IdentityStoreException
90 {
91 final ServiceContractChangeResponse response = new ServiceContractChangeResponse( );
92 final ClientApplication clientApplication = ClientApplicationHome.findByCode( _serviceContractDto.getClientCode( ) );
93 if ( clientApplication == null )
94 {
95 response.setStatus(
96 ResponseStatusFactory.notFound( ).setMessage( "No application could be found with code " + _serviceContractDto.getClientCode( ) )
97 .setMessageKey( Constants.PROPERTY_REST_ERROR_APPLICATION_NOT_FOUND ) );
98 }
99 else
100 {
101 final Optional<ServiceContract> serviceContractToUpdate = ServiceContractHome.findByPrimaryKey( _serviceContractId );
102 if ( !serviceContractToUpdate.isPresent( ) )
103 {
104 response.setStatus( ResponseStatusFactory.notFound( ).setMessage( "No service contract could be found with code " + _serviceContractId )
105 .setMessageKey( Constants.PROPERTY_REST_ERROR_SERVICE_CONTRACT_NOT_FOUND ) );
106 }
107 else
108 {
109 _serviceContractDto.setId( _serviceContractId );
110 final ServiceContract serviceContract = ServiceContractService.instance( ).update( DtoConverter.convertDtoToContract( _serviceContractDto ),
111 clientApplication.getId( ) );
112
113 for ( final AttributeCertification certification : serviceContract.getAttributeCertifications( ) )
114 {
115 for ( final RefAttributeCertificationProcessus processus : certification.getRefAttributeCertificationProcessus( ) )
116 {
117 processus.setLevel( AttributeCertificationDefinitionService.instance( ).get( processus.getCode( ),
118 certification.getAttributeKey( ).getKeyName( ) ) );
119 }
120 }
121 response.setServiceContract( DtoConverter.convertContractToDto( serviceContract ) );
122 response.setStatus( ResponseStatusFactory.success( ).setMessageKey( Constants.PROPERTY_REST_INFO_SUCCESSFUL_OPERATION ) );
123 }
124 }
125
126 return response;
127 }
128
129 }