001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.demo.travel.exporter;
017
018import org.jdom.Document;
019import org.jdom.Element;
020import org.jdom.output.Format;
021import org.jdom.output.XMLOutputter;
022import org.kuali.rice.krad.bo.Exporter;
023import org.kuali.rice.krad.demo.travel.dataobject.TravelAccountType;
024import org.kuali.rice.krad.exception.ExportNotSupportedException;
025import org.kuali.rice.krad.util.KRADConstants;
026
027import java.io.IOException;
028import java.io.OutputStream;
029import java.util.Collections;
030import java.util.List;
031
032/**
033 * Demonstrates exporting a {@code TravelAccountType} to a custom XML format.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class TravelAccountTypeExporter implements Exporter {
038
039    @Override
040    public void export(Class<?> dataObjectClass, List<? extends Object> dataObjects, String exportFormat,
041            OutputStream outputStream) throws IOException, ExportNotSupportedException {
042        Document document = new Document(new Element("travelAccountTypes"));
043
044        for (Object dataObject : dataObjects) {
045            Element travelAccountTypeElement = new Element("travelAccountType");
046            TravelAccountType travelAccountType = (TravelAccountType) dataObject;
047
048            Element accountTypeCodeElement = new Element("accountTypeCode");
049            accountTypeCodeElement.setText(travelAccountType.getAccountTypeCode());
050            travelAccountTypeElement.addContent(accountTypeCodeElement);
051
052            Element nameElement = new Element("name");
053            nameElement.setText(travelAccountType.getName());
054            travelAccountTypeElement.addContent(nameElement);
055
056            Element activeElement = new Element("active");
057            activeElement.setText(Boolean.toString(travelAccountType.isActive()));
058            travelAccountTypeElement.addContent(activeElement);
059
060            document.getRootElement().addContent(travelAccountTypeElement);
061        }
062
063        XMLOutputter outputer = new XMLOutputter(Format.getPrettyFormat());
064        try {
065            outputer.output(document, outputStream);
066        } catch (IOException e) {
067            throw new RuntimeException("Could not write XML data export.", e);
068        }
069    }
070
071    @Override
072    public List<String> getSupportedFormats(Class<?> dataObjectClass) {
073        return Collections.singletonList(KRADConstants.XML_FORMAT);
074    }
075}