code: private Map<PortalDataKey, Set<BucketTuple>> prepareImportQueue(
              final ITenant tenant, final Set<Resource> templates) throws Exception {
          final Map<PortalDataKey, Set<BucketTuple>> rslt = new HashMap<>();
          Resource rsc = null;
          try {
              for (Resource r : templates) {
                  rsc = r;
                  if (log.isDebugEnabled()) {
                      log.debug(
                              "Loading template resource file for tenant "
                                      + "'"
                                      + tenant.getFname()
                                      + "':  "
                                      + rsc.getFilename());
                  }
                  final Document doc = reader.read(rsc.getInputStream());
                  PortalDataKey atLeastOneMatchingDataKey = null;
                  for (PortalDataKey pdk : dataKeyImportOrder) {
                      boolean matches = evaluatePortalDataKeyMatch(doc, pdk);
                      if (matches) {
                          // Found the right bucket...
                          log.debug("Found PortalDataKey '{}' for data document {}", pdk, r.getURI());
                          atLeastOneMatchingDataKey = pdk;
                          Set<BucketTuple> bucket = rslt.get(atLeastOneMatchingDataKey);
                          if (bucket == null) {
                              // First of these we've seen;  create the bucket;
                              bucket = new HashSet<>();
                              rslt.put(atLeastOneMatchingDataKey, bucket);
                          }
                          BucketTuple tuple = new BucketTuple(rsc, doc);
                          bucket.add(tuple);
                          /*
                           * At this point, we would normally add a break;
                           * statement, but group_membership.xml files need to
                           * match more than one PortalDataKey.
                           */
                      }
                  }
                  if (atLeastOneMatchingDataKey == null) {
                      // We can't proceed
                      throw new RuntimeException(
                              "No PortalDataKey found for QName:  "
                                      + doc.getRootElement().getQName());
                  }
              }
          } catch (Exception e) {
              log.error(
                      "Failed to process the specified template:  {}",
                      (rsc != null ? rsc.getFilename() : "null"),
                      e);
              throw e;
          }
          return rslt;
      }
docs: Loads dom4j Documents and sorts the entity files into the proper order for Import.