/**
   * NSAUTOCLOSE causes the process to end on import completion
   *
   * <p>NSTARGETTEMPLATE sets a target for the import <em>if none is set</em>
   */
  private void detectKnownAnnotations() throws Exception {

    // PythonImporter et al. may pass a null settings.
    if (settings == null || settings.userSpecifiedAnnotationList == null) {
      return;
    }

    ome.model.annotations.CommentAnnotation lastCA = null;
    long maxCAId = 0;
    for (Annotation a : settings.userSpecifiedAnnotationList) {
      if (a == null) {
        continue;
      }

      ome.model.annotations.Annotation ann = null;

      String ns = null;
      if (a.isLoaded()) {
        ns = a.getNs() == null ? null : a.getNs().getValue();
        ann = (ome.model.annotations.Annotation) new IceMapper().reverse(a);
      } else {
        if (a.getId() == null) {
          // not sure what we can do with this annotation then.
          continue;
        }
        ann =
            (ome.model.annotations.Annotation)
                helper
                    .getSession()
                    .get(ome.model.annotations.Annotation.class, a.getId().getValue());
        ns = ann.getNs();
      }
      if (NSAUTOCLOSE.value.equals(ns)) {
        autoClose = true;
      } else if (NSTARGETTEMPLATE.value.equals(ns)) {
        ome.model.annotations.CommentAnnotation ca = (ome.model.annotations.CommentAnnotation) ann;
        if (ca.getId().longValue() > maxCAId) {
          maxCAId = ca.getId().longValue();
          lastCA = ca;
        }
      }
    }
    if (lastCA != null) {
      if (settings.userSpecifiedTarget != null) {
        // TODO: Exception
        String kls = settings.userSpecifiedTarget.getClass().getSimpleName();
        long id = settings.userSpecifiedTarget.getId().getValue();
        log.error("User-specified template target '{}' AND {}:{}", lastCA.getTextValue(), kls, id);
      } else {
        // Path converted to unix slashes.
        String path = lastCA.getDescription();
        File file = new File(path);
        for (int i = 0; i < location.omittedLevels; i++) {
          file = file.getParentFile();
        }
        path = file.toString();
        log.debug("Using target path {}", path);
        // Here we use the client-side (but unix-separated) path, since
        // for simple imports, we don't have any context about the directory
        // from the client.
        ServerTemplateImportTarget target = new ServerTemplateImportTarget(path);
        target.init(lastCA.getTextValue());
        settings.userSpecifiedTarget = target.load(store, reader.isSPWReader());
      }
    }
  }