001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.rice.kim.impl.jaxb;
020
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.util.List;
024
025import javax.xml.bind.JAXBContext;
026import javax.xml.bind.JAXBException;
027import javax.xml.bind.Marshaller;
028import javax.xml.bind.Unmarshaller;
029
030import org.kuali.rice.core.impl.jaxb.DataXmlDTO;
031
032/**
033 * Helper class for importing and exporting KIM XML.
034 * 
035 * <p>TODO: Should this be converted into a service instead?
036 * 
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public final class KimXmlUtil {
040    // Do not allow outside code to instantiate this class.
041    private KimXmlUtil() {}
042    
043    /**
044     * Parses permissions and/or roles from XML.
045     * 
046     * @param inputStream The input stream to read the XML from.
047     */
048    public static void parseKimXml(InputStream inputStream) {
049        try {
050            JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
051            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
052            unmarshaller.unmarshal(inputStream);
053        } catch (JAXBException e) {
054            throw new RuntimeException(e);
055        }
056    }
057    
058    /**
059     * Exports one or more sets of KIM objects to XML.
060     * 
061     * @param outputStream The output stream to write the XML to.
062     * @param permissions The KIM permissions to export; set to a null or empty list to prevent exportation of a &lt;permissionData&gt; element.
063     * @param roles The KIM roles to export; set to a null or empty list to prevent exportation of a &lt;roleData&gt; element.
064     */
065    public static void exportKimXml(OutputStream outputStream, List<? extends Object> permissions, List<? extends Object> roles) {
066        PermissionDataXmlDTO permissionData = (permissions != null && !permissions.isEmpty()) ?
067                new PermissionDataXmlDTO(new PermissionsXmlDTO(permissions)) : null;
068        RoleDataXmlDTO roleData = (roles != null && !roles.isEmpty()) ?
069                new RoleDataXmlDTO(new RolesXmlDTO(roles)) : null;
070        try {
071            JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
072            Marshaller marshaller = jaxbContext.createMarshaller();
073            marshaller.marshal(new DataXmlDTO(permissionData, roleData), outputStream);
074        } catch (JAXBException e) {
075            throw new RuntimeException(e);
076        }
077    }
078}