/* $Id: QedeqBo.java,v 1.6 2005/10/13 03:35:25 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" 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 2 of the License, or (at your option) any later version.
 *
 * This program 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.
 */

package org.qedeq.kernel.bo.module;

import org.qedeq.kernel.base.module.Chapter;
import org.qedeq.kernel.base.module.ChapterList;
import org.qedeq.kernel.base.module.Header;
import org.qedeq.kernel.base.module.Qedeq;
import org.qedeq.kernel.bo.control.ModuleLabels;
import org.qedeq.kernel.utility.EqualsUtility;


/**
 * A complete qedeq module. This is the root business object.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public final class QedeqBo implements Qedeq {

    /** Header of module. */
    private Header header;

    /** All module chapters. */
    private ChapterListBo chapterList;

    /** All module labels and their business objects. */
    private final ModuleLabels moduleLabels;

    /** Module state. */
    private QedeqBoState state;

    /**
     * Constructs a new empty qedeq module.
     */
    public QedeqBo() {
        moduleLabels = new ModuleLabels();
        state = QedeqBoState.STATE_CREATING;
    }

    /**
     * Set header of module.
     *
     * @param   header   Module header.
     */
    public final void setHeader(final Header header) {
        this.header = header;
    }

    public final Header getHeader() {
        return header;
    }

    /**
     * Set chapter list of this module.
     *
     * @param   chapters    Chapter list.
     */
    public final void setChapterList(final ChapterListBo chapters) {
        this.chapterList = chapters;
    }

    public final ChapterList getChapterList() {
        return chapterList;
    }

    /**
     * Add chapter to this module.
     *
     * @param   chapter Chapter to add.
     */
    public final void addChapter(final Chapter chapter) {
        if (chapterList == null) {
            chapterList = new ChapterListBo();
        }
        chapterList.add(chapter);
    }

    public boolean equals(final Object obj) {
        if (!(obj instanceof QedeqBo)) {
            return false;
        }
        final QedeqBo other = (QedeqBo) obj;
        return  EqualsUtility.equals(getHeader(), other.getHeader())
            &&  EqualsUtility.equals(getChapterList(), other.getChapterList());
    }

    public int hashCode() {
        return (getHeader() != null ? getHeader().hashCode() : 0)
            ^ (getChapterList() != null ? 1 ^ getChapterList().hashCode() : 0);
    }

    public String toString() {
        final StringBuffer buffer = new StringBuffer();
        buffer.append(getHeader() + "\n\n");
        buffer.append(getChapterList());
        return buffer.toString();
    }

    /**
     * Get module label associations for this module.
     *
     * @return  Label associations.
     */
    public final ModuleLabels getModuleLabels() {
        return moduleLabels;
    }

    /**
     * Get module state.
     *
     * @return  Module state.
     */
    public final QedeqBoState getState() {
        return state;
    }

    /**
     * Set module state.
     *
     * @param   state   Module state to set.
     */
    public final void setState(final QedeqBoState state) {
        this.state = state;
    }

    /**
     * Get physical addresses of this module.
     *
     * @return  Module file access information.
     */
    public final ModuleAddress getModuleAddress() {
        // TODO mime 20051005: implementation
        return null;
    }

}
