11package org .mifos .processor .bulk .api .implementation ;
22
3+ import static org .mifos .processor .bulk .camel .config .CamelProperties .*;
4+ import static org .mifos .processor .bulk .camel .config .CamelProperties .HEADER_PROGRAM_ID ;
5+ import static org .mifos .processor .bulk .camel .config .CamelProperties .HEADER_REGISTERING_INSTITUTE_ID ;
6+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .*;
7+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .HEADER_CLIENT_CORRELATION_ID ;
8+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .HEADER_PLATFORM_TENANT_ID ;
9+
310import com .fasterxml .jackson .core .type .TypeReference ;
411import com .fasterxml .jackson .databind .ObjectMapper ;
512import com .fasterxml .jackson .dataformat .csv .CsvMapper ;
13+ import java .nio .charset .Charset ;
14+ import java .util .List ;
15+ import java .util .Optional ;
16+ import java .util .UUID ;
17+ import javax .servlet .http .HttpServletRequest ;
18+ import javax .servlet .http .HttpServletResponse ;
619import lombok .SneakyThrows ;
720import lombok .extern .slf4j .Slf4j ;
821import org .apache .camel .Exchange ;
2235import org .springframework .beans .factory .annotation .Autowired ;
2336import org .springframework .beans .factory .annotation .Value ;
2437import org .springframework .web .bind .annotation .ExceptionHandler ;
25- import org .springframework .web .bind .annotation .RequestHeader ;
2638import org .springframework .web .bind .annotation .RestController ;
2739import org .springframework .web .multipart .MultipartException ;
28- import javax .servlet .http .HttpServletRequest ;
29- import javax .servlet .http .HttpServletResponse ;
30- import java .io .IOException ;
31- import static org .mifos .processor .bulk .camel .config .CamelProperties .*;
32- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .PURPOSE ;
33- import java .nio .charset .Charset ;
34- import java .util .List ;
35- import java .util .Optional ;
36- import java .util .UUID ;
37- import static org .mifos .processor .bulk .camel .config .CamelProperties .HEADER_PROGRAM_ID ;
38- import static org .mifos .processor .bulk .camel .config .CamelProperties .HEADER_REGISTERING_INSTITUTE_ID ;
39- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .FILE_NAME ;
40- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .HEADER_CLIENT_CORRELATION_ID ;
41- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .HEADER_PLATFORM_TENANT_ID ;
42- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .HEADER_TYPE ;
43- import static org .mifos .processor .bulk .zeebe .ZeebeVariables .PURPOSE ;
4440
4541@ Slf4j
4642@ RestController
@@ -65,25 +61,17 @@ public class BatchTransactionsController implements BatchTransactions {
6561
6662 @ SneakyThrows
6763 @ Override
68- public String batchTransactions (
69- HttpServletRequest httpServletRequest ,
70- HttpServletResponse httpServletResponse ,
71- String requestId ,
72- String fileName ,
73- String purpose ,
74- String type ,
75- String tenant ,
76- String registeringInstitutionId ,
64+ public String batchTransactions (HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse , String requestId ,
65+ String fileName , String purpose , String type , String tenant , String callbackUrl , String registeringInstitutionId ,
7766 String programId ) {
7867
7968 log .info ("Inside api logic" );
8069 Headers .HeaderBuilder headerBuilder = new Headers .HeaderBuilder ()
8170 .addHeader (HEADER_CLIENT_CORRELATION_ID , requestId )
82- .addHeader (PURPOSE , purpose )
83- .addHeader (HEADER_TYPE , type )
71+ .addHeader (PURPOSE , purpose ).addHeader (HEADER_TYPE , type )
8472 .addHeader (HEADER_PLATFORM_TENANT_ID , tenant )
85- .addHeader (HEADER_REGISTERING_INSTITUTE_ID , registeringInstitutionId )
86- .addHeader (HEADER_PROGRAM_ID , programId );
73+ .addHeader (HEADER_REGISTERING_INSTITUTE_ID , registeringInstitutionId ). addHeader ( HEADER_PROGRAM_ID , programId )
74+ .addHeader (CALLBACK , callbackUrl );
8775
8876 Optional <String > validationResponse = isValidRequest (httpServletRequest , fileName , type );
8977 if (validationResponse .isPresent ()) {
@@ -108,9 +96,7 @@ public String batchTransactions(
10896
10997 String localFileName = UUID .randomUUID () + ".csv" ;
11098 CsvWriter .writeToCsv (transactionList , Transaction .class , csvMapper , true , localFileName );
111- Headers headers = headerBuilder
112- .addHeader (HEADER_TYPE , "csv" )
113- .addHeader (FILE_NAME , localFileName ).build ();
99+ Headers headers = headerBuilder .addHeader (HEADER_TYPE , "csv" ).addHeader (FILE_NAME , localFileName ).build ();
114100
115101 CamelApiResponse response = sendRequestToCamel (headers );
116102 httpServletResponse .setStatus (response .getStatus ());
@@ -121,8 +107,8 @@ public String batchTransactions(
121107 @ ExceptionHandler ({ MultipartException .class })
122108 public String handleMultipartException (HttpServletResponse httpServletResponse ) {
123109 httpServletResponse .setStatus (httpServletResponse .SC_BAD_REQUEST );
124- return getErrorResponse ("File not uploaded" , "There was no fie uploaded with the request. " +
125- "Please upload a file and try again." , 400 );
110+ return getErrorResponse ("File not uploaded" , "There was no fie uploaded with the request. " + "Please upload a file and try again." ,
111+ 400 );
126112 }
127113
128114 private CamelApiResponse sendRequestToCamel (Headers headers ) {
@@ -142,31 +128,26 @@ private String getErrorResponse(String information, String description, int code
142128 }
143129
144130 // validates the request header, and return errorJson string if the request is invalid else an empty optional
145- private Optional <String > isValidRequest (HttpServletRequest httpServletRequest ,
146- String fileName ,
147- String type ) {
131+ private Optional <String > isValidRequest (HttpServletRequest httpServletRequest , String fileName , String type ) {
148132
149- Optional <String > response = Optional .empty ();
150- if ((JWSUtil .isMultipartRequest (httpServletRequest ) && !type .equalsIgnoreCase ("csv" )) ||
151- (!JWSUtil .isMultipartRequest (httpServletRequest ) && !type .equalsIgnoreCase ("raw" ))) {
133+ Optional <String > response = Optional .empty ();
134+ if ((JWSUtil .isMultipartRequest (httpServletRequest ) && !type .equalsIgnoreCase ("csv" ))
135+ || (!JWSUtil .isMultipartRequest (httpServletRequest ) && !type .equalsIgnoreCase ("raw" ))) {
152136 String errorJson = getErrorResponse ("Type mismatch" ,
153- "The value of the header \" " + HEADER_TYPE +
154- "\" doesn't match with the request content-type" , 400 );
137+ "The value of the header \" " + HEADER_TYPE + "\" doesn't match with the request content-type" , 400 );
155138 response = Optional .of (errorJson );
156139
157140 }
158141 if (JWSUtil .isMultipartRequest (httpServletRequest ) && fileName .isEmpty ()) {
159142 String errorJson = getErrorResponse ("Header can't be empty" ,
160- "If the request is of type csv, the header \" " +
161- FILE_NAME + "\" can't be empty" , 400 );
143+ "If the request is of type csv, the header \" " + FILE_NAME + "\" can't be empty" , 400 );
162144 response = Optional .of (errorJson );
163145 }
164146 if (!type .equalsIgnoreCase ("raw" ) && !type .equalsIgnoreCase ("csv" )) {
165147 String errorJson = getErrorResponse ("Invalid TYPE header value passed" ,
166- "The value of the header \" " + HEADER_TYPE +
167- "\" can be \" [raw,csv]\" but is " + type , 400 );
148+ "The value of the header \" " + HEADER_TYPE + "\" can be \" [raw,csv]\" but is " + type , 400 );
168149 response = Optional .of (errorJson );
169150 }
170- return response ;
151+ return response ;
171152 }
172153}
0 commit comments