|
| 1 | +package com.trolley; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 7 | +import java.util.List; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.trolley.Exceptions.InvalidFieldException; |
| 10 | +import com.trolley.types.Batch; |
| 11 | +import com.trolley.types.Payment; |
| 12 | +import com.trolley.types.supporting.BatchSummary; |
| 13 | +import com.trolley.types.supporting.Batches; |
| 14 | +import com.trolley.types.supporting.BatchesIterator; |
| 15 | +import com.trolley.types.supporting.Meta; |
| 16 | + |
| 17 | +public class BatchGateway |
| 18 | +{ |
| 19 | + Client client; |
| 20 | + |
| 21 | + public BatchGateway(final Configuration config) { |
| 22 | + this.client = new Client(config); |
| 23 | + } |
| 24 | + |
| 25 | + public Batch find(final String batchId) throws Exception { |
| 26 | + if (batchId == null || batchId.isEmpty()) { |
| 27 | + throw new InvalidFieldException("Batch id cannot be null or empty."); |
| 28 | + } |
| 29 | + final String endPoint = "/v1/batches/" + batchId; |
| 30 | + final String response = this.client.get(endPoint); |
| 31 | + return this.batchFactory(response); |
| 32 | + } |
| 33 | + |
| 34 | + public boolean update(final String batchId, final Batch batch) throws Exception { |
| 35 | + if (batchId == null || batchId.isEmpty()) { |
| 36 | + throw new InvalidFieldException("Batch id cannot be null or empty."); |
| 37 | + } |
| 38 | + if (batch == null) { |
| 39 | + throw new InvalidFieldException("Batch object cannot be null or empty."); |
| 40 | + } |
| 41 | + final String jsonBatch = new ObjectMapper().writeValueAsString((Object)batch); |
| 42 | + final String endPoint = "/v1/batches/" + batchId; |
| 43 | + this.client.patch(endPoint, jsonBatch); |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + public boolean delete(final String batchId) throws Exception { |
| 48 | + if (batchId == null || batchId.isEmpty()) { |
| 49 | + throw new InvalidFieldException("Batch id cannot be null or empty."); |
| 50 | + } |
| 51 | + final String endPoint = "/v1/batches/" + batchId; |
| 52 | + this.client.delete(endPoint); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Delete multiple batches. |
| 58 | + * <p>You should pass a {@code List<Batch>} object to this method, with each item of the list filled with the ID of the batch you want to delete. |
| 59 | + * <p>This method will serialize only the IDs. |
| 60 | + * @param batches a List<Batch> representing the batches that need to be deleted. |
| 61 | + * @return True if delete operation was successful |
| 62 | + * @throws Exception Thrown if the delete operation wasn't successful or if any other exception occurs. |
| 63 | + */ |
| 64 | + public boolean delete(final List<Batch> batches) throws Exception { |
| 65 | + if (batches == null || batches.isEmpty()) { |
| 66 | + throw new InvalidFieldException("batches cannot be null or empty."); |
| 67 | + } |
| 68 | + final String endPoint = "/v1/batches"; |
| 69 | + |
| 70 | + String body = "{\"ids\" : ["; |
| 71 | + |
| 72 | + for (int i = 0; i < batches.size(); i++) { |
| 73 | + body+=new ObjectMapper().writeValueAsString(batches.get(i).getId()); |
| 74 | + if(i<(batches.size()-1)){ |
| 75 | + body+=","; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + body+="]}"; |
| 80 | + this.client.delete(endPoint, body); |
| 81 | + |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + public Batch create(final Batch batch) throws Exception { |
| 86 | + if (batch == null) { |
| 87 | + throw new InvalidFieldException("Batch cannot be null."); |
| 88 | + } |
| 89 | + final String body = new ObjectMapper().writeValueAsString((Object)batch); |
| 90 | + final String endPoint = "/v1/batches/"; |
| 91 | + final String response = this.client.post(endPoint, body); |
| 92 | + return this.batchFactory(response); |
| 93 | + } |
| 94 | + |
| 95 | + public String generateQuote(final String batchId) throws Exception { |
| 96 | + if (batchId == null || batchId.isEmpty()) { |
| 97 | + throw new InvalidFieldException("Batch id cannot be null or empty."); |
| 98 | + } |
| 99 | + final String endPoint = "/v1/batches/" + batchId + "/generate-quote"; |
| 100 | + final String response = this.client.post(endPoint); |
| 101 | + return response; |
| 102 | + } |
| 103 | + |
| 104 | + public String processBatch(final String batchId) throws Exception { |
| 105 | + if (batchId == null || batchId.isEmpty()) { |
| 106 | + throw new InvalidFieldException("Batch id cannot be null or empty."); |
| 107 | + } |
| 108 | + final String endPoint = "/v1/batches/" + batchId + "/start-processing"; |
| 109 | + final String response = this.client.post(endPoint); |
| 110 | + return response; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Search for batches. |
| 115 | + * This method returns an iterator which auto-paginate with 10 items per page. |
| 116 | + * If you want to paginate manually, please use the {@code search(page, pageSize, searchTerm)} method |
| 117 | + * @param searchTerm the search keyword to be searched for |
| 118 | + * @return BatchIterator which auto paginates through all available payments 10 items per page |
| 119 | + * @throws Exception |
| 120 | + */ |
| 121 | + public BatchesIterator search(final String searchTerm) throws Exception { |
| 122 | + if (searchTerm == null) { |
| 123 | + throw new InvalidFieldException("searchTerm cannot be null. If you don't wish to provide a searchTerm, pass a blank String."); |
| 124 | + } |
| 125 | + int pageSize = 10; |
| 126 | + Batches b = search(1, pageSize, searchTerm); |
| 127 | + return new BatchesIterator(this, b, searchTerm); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Search for Batches with manual pagination. |
| 132 | + * @param page which page number you want to access |
| 133 | + * @param pageSize number of items you want per page |
| 134 | + * @param searchTerm keyword to search for |
| 135 | + * @return {@code Batches} object, containing a {@code List<Batch>} object and a {@code Meta} object to access pagination information |
| 136 | + * @throws Exception |
| 137 | + */ |
| 138 | + public Batches search(final int page, final int pageSize, final String searchTerm) throws Exception { |
| 139 | + if (page < 0) { |
| 140 | + throw new InvalidFieldException("page cannot be less than 0"); |
| 141 | + } |
| 142 | + if (pageSize < 0) { |
| 143 | + throw new InvalidFieldException("pageSize cannot be less than 0"); |
| 144 | + } |
| 145 | + if (searchTerm == null) { |
| 146 | + throw new InvalidFieldException("searchTerm cannot be null. If you don't wish to provide a searchTerm, pass a blank String."); |
| 147 | + } |
| 148 | + final String endPoint = "/v1/batches/?&search=" + searchTerm + "&page=" + page + "&pageSize=" + pageSize; |
| 149 | + final String response = this.client.get(endPoint); |
| 150 | + return this.batchListFactory(response); |
| 151 | + } |
| 152 | + |
| 153 | + public BatchSummary summary(final String batchId) throws Exception { |
| 154 | + if (batchId == null || batchId.isEmpty()) { |
| 155 | + throw new InvalidFieldException("Batch id cannot be null os empty"); |
| 156 | + } |
| 157 | + final String endPoint = "/v1/batches/" + batchId + "/summary"; |
| 158 | + final String response = this.client.get(endPoint); |
| 159 | + final ObjectMapper mapper = new ObjectMapper(); |
| 160 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 161 | + final JsonNode node = mapper.readTree(response); |
| 162 | + final BatchSummary batchSummary = (BatchSummary)mapper.readValue(node.get("batchSummary").traverse(), (Class)BatchSummary.class); |
| 163 | + return batchSummary; |
| 164 | + } |
| 165 | + |
| 166 | + private Batch batchFactory(final String data) throws IOException { |
| 167 | + final ObjectMapper mapper = new ObjectMapper(); |
| 168 | + final JsonNode node = mapper.readTree(data); |
| 169 | + mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); |
| 170 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 171 | + final Object payments = mapper.readValue(node.get("batch").get("payments").get("payments").traverse(), (Class)Object.class); |
| 172 | + final List<Payment> castPayments = (List<Payment>)payments; |
| 173 | + final List<Payment> paymentList = new ArrayList<Payment>(); |
| 174 | + for (int i = 0; i < castPayments.size(); ++i) { |
| 175 | + final Payment payment = (Payment)mapper.convertValue((Object)castPayments.get(i), (Class)Payment.class); |
| 176 | + paymentList.add(payment); |
| 177 | + } |
| 178 | + final Batch batch = (Batch)mapper.readValue(node.get("batch").traverse(), (Class)Batch.class); |
| 179 | + batch.setPayments(paymentList); |
| 180 | + return batch; |
| 181 | + } |
| 182 | + |
| 183 | + private Batches batchListFactory(final String data) throws IOException { |
| 184 | + final ObjectMapper mapper = new ObjectMapper(); |
| 185 | + final JsonNode node = mapper.readTree(data); |
| 186 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 187 | + |
| 188 | + final List<Batch> batchesFromResponse = (List<Batch>)mapper.readValue(node.get("batches").traverse(), (Class)Object.class); |
| 189 | + final Meta meta = (Meta)mapper.readValue(node.get("meta").traverse(), (Class)Meta.class); |
| 190 | + |
| 191 | + final List<Batch> batches = new ArrayList<Batch>(); |
| 192 | + for (int i = 0; i < batchesFromResponse.size(); ++i) { |
| 193 | + final Batch pojo = (Batch)mapper.convertValue((Object)batchesFromResponse.get(i), (Class)Batch.class); |
| 194 | + batches.add(pojo); |
| 195 | + } |
| 196 | + |
| 197 | + return new Batches(batches, meta); |
| 198 | + } |
| 199 | +} |
0 commit comments