com.sfs.whichdoctor.xml.writer.helper.ProjectXmlHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.xml.writer.helper.ProjectXmlHelper.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.whichdoctor.xml.writer.helper;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;

import com.sfs.DataFilter;
import com.sfs.Formatter;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.ProjectBean;
import com.sfs.whichdoctor.xml.writer.XmlWriter;

/**
 * The Class ProjectXmlHelper.
 */
public class ProjectXmlHelper {

    /**
     * Instantiates a new project xml helper.
     */
    protected ProjectXmlHelper() {
        throw new UnsupportedOperationException();
    }

    /**
     * Output the project collection as an XML string.
     *
     * @param projects the projects
     *
     * @return the xml string
     */
    public static String getProjectsXml(final Collection<ProjectBean> projects) {

        final XmlWriter xmlwriter = new XmlWriter();

        xmlwriter.writeEntity("projects");

        for (ProjectBean project : projects) {

            xmlwriter.writeEntity("project").writeAttribute("GUID", project.getGUID()).writeAttribute("projectId",
                    project.getId());

            xmlwriter.writeEntity("assessmentType").writeText(project.getAssessmentType()).endEntity();
            xmlwriter.writeEntity("projectType").writeText(project.getProjectType()).endEntity();
            xmlwriter.writeEntity("status").writeText(project.getStatus()).endEntity();
            if (StringUtils.isNotBlank(project.getTitle())) {
                xmlwriter.writeEntity("title").writeText(project.getTitle()).endEntity();
            }
            if (project.getSubmitted() != null) {
                xmlwriter.writeEntity("submitted").writeText(Formatter.convertDate(project.getSubmitted()))
                        .endEntity();
            }
            if (project.getResubmitted() != null) {
                xmlwriter.writeEntity("resubmitted").writeText(Formatter.convertDate(project.getResubmitted()))
                        .endEntity();
            }
            if (project.getYear() > 0) {
                xmlwriter.writeEntity("year").writeText(String.valueOf(project.getYear())).endEntity();
            }

            if (StringUtils.isNotBlank(project.getTrainingOrganisation())) {

                xmlwriter.writeEntity("specialty");

                xmlwriter.writeEntity("organisation").writeText(project.getTrainingOrganisation()).endEntity();

                if (StringUtils.isNotBlank(project.getTrainingProgram())) {
                    xmlwriter.writeEntity("program").writeText(project.getTrainingProgram()).endEntity();
                }
                xmlwriter.endEntity();
            }

            xmlwriter.writeEntity("assessors");
            writeAssessor(project.getAssessor1(), xmlwriter);
            writeAssessor(project.getAssessor2(), xmlwriter);
            writeAssessor(project.getAssessor3(), xmlwriter);
            xmlwriter.endEntity();

            if (StringUtils.isNotBlank(project.getMemo())) {
                xmlwriter.writeEntity("memo");
                xmlwriter.writeXml(DataFilter.getSafeXml(project.getMemo()));
                xmlwriter.endEntity();
            }
            xmlwriter.endEntity();
        }
        xmlwriter.endEntity();

        return xmlwriter.getXml();
    }

    /**
     * Write assessor details.
     *
     * @param person the person
     * @param xml the xml
     */
    private static void writeAssessor(final PersonBean person, final XmlWriter xml) {
        if (person != null) {
            xml.writeEntity("assessor");
            xml.writeEntity("title").writeText(person.getTitle()).endEntity();
            xml.writeEntity("firstName").writeText(person.getFirstName()).endEntity();
            xml.writeEntity("middleName").writeText(person.getMiddleName()).endEntity();
            xml.writeEntity("lastName").writeText(person.getLastName()).endEntity();
            xml.writeEntity("MIN").writeText(person.getPersonIdentifier()).endEntity();
            xml.endEntity();
        }
    }
}