001//@formatter:off
002/*
003 * Data Manager - supplementary class for MAPPER demonstration
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.mapper;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import de.calamanari.pk.mapper.firstsys.Address;
029import de.calamanari.pk.mapper.firstsys.Person;
030import de.calamanari.pk.mapper.secondsys.Customer;
031
032/**
033 * Data Manager - supplementary class for MAPPER demonstration, this is rather a placeholder for some way to deal with the database.
034 * 
035 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
036 */
037public class DataManager {
038
039    private static final Logger LOGGER = LoggerFactory.getLogger(DataManager.class);
040
041    /**
042     * simulated database table with persons
043     */
044    private Map<String, Person> persons = new HashMap<>();
045
046    /**
047     * simulated database table with addresses
048     */
049    private Map<String, Address> addresses = new HashMap<>();
050
051    /**
052     * utility method to fill "test database"
053     * 
054     * @param person instance of a person
055     * @param address person's address
056     */
057    public void addPerson(Person person, Address address) {
058        String id = person.getPersonId();
059        this.persons.put(id, person);
060        this.addresses.put(id, address);
061    }
062
063    /**
064     * Finds the customer according to the given id and adds it to session management
065     * 
066     * @param session persistence session
067     * @param customerId identifier
068     * @return customer or null if not found
069     */
070    public Customer findCustomer(Session session, String customerId) {
071        LOGGER.debug("{}.findCustomer('{}') called", this.getClass().getSimpleName(), customerId);
072
073        Customer customer = null;
074        Person person = persons.get(customerId);
075        if (person != null) {
076            Address address = addresses.get(customerId);
077            LOGGER.debug("Person and Address found, preparing customer");
078            customer = new Customer(customerId);
079            CustomerMapper mapper = new CustomerMapper(person, address, customer);
080
081            LOGGER.debug("Adding Mapper to session");
082            session.add(mapper);
083
084        }
085        return customer;
086    }
087
088}