1
2
3
4
5 package org.company.thesandbox.domain;
6
7 import java.util.Collections;
8 import java.util.Set;
9 import java.util.HashSet;
10 import org.mod4j.runtime.validation.MinValueValidator;
11 import org.mod4j.runtime.validation.MaxValueValidator;
12 import org.mod4j.runtime.validation.MinLengthValidator;
13 import org.mod4j.runtime.validation.MaxLengthValidator;
14 import org.mod4j.runtime.validation.RegExpValidator;
15
16 import org.company.thesandbox.domain.businessrules.AnotherRule;
17 import org.company.thesandbox.domain.businessrules.OneEarDiscount;
18
19
20
21
22
23
24
25 @SuppressWarnings("serial")
26 public abstract class CustomerImplBase extends Person implements java.io.Serializable {
27
28
29
30
31 private int customerNr;
32
33
34
35
36 private String username;
37
38 public static final Integer USERNAME_MINLENGTH = 3;
39
40 public static final Integer USERNAME_MAXLENGTH = 10;
41
42
43
44
45 private String emailAddress;
46
47 public static final String EMAILADDRESS_REGEXP = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$";
48
49
50
51
52 private Boolean blackListed = false;
53
54
55
56
57 private int discountPercentage = 0;
58
59 public static final int DISCOUNTPERCENTAGE_MINVALUE = 0;
60
61 public static final int DISCOUNTPERCENTAGE_MAXVALUE = 100;
62
63
64
65
66 private Set<Order> orders = new HashSet<Order>();
67
68
69
70
71 public Set<Order> getOrders() {
72 return Collections.unmodifiableSet(this.orders);
73 }
74
75
76
77
78 public void addToOrders(Order element) {
79 if (element == null) {
80 return;
81 }
82 if (this.orders.contains(element)) {
83 return;
84 }
85 this.orders.add(element);
86
87 if (element.getCustomer() != null) {
88 element.getCustomer().z_internalRemoveFromorders(element);
89 }
90 element.z_internalAddTocustomer((Customer) ((Customer) this));
91 validation.validate();
92
93 }
94
95
96
97
98
99
100 public void removeFromOrders(Order element) {
101 if (element == null) {
102 return;
103 }
104 this.orders.remove(element);
105 element.z_internalRemoveFromcustomer((Customer) ((Customer) this));
106
107 validation.validate();
108 }
109
110
111
112
113
114
115
116 public void z_internalAddToorders(Order element) {
117 this.orders.add(element);
118 }
119
120
121
122
123
124
125
126 public void z_internalRemoveFromorders(Order element) {
127 this.orders.remove(element);
128 }
129
130
131
132
133 protected CustomerImplBase() {
134 super();
135 }
136
137
138
139
140
141
142
143
144
145
146
147 public CustomerImplBase(String firstName, String lastName, int customerNr) {
148 super(firstName, lastName);
149
150 this.customerNr = customerNr;
151
152 validation.addValidator(new MinLengthValidator(Customer.class, "username",
153 USERNAME_MINLENGTH));
154
155 validation.addValidator(new MaxLengthValidator(Customer.class, "username",
156 USERNAME_MAXLENGTH));
157
158 validation.addValidator(new RegExpValidator(Customer.class, "emailAddress",
159 EMAILADDRESS_REGEXP));
160
161 validation.addValidator(new MinValueValidator(Customer.class, "discountPercentage",
162 DISCOUNTPERCENTAGE_MINVALUE));
163
164 validation.addValidator(new MaxValueValidator(Customer.class, "discountPercentage",
165 DISCOUNTPERCENTAGE_MAXVALUE));
166
167 validation.addValidator(new OneEarDiscount());
168 validation.addValidator(new AnotherRule());
169
170 validation.validate();
171 }
172
173
174
175
176
177 public int getCustomerNr() {
178 return this.customerNr;
179 }
180
181
182
183
184
185 public String getUsername() {
186 return this.username;
187 }
188
189
190
191
192
193 public String getEmailAddress() {
194 return this.emailAddress;
195 }
196
197
198
199
200
201 public Boolean isBlackListed() {
202 return this.blackListed;
203 }
204
205
206
207
208
209 public int getDiscountPercentage() {
210 return this.discountPercentage;
211 }
212
213
214
215
216
217 public void setCustomerNr(final int customerNr) {
218 this.customerNr = customerNr;
219 validation.validate();
220 }
221
222
223
224
225
226 public void setUsername(final String username) {
227 this.username = username;
228 validation.validate();
229 }
230
231
232
233
234
235 public void setEmailAddress(final String emailAddress) {
236 this.emailAddress = emailAddress;
237 validation.validate();
238 }
239
240
241
242
243
244 public void setBlackListed(final Boolean blackListed) {
245 this.blackListed = blackListed;
246 validation.validate();
247 }
248
249
250
251
252
253 public void setDiscountPercentage(final int discountPercentage) {
254 this.discountPercentage = discountPercentage;
255 validation.validate();
256 }
257
258
259
260
261
262
263
264
265 public void activateValidation(boolean value) {
266
267 this.validation.setActive(value);
268 if (value) {
269 validation.validate();
270 }
271 }
272
273 }