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.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.util.Collections;
025import java.util.List;
026
027import org.kuali.rice.core.framework.impex.xml.XmlLoader;
028import org.kuali.rice.kim.api.permission.PermissionContract;
029import org.kuali.rice.kim.api.role.RoleContract;
030import org.kuali.rice.krad.bo.Exporter;
031import org.kuali.rice.krad.exception.ExportNotSupportedException;
032import org.kuali.rice.krad.util.KRADConstants;
033
034/**
035 * Imports and exports roles and permissions from/to XML via JAXB.
036 * 
037 * <p>TODO: Do we need to restrict XML additions or updates based on which user is performing the ingestion?
038 * 
039 * <p>TODO: It may be best to make this class into a "service" instead.
040 * 
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043public class KimImporterAndExporter implements XmlLoader, Exporter {
044
045    private final List<String> supportedFormats = Collections.singletonList(KRADConstants.XML_FORMAT);
046    
047    /**
048     * @see org.kuali.rice.core.framework.impex.xml.XmlLoader#loadXml(java.io.InputStream, java.lang.String)
049     */
050    @Override
051    public void loadXml(InputStream inputStream, String principalId) {
052        KimXmlUtil.parseKimXml(inputStream);
053    }
054
055    /**
056     * @see org.kuali.rice.krad.bo.Exporter#export(java.lang.Class, java.util.List, java.lang.String, java.io.OutputStream)
057     */
058    @Override
059    public void export(Class<?> dataObjectClass, List<? extends Object> dataObjects, String exportFormat,
060            OutputStream outputStream) throws IOException, ExportNotSupportedException {
061        if (!supportedFormats.contains(exportFormat)) {
062            throw new ExportNotSupportedException("The KimImporterAndExporter does not support the \"" + exportFormat + "\" export format");
063        }
064        
065        if (PermissionContract.class.isAssignableFrom(dataObjectClass)) {
066            KimXmlUtil.exportKimXml(outputStream, dataObjects, null);
067        } else if (RoleContract.class.isAssignableFrom(dataObjectClass)) {
068            KimXmlUtil.exportKimXml(outputStream, null, dataObjects);
069        } else {
070            throw new ExportNotSupportedException("The KimImporterAndExporter cannot export non-permission and non-role objects");
071        }
072        
073    }
074
075    /**
076     * @see org.kuali.rice.krad.bo.Exporter#getSupportedFormats(java.lang.Class)
077     */
078    @Override
079    public List<String> getSupportedFormats(Class<?> dataObjectClass) {
080        return supportedFormats;
081    }
082}