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