|
| 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.database; |
| 24 | + |
| 25 | +import com.iluwatar.hexagonal.domain.LotteryNumbers; |
| 26 | +import com.iluwatar.hexagonal.domain.LotteryTicket; |
| 27 | +import com.iluwatar.hexagonal.domain.LotteryTicketId; |
| 28 | +import com.iluwatar.hexagonal.domain.PlayerDetails; |
| 29 | +import com.mongodb.MongoClient; |
| 30 | +import com.mongodb.client.MongoCollection; |
| 31 | +import com.mongodb.client.MongoDatabase; |
| 32 | +import org.bson.Document; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +/** |
| 42 | + * Mongo lottery ticket database |
| 43 | + */ |
| 44 | +public class MongoTicketRepository implements LotteryTicketRepository { |
| 45 | + |
| 46 | + private static final String DEFAULT_HOST = "localhost"; |
| 47 | + private static final int DEFAULT_PORT = 27017; |
| 48 | + private static final String DEFAULT_DB = "lotteryDB"; |
| 49 | + private static final String DEFAULT_TICKETS_COLLECTION = "lotteryTickets"; |
| 50 | + private static final String DEFAULT_COUNTERS_COLLECTION = "counters"; |
| 51 | + |
| 52 | + private MongoClient mongoClient; |
| 53 | + private MongoDatabase database; |
| 54 | + private MongoCollection<Document> ticketsCollection; |
| 55 | + private MongoCollection<Document> countersCollection; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor |
| 59 | + */ |
| 60 | + public MongoTicketRepository() { |
| 61 | + connect(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Constructor accepting parameters |
| 66 | + */ |
| 67 | + public MongoTicketRepository(String host, int port, String dbName, String ticketsCollectionName, |
| 68 | + String countersCollectionName) { |
| 69 | + connect(host, port, dbName, ticketsCollectionName, countersCollectionName); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Connect to database with default parameters |
| 74 | + */ |
| 75 | + public void connect() { |
| 76 | + connect(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_DB, DEFAULT_TICKETS_COLLECTION, DEFAULT_COUNTERS_COLLECTION); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Connect to database with given parameters |
| 81 | + */ |
| 82 | + public void connect(String host, int port, String dbName, String ticketsCollectionName, |
| 83 | + String countersCollectionName) { |
| 84 | + if (mongoClient != null) { |
| 85 | + mongoClient.close(); |
| 86 | + } |
| 87 | + mongoClient = new MongoClient(host , port); |
| 88 | + database = mongoClient.getDatabase(dbName); |
| 89 | + ticketsCollection = database.getCollection(ticketsCollectionName); |
| 90 | + countersCollection = database.getCollection(countersCollectionName); |
| 91 | + if (countersCollection.count() <= 0) { |
| 92 | + initCounters(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void initCounters() { |
| 97 | + Document doc = new Document("_id", "ticketId").append("seq", 1); |
| 98 | + countersCollection.insertOne(doc); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return next ticket id |
| 103 | + */ |
| 104 | + public int getNextId() { |
| 105 | + Document find = new Document("_id", "ticketId"); |
| 106 | + Document increase = new Document("seq", 1); |
| 107 | + Document update = new Document("$inc", increase); |
| 108 | + Document result = countersCollection.findOneAndUpdate(find, update); |
| 109 | + return result.getInteger("seq"); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return mongo client |
| 114 | + */ |
| 115 | + public MongoClient getMongoClient() { |
| 116 | + return mongoClient; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * |
| 121 | + * @return mongo database |
| 122 | + */ |
| 123 | + public MongoDatabase getMongoDatabase() { |
| 124 | + return database; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * |
| 129 | + * @return tickets collection |
| 130 | + */ |
| 131 | + public MongoCollection<Document> getTicketsCollection() { |
| 132 | + return ticketsCollection; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * |
| 137 | + * @return counters collection |
| 138 | + */ |
| 139 | + public MongoCollection<Document> getCountersCollection() { |
| 140 | + return countersCollection; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public Optional<LotteryTicket> findById(LotteryTicketId id) { |
| 145 | + Document find = new Document("ticketId", id.getId()); |
| 146 | + ArrayList<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>()); |
| 147 | + if (results.size() > 0) { |
| 148 | + LotteryTicket lotteryTicket = docToTicket(results.get(0)); |
| 149 | + return Optional.of(lotteryTicket); |
| 150 | + } else { |
| 151 | + return Optional.empty(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Optional<LotteryTicketId> save(LotteryTicket ticket) { |
| 157 | + int ticketId = getNextId(); |
| 158 | + Document doc = new Document("ticketId", ticketId); |
| 159 | + doc.put("email", ticket.getPlayerDetails().getEmail()); |
| 160 | + doc.put("bank", ticket.getPlayerDetails().getBankAccount()); |
| 161 | + doc.put("phone", ticket.getPlayerDetails().getPhoneNumber()); |
| 162 | + doc.put("numbers", ticket.getNumbers().getNumbersAsString()); |
| 163 | + ticketsCollection.insertOne(doc); |
| 164 | + return Optional.of(new LotteryTicketId(ticketId)); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Map<LotteryTicketId, LotteryTicket> findAll() { |
| 169 | + Map<LotteryTicketId, LotteryTicket> map = new HashMap<>(); |
| 170 | + ArrayList<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>()); |
| 171 | + for (Document doc: docs) { |
| 172 | + LotteryTicket lotteryTicket = docToTicket(doc); |
| 173 | + map.put(lotteryTicket.getId(), lotteryTicket); |
| 174 | + } |
| 175 | + return map; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void deleteAll() { |
| 180 | + ticketsCollection.deleteMany(new Document()); |
| 181 | + } |
| 182 | + |
| 183 | + private LotteryTicket docToTicket(Document doc) { |
| 184 | + PlayerDetails playerDetails = PlayerDetails.create(doc.getString("email"), doc.getString("bank"), |
| 185 | + doc.getString("phone")); |
| 186 | + int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray(); |
| 187 | + HashSet<Integer> numbers = new HashSet<>(); |
| 188 | + for (int num: numArray) { |
| 189 | + numbers.add(num); |
| 190 | + } |
| 191 | + LotteryNumbers lotteryNumbers = LotteryNumbers.create(numbers); |
| 192 | + return LotteryTicket.create(new LotteryTicketId(doc.getInteger("ticketId")), playerDetails, lotteryNumbers); |
| 193 | + } |
| 194 | +} |
0 commit comments