com.sfs.whichdoctor.xml.writer.PreferencesXmlWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.xml.writer.PreferencesXmlWriter.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;

import com.sfs.DataFilter;
import com.sfs.Formatter;
import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.PreferencesBean;

import org.apache.commons.lang.StringUtils;

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

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

    /**
     * Save.
     *
     * @param preferences the preferences
     * @param user the user
     * @param internalUse the flag whether this is for internal use
     *
     * @return the xml string
     */
    public static String save(final PreferencesBean preferences, final UserBean user, final boolean internalUse) {

        final XmlWriter xw = new XmlWriter();

        // Add preferences information
        xw.writeEntity("preferences");

        xw.writeEntity("company");
        xw.writeEntity("fullName").writeText(preferences.getOption("system", "companyname")).endEntity();
        xw.writeEntity("abbreviation").writeText(preferences.getOption("system", "companyabbrev")).endEntity();
        xw.endEntity();

        xw.writeEntity("gstNumber").writeText(preferences.getOption("finance", "gstnumber")).endEntity();
        xw.writeEntity("chequeName").writeText(preferences.getOption("finance", "chequename")).endEntity();
        xw.writeEntity("gst").writeText(preferences.getOption("finance", "gst")).endEntity();
        xw.writeEntity("paymentDue").writeText(preferences.getOption("finance", "paymentdue")).endEntity();

        xw.writeEntity("bank");
        xw.writeEntity("name").writeText(preferences.getOption("finance", "bankname")).endEntity();
        xw.writeEntity("branchNo").writeText(preferences.getOption("finance", "bankbranchno")).endEntity();
        xw.writeEntity("accountNo").writeText(preferences.getOption("finance", "bankaccountno")).endEntity();
        xw.endEntity();

        if (internalUse) {

            xw.writeEntity("credit");
            xw.writeEntity("receiptType").writeText(preferences.getOption("credit", "receipttype")).endEntity();
            xw.writeEntity("receiptClass").writeText(preferences.getOption("credit", "receiptclass")).endEntity();
            xw.writeEntity("processType").writeText(preferences.getOption("credit", "processtype")).endEntity();
            xw.endEntity();

            xw.writeEntity("specialty");
            xw.writeEntity("adult").writeText(preferences.getOption("specialty", "adult")).endEntity();
            xw.writeEntity("paediatric").writeText(preferences.getOption("specialty", "paediatric")).endEntity();
            xw.endEntity();

            xw.writeEntity("exam");
            xw.writeEntity("defaulttotalmarks").writeText(preferences.getOption("exam", "defaulttotalmarks"))
                    .endEntity();
            xw.endEntity();
        }

        xw.writeEntity("mailingCountry").writeText(preferences.getOption("system", "mailingcountry")).endEntity();

        if (internalUse) {
            xw.writeEntity("membershipUpgrade").writeText(preferences.getOption("system", "membershipupgrade"))
                    .endEntity();
            xw.writeEntity("addressVerification").writeText(preferences.getOption("system", "addressverification"))
                    .endEntity();
        }

        xw.writeEntity("theme").writeText(preferences.getOption("system", "theme")).endEntity();

        if (internalUse) {
            xw.writeEntity("status").writeText(preferences.getOption("system", "status")).endEntity();
        }

        if (user != null) {
            xw.writeEntity("currentUser");
            xw.writeEntity("dn").writeText(user.getDN()).endEntity();
            xw.writeEntity("preferredName").writeText(user.getPreferredName()).endEntity();
            xw.writeEntity("lastName").writeText(user.getLastName()).endEntity();
            xw.endEntity();
        }

        final String currentDate = Formatter.convertDate(preferences.getCurrentDate());
        if (StringUtils.isNotBlank(currentDate)) {
            xw.writeEntity("currentDate").writeText(Formatter.convertDate(preferences.getCurrentDate()))
                    .endEntity();
        }
        if (preferences.getOption("system", "webaddress").compareTo("") != 0) {
            xw.writeEntity("webAddress").writeText(preferences.getOption("system", "webaddress")).endEntity();
        }

        // Footer details
        xw.writeEntity("contact");
        xw.writeEntity("postal").writeText(preferences.getOption("contact", "postal")).endEntity();
        xw.writeEntity("physical").writeText(preferences.getOption("contact", "physical")).endEntity();
        xw.writeEntity("telephone").writeText(preferences.getOption("contact", "telephone")).endEntity();
        xw.writeEntity("fax").writeText(preferences.getOption("contact", "fax")).endEntity();
        xw.writeEntity("email").writeText(preferences.getOption("contact", "email")).endEntity();
        xw.writeEntity("web").writeText(preferences.getOption("contact", "web")).endEntity();
        xw.endEntity();

        if (internalUse) {
            String privacyHtml = DataFilter.getSafeXml(preferences.getOption("email", "privacy"));
            privacyHtml = StringUtils.replace(privacyHtml, "<", "<");
            privacyHtml = StringUtils.replace(privacyHtml, ">", ">");
            privacyHtml = StringUtils.replace(privacyHtml, "<", "&lt;");
            privacyHtml = StringUtils.replace(privacyHtml, ">", "&gt;");

            xw.writeEntity("email");
            xw.writeEntity("client").writeText(preferences.getOption("email", "client")).endEntity();
            xw.writeEntity("maxAttachments").writeText(preferences.getOption("email", "maxAttachments"))
                    .endEntity();
            xw.writeEntity("stylesheet").writeText(preferences.getOption("email", "stylesheet")).endEntity();
            xw.writeEntity("logo").writeText(preferences.getOption("email", "logo")).endEntity();
            xw.writeEntity("privacy").writeXml(privacyHtml).endEntity();
            xw.endEntity();
        }

        if (StringUtils.isNotBlank(preferences.getExportTitle())) {
            xw.writeEntity("exportTitle").writeText(preferences.getExportTitle()).endEntity();
        }
        if (StringUtils.isNotBlank(preferences.getExportMessage())) {
            xw.writeEntity("exportMessage");
            xw.writeEntity("p").writeXml(StringUtils.replace(preferences.getExportMessage(), "\n", "</p><p>"))
                    .endEntity();
            xw.endEntity();
        }

        xw.endEntity();

        return xw.getXml();
    }
}