Skip to content

Commit df32a7b

Browse files
committed
Hexagonal pattern: Introduced lottery events port with two adapters
1 parent c4c5e78 commit df32a7b

File tree

11 files changed

+282
-41
lines changed

11 files changed

+282
-41
lines changed

hexagonal/etc/hexagonal.ucls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<class-diagram version="1.1.10" icons="true" automaticImage="PNG" always-add-relationships="false"
33
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
44
router="FAN">
5-
<class id="1" language="java" name="com.iluwatar.hexagonal.notifications.StdOutNotifications" project="hexagonal"
5+
<class id="1" language="java" name="com.iluwatar.hexagonal.eventlog.StdOutEventLog" project="hexagonal"
66
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/notifications/StdOutNotifications.java" binary="false"
77
corner="BOTTOM_RIGHT">
88
<position height="167" width="235" x="731" y="122"/>
@@ -161,7 +161,7 @@
161161
<operations public="true" package="true" protected="true" private="true" static="true"/>
162162
</display>
163163
</class>
164-
<interface id="17" language="java" name="com.iluwatar.hexagonal.notifications.LotteryNotifications"
164+
<interface id="17" language="java" name="com.iluwatar.hexagonal.eventlog.LotteryEventLog"
165165
project="hexagonal" file="/hexagonal/src/main/java/com/iluwatar/hexagonal/notifications/LotteryNotifications.java"
166166
binary="false" corner="BOTTOM_RIGHT">
167167
<position height="149" width="235" x="731" y="347"/>

hexagonal/etc/hexagonal.urm.puml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ package com.iluwatar.hexagonal.database {
116116
+ save(LotteryTicket) : Optional<LotteryTicketId> {abstract}
117117
}
118118
}
119-
package com.iluwatar.hexagonal.notifications {
119+
package com.iluwatar.hexagonal.eventlog {
120120
interface LotteryNotifications {
121121
+ notifyNoWin(PlayerDetails) {abstract}
122122
+ notifyPrize(PlayerDetails, int) {abstract}

hexagonal/src/main/java/com/iluwatar/hexagonal/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
*
5757
* The secondary ports that application core uses are {@link WireTransfers}
5858
* which is a banking service, {@link LotteryNotifications} that delivers
59-
* notifications as lottery events occur and {@link LotteryTicketRepository}
59+
* eventlog as lottery events occur and {@link LotteryTicketRepository}
6060
* that is the storage for the lottery tickets.
6161
*
6262
*/

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.google.inject.Inject;
2626
import com.iluwatar.hexagonal.banking.WireTransfers;
2727
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
28-
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
28+
import com.iluwatar.hexagonal.eventlog.LotteryEventLog;
2929

3030
import java.util.Map;
3131

@@ -37,15 +37,15 @@
3737
public class LotteryAdministration {
3838

3939
private final LotteryTicketRepository repository;
40-
private final LotteryNotifications notifications;
40+
private final LotteryEventLog notifications;
4141
private final WireTransfers wireTransfers;
4242
private final LotteryTicketChecker checker;
4343

4444
/**
4545
* Constructor
4646
*/
4747
@Inject
48-
public LotteryAdministration(LotteryTicketRepository repository, LotteryNotifications notifications,
48+
public LotteryAdministration(LotteryTicketRepository repository, LotteryEventLog notifications,
4949
WireTransfers wireTransfers) {
5050
this.repository = repository;
5151
this.notifications = notifications;
@@ -72,12 +72,12 @@ public LotteryNumbers performLottery() {
7272
boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT,
7373
LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
7474
if (transferred) {
75-
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
75+
notifications.ticketWon(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
7676
} else {
77-
notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
77+
notifications.prizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
7878
}
7979
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
80-
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
80+
notifications.ticketDidNotWin(tickets.get(id).getPlayerDetails());
8181
}
8282
}
8383
return numbers;

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.google.inject.Inject;
2626
import com.iluwatar.hexagonal.banking.WireTransfers;
2727
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
28-
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
28+
import com.iluwatar.hexagonal.eventlog.LotteryEventLog;
2929

3030
import java.util.Optional;
3131

@@ -37,15 +37,15 @@
3737
public class LotteryService {
3838

3939
private final LotteryTicketRepository repository;
40-
private final LotteryNotifications notifications;
40+
private final LotteryEventLog notifications;
4141
private final WireTransfers wireTransfers;
4242
private final LotteryTicketChecker checker;
4343

4444
/**
4545
* Constructor
4646
*/
4747
@Inject
48-
public LotteryService(LotteryTicketRepository repository, LotteryNotifications notifications,
48+
public LotteryService(LotteryTicketRepository repository, LotteryEventLog notifications,
4949
WireTransfers wireTransfers) {
5050
this.repository = repository;
5151
this.notifications = notifications;
@@ -60,12 +60,12 @@ public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
6060
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
6161
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
6262
if (result == false) {
63-
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
63+
notifications.ticketSubmitError(ticket.getPlayerDetails());
6464
return Optional.empty();
6565
}
6666
Optional<LotteryTicketId> optional = repository.save(ticket);
6767
if (optional.isPresent()) {
68-
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
68+
notifications.ticketSubmitted(ticket.getPlayerDetails());
6969
}
7070
return optional;
7171
}

hexagonal/src/main/java/com/iluwatar/hexagonal/notifications/LotteryNotifications.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/eventlog/LotteryEventLog.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,40 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23-
package com.iluwatar.hexagonal.notifications;
23+
package com.iluwatar.hexagonal.eventlog;
2424

2525
import com.iluwatar.hexagonal.domain.PlayerDetails;
2626

2727
/**
2828
*
29-
* Provides notifications for lottery events.
29+
* Event log for lottery events
3030
*
3131
*/
32-
public interface LotteryNotifications {
32+
public interface LotteryEventLog {
3333

3434
/**
35-
* Notify lottery ticket was submitted
35+
* lottery ticket submitted
3636
*/
37-
void notifyTicketSubmitted(PlayerDetails details);
37+
void ticketSubmitted(PlayerDetails details);
3838

3939
/**
40-
* Notify there was an error submitting lottery ticket
40+
* error submitting lottery ticket
4141
*/
42-
void notifyTicketSubmitError(PlayerDetails details);
42+
void ticketSubmitError(PlayerDetails details);
4343

4444
/**
45-
* Notify lottery ticket did not win
45+
* lottery ticket did not win
4646
*/
47-
void notifyNoWin(PlayerDetails details);
47+
void ticketDidNotWin(PlayerDetails details);
4848

4949
/**
50-
* Notify that prize has been paid
50+
* lottery ticket won
5151
*/
52-
void notifyPrize(PlayerDetails details, int prizeAmount);
52+
void ticketWon(PlayerDetails details, int prizeAmount);
5353

5454
/**
55-
* Notify that there was an error paying the prize
55+
* error paying the prize
5656
*/
57-
void notifyPrizeError(PlayerDetails details, int prizeAmount);
57+
void prizeError(PlayerDetails details, int prizeAmount);
5858

5959
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.eventlog;
24+
25+
import com.iluwatar.hexagonal.domain.PlayerDetails;
26+
import com.mongodb.MongoClient;
27+
import com.mongodb.client.MongoCollection;
28+
import com.mongodb.client.MongoDatabase;
29+
import org.bson.Document;
30+
31+
/**
32+
* Mongo based event log
33+
*/
34+
public class MongoEventLog implements LotteryEventLog {
35+
36+
private static final String DEFAULT_DB = "lotteryDB";
37+
private static final String DEFAULT_EVENTS_COLLECTION = "events";
38+
39+
private MongoClient mongoClient;
40+
private MongoDatabase database;
41+
private MongoCollection<Document> eventsCollection;
42+
43+
private StdOutEventLog stdOutEventLog = new StdOutEventLog();
44+
45+
/**
46+
* Constructor
47+
*/
48+
public MongoEventLog() {
49+
connect();
50+
}
51+
52+
/**
53+
* Constructor accepting parameters
54+
*/
55+
public MongoEventLog(String dbName, String eventsCollectionName) {
56+
connect(dbName, eventsCollectionName);
57+
}
58+
59+
/**
60+
* Connect to database with default parameters
61+
*/
62+
public void connect() {
63+
connect(DEFAULT_DB, DEFAULT_EVENTS_COLLECTION);
64+
}
65+
66+
/**
67+
* Connect to database with given parameters
68+
*/
69+
public void connect(String dbName, String eventsCollectionName) {
70+
if (mongoClient != null) {
71+
mongoClient.close();
72+
}
73+
mongoClient = new MongoClient(System.getProperty("mongo-host"),
74+
Integer.parseInt(System.getProperty("mongo-port")));
75+
database = mongoClient.getDatabase(dbName);
76+
eventsCollection = database.getCollection(eventsCollectionName);
77+
}
78+
79+
/**
80+
* @return mongo client
81+
*/
82+
public MongoClient getMongoClient() {
83+
return mongoClient;
84+
}
85+
86+
/**
87+
*
88+
* @return mongo database
89+
*/
90+
public MongoDatabase getMongoDatabase() {
91+
return database;
92+
}
93+
94+
/**
95+
*
96+
* @return accounts collection
97+
*/
98+
public MongoCollection<Document> getEventsCollection() {
99+
return eventsCollection;
100+
}
101+
102+
103+
@Override
104+
public void ticketSubmitted(PlayerDetails details) {
105+
Document document = new Document("email", details.getEmail());
106+
document.put("phone", details.getPhoneNumber());
107+
document.put("bank", details.getBankAccount());
108+
document.put("message", String.format("Lottery ticket was submitted and bank account was charged for 3 credits."));
109+
eventsCollection.insertOne(document);
110+
stdOutEventLog.ticketSubmitted(details);
111+
}
112+
113+
@Override
114+
public void ticketSubmitError(PlayerDetails details) {
115+
Document document = new Document("email", details.getEmail());
116+
document.put("phone", details.getPhoneNumber());
117+
document.put("bank", details.getBankAccount());
118+
document.put("message", String.format("Lottery ticket could not be submitted because lack of funds."));
119+
eventsCollection.insertOne(document);
120+
stdOutEventLog.ticketSubmitError(details);
121+
}
122+
123+
@Override
124+
public void ticketDidNotWin(PlayerDetails details) {
125+
Document document = new Document("email", details.getEmail());
126+
document.put("phone", details.getPhoneNumber());
127+
document.put("bank", details.getBankAccount());
128+
document.put("message", String.format("Lottery ticket was checked and unfortunately did not win this time."));
129+
eventsCollection.insertOne(document);
130+
stdOutEventLog.ticketDidNotWin(details);
131+
}
132+
133+
@Override
134+
public void ticketWon(PlayerDetails details, int prizeAmount) {
135+
Document document = new Document("email", details.getEmail());
136+
document.put("phone", details.getPhoneNumber());
137+
document.put("bank", details.getBankAccount());
138+
document.put("message", String.format("Lottery ticket won! The bank account was deposited with %d credits.",
139+
prizeAmount));
140+
eventsCollection.insertOne(document);
141+
stdOutEventLog.ticketWon(details, prizeAmount);
142+
}
143+
144+
@Override
145+
public void prizeError(PlayerDetails details, int prizeAmount) {
146+
Document document = new Document("email", details.getEmail());
147+
document.put("phone", details.getPhoneNumber());
148+
document.put("bank", details.getBankAccount());
149+
document.put("message", String.format("Lottery ticket won! Unfortunately the bank credit transfer of %d failed.",
150+
prizeAmount));
151+
eventsCollection.insertOne(document);
152+
stdOutEventLog.prizeError(details, prizeAmount);
153+
}
154+
}

hexagonal/src/main/java/com/iluwatar/hexagonal/notifications/StdOutNotifications.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/eventlog/StdOutEventLog.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,43 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23-
package com.iluwatar.hexagonal.notifications;
23+
package com.iluwatar.hexagonal.eventlog;
2424

2525
import com.iluwatar.hexagonal.domain.PlayerDetails;
2626

27-
public class StdOutNotifications implements LotteryNotifications {
27+
/**
28+
* Standard output event log
29+
*/
30+
public class StdOutEventLog implements LotteryEventLog {
2831

2932
@Override
30-
public void notifyTicketSubmitted(PlayerDetails details) {
33+
public void ticketSubmitted(PlayerDetails details) {
3134
System.out.println(String.format("Lottery ticket for %s was submitted. Bank account %s was charged for 3 credits.",
3235
details.getEmail(), details.getBankAccount()));
3336
}
3437

3538
@Override
36-
public void notifyNoWin(PlayerDetails details) {
39+
public void ticketDidNotWin(PlayerDetails details) {
3740
System.out.println(String.format("Lottery ticket for %s was checked and unfortunately did not win this time.",
3841
details.getEmail()));
3942
}
4043

4144
@Override
42-
public void notifyPrize(PlayerDetails details, int prizeAmount) {
45+
public void ticketWon(PlayerDetails details, int prizeAmount) {
4346
System.out
4447
.println(String.format("Lottery ticket for %s has won! The bank account %s was deposited with %d credits.",
4548
details.getEmail(), details.getBankAccount(), prizeAmount));
4649
}
4750

4851
@Override
49-
public void notifyPrizeError(PlayerDetails details, int prizeAmount) {
52+
public void prizeError(PlayerDetails details, int prizeAmount) {
5053
System.out
5154
.println(String.format("Lottery ticket for %s has won! Unfortunately the bank credit transfer of %d failed.",
5255
details.getEmail(), prizeAmount));
5356
}
5457

5558
@Override
56-
public void notifyTicketSubmitError(PlayerDetails details) {
59+
public void ticketSubmitError(PlayerDetails details) {
5760
System.out.println(
5861
String.format("Lottery ticket for %s could not be submitted because the credit transfer of 3 credits failed.",
5962
details.getEmail()));

0 commit comments

Comments
 (0)