11package org .mifos .processor .bulk .camel .routes ;
22
33import static org .mifos .processor .bulk .camel .config .CamelProperties .LOCAL_FILE_PATH ;
4+ import static org .mifos .processor .bulk .camel .config .CamelProperties .REGISTERING_INSTITUTE_ID ;
45import static org .mifos .processor .bulk .camel .config .CamelProperties .SERVER_FILE_NAME ;
56import static org .mifos .processor .bulk .camel .config .CamelProperties .SERVER_SUB_BATCH_FILE_NAME_ARRAY ;
67import static org .mifos .processor .bulk .camel .config .CamelProperties .SUB_BATCH_COUNT ;
78import static org .mifos .processor .bulk .camel .config .CamelProperties .SUB_BATCH_CREATED ;
9+ import static org .mifos .processor .bulk .camel .config .CamelProperties .SUB_BATCH_DETAILS ;
810import static org .mifos .processor .bulk .camel .config .CamelProperties .SUB_BATCH_FILE_ARRAY ;
11+ import static org .mifos .processor .bulk .camel .config .CamelProperties .TRANSACTION_LIST ;
12+ import static org .mifos .processor .bulk .camel .config .CamelProperties .ZEEBE_VARIABLE ;
13+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .BATCH_ID ;
14+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .CLIENT_CORRELATION_ID ;
15+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .PAYER_IDENTIFIER ;
16+ import static org .mifos .processor .bulk .zeebe .ZeebeVariables .REQUEST_ID ;
917import static org .mifos .processor .bulk .zeebe .ZeebeVariables .SPLITTING_FAILED ;
1018
1119import java .io .BufferedReader ;
1220import java .io .FileReader ;
1321import java .io .FileWriter ;
1422import java .util .ArrayList ;
23+ import java .util .Date ;
1524import java .util .List ;
25+ import java .util .Map ;
26+ import java .util .UUID ;
27+ import org .apache .camel .LoggingLevel ;
28+ import org .mifos .processor .bulk .schema .SubBatchEntity ;
29+ import org .mifos .processor .bulk .schema .Transaction ;
1630import org .springframework .beans .factory .annotation .Value ;
1731import org .springframework .stereotype .Component ;
1832
@@ -56,7 +70,7 @@ public void configure() throws Exception {
5670 List <String > subBatchFile = new ArrayList <>();
5771 int subBatchCount = 1 ;
5872 for (int i = 0 ; i < lines .size (); i += subBatchSize ) {
59- String filename = System . currentTimeMillis () + "_" + "sub-batch-" + subBatchCount + ".csv" ;
73+ String filename = UUID . randomUUID () + "_" + "sub-batch-" + subBatchCount + ".csv" ;
6074 FileWriter writer = new FileWriter (filename );
6175 writer .write (header );
6276 for (int j = i ; j < Math .min (i + subBatchSize , lines .size ()); j ++) {
@@ -77,13 +91,71 @@ public void configure() throws Exception {
7791 from ("direct:upload-sub-batch-file" ).id ("direct:upload-sub-batch-file" ).log ("Starting upload of sub-batch file" )
7892 .loopDoWhile (exchange -> exchange .getProperty (SUB_BATCH_FILE_ARRAY , List .class ).size () > 0 ).process (exchange -> {
7993 List <String > subBatchFile = exchange .getProperty (SUB_BATCH_FILE_ARRAY , List .class );
80- exchange .setProperty (LOCAL_FILE_PATH , subBatchFile .remove (0 ));
94+ String localFilePath = subBatchFile .remove (0 );
95+ exchange .setProperty (LOCAL_FILE_PATH , localFilePath );
8196 exchange .setProperty (SUB_BATCH_FILE_ARRAY , subBatchFile );
82- }).to ("direct:upload-file" ).process (exchange -> {
97+ logger .debug ("Local file path: {}" , localFilePath );
98+ logger .debug ("Sub batch file array: {}, " , subBatchFile );
99+ }).log (LoggingLevel .DEBUG , "LOCAL_FILE_PATH: ${exchangeProperty." + LOCAL_FILE_PATH + "}" )
100+ .to ("direct:generate-sub-batch-entity" ).log ("direct:generate-sub-batch-entity completed" ).to ("direct:upload-file" )
101+ .process (exchange -> {
83102 String serverFilename = exchange .getProperty (SERVER_FILE_NAME , String .class );
84103 List <String > serverSubBatchFile = exchange .getProperty (SERVER_SUB_BATCH_FILE_NAME_ARRAY , List .class );
85104 serverSubBatchFile .add (serverFilename );
86105 exchange .setProperty (SERVER_SUB_BATCH_FILE_NAME_ARRAY , serverSubBatchFile );
106+ logger .debug ("Server subbatch filename array: {}" , serverSubBatchFile );
87107 });
108+
109+ // generate subBatchEntityDetails, make sure [LOCAL_FILE_PATH] has the absolute sub batch file path
110+ from ("direct:generate-sub-batch-entity" ).id ("direct:generate-sub-batch-entity" ).log ("Generating sub batch entity" )
111+ .to ("direct:get-transaction-array" ).process (exchange -> {
112+ List <Transaction > transactionList = exchange .getProperty (TRANSACTION_LIST , List .class );
113+ Map <String , Object > zeebeVariables = exchange .getProperty (ZEEBE_VARIABLE , Map .class );
114+ String serverFileName = exchange .getProperty (LOCAL_FILE_PATH , String .class );
115+
116+ logger .info ("Generating sub batch entity for file {}" , serverFileName );
117+ if (transactionList .isEmpty ()) {
118+ logger .info ("Transaction list is empty" );
119+ return ;
120+ }
121+
122+ Long totalAmount = getTotalAmount (transactionList );
123+
124+ SubBatchEntity subBatchEntity = getDefaultSubBatchEntity ();
125+ subBatchEntity .setBatchId ((String ) zeebeVariables .get (BATCH_ID ));
126+ subBatchEntity .setSubBatchId (UUID .randomUUID ().toString ());
127+ subBatchEntity .setRequestId ((String ) zeebeVariables .get (REQUEST_ID ));
128+ subBatchEntity .setCorrelationId ((String ) zeebeVariables .get (CLIENT_CORRELATION_ID ));
129+ subBatchEntity .setPayerFsp ((String ) zeebeVariables .get (PAYER_IDENTIFIER ));
130+ subBatchEntity .setRegisteringInstitutionId ((String ) zeebeVariables .get (REGISTERING_INSTITUTE_ID ));
131+ subBatchEntity .setPaymentMode (transactionList .get (0 ).getPaymentMode ());
132+ subBatchEntity .setRequestFile (serverFileName );
133+ subBatchEntity .setTotalTransactions ((long ) transactionList .size ());
134+ subBatchEntity .setOngoing ((long ) transactionList .size ());
135+ subBatchEntity .setTotalAmount (totalAmount );
136+ subBatchEntity .setOngoingAmount (totalAmount );
137+ subBatchEntity .setStartedAt (new Date (System .currentTimeMillis ()));
138+
139+ logger .debug ("SubBatchEntity: {}" , objectMapper .writeValueAsString (subBatchEntity ));
140+ // update the sub batch details array
141+ List <SubBatchEntity > subBatchEntityList = exchange .getProperty (SUB_BATCH_DETAILS , List .class );
142+ subBatchEntityList .add (subBatchEntity );
143+ exchange .setProperty (SUB_BATCH_DETAILS , subBatchEntityList );
144+ logger .debug ("generate-sub-batch-entity route end: {}" , objectMapper .writeValueAsString (subBatchEntityList ));
145+ });
146+ }
147+
148+ private SubBatchEntity getDefaultSubBatchEntity () {
149+ SubBatchEntity subBatchEntity = new SubBatchEntity ();
150+ subBatchEntity .setAllEmptyAmount ();
151+ return subBatchEntity ;
152+ }
153+
154+ private long getTotalAmount (List <Transaction > transactionList ) {
155+ long totalAmount = 0L ;
156+ for (Transaction transaction : transactionList ) {
157+ totalAmount += Long .parseLong (transaction .getAmount ());
158+ }
159+ return totalAmount ;
88160 }
89161}
0 commit comments