001//@formatter:off
002/*
003 * Geo Bad Payer Info DTO Assembler - the TRANSFER OBJECT ASSEMBLER
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 org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * Geo Bad Payer Info DTO Assembler - the TRANSFER OBJECT ASSEMBLER<br>
027 * The knowlegde which entities' data to query and which particular attributes to include has been centralized here.
028 * 
029 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
030 */
031public class GeoBadPayerInfoDtoAssembler {
032
033    private static final Logger LOGGER = LoggerFactory.getLogger(GeoBadPayerInfoDtoAssembler.class);
034
035    /**
036     * Creates new data transfer object for the customer
037     * 
038     * @param customerId identifier
039     * @return data transfer object or null if customer not found or no bad payer
040     */
041    public GeoBadPayerInfoDto assembleDto(String customerId) {
042
043        LOGGER.debug("{}.assembleDto('{}' called", this.getClass().getSimpleName(), customerId);
044
045        GeoBadPayerInfoDto res = null;
046        CustomerDwhInfoEntity dwhInfoEntity = Database.CUSTOMER_DWH_INFOS.get(customerId);
047
048        if (dwhInfoEntity != null) {
049            CustomerEntity customerEntity = Database.CUSTOMERS.get(customerId);
050            AddressEntity addressEntity = Database.ADDRESSES.get(customerId);
051
052            if (dwhInfoEntity.isBadPayer() && customerEntity != null && addressEntity != null) {
053                LOGGER.debug("Creating data transfer object from entities customer, address and dwh information");
054
055                // @formatter:off
056                res = GeoBadPayerInfoDto.forCustomer(customerEntity.getCustomerId())
057                            .withTitle(customerEntity.getTitle())
058                            .withLastName(customerEntity.getLastName())
059                            .withFirstName(customerEntity.getFirstName())
060                            .withZipCode(addressEntity.getZipCode())          
061                            .withCity(addressEntity.getCity())
062                            .withCountry(addressEntity.getCountry())
063                            .withCustomerType(dwhInfoEntity.getCustomerType())
064                            .withDueInvoice(dwhInfoEntity.isDueInvoice())
065                      .build();
066                // @formatter:on
067
068            }
069        }
070        if (res == null) {
071            LOGGER.debug("No data transfer object created.");
072        }
073        return res;
074    }
075
076}