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

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.xml.writer.helper.AccreditationXmlHelper.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.ArrayList;
import java.util.Collection;
import java.util.TreeMap;

import com.sfs.Formatter;
import com.sfs.whichdoctor.beans.AccreditationBean;
import com.sfs.whichdoctor.xml.writer.XmlWriter;

import org.apache.commons.lang.StringUtils;

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

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

    /**
     * Output the list of accreditations as an XML string.
     *
     * @param accreditations the accreditations
     *
     * @return the xml string
     */
    public static String getAccreditationsXml(final Collection<AccreditationBean> accreditations) {

        final XmlWriter xmlwriter = new XmlWriter();

        xmlwriter.writeEntity("accreditations");

        for (AccreditationBean accreditation : accreditations) {

            xmlwriter.writeEntity("accreditation").writeAttribute("GUID", accreditation.getGUID())
                    .writeAttribute("accreditationId", accreditation.getId());
            if (accreditation.getCore()) {
                xmlwriter.writeAttribute("core", "Yes");
            } else {
                xmlwriter.writeAttribute("core", "No");
            }

            xmlwriter.writeEntity("approved");
            xmlwriter.writeEntity("totalWeeks").writeText(accreditation.getWeeksApproved()).endEntity();
            xmlwriter.writeEntity("wholeMonths")
                    .writeText(Formatter.getWholeMonths(accreditation.getWeeksApproved())).endEntity();
            xmlwriter.writeEntity("formattedTime")
                    .writeText(Formatter.getFormattedDuration(accreditation.getWeeksApproved())).endEntity();
            xmlwriter.endEntity();

            xmlwriter.writeEntity("certified");
            xmlwriter.writeEntity("totalWeeks").writeText(accreditation.getWeeksCertified()).endEntity();
            xmlwriter.writeEntity("wholeMonths")
                    .writeText(Formatter.getWholeMonths(accreditation.getWeeksCertified())).endEntity();
            xmlwriter.writeEntity("formattedTime")
                    .writeText(Formatter.getFormattedDuration(accreditation.getWeeksCertified())).endEntity();
            xmlwriter.endEntity();

            xmlwriter.writeEntity("accreditationType").writeText(accreditation.getAccreditationType()).endEntity();
            xmlwriter.writeEntity("abbreviation").writeText(accreditation.getAbbreviation()).endEntity();

            if (StringUtils.isNotBlank(accreditation.getSpecialtyType())) {
                xmlwriter.writeEntity("type").writeText(accreditation.getSpecialtyType()).endEntity();
            }
            if (StringUtils.isNotBlank(accreditation.getSpecialtySubType())) {
                xmlwriter.writeEntity("subtype").writeText(accreditation.getSpecialtySubType()).endEntity();
            }
            xmlwriter.endEntity();
        }
        xmlwriter.endEntity();

        return xmlwriter.getXml();
    }

    /**
     * Output the training summary as an XML string.
     *
     * @param trainingSummary the training summary
     * @param type the type
     *
     * @return the xml string
     */
    public static String getSummaryXml(final TreeMap<String, AccreditationBean[]> trainingSummary,
            final String type) {

        final XmlWriter xmlwriter = new XmlWriter();

        if (trainingSummary.size() > 0) {

            int totalCore = 0;
            int totalNonCore = 0;

            TreeMap<String, ArrayList<AccreditationBean[]>> summaryTreemap = new TreeMap<String, ArrayList<AccreditationBean[]>>();

            for (String summaryKey : trainingSummary.keySet()) {

                AccreditationBean[] details = trainingSummary.get(summaryKey);
                AccreditationBean core = details[0];
                AccreditationBean nonCore = details[1];

                totalCore += core.getWeeksCertified();
                totalNonCore += nonCore.getWeeksCertified();

                if (StringUtils.isNotBlank(core.getSpecialtyType())) {

                    ArrayList<AccreditationBean[]> summaries = new ArrayList<AccreditationBean[]>();

                    if (!summaryTreemap.containsKey(core.getSpecialtyType())) {
                        /* New type of specialty */
                        summaries.add(details);
                    } else {
                        /* Existing specialty */
                        summaries = summaryTreemap.get(core.getSpecialtyType());
                        summaries.add(details);
                    }
                    summaryTreemap.put(core.getSpecialtyType(), summaries);
                }
            }

            xmlwriter.writeEntity("trainingSummary");
            xmlwriter.writeAttribute("type", type);
            xmlwriter.writeAttribute("totalCore", Formatter.getWholeMonths(totalCore));
            xmlwriter.writeAttribute("totalNonCore", Formatter.getWholeMonths(totalNonCore));

            xmlwriter.writeEntity("specialtyTraining");

            for (String specialtyType : summaryTreemap.keySet()) {

                ArrayList<AccreditationBean[]> summaries = summaryTreemap.get(specialtyType);
                int typeCoreWeeks = 0;
                int typeNCWeeks = 0;

                if (summaries != null) {
                    // For each accredited specialty create an element
                    xmlwriter.writeEntity("specialty");
                    xmlwriter.writeEntity("subtypes");
                    String division = "";
                    String abbreviation = "";
                    String specialtytype = "";
                    String typeAbbreviation = "";
                    for (Object[] summary : summaries) {
                        boolean blSubType = false;
                        AccreditationBean core = (AccreditationBean) summary[0];
                        AccreditationBean nonCore = (AccreditationBean) summary[1];

                        division = core.getAccreditationClass();
                        abbreviation = core.getAbbreviation();
                        specialtytype = core.getSpecialtyType();
                        typeAbbreviation = core.getSpecialtyTypeAbbreviation();

                        if (StringUtils.isNotBlank(core.getSpecialtySubType())) {
                            blSubType = true;
                            xmlwriter.writeEntity("subtype");
                            xmlwriter.writeEntity("name").writeText(core.getSpecialtySubType()).endEntity();
                            xmlwriter.writeEntity("coreMonths")
                                    .writeText(Formatter.getWholeMonths(core.getWeeksCertified())).endEntity();
                            xmlwriter.writeEntity("nonCoreMonths")
                                    .writeText(Formatter.getWholeMonths(nonCore.getWeeksCertified())).endEntity();

                            xmlwriter.endEntity();

                            typeCoreWeeks += core.getWeeksCertified();
                            typeNCWeeks += nonCore.getWeeksCertified();
                        }
                        if (!blSubType) {
                            xmlwriter.writeEntity("subtype");
                            xmlwriter.writeEntity("coreMonths")
                                    .writeText(Formatter.getWholeMonths(core.getWeeksCertified())).endEntity();
                            xmlwriter.writeEntity("nonCoreMonths")
                                    .writeText(Formatter.getWholeMonths(nonCore.getWeeksCertified())).endEntity();

                            xmlwriter.endEntity();

                            typeCoreWeeks += core.getWeeksCertified();
                            typeNCWeeks += nonCore.getWeeksCertified();
                        }
                    }
                    xmlwriter.endEntity();

                    xmlwriter.writeEntity("division").writeText(division).endEntity();
                    xmlwriter.writeEntity("abbreviation").writeText(abbreviation).endEntity();
                    xmlwriter.writeEntity("type").writeText(specialtytype).endEntity();
                    xmlwriter.writeEntity("typeAbbreviation").writeText(typeAbbreviation).endEntity();
                    xmlwriter.writeEntity("coreMonths").writeText(Formatter.getWholeMonths(typeCoreWeeks))
                            .endEntity();
                    xmlwriter.writeEntity("nonCoreMonths").writeText(Formatter.getWholeMonths(typeNCWeeks))
                            .endEntity();

                    xmlwriter.endEntity();
                }
            }
            xmlwriter.endEntity();

            xmlwriter.endEntity();
        }
        return xmlwriter.getXml();
    }

}