001//@formatter:off
002/*
003 * Session - 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.ArrayList;
023import java.util.List;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Session - supplementary class for MAPPER demonstration<br>
030 * According to Martin Fowler the mapper can't be invoked directly by either of the two components it is connected to, because they both don't even know of the
031 * mapper.<br>
032 * One solution is a third party <i>driving</i> the mapper. In this example a session is responsible for this task.
033 * 
034 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
035 */
036public class Session {
037
038    private static final Logger LOGGER = LoggerFactory.getLogger(Session.class);
039
040    /**
041     * all mappers created during the session
042     */
043    private List<AbstractMapper> mappers = new ArrayList<>();
044
045    /**
046     * Creates new session
047     */
048    public Session() {
049        LOGGER.debug("{} created", this.getClass().getSimpleName());
050    }
051
052    /**
053     * Adds a mapper to the session management
054     * 
055     * @param mapper session-managed mapper instance
056     */
057    public void add(AbstractMapper mapper) {
058        LOGGER.debug("{}.add(...) called ", this.getClass().getSimpleName());
059        LOGGER.debug("Triggering Mapper to map data forward");
060        mapper.map();
061        mappers.add(mapper);
062    }
063
064    /**
065     * Write-back any changes and close session, instances obtained within this session are no longer valid
066     */
067    public void confirm() {
068        LOGGER.debug("{}.confirm() called ", this.getClass().getSimpleName());
069        flush();
070        mappers.clear();
071    }
072
073    /**
074     * Flush changes, instances obtained within this session remain valid
075     */
076    public void flush() {
077        LOGGER.debug("{}.flush() called ", this.getClass().getSimpleName());
078        for (AbstractMapper mapper : mappers) {
079            LOGGER.debug("Triggering mapper to map data backwards");
080            mapper.mapBack();
081        }
082    }
083
084    /**
085     * Discard any changes and close session, instances obtained within this session are no longer valid
086     */
087    public void discard() {
088        LOGGER.debug("{}.clear() called ", this.getClass().getSimpleName());
089        LOGGER.debug("Invalidating all manged mappers");
090        mappers.clear();
091    }
092
093}