View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.kim.impl.jaxb;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.kuali.rice.core.framework.impex.xml.XmlLoader;
28  import org.kuali.rice.kim.api.permission.PermissionContract;
29  import org.kuali.rice.kim.api.role.RoleContract;
30  import org.kuali.rice.krad.bo.Exporter;
31  import org.kuali.rice.krad.exception.ExportNotSupportedException;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  /**
35   * Imports and exports roles and permissions from/to XML via JAXB.
36   * 
37   * <p>TODO: Do we need to restrict XML additions or updates based on which user is performing the ingestion?
38   * 
39   * <p>TODO: It may be best to make this class into a "service" instead.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class KimImporterAndExporter implements XmlLoader, Exporter {
44  
45      private final List<String> supportedFormats = Collections.singletonList(KRADConstants.XML_FORMAT);
46      
47      /**
48       * @see org.kuali.rice.core.framework.impex.xml.XmlLoader#loadXml(java.io.InputStream, java.lang.String)
49       */
50      @Override
51      public void loadXml(InputStream inputStream, String principalId) {
52          KimXmlUtil.parseKimXml(inputStream);
53      }
54  
55      /**
56       * @see org.kuali.rice.krad.bo.Exporter#export(java.lang.Class, java.util.List, java.lang.String, java.io.OutputStream)
57       */
58      @Override
59      public void export(Class<?> dataObjectClass, List<? extends Object> dataObjects, String exportFormat,
60              OutputStream outputStream) throws IOException, ExportNotSupportedException {
61          if (!supportedFormats.contains(exportFormat)) {
62              throw new ExportNotSupportedException("The KimImporterAndExporter does not support the \"" + exportFormat + "\" export format");
63          }
64          
65          if (PermissionContract.class.isAssignableFrom(dataObjectClass)) {
66              KimXmlUtil.exportKimXml(outputStream, dataObjects, null);
67          } else if (RoleContract.class.isAssignableFrom(dataObjectClass)) {
68              KimXmlUtil.exportKimXml(outputStream, null, dataObjects);
69          } else {
70              throw new ExportNotSupportedException("The KimImporterAndExporter cannot export non-permission and non-role objects");
71          }
72          
73      }
74  
75      /**
76       * @see org.kuali.rice.krad.bo.Exporter#getSupportedFormats(java.lang.Class)
77       */
78      @Override
79      public List<String> getSupportedFormats(Class<?> dataObjectClass) {
80          return supportedFormats;
81      }
82  }