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.accountgenerator.web.request;
35
36 import fr.paris.lutece.plugins.accountgenerator.service.IdentityAccountGeneratorService;
37 import fr.paris.lutece.plugins.identitystore.v3.web.rs.AbstractIdentityStoreRequest;
38 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationDto;
39 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationRequest;
40 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationResponse;
41 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.GeneratedAccountDto;
42 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
43 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.ResponseStatusFactory;
44 import fr.paris.lutece.plugins.identitystore.web.exception.*;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46
47 import java.util.List;
48
49 public class AccountGeneratorCreateRequest extends AbstractIdentityStoreRequest
50 {
51 private static final int generationLimit = AppPropertiesService.getPropertyInt( "accountgenerator.generation.limit", 50 );
52 private static String JSON_EXAMPLE = "{\n" + " \"generation\": {\n" + " \"generateAccount\": true,\n"
53 + " \"generationPattern\": \"XYZ\",\n" + " \"generationIncrementOffset\": 30,\n" + " \"nbDaysOfValidity\": 300,\n"
54 + " \"batchSize\": 50\n" + " }\n" + "}";
55 private final AccountGenerationRequest _accountGenerationRequest;
56
57 public AccountGeneratorCreateRequest( AccountGenerationRequest request, String strClientAppCode, String authorName, String authorType )
58 throws IdentityStoreException
59 {
60 super( strClientAppCode, authorName, authorType );
61 _accountGenerationRequest = request;
62 }
63
64 @Override
65 protected void fetchResources() throws ResourceNotFoundException {
66
67 }
68
69 @Override
70 protected void validateRequestFormat() throws RequestFormatException {
71 if ( _accountGenerationRequest == null || _accountGenerationRequest.getAccountGenerationDto( ) == null )
72 {
73 throw new RequestFormatException( "The request must specify an account generation. I.e: " + JSON_EXAMPLE, Constants.PROPERTY_REST_ERROR_ACCOUNT_GENERATION_REQUEST_FORMAT );
74 }
75 }
76
77 @Override
78 protected void validateClientAuthorization() throws ClientAuthorizationException {
79
80 }
81
82 @Override
83 protected void validateResourcesConsistency() throws ResourceConsistencyException {
84
85 }
86
87 @Override
88 protected void formatRequestContent() throws RequestContentFormattingException {
89
90 }
91
92 @Override
93 protected void checkDuplicatesConsistency() throws DuplicatesConsistencyException {
94
95 }
96
97 @Override
98 protected AccountGenerationResponse doSpecificRequest( ) throws IdentityStoreException
99 {
100 final AccountGenerationResponse response = new AccountGenerationResponse( );
101 final AccountGenerationDto accountGenerationDto = _accountGenerationRequest.getAccountGenerationDto( );
102 String message = "";
103 final int batchSize = Math.min( accountGenerationDto.getBatchSize( ), generationLimit );
104 if ( batchSize != accountGenerationDto.getBatchSize( ) )
105 {
106 accountGenerationDto.setBatchSize( batchSize );
107 message = "The number of accounts that can be generated in a single request is limited to " + generationLimit
108 + ", so the batch size has been override to this value.";
109 }
110
111 final List<GeneratedAccountDto> generatedAccounts = IdentityAccountGeneratorService.instance( ).createIdentityAccountBatch( accountGenerationDto );
112 response.setGeneratedAccountList( generatedAccounts );
113 response.setStatus( ResponseStatusFactory.success( ) );
114 final long accountsCount = generatedAccounts.stream( ).filter( GeneratedAccountDto::hasAccount ).count( );
115 final long identitiesCount = generatedAccounts.stream( ).filter( GeneratedAccountDto::hasIdentity ).count( );
116 response.getStatus( ).setMessage(
117 message + " Generated " + accountsCount + " accounts and/or " + identitiesCount + " identities. See each account status if some are missing." );
118 return response;
119 }
120 }