001//@formatter:off
002/*
003 * Customer Dwh Info Entity - one of the business entities in this example.
004 * Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
005 * Copyright 2014 Karl Eilebrecht
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License"):
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019//@formatter:on
020package de.calamanari.pk.transferobjectassembler;
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Customer Dwh Info Entity - one of the business entities in this example.<br>
030 * This entity gives us a lot of information collected or derived in our company's data warehouse
031 * 
032 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
033 */
034public class CustomerDwhInfoEntity {
035
036    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerDwhInfoEntity.class);
037
038    /**
039     * ID of customer
040     */
041    private String customerId = null;
042
043    /**
044     * some type classification
045     */
046    private String customerType = null;
047
048    /**
049     * technical score value
050     */
051    private int scorePoints = 0;
052
053    /**
054     * date of first order
055     */
056    private Date firstOrderDate = null;
057
058    /**
059     * least recent order date
060     */
061    private Date lastOrderDate = null;
062
063    /**
064     * there is an invoice not payed yet
065     */
066    private boolean dueInvoice = false;
067
068    /**
069     * data leads to the conclusion that this is customer is a fraud
070     */
071    private boolean fraudSuspicion = false;
072
073    /**
074     * customer usually pays late
075     */
076    private boolean badPayer = false;
077
078    // more flags and infos
079
080    /**
081     * Creates a new builder for easier construction
082     * @param customerId mandatory
083     * @return builder
084     */
085    public static Builder forCustomerId(String customerId) {
086        return new Builder(customerId);
087    }
088
089    /**
090     * Returns the customer ID
091     * 
092     * @return customerId
093     */
094    public String getCustomerId() {
095        return customerId;
096    }
097
098    /**
099     * Sets the customer ID
100     * 
101     * @param customerId identifier
102     */
103    public void setCustomerId(String customerId) {
104        this.customerId = customerId;
105    }
106
107    /**
108     * Returns the type of customer (classification)
109     * 
110     * @return customerType
111     */
112    public String getCustomerType() {
113        return customerType;
114    }
115
116    /**
117     * Sets the customer type
118     * 
119     * @param customerType type of customer
120     */
121    public void setCustomerType(String customerType) {
122        this.customerType = customerType;
123    }
124
125    /**
126     * Returns the score points for this customer
127     * 
128     * @return scorePoints
129     */
130    public int getScorePoints() {
131        return scorePoints;
132    }
133
134    /**
135     * Sets the score points
136     * 
137     * @param scorePoints value from scoring process
138     */
139    public void setScorePoints(int scorePoints) {
140        this.scorePoints = scorePoints;
141    }
142
143    /**
144     * Returns the date of first order placed by this customer
145     * 
146     * @return firstOrderDate
147     */
148    public Date getFirstOrderDate() {
149        return firstOrderDate;
150    }
151
152    /**
153     * Sets the date of first order placed by this customer
154     * 
155     * @param firstOrderDate date of first order
156     */
157    public void setFirstOrderDate(Date firstOrderDate) {
158        this.firstOrderDate = firstOrderDate;
159    }
160
161    /**
162     * Returns the date of least recent order placed by this customer
163     * 
164     * @return lastOrderDate
165     */
166    public Date getLastOrderDate() {
167        return lastOrderDate;
168    }
169
170    /**
171     * Sets the date of least recent order placed by this customer
172     * 
173     * @param lastOrderDate date of least recent order
174     */
175    public void setLastOrderDate(Date lastOrderDate) {
176        this.lastOrderDate = lastOrderDate;
177    }
178
179    /**
180     * Returns whether this customer has an open invoice
181     * 
182     * @return true whether there is an open invoice
183     */
184    public boolean isDueInvoice() {
185        return dueInvoice;
186    }
187
188    /**
189     * Sets the due invoice flag
190     * 
191     * @param dueInvoice true indicates an open invoice not payed yet
192     */
193    public void setDueInvoice(boolean dueInvoice) {
194        LOGGER.debug("{}.toDto() called", this.getClass().getSimpleName());
195        this.dueInvoice = dueInvoice;
196    }
197
198    /**
199     * Returns whether this customer could be a fraud
200     * 
201     * @return fraudSuspicion
202     */
203    public boolean isFraudSuspicion() {
204        return fraudSuspicion;
205    }
206
207    /**
208     * Sets the fraud suspicion flag
209     * 
210     * @param fraudSuspicion flag to indicate that we suspect illegal activities
211     */
212    public void setFraudSuspicion(boolean fraudSuspicion) {
213        this.fraudSuspicion = fraudSuspicion;
214    }
215
216    /**
217     * Returns whether this customer is a bad payer
218     * 
219     * @return badPayer flag
220     */
221    public boolean isBadPayer() {
222        return badPayer;
223    }
224
225    /**
226     * Sets the bad payer flag
227     * 
228     * @param badPayer flag to indicate a customer who pays late or only after reminding
229     */
230    public void setBadPayer(boolean badPayer) {
231        this.badPayer = badPayer;
232    }
233
234    /**
235     * Returns a data transfer object with the data from this entity
236     * 
237     * @return data transfer object
238     */
239    public CustomerDwhInfoDto toDto() {
240
241        // @formatter:off
242        return CustomerDwhInfoDto.forCustomerId(customerId)
243                .withCustomerType(customerType)
244                .withScorePoints(scorePoints)
245                .withFirstOrderDate(firstOrderDate)
246                .withLastOrderDate(lastOrderDate)
247                .withDueInvoice(dueInvoice)
248                .withFraudSuspicion(fraudSuspicion)
249                .withBadPayer(badPayer)
250                .build();
251        // @formatter:on
252    }
253
254    /**
255     * Updates this entity from the given data transfer object
256     * 
257     * @param dto data transfer object to copy data from into this object
258     */
259    public void fromDto(CustomerDwhInfoDto dto) {
260        this.setCustomerId(dto.getCustomerId());
261        this.setCustomerType(dto.getCustomerType());
262        this.setLastOrderDate(dto.getLastOrderDate());
263        this.setFirstOrderDate(dto.getFirstOrderDate());
264        this.setScorePoints(dto.getScorePoints());
265        this.setDueInvoice(dto.isDueInvoice());
266        this.setFraudSuspicion(dto.isFraudSuspicion());
267        this.setBadPayer(dto.isBadPayer());
268    }
269
270    @Override
271    public String toString() {
272        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
273
274        return this.getClass().getSimpleName() + "({customerId=" + customerId + ", customerType=" + customerType + ", scorePoints=" + scorePoints
275                + ", firstOrderDate=" + (firstOrderDate == null ? null : "'" + sdf.format(firstOrderDate) + "'") + ", lastOrderDate="
276                + (lastOrderDate == null ? null : "'" + sdf.format(lastOrderDate) + "'") + ", dueInvoice=" + dueInvoice + ", fraudSuspicion=" + fraudSuspicion
277                + ", badPayer=" + badPayer + "})";
278    }
279
280    /**
281     * Fluent builder to prevent too many constructor arguments
282     *
283     */
284    public static final class Builder {
285
286        private final CustomerDwhInfoEntity result = new CustomerDwhInfoEntity();
287
288        /**
289         * Creates new builder for a DTO regarding the given customer-ID
290         * 
291         * @param customerId
292         */
293        Builder(String customerId) {
294            result.setCustomerId(customerId);
295        }
296
297        /**
298         * @param customerType type of customer
299         * @return builder
300         */
301        public Builder withCustomerType(String customerType) {
302            result.setCustomerType(customerType);
303            return this;
304        }
305
306        /**
307         * @param scorePoints value from scoring
308         * @return builder
309         */
310        public Builder withScorePoints(int scorePoints) {
311            result.setScorePoints(scorePoints);
312            return this;
313        }
314
315        /**
316         * @param firstOrderDate date of first order
317         * @return builder
318         */
319        public Builder withFirstOrderDate(Date firstOrderDate) {
320            result.setFirstOrderDate(firstOrderDate);
321            return this;
322        }
323
324        /**
325         * @param lastOrderDate date of least recent order
326         * @return builder
327         */
328        public Builder withLastOrderDate(Date lastOrderDate) {
329            result.setLastOrderDate(lastOrderDate);
330            return this;
331        }
332
333        /**
334         * @param dueInvoice true indicates an open invoice not payed yet
335         * @return builder
336         */
337        public Builder withDueInvoice(boolean dueInvoice) {
338            result.setDueInvoice(dueInvoice);
339            return this;
340        }
341
342        /**
343         * @param fraudSuspicion flag to indicate that we suspect illegal activities
344         * @return builder
345         */
346        public Builder withFraudSuspicion(boolean fraudSuspicion) {
347            result.setFraudSuspicion(fraudSuspicion);
348            return this;
349        }
350
351        /**
352         * @param badPayer flag to indicate a customer who pays late or only after reminding
353         * @return builder
354         */
355        public Builder withBadPayer(boolean badPayer) {
356            result.setBadPayer(badPayer);
357            return this;
358        }
359
360        /**
361         * @return the DTO
362         */
363        public CustomerDwhInfoEntity build() {
364            LOGGER.debug("{} created: {}", CustomerDwhInfoDto.class.getSimpleName(), this);
365            return result;
366        }
367    }
368
369}