/**
   * Returns all the FeatureId and ID attributes based on the query against the spatial index
   *
   * @return all the FeatureId and ID attributes based on the query against the spatial index
   */
  protected synchronized Map<String, FeatureId> unrefinedSpatialMatches() {
    if (_unrefinedMatches == null) {
      Geometry geom = null;

      // _index.query returns geometries that intersect with provided envelope. To use later a
      // spatial filter that
      // provides geometries that don't intersect with the query envelope (_geom) should be used a
      // full extent
      // envelope in this method, instead of the query envelope (_geom)
      if (getFilter().getClass().getName().equals("org.geotools.filter.spatial.DisjointImpl")) {
        try {
          geom = WORLD_BOUNDS;
        } catch (Exception ex) {
          ex.printStackTrace();
          return _unrefinedMatches;
        }

      } else {
        geom = _geom;
      }

      SpatialIndex spatialIndex = sourceAccessor.two();
      @SuppressWarnings("unchecked")
      List<Pair<FeatureId, String>> fids = spatialIndex.query(geom.getEnvelopeInternal());
      _unrefinedMatches = new HashMap<>();
      for (Pair<FeatureId, String> match : fids) {
        _unrefinedMatches.put(match.two(), match.one());
      }
    }
    return _unrefinedMatches;
  }
  /**
   * Create a metadata folder according to MEF {@link Version} 2 specification. If current record is
   * based on an ISO profil, the stylesheet /convert/to19139.xsl is used to map to ISO. Both files
   * are included in MEF file. Export relevant information according to format parameter.
   *
   * @param context
   * @param uuid Metadata record to export
   * @param zipFs Zip file to add new record
   * @param skipUUID
   * @param stylePath
   * @param format
   * @throws Exception
   */
  private static void createMetadataFolder(
      ServiceContext context,
      String uuid,
      FileSystem zipFs,
      boolean skipUUID,
      Path stylePath,
      Format format,
      boolean resolveXlink,
      boolean removeXlinkAttribute)
      throws Exception {

    final Path metadataRootDir = zipFs.getPath(uuid);
    Files.createDirectories(metadataRootDir);

    Pair<Metadata, String> recordAndMetadataForExport =
        MEFLib.retrieveMetadata(context, uuid, resolveXlink, removeXlinkAttribute);
    Metadata record = recordAndMetadataForExport.one();
    String xmlDocumentAsString = recordAndMetadataForExport.two();

    String id = "" + record.getId();
    String isTemp = record.getDataInfo().getType().codeString;

    if (!"y".equals(isTemp) && !"n".equals(isTemp))
      throw new Exception("Cannot export sub template");

    Path pubDir = Lib.resource.getDir(context, "public", id);
    Path priDir = Lib.resource.getDir(context, "private", id);

    final Path metadataXmlDir = metadataRootDir.resolve(MD_DIR);
    Files.createDirectories(metadataXmlDir);

    Collection<ExportFormat> formats =
        context.getApplicationContext().getBeansOfType(ExportFormat.class).values();
    for (ExportFormat exportFormat : formats) {
      for (Pair<String, String> output : exportFormat.getFormats(context, record)) {
        Files.write(metadataXmlDir.resolve(output.one()), output.two().getBytes(CHARSET));
      }
    }

    // --- save native metadata
    Files.write(metadataXmlDir.resolve(FILE_METADATA), xmlDocumentAsString.getBytes(CHARSET));

    // --- save Feature Catalog
    String ftUUID = getFeatureCatalogID(context, record.getId());
    if (!ftUUID.equals("")) {
      Pair<Metadata, String> ftrecordAndMetadata =
          MEFLib.retrieveMetadata(context, ftUUID, resolveXlink, removeXlinkAttribute);
      Path featureMdDir = metadataRootDir.resolve(SCHEMA);
      Files.createDirectories(featureMdDir);
      Files.write(featureMdDir.resolve(FILE_METADATA), ftrecordAndMetadata.two().getBytes(CHARSET));
    }

    // --- save info file
    byte[] binData =
        MEFLib.buildInfoFile(context, record, format, pubDir, priDir, skipUUID)
            .getBytes(Constants.ENCODING);

    Files.write(metadataRootDir.resolve(FILE_INFO), binData);

    // --- save thumbnails and maps

    if (format == Format.PARTIAL || format == Format.FULL) {
      IO.copyDirectoryOrFile(pubDir, metadataRootDir, true);
    }

    if (format == Format.FULL) {
      try {
        Lib.resource.checkPrivilege(context, id, ReservedOperation.download);
        IO.copyDirectoryOrFile(priDir, metadataRootDir, true);
      } catch (Exception e) {
        // Current user could not download private data
      }
    }
  }