Java tutorial
/******************************************************************************* * 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.Formatter; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.GroupBean; import com.sfs.whichdoctor.beans.ItemBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.WhichDoctorCoreIdentityBean; import com.sfs.whichdoctor.xml.writer.helper.AddressXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.EmailXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.HistoryXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.MemoXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.PhoneXmlHelper; import java.util.Collection; import org.apache.commons.lang.StringUtils; /** * The Class GroupXmlWriter. */ public class GroupXmlWriter { /** * Instantiates a new group xml writer. */ protected GroupXmlWriter() { throw new UnsupportedOperationException(); } /** * Output the list of groups as an XML string. * * @param groups the groups * @param preferences the preferences * @param user the user * * @return the string */ public static String save(final Collection<GroupBean> groups, final PreferencesBean preferences, final UserBean user) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("whichdoctor").writeAttribute("version", preferences.getOption("system", "version")); xmlwriter.writeXml(getGroupsXml(groups)); xmlwriter.writeXml(PreferencesXmlWriter.save(preferences, user, false)); xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Output the list of groups as an XML string. * * @param groups the groups * * @return the xml string */ public static String getGroupsXml(final Collection<GroupBean> groups) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("groups"); for (GroupBean group : groups) { xmlwriter.writeXml(getGroupXml(group)); } xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Output the group as an XML string. * * @param group the group * * @return the xml string */ public static String getGroupXml(final GroupBean group) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("group").writeAttribute("GUID", group.getGUID()).writeAttribute("groupId", group.getId()); xmlwriter.writeXml(HistoryXmlHelper.getHistoryXml(group)); xmlwriter.writeEntity("name").writeText(group.getName()).endEntity(); xmlwriter.writeEntity("description").writeXml(StringUtils.replace(group.getDescription(), "\n", "<br />")) .endEntity(); xmlwriter.writeEntity("type").writeText(group.getType()).endEntity(); // Process memos if (group.getMemo() != null) { xmlwriter.writeXml(MemoXmlHelper.getMemosXml(group.getMemo())); } // Get list of Employees int commentReference = 1; if (group.getItems() != null) { xmlwriter.writeEntity("items"); for (String itemIndex : group.getItems().keySet()) { ItemBean item = group.getItems().get(itemIndex); if (item != null) { xmlwriter.writeEntity("item").writeAttribute("refguid", item.getObject2GUID()); xmlwriter.writeEntity("name").writeText(item.getName()).endEntity(); if (item.getStartDate() != null) { xmlwriter.writeEntity("startDate").writeText(Formatter.convertDate(item.getStartDate())) .endEntity(); } if (item.getEndDate() != null) { xmlwriter.writeEntity("endDate").writeText(Formatter.convertDate(item.getEndDate())) .endEntity(); } if (item.getTitle() != null) { xmlwriter.writeEntity("title").writeText(item.getTitle()).endEntity(); } if (item.getComment() != null) { if (item.getComment().compareTo("") != 0) { xmlwriter.writeEntity("comment"); xmlwriter.writeAttribute("commentId", String.valueOf(commentReference)); xmlwriter.writeXml(StringUtils.replace(item.getComment(), "\n", "<br />")).endEntity(); commentReference += 1; } } xmlwriter.writeEntity("weighting").writeText(item.getWeighting()).endEntity(); if (item.getIdentity() != null) { // Output the identity information for this item WhichDoctorCoreIdentityBean identity = item.getIdentity(); // Process addresses if (identity.getAddress().size() > 0) { xmlwriter.writeXml(AddressXmlHelper.getAddressesXml(identity.getAddress())); } // Process phones if (identity.getPhone().size() > 0) { xmlwriter.writeXml(PhoneXmlHelper.getPhonesXml(identity.getPhone())); } // Process emails if (identity.getEmail().size() > 0) { xmlwriter.writeXml(EmailXmlHelper.getEmailsXml(identity.getEmail())); } } xmlwriter.endEntity(); } } xmlwriter.endEntity(); } xmlwriter.endEntity(); return xmlwriter.getXml(); } }