forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 4
/
XmlAdaptedItem.java
159 lines (139 loc) · 5.29 KB
/
XmlAdaptedItem.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package seedu.inventory.storage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.bind.annotation.XmlElement;
import seedu.inventory.commons.exceptions.IllegalValueException;
import seedu.inventory.model.item.Image;
import seedu.inventory.model.item.Item;
import seedu.inventory.model.item.Name;
import seedu.inventory.model.item.Price;
import seedu.inventory.model.item.Quantity;
import seedu.inventory.model.item.Sku;
import seedu.inventory.model.tag.Tag;
/**
* JAXB-friendly version of the Item.
*/
public class XmlAdaptedItem {
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Item's %s field is missing!";
@XmlElement(required = true)
private String name;
@XmlElement(required = true)
private String price;
@XmlElement(required = true)
private String quantity;
@XmlElement(required = true)
private String sku;
@XmlElement(required = true)
private String image;
@XmlElement
private List<XmlAdaptedTag> tagged = new ArrayList<>();
/**
* Constructs an XmlAdaptedItem.
* This is the no-arg constructor that is required by JAXB.
*/
public XmlAdaptedItem() {}
/**
* Constructs an {@code XmlAdaptedItem} with the given item details.
*/
public XmlAdaptedItem(String name, String price, String quantity, String sku,
String image, List<XmlAdaptedTag> tagged) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.sku = sku;
this.image = image;
if (tagged != null) {
this.tagged = new ArrayList<>(tagged);
}
}
/**
* Converts a given Item into this class for JAXB use.
*
* @param source future changes to this will not affect the created XmlAdaptedItem
*/
public XmlAdaptedItem(Item source) {
name = source.getName().fullName;
price = source.getPrice().value;
quantity = source.getQuantity().value;
sku = source.getSku().value;
image = source.getImage().value;
tagged = source.getTags().stream()
.map(XmlAdaptedTag::new)
.collect(Collectors.toList());
}
/**
* Converts this jaxb-friendly adapted item object into the model's Item object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted item
*/
public Item toModelType() throws IllegalValueException {
final List<Tag> itemTags = new ArrayList<>();
for (XmlAdaptedTag tag : tagged) {
itemTags.add(tag.toModelType());
}
if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
}
if (!Name.isValidName(name)) {
throw new IllegalValueException(Name.MESSAGE_NAME_CONSTRAINTS);
}
final Name modelName = new Name(name);
if (price == null) {
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, Price.class.getSimpleName())
);
}
if (!Price.isValidPrice(price)) {
throw new IllegalValueException(Price.MESSAGE_PRICE_CONSTRAINTS);
}
final Price modelPrice = new Price(price);
if (quantity == null) {
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, Quantity.class.getSimpleName())
);
}
if (!Quantity.isValidQuantity(quantity)) {
throw new IllegalValueException(Quantity.MESSAGE_QUANTITY_CONSTRAINTS);
}
if (!Quantity.isNotOverflowInteger(quantity)) {
throw new IllegalValueException(Quantity.MESSAGE_QUANTITY_CONSTRAINTS);
}
final Quantity modelQuantity = new Quantity(quantity);
if (sku == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Sku.class.getSimpleName()));
}
if (!Sku.isValidSku(sku)) {
throw new IllegalValueException(Sku.MESSAGE_SKU_CONSTRAINTS);
}
final Sku modelSku = new Sku(sku);
if (image == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Image.class.getSimpleName()));
}
if (!Image.isValidImage(image)) {
throw new IllegalValueException(Image.MESSAGE_IMAGE_CONSTRAINTS);
}
final Image modelImage = new Image(image);
final Set<Tag> modelTags = new HashSet<>(itemTags);
return new Item(modelName, modelPrice, modelQuantity, modelSku, modelImage, modelTags);
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof XmlAdaptedItem)) {
return false;
}
XmlAdaptedItem otherItem = (XmlAdaptedItem) other;
return Objects.equals(name, otherItem.name)
&& Objects.equals(price, otherItem.price)
&& Objects.equals(quantity, otherItem.quantity)
&& Objects.equals(sku, otherItem.sku)
&& Objects.equals(image, otherItem.image)
&& tagged.equals(otherItem.tagged);
}
}