/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.project;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;

/**
 * Holds all elements that make up a project. This class is XML serializable. A
 * project contains of the following elements:
 * 
 * 
 * @author Markus Kraetzig 
 */
public final class Project {
    private static final Logger log = Logger.getLogger(Project.class);

    private final Date date;

    private final String name;

    private String currentModuleFrame = "";

    private final String description;

    private final String version = "1.0";

    private final List outputDataList = new ArrayList();

    private final List projectDataList = new ArrayList();

    private final List projectStatesList = new ArrayList();

    public static final XmlFormat Project_XML = new XmlFormat(
            Project.class) {
        public void format(Project data, XmlElement xml) {
            xml.setAttribute("version", data.version);
            xml.setAttribute("date", data.date.getTime());
            xml.setAttribute("name", data.name);
            xml.setAttribute("description", data.description);
            xml.setAttribute("showing_module", data.currentModuleFrame);

            // add data types
            for (ProjectData el : data.projectDataList)
                xml.add(el);
            // add output data
            for (OutputData el : data.outputDataList)
                xml.add(el);
            // add project states
            for (ProjectState el : data.projectStatesList)
                xml.add(el);
        }

        public Project parse(XmlElement xml) {
            long dateLong = Long.valueOf(xml.getAttribute("date") + "")
                    .longValue();
            String nam = xml.getAttribute("name") + "";

            String des = xml.getAttribute("description") + "";
            String showing_module = xml.getAttribute("showing_module") + "";

            if (nam.equalsIgnoreCase("null"))
                nam = "";
            if (des.equalsIgnoreCase("null"))
                des = "";
            if (showing_module.equalsIgnoreCase("null"))
                showing_module = "";
            Project project = new Project(new Date(dateLong), nam, des);
            project.setCurrentModuleFrame(showing_module);

            while (xml.hasNext()) {

                try {
                    Object el = xml.getNext();
                    if (el instanceof OutputData)
                        project.addOutputData((OutputData) el);
                    else if (el instanceof ProjectData)
                        project.addProjectData((ProjectData) el);
                    else if (el instanceof ProjectState)
                        project.addProjectState((ProjectState) el);
                } catch (Throwable t) {
                    log.warn("problem reading parts of project: "
                            + project.name, t);
                }

            }

            return project;

        }
    };

    /**
     * Creates a new project with a name and a project description.
     * 
     * @param name
     *            name of the project, will automatically be set to
     *            "project" if null or empty
     * @param description
     *            description of this project, can be null
     */
    public Project(String name, String description) {
        this(new Date(), name, description);
    }

    /**
     * Deserialization constructor.
     */
    private Project(Date date, String name, String description) {
        if (name == null || name.length() == 0)
            name = "project";
        if (description == null)
            description = "";

        this.date = date;
        this.name = name;
        this.description = description;
    }

    /**
     * Adds project data to this project. Nothing happens if the same object was
     * added before.
     * 
     * @param pData
     *            element of the project data store to be serialized
     * @throws IllegalArgumentException
     *             if (dataStoreType == null)
     */
    public void addProjectData(ProjectData pData) {
        if (pData == null)
            throw new IllegalArgumentException("Argument was null.");
        if (!projectDataList.contains(pData))
            projectDataList.add(pData);
    }

    /**
     * Adds a new output data element to this project.
     * 
     * @param outputData
     *            output data object
     * @throws IllegalArgumentException
     *             if (outputData == null)
     */
    public void addOutputData(OutputData outputData) {
        if (outputData == null)
            throw new IllegalArgumentException("Argument was null.");
        outputDataList.add(outputData);
    }

    /**
     * Adds a new project state to this project. Nothing happens if
     * projectState == null.
     * 
     * @param projectState
     *            project state object
     */
    public void addProjectState(ProjectState projectState) {
        if (projectState == null)
            return;
        projectStatesList.add(projectState);
    }

    /**
     * Gets an iterator over all output data elements.
     * 
     * @return output iterator
     */
    public Iterator getOutputDataIterator() {
        return outputDataList.iterator();
    }

    /**
     * Gets an iterator over all added project data object.
     * 
     * @return project data iterator
     */
    public Iterator getProjectDataIterator() {
        return projectDataList.iterator();
    }

    /**
     * Gets an iterator over all project states.
     * 
     * @return project states iterator
     */
    public Iterator getProjectStatesIterator() {
        return projectStatesList.iterator();
    }

    /**
     * Gets the creation date of this project.
     * 
     * @return creation date
     */
    public Date getDate() {
        return date;
    }

    /**
     * Gets the description of this project.
     * 
     * @return description, never returns null but
     *         "" if empty
     */
    public String getDescription() {
        return description;
    }

    /**
     * Gets the name of this project.
     * 
     * @return the name of this project
     */
    public String getName() {
        return name;
    }

    /**
     * Gets the name of the module frame that should be displayed when this
     * project is loaded.
     * 
     * @return name of the module frame
     */
    public String getCurrentModuleFrame() {
        return currentModuleFrame;
    }

    /**
     * Sets the name of the module frame that should be displayed when this
     * project is loaded. Does nothing if the current module frame is null.
     * 
     * @param currentModuleFrame
     *            frame title of the module
     */
    public void setCurrentModuleFrame(String currentModuleFrame) {
        if (currentModuleFrame == null)
            return;
        this.currentModuleFrame = currentModuleFrame;
    }
}
 

Ads help maintain this website.