ch.eitchnet.android.mabea.model.MabeaContext.java Source code

Java tutorial

Introduction

Here is the source code for ch.eitchnet.android.mabea.model.MabeaContext.java

Source

/*
 * Copyright (c) 2012, Robert von Burg
 *
 * All rights reserved.
 *
 * This file is part of the XXX.
 *
 *  XXX is free software: you can redistribute 
 *  it and/or modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation, either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  XXX is distributed in the hope that it will 
 *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XXX.  If not, see 
 *  <http://www.gnu.org/licenses/>.
 */
package ch.eitchnet.android.mabea.model;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;

import ch.eitchnet.android.mabea.MabeaConnection;
import ch.eitchnet.android.mabea.model.MabeaState.State;

/**
 * @author Robert von Burg <eitch@eitchnet.ch>
 * 
 */
public class MabeaContext {

    private Setting setting;
    private MabeaConnection connection;
    private transient String connectionStateMsg;
    private Today today;
    private boolean debug;

    /**
     * @return the setting
     */
    public Setting getSetting() {
        if (setting == null)
            this.setting = new Setting();
        return this.setting;
    }

    /**
     * @return the connection
     */
    public MabeaConnection getConnection() {
        if (this.connection == null)
            this.connection = new MabeaConnection(this.setting);
        return this.connection;
    }

    /**
     * @return the connectionStateMsg
     */
    public String getConnectionStateMsg() {
        return connectionStateMsg;
    }

    /**
     * @param connectionStateMsg
     *            the connectionStateMsg to set
     */
    public void setConnectionStateMsg(String connectionStateMsg) {
        this.connectionStateMsg = connectionStateMsg;
    }

    public Today getToday() {
        if (this.today == null) {
            MabeaState state = getConnection().getMabeaState();
            LocalDateTime midnight = LocalDate.now().toLocalDateTime(LocalTime.MIDNIGHT);
            Booking initialBooking = new Booking(State.LOGGED_OUT, midnight, state.getBalance());
            this.today = new Today(this.setting.getRequiredWorkPerDay(), initialBooking);
        }

        return this.today;
    }

    /**
     * @return
     */
    public boolean isInitialized() {
        return getSetting().isPersisted();
    }

    /**
     * @return
     */
    public boolean isConnectionOk() {
        return isInitialized() && !getConnection().hasError();
    }

    public void toggleDebug() {
        this.debug = !debug;
    }

    /**
     * @return
     */
    public boolean isDebugEnabled() {
        return debug;
    }
}