-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountMongoService.java
104 lines (92 loc) · 3.04 KB
/
AccountMongoService.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package kp.services;
import java.util.Optional;
import org.jboss.logging.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.quarkus.mongodb.panache.PanacheQuery;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import kp.Constants;
import kp.mongo.entities.AccountM;
import kp.mongo.entities.MoneyM;
import kp.mongo.entities.OwnerM;
import kp.mongo.entities.RecordKey;
/**
* The service for the MongoDB database.<br/>
* <br/>
* This service uses the 'Active Record' design pattern.<br/>
* The Quarkus guide proposes two solutions for MongoDB:
* <ol>
* <li>using the 'Active Record' pattern</li>
* <li>using the 'Repository' pattern</li>
* </ol>
*/
@ApplicationScoped
public class AccountMongoService {
@SuppressWarnings("java:S6813")
// switch off Sonarqube rule 'Avoid field dependency injection'
@Inject
private Logger logger;
private final ObjectMapper objectMapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
/**
* The constructor.
*/
public AccountMongoService() {
super();
}
/**
* Creates the account in MongoDB database from the payload in JSON format.
*
* @param key the key
* @param payload the payload in JSON format
*/
@SuppressWarnings("java:S3252")
// switch off Sonarqube rule 'static base class members should not be accessed
// via derived types'
public void processPayload(String key, String payload) {
if (RecordKey.count("key", key) > 0) {
if (logger.isDebugEnabled()) {
logger.debugf("processPayload(): account not created because key exists, key[%s]", key);
}
return;
}
final RecordKey recordKey = new RecordKey();
recordKey.key = key;
recordKey.persist();
try {
final AccountM accountM = objectMapper.readValue(payload, AccountM.class);
accountM.money.persist();
accountM.owners.getFirst().persist();
accountM.persist();
logger.infof("processPayload(): key[%s], account %s, name[%s]", key, Constants.MONGO_ID_FUN.apply(accountM),
accountM.name);
} catch (Exception e) {
logger.errorf("processPayload(): exception[%s]", e.getMessage());
}
}
/**
* Finds the {@link AccountM}.
*
* @param name the {@link AccountM} name
* @return the {@link Optional} with the {@link AccountM}
*/
@SuppressWarnings("java:S3252")
// switch off Sonarqube rule 'static base class members should not be accessed
// via derived types'
public Optional<AccountM> findAccount(String name) {
return Optional.ofNullable(AccountM.find("name", name)).map(PanacheQuery::firstResult);
}
/**
* Deletes all accounts in MongoDB database.
*/
@SuppressWarnings("java:S3252")
// switch off Sonarqube rule 'static base class members should not be accessed
// via derived types'
public void deleteAllAccounts() {
AccountM.deleteAll();
MoneyM.deleteAll();
OwnerM.deleteAll();
RecordKey.deleteAll();
}
}