Skip to content

Commit c86e074

Browse files
authored
Remove the outdated ParsedTransaction class (#11)
* Mark the transaction deprecated to find all uses and create build warnings. * Move all the logic away from the old ParsedTransaction.java and towards the new TransactionDTO.
1 parent 38950f5 commit c86e074

11 files changed

+113
-71
lines changed

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/ProcessEngineConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.jongsoft.finance.ProcessVariable;
44
import com.jongsoft.finance.bpmn.camunda.*;
55
import com.jongsoft.finance.core.DataSourceMigration;
6-
import com.jongsoft.finance.serialized.ImportJobSettings;
6+
import com.jongsoft.finance.importer.api.TransactionDTO;
77
import io.micronaut.context.ApplicationContext;
88
import io.micronaut.context.annotation.Context;
99
import io.micronaut.context.annotation.Factory;
@@ -58,7 +58,8 @@ public ProcessEngine processEngine() throws IOException {
5858
configuration.setHistoryTimeToLive("P1D");
5959
configuration.setResolverFactories(List.of(new MicronautBeanResolver(applicationContext)));
6060
configuration.setCustomPreVariableSerializers(List.of(
61-
new JsonRecordSerializer<>(applicationContext.getBean(ObjectMapper.class), ProcessVariable.class)));
61+
new JsonRecordSerializer<>(applicationContext.getBean(ObjectMapper.class), ProcessVariable.class),
62+
new JsonRecordSerializer<>(applicationContext.getBean(ObjectMapper.class), TransactionDTO.class)));
6263

6364
var processEngine = configuration.buildProcessEngine();
6465
log.info("Created camunda process engine");

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/camunda/JsonRecordSerializer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public JsonRecordSerializer(ObjectMapper objectMapper, Class<T> supportedClass)
2727

2828
@Override
2929
public String getName() {
30-
return "record-json";
30+
return supportedClass.getName();
3131
}
3232

3333
@Override
@@ -37,7 +37,7 @@ public String getSerializationDataformat() {
3737

3838
@Override
3939
public TypedValue convertToTypedValue(UntypedValueImpl untypedValue) {
40-
logger.debug("Converting untyped value to typed value: {}", untypedValue.getType());
40+
logger.trace("Converting untyped value to typed value: {}", untypedValue.getValue().getClass().getSimpleName());
4141

4242
var importJobSettings = (Record) untypedValue.getValue();
4343
String jsonString;
@@ -46,8 +46,9 @@ public TypedValue convertToTypedValue(UntypedValueImpl untypedValue) {
4646
} catch (IOException e) {
4747
throw new RuntimeException("Could not serialize ImportJobSettings", e);
4848
}
49+
4950
return Variables.serializedObjectValue(jsonString)
50-
.serializationDataFormat(getName())
51+
.serializationDataFormat("application/json")
5152
.create();
5253
}
5354

@@ -59,12 +60,12 @@ public void writeValue(TypedValue typedValue, ValueFields valueFields) {
5960

6061
@Override
6162
public TypedValue readValue(ValueFields valueFields, boolean b, boolean b1) {
62-
logger.debug("Reading value from value fields: {}", valueFields.getName());
63+
logger.trace("Reading value from value fields: {}", valueFields.getName());
6364
try {
6465
return Variables.objectValue(objectMapper.readValue(
6566
new String(valueFields.getByteArrayValue()),
6667
supportedClass))
67-
.serializationDataFormat(getName())
68+
.serializationDataFormat("application/json")
6869
.create();
6970
} catch (IOException e) {
7071
throw new RuntimeException("Could not deserialize ImportJobSettings", e);

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/importer/ImportAccountExtractorDelegate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
import java.util.HashSet;
99
import java.util.Set;
1010

11+
/**
12+
* Extracts account mappings from the import transaction.
13+
* <p>
14+
* This delegate is used to extract account mappings from the imported transaction.
15+
* The account mappings are stored in a set of {@link ExtractionMapping} objects.
16+
* The account mappings are used to map the account names in the import transaction to the account IDs in the finance system.
17+
* <p>
18+
*/
1119
@Slf4j
1220
public class ImportAccountExtractorDelegate implements JavaDelegate {
1321

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/importer/LocateAccountInMapping.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
import java.util.Collection;
1010

11+
/**
12+
* Locates an account in a mapping and sets the account ID as a process variable.
13+
* <p>
14+
* The account name is retrieved from a process variable named {@code name}.
15+
* The account mappings are retrieved from a process variable named {@code accountMappings}
16+
* </p>
17+
* <p>
18+
* The account ID is set as a process variable named {@code accountId}.
19+
* If the account name does not match the account name in the mapping, a synonym is registered.
20+
* The synonym is the account name from the mapping.
21+
* </p>
22+
**/
1123
@Slf4j
1224
@Singleton
1325
public class LocateAccountInMapping implements JavaDelegate {

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/importer/ParsedTransaction.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/importer/ReadTransactionFromStorage.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
import com.jongsoft.finance.ProcessMapper;
44
import com.jongsoft.finance.StorageService;
5+
import com.jongsoft.finance.importer.api.TransactionDTO;
56
import jakarta.inject.Inject;
67
import jakarta.inject.Singleton;
78
import org.camunda.bpm.engine.delegate.DelegateExecution;
89
import org.camunda.bpm.engine.delegate.JavaDelegate;
910

1011
import java.nio.charset.StandardCharsets;
1112

13+
/**
14+
* Reads a transaction from storage.
15+
* <p>
16+
* This delegate reads a transaction from storage using the {@code storageToken} provided in the process variables.
17+
* The transaction is then stored in the process variables as {@code transaction}.
18+
* The transaction is stored as a {@link TransactionDTO} object.
19+
* </p>
20+
*/
1221
@Singleton
1322
public class ReadTransactionFromStorage implements JavaDelegate {
1423

@@ -27,7 +36,7 @@ public void execute(DelegateExecution delegateExecution) throws Exception {
2736

2837
var transaction = storageService.read(storageToken)
2938
.map(byteArray -> new String(byteArray, StandardCharsets.UTF_8))
30-
.map(json -> processMapper.readSafe(json, ParsedTransaction.class))
39+
.map(json -> processMapper.readSafe(json, TransactionDTO.class))
3140
.getOrThrow(() -> new RuntimeException("Failed to read transaction from storage"));
3241

3342
delegateExecution.setVariableLocal("transaction", transaction);

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/importer/ReadTransactionLogDelegate.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818

19+
/**
20+
* Delegate to trigger the actual {@link ImporterProvider} to start the job of fetching and converting the transactions.
21+
* <p>
22+
* The delegate will look up the {@link ImporterProvider} that supports the {@link ImporterConfiguration} of the
23+
* current import job and start the process of reading the transactions.
24+
* The transactions are then serialized and stored in the {@link StorageService} and the tokens are stored in the
25+
* process variables called {@code storageTokens}.
26+
* </p>
27+
* <p>
28+
* The delegate will also set the process variables {@code generateAccounts}, {@code applyRules} and {@code targetAccountId}
29+
* based on the {@link ImportJobSettings} of the current import job.
30+
* The delegate will log a warning if no {@link ImporterProvider} is found for the {@link ImporterConfiguration} of the
31+
* current import job.
32+
* </p>
33+
*/
1934
@Slf4j
2035
@Singleton
2136
public class ReadTransactionLogDelegate implements JavaDelegate {

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/scheduler/GenerateTransactionJsonDelegate.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.jongsoft.finance.bpmn.delegate.scheduler;
22

3-
import com.jongsoft.finance.bpmn.delegate.importer.ParsedTransaction;
3+
import com.jongsoft.finance.core.TransactionType;
44
import com.jongsoft.finance.domain.transaction.ScheduledTransaction;
5-
import com.jongsoft.finance.domain.transaction.Transaction;
5+
import com.jongsoft.finance.importer.api.TransactionDTO;
66
import com.jongsoft.finance.providers.TransactionScheduleProvider;
77
import jakarta.inject.Singleton;
88
import org.camunda.bpm.engine.delegate.DelegateExecution;
@@ -28,13 +28,15 @@ public void execute(DelegateExecution execution) throws Exception {
2828

2929
transactionScheduleProvider.lookup(scheduledTransactionId)
3030
.ifPresent(schedule -> {
31-
var transaction = ParsedTransaction.builder()
32-
.amount(schedule.getAmount())
33-
.type(Transaction.Type.CREDIT)
34-
.transactionDate(LocalDate.parse(isoDate))
35-
.description(generateTransactionDescription(schedule))
36-
.build();
37-
31+
var transaction = new TransactionDTO(
32+
schedule.getAmount(),
33+
TransactionType.CREDIT,
34+
generateTransactionDescription(schedule),
35+
LocalDate.parse(isoDate),
36+
null,
37+
null,
38+
schedule.getDestination().getIban(),
39+
schedule.getDestination().getName());
3840

3941
execution.setVariable("destinationId", schedule.getDestination().getId());
4042
execution.setVariable("sourceId", schedule.getSource().getId());

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/transaction/CreateTransactionDelegate.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.jongsoft.finance.bpmn.delegate.transaction;
22

3-
import com.jongsoft.finance.bpmn.delegate.importer.ParsedTransaction;
43
import com.jongsoft.finance.domain.account.Account;
54
import com.jongsoft.finance.domain.transaction.Transaction;
5+
import com.jongsoft.finance.importer.api.TransactionDTO;
66
import com.jongsoft.finance.messaging.commands.transaction.CreateTransactionCommand;
77
import com.jongsoft.finance.messaging.handlers.TransactionCreationHandler;
88
import com.jongsoft.finance.providers.AccountProvider;
@@ -12,6 +12,23 @@
1212
import org.camunda.bpm.engine.delegate.JavaDelegate;
1313
import org.camunda.bpm.engine.variable.value.LongValue;
1414

15+
/**
16+
* Delegate for creating a transaction in the system.
17+
* <p>
18+
* This delegate is responsible for creating a transaction in the system. It is used in the BPMN process to
19+
* create a transaction from the parsed transaction data.
20+
* </p>
21+
* <p>
22+
* The delegate expects the following variables to be present in the execution:
23+
* <ul>
24+
* <li>transaction: The parsed transaction data from the import job</li>
25+
* <li>accountId: The ID of the account to create the transaction in</li>
26+
* <li>targetAccount: The ID of the account to create the transaction for</li>
27+
* <li>importJobSlug: The slug of the import job that the transaction is part of</li>
28+
* </ul>
29+
* The delegate will create the transaction in the target account and set the {@code transactionId} in the execution.
30+
* </p>
31+
*/
1532
@Slf4j
1633
@Singleton
1734
public class CreateTransactionDelegate implements JavaDelegate {
@@ -27,25 +44,31 @@ public class CreateTransactionDelegate implements JavaDelegate {
2744
@Override
2845
public void execute(DelegateExecution execution) throws Exception {
2946
var batchImportSlug = (String) execution.getVariable("importJobSlug");
30-
var parsedTransaction = (ParsedTransaction) execution.getVariableLocal("transaction");
47+
var parsedTransaction = (TransactionDTO) execution.getVariableLocal("transaction");
3148
var toAccount = lookupAccount(execution, "accountId");
3249
var targetAccount = lookupAccount(execution, "targetAccount");
3350

3451
log.debug("{}: Creating transaction into {} from {} with amount {}",
3552
execution.getCurrentActivityName(),
3653
targetAccount.getName(),
3754
toAccount.getName(),
38-
parsedTransaction.getAmount());
55+
parsedTransaction.amount());
56+
57+
var type = switch (parsedTransaction.type()) {
58+
case DEBIT -> Transaction.Type.DEBIT;
59+
case CREDIT -> Transaction.Type.CREDIT;
60+
case TRANSFER -> Transaction.Type.TRANSFER;
61+
};
3962

4063
Transaction transaction = targetAccount.createTransaction(
4164
toAccount,
42-
parsedTransaction.getAmount(),
43-
parsedTransaction.getType(),
65+
parsedTransaction.amount(),
66+
type,
4467
t -> t.currency(targetAccount.getCurrency())
45-
.date(parsedTransaction.getTransactionDate())
46-
.bookDate(parsedTransaction.getBookDate())
47-
.interestDate(parsedTransaction.getInterestDate())
48-
.description(parsedTransaction.getDescription())
68+
.date(parsedTransaction.transactionDate())
69+
.bookDate(parsedTransaction.bookDate())
70+
.interestDate(parsedTransaction.interestDate())
71+
.description(parsedTransaction.description())
4972
.importSlug(batchImportSlug));
5073

5174
long transactionId = creationHandler.handleCreatedEvent(new CreateTransactionCommand(transaction));

bpmn-process/src/main/java/com/jongsoft/finance/bpmn/delegate/transaction/PrepareAccountGenerationDelegate.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.jongsoft.finance.bpmn.delegate.transaction;
22

33
import com.jongsoft.finance.ProcessMapper;
4-
import com.jongsoft.finance.bpmn.delegate.importer.ParsedTransaction;
4+
import com.jongsoft.finance.core.TransactionType;
5+
import com.jongsoft.finance.importer.api.TransactionDTO;
6+
import com.jongsoft.finance.serialized.AccountJson;
57
import jakarta.inject.Singleton;
68
import lombok.extern.slf4j.Slf4j;
79
import org.camunda.bpm.engine.delegate.DelegateExecution;
@@ -23,15 +25,22 @@ public class PrepareAccountGenerationDelegate implements JavaDelegate {
2325

2426
@Override
2527
public void execute(DelegateExecution execution) throws Exception {
26-
var transaction = (ParsedTransaction) execution.getVariableLocal("transaction");
28+
var transaction = (TransactionDTO) execution.getVariableLocal("transaction");
2729

2830
log.debug("{}: Extracting the account to be created from the transaction {}.",
2931
execution.getCurrentActivityName(),
30-
transaction.getAccount().getName());
32+
transaction.opposingName());
33+
34+
var accountJson = AccountJson.builder()
35+
.name(transaction.opposingName())
36+
.iban(transaction.opposingIBAN())
37+
.type(transaction.type() == TransactionType.CREDIT ? "creditor" : "debtor")
38+
.currency("EUR")// todo this needs to be fixed later on
39+
.build();
3140

3241
execution.setVariableLocal(
3342
"accountJson",
34-
mapper.writeSafe(transaction.getAccount()));
43+
mapper.writeSafe(accountJson));
3544
}
3645

3746
}

0 commit comments

Comments
 (0)