|
| 1 | +/* |
| 2 | + * Copyright Inrupt Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to use, |
| 7 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | + * Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.inrupt.client.solid; |
| 22 | + |
| 23 | +import com.inrupt.client.spi.JsonService; |
| 24 | +import com.inrupt.client.spi.ServiceProvider; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.UncheckedIOException; |
| 31 | +import java.net.URI; |
| 32 | +import java.time.Instant; |
| 33 | + |
| 34 | +class Transaction extends SolidNonRDFSource { |
| 35 | + |
| 36 | + private final JsonService jsonService; |
| 37 | + private final TransactionData data; |
| 38 | + |
| 39 | + public Transaction(final URI identifier, final String contentType, final InputStream entity) { |
| 40 | + super(identifier, contentType, entity); |
| 41 | + |
| 42 | + this.jsonService = ServiceProvider.getJsonService(); |
| 43 | + try { |
| 44 | + this.data = jsonService.fromJson(super.getEntity(), TransactionData.class); |
| 45 | + } catch (IOException ex) { |
| 46 | + throw new UncheckedIOException("Unable to parse Transaction data", ex); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public String getId() { |
| 51 | + return data.getId(); |
| 52 | + } |
| 53 | + |
| 54 | + public void setId(final String id) { |
| 55 | + data.setId(id); |
| 56 | + } |
| 57 | + |
| 58 | + public String getDescription() { |
| 59 | + return data.getDescription(); |
| 60 | + } |
| 61 | + |
| 62 | + public void setDescription(final String description) { |
| 63 | + data.setDescription(description); |
| 64 | + } |
| 65 | + |
| 66 | + public Instant getDate() { |
| 67 | + return data.getDate(); |
| 68 | + } |
| 69 | + |
| 70 | + public void setDate(final Instant date) { |
| 71 | + data.setDate(date); |
| 72 | + } |
| 73 | + |
| 74 | + public double getAmount() { |
| 75 | + return data.getAmount(); |
| 76 | + } |
| 77 | + |
| 78 | + public void setAmount(final double amount) { |
| 79 | + data.setAmount(amount); |
| 80 | + } |
| 81 | + |
| 82 | + public String getCategory() { |
| 83 | + return data.getCategory(); |
| 84 | + } |
| 85 | + |
| 86 | + public void setCategory(final String category) { |
| 87 | + data.setCategory(category); |
| 88 | + } |
| 89 | + |
| 90 | + public TransactionType getType() { |
| 91 | + return data.getType(); |
| 92 | + } |
| 93 | + |
| 94 | + public void setType(final TransactionType type) { |
| 95 | + data.setType(type); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public InputStream getEntity() throws IOException { |
| 100 | + try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) { |
| 101 | + jsonService.toJson(data, output); |
| 102 | + return new ByteArrayInputStream(output.toByteArray()); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments