Skip to content

Commit e2434e1

Browse files
committed
FIN-350 add the possibility to import transactions using a profile export json.
1 parent 2bad2f4 commit e2434e1

File tree

6 files changed

+336
-111
lines changed

6 files changed

+336
-111
lines changed

bpmn-process/src/.camunda/element-templates/transaction.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,48 @@
313313
}
314314
}
315315
]
316+
},
317+
{
318+
"$schema": "https://unpkg.com/@camunda/element-templates-json-schema/resources/schema.json",
319+
"name": "Transaction: Create from TransactionJSON",
320+
"id": "com.jongsoft.finance.bpmn.delegate.scheduler.ImportTransactionJsonDelegate",
321+
"description": "Create a transaction in the application using a JSON representation stored on disk.",
322+
"version": 1,
323+
"appliesTo": [
324+
"bpmn:ServiceTask"
325+
],
326+
"properties": [
327+
{
328+
"label": "Implementation",
329+
"type": "String",
330+
"value": "${importTransactionJsonDelegate}",
331+
"editable": false,
332+
"binding": {
333+
"type": "property",
334+
"name": "camunda:delegateExpression"
335+
}
336+
},
337+
{
338+
"label": "Transaction",
339+
"description": "The JSON transaction.",
340+
"type": "String",
341+
"binding": {
342+
"type": "camunda:inputParameter",
343+
"name": "transaction"
344+
},
345+
"constraint": {
346+
"notEmpty": true
347+
}
348+
},
349+
{
350+
"label": "Transaction id",
351+
"description": "The id of the created transaction",
352+
"type": "String",
353+
"binding": {
354+
"type": "camunda:outputParameter",
355+
"source": "${transactionId}"
356+
}
357+
}
358+
]
316359
}
317360
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.jongsoft.finance.bpmn.delegate.transaction;
2+
3+
import com.jongsoft.finance.domain.transaction.Transaction;
4+
import com.jongsoft.finance.messaging.commands.transaction.CreateTransactionCommand;
5+
import com.jongsoft.finance.messaging.handlers.TransactionCreationHandler;
6+
import com.jongsoft.finance.providers.AccountProvider;
7+
import com.jongsoft.finance.serialized.TransactionJson;
8+
import jakarta.inject.Singleton;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.camunda.bpm.engine.delegate.DelegateExecution;
11+
import org.camunda.bpm.engine.delegate.JavaDelegate;
12+
13+
@Slf4j
14+
@Singleton
15+
public class ImportTransactionJsonDelegate implements JavaDelegate {
16+
17+
private final AccountProvider accountProvider;
18+
private final TransactionCreationHandler creationHandler;
19+
20+
public ImportTransactionJsonDelegate(AccountProvider accountProvider, TransactionCreationHandler creationHandler) {
21+
this.accountProvider = accountProvider;
22+
this.creationHandler = creationHandler;
23+
}
24+
25+
@Override
26+
public void execute(DelegateExecution execution) throws Exception {
27+
var transaction = (TransactionJson) execution.getVariableLocal("transaction");
28+
29+
log.debug("{}: Importing a transaction from '{}' to '{}'.",
30+
execution.getCurrentActivityName(),
31+
transaction.getFromAccount(),
32+
transaction.getToAccount());
33+
34+
var fromAccount = accountProvider.lookup(transaction.getFromAccount())
35+
.getOrThrow(() ->
36+
new IllegalStateException(
37+
"Unable to find account with name " + transaction.getFromAccount()));
38+
var toAccount = accountProvider.lookup(transaction.getToAccount())
39+
.getOrThrow(() ->
40+
new IllegalStateException(
41+
"Unable to find account with name " + transaction.getToAccount()));
42+
43+
var created = fromAccount.createTransaction(
44+
toAccount,
45+
transaction.getAmount(),
46+
Transaction.Type.CREDIT,
47+
t -> t.currency(transaction.getCurrency())
48+
.date(transaction.getDate())
49+
.bookDate(transaction.getBookDate())
50+
.interestDate(transaction.getInterestDate())
51+
.description(transaction.getDescription()));
52+
53+
long transactionId = creationHandler.handleCreatedEvent(new CreateTransactionCommand(created));
54+
execution.setVariable("transactionId", transactionId);
55+
}
56+
}

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/user/ParseUserConfigurationDelegate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ public void execute(DelegateExecution execution) throws Exception {
6262
execution.setVariableLocal("budgetPeriods", List.of());
6363
}
6464

65+
execution.setVariableLocal("transactions", Control.Option(profileJson.getTransactions())
66+
.getOrSupply(List::of));
67+
6568
execution.setVariableLocal("accounts", serialize(profileJson.getAccounts()));
6669
execution.setVariableLocal("contracts", serialize(profileJson.getContracts()));
6770
execution.setVariableLocal("categories", serialize(profileJson.getCategories()));
6871
execution.setVariableLocal("tags", Control.Option(profileJson.getTags()).getOrSupply(List::of));
6972
}
7073

74+
7175
List<String> serialize(List<?> input) {
7276
if (input == null) {
7377
return List.of();

0 commit comments

Comments
 (0)