View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.jaxb;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.kuali.rice.core.framework.impex.xml.XmlLoader;
25  import org.kuali.rice.kim.api.permission.PermissionContract;
26  import org.kuali.rice.kim.api.role.RoleContract;
27  import org.kuali.rice.krad.bo.Exporter;
28  import org.kuali.rice.krad.exception.ExportNotSupportedException;
29  import org.kuali.rice.krad.util.KRADConstants;
30  
31  /**
32   * Imports and exports roles and permissions from/to XML via JAXB.
33   * 
34   * <p>TODO: Do we need to restrict XML additions or updates based on which user is performing the ingestion?
35   * 
36   * <p>TODO: It may be best to make this class into a "service" instead.
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class KimImporterAndExporter implements XmlLoader, Exporter {
41  
42      private final List<String> supportedFormats = Collections.singletonList(KRADConstants.XML_FORMAT);
43      
44      /**
45       * @see org.kuali.rice.core.framework.impex.xml.XmlLoader#loadXml(java.io.InputStream, java.lang.String)
46       */
47      @Override
48      public void loadXml(InputStream inputStream, String principalId) {
49          KimXmlUtil.parseKimXml(inputStream);
50      }
51  
52      /**
53       * @see org.kuali.rice.krad.bo.Exporter#export(java.lang.Class, java.util.List, java.lang.String, java.io.OutputStream)
54       */
55      @Override
56      public void export(Class<?> dataObjectClass, List<? extends Object> dataObjects, String exportFormat,
57              OutputStream outputStream) throws IOException, ExportNotSupportedException {
58          if (!supportedFormats.contains(exportFormat)) {
59              throw new ExportNotSupportedException("The KimImporterAndExporter does not support the \"" + exportFormat + "\" export format");
60          }
61          
62          if (PermissionContract.class.isAssignableFrom(dataObjectClass)) {
63              KimXmlUtil.exportKimXml(outputStream, dataObjects, null);
64          } else if (RoleContract.class.isAssignableFrom(dataObjectClass)) {
65              KimXmlUtil.exportKimXml(outputStream, null, dataObjects);
66          } else {
67              throw new ExportNotSupportedException("The KimImporterAndExporter cannot export non-permission and non-role objects");
68          }
69          
70      }
71  
72      /**
73       * @see org.kuali.rice.krad.bo.Exporter#getSupportedFormats(java.lang.Class)
74       */
75      @Override
76      public List<String> getSupportedFormats(Class<?> dataObjectClass) {
77          return supportedFormats;
78      }
79  }