/**
  * This should only save the specified ontology
  *
  * @param ont the ontology to save
  * @throws Exception
  */
 protected boolean hghandleSaveAs(OWLOntology ont) throws Exception {
   PHGDBOntologyManagerImpl man =
       (PHGDBOntologyManagerImpl) getModelManager().getOWLOntologyManager();
   OWLOntologyFormat oldFormat = man.getOntologyFormat(ont);
   // IRI oldDocumentIRI = man.getOntologyDocumentIRI(ont);
   // HGDBOntologyRepository repo = man.getOntologyRepository();
   OWLOntologyFormat format =
       HGOntologyFormatPanel.showDialog(
           this,
           oldFormat,
           "Choose a format to use when saving the "
               + getModelManager().getRendering(ont)
               + " ontology");
   if (format == null) {
     System.err.println("Please select a valid format");
     // logger.warn("Please select a valid format");
     return false;
   }
   return hghandleSaveAs(ont, format);
 }
 /**
  * Deletes an ontology from Hypergraph Repository.
  *
  * @param physicalURI
  * @return
  */
 protected boolean handleDeleteFrom(OntologyRepositoryEntry ontologyEntry) {
   // A) Check, if the ontology is already loaded and/or managed and
   // whether it can be found in
   // the repository.
   OWLOntologyID oID = ((HGOntologyRepositoryEntry) ontologyEntry).getOntologyID();
   if (oID == null) throw new IllegalStateException();
   HGOwlModelManagerImpl hmm = (HGOwlModelManagerImpl) getOWLModelManager();
   PHGDBOntologyManagerImpl hom = (PHGDBOntologyManagerImpl) hmm.getOWLOntologyManager();
   OWLOntology loadedOntoToDelete = hom.getOntology(oID);
   // will be null if not loaded.
   // getOntologyCatalogManager().Ontologies()
   // B) Provide a confirmation Dialog with as much information as
   // possible.
   boolean userConfirmsDelete =
       showDeleteConfirmation(oID, ontologyEntry.getPhysicalURI(), loadedOntoToDelete);
   if (userConfirmsDelete) {
     // C) Actual Removal:
     // C-A) if ontology managed, remove from OwlModelManager,
     // Owlontologymanager
     if (loadedOntoToDelete != null) {
       if (!(hom.getOntologyFormat(loadedOntoToDelete) instanceof HGDBOntologyFormat)) {
         hmm.removeOntology(loadedOntoToDelete);
       } else {
         System.out.println(
             "File based ontology not unloaded :" + loadedOntoToDelete.getOntologyID());
       }
     }
     // C-B) delete in repository
     boolean repoDeleteOk = hom.getOntologyRepository().deleteOntology(oID);
     showDeleteSuccessOrFailure(repoDeleteOk, oID, ontologyEntry.getPhysicalURI());
     return repoDeleteOk;
   } else {
     JOptionPane.showMessageDialog(
         getWorkspace(),
         "Delete cancelled by user.                                ",
         "Delete Hypergraph Database Backed Ontology - Cancelled",
         JOptionPane.WARNING_MESSAGE);
     return false;
   }
 }
  protected boolean hghandleSaveAs(OWLOntology ont, OWLOntologyFormat format) throws Exception {
    PHGDBOntologyManagerImpl man =
        (PHGDBOntologyManagerImpl) getModelManager().getOWLOntologyManager();
    OWLOntologyFormat oldFormat = man.getOntologyFormat(ont);
    IRI oldDocumentIRI = man.getOntologyDocumentIRI(ont);
    HGDBOntologyRepository repo = man.getOntologyRepository();
    if (oldFormat instanceof PrefixOWLOntologyFormat && format instanceof PrefixOWLOntologyFormat) {
      PrefixOWLOntologyFormat oldPrefixes = (PrefixOWLOntologyFormat) oldFormat;
      for (String name : oldPrefixes.getPrefixNames()) {
        ((PrefixOWLOntologyFormat) format).setPrefix(name, oldPrefixes.getPrefix(name));
      }
    }
    if (format instanceof HGDBOntologyFormat) {
      // Case A1) OntololgyHGDB -> Repository Same Name: Already in
      // repository
      // Case A2) OntololgyHGDB -> Repository: Copy Ontology in Repository
      // NOT CURRENTLY SUPPORTED DIALOLG
      if (ont instanceof HGDBOntology) {
        String message =
            "This ontology is database backed and does not need to be saved to the database again.\n"
                + "All changes to it are instantly persisted in the Hypergraph Ontology Repository.\n"
                + "A copy operation to a different name in the repository is currently not supported.";
        System.err.println(message);
        // logger.warn(message);
        JOptionPane.showMessageDialog(
            getWorkspace(),
            message,
            "Hypergraph Database Backed Ontology",
            JOptionPane.ERROR_MESSAGE);

        return false;
      } else {
        // IN MEMORY ONTOLOGY
        // Case B) OntololgyImpl -> Repository: Import
        String message =
            "This in-memory ontology will be imported into the Hypergraph Ontology Repository.\n"
                + "This process is estimated to take one minute per 35000 Axioms. \n"
                + ont.getOntologyID().toString()
                + " has "
                + ont.getAxiomCount()
                + " Axioms. \n"
                + "Please be patient. A Success Dialog will pop up when the process is finished.";
        System.err.println(message);
        // logger.info(message);
        JOptionPane.showMessageDialog(
            getWorkspace(), message, "Hypergraph Database Import", JOptionPane.INFORMATION_MESSAGE);
        System.out.println("IMPORTING INTO HYPERGRAPH " + ont.getOntologyID());
        // logger.info("IMPORTING INTO HYPERGRAPH " +
        // ont.getOntologyID());
        long startTime = System.currentTimeMillis();
        man.setOntologyFormat(ont, format);
        // TODO OPEN A DIALOG FOR SELECTING A documentIRI
        IRI documentIri;
        if (ont.getOntologyID().isAnonymous()) {
          int i = 0;
          do {
            documentIri = IRI.create("hgdb://" + "anonymous-" + i);
            i++;
          } while (repo.existsOntologyByDocumentIRI(documentIri));

        } else {
          // HGDBOntologyFormat.convertToHGDBDocumentIRI(ontologyIRI)
          // IRI defaultIri =
          // ont.getOntologyID().getDefaultDocumentIRI();
          // String defaultIriStr = defaultIri.toString();
          // int schemaLength = defaultIri.getScheme().length();
          // String hgdbIRIStr = "hgdb" +
          // defaultIriStr.toString().substring(schemaLength);
          documentIri =
              HGDBOntologyFormat.convertToHGDBDocumentIRI(
                  ont.getOntologyID().getDefaultDocumentIRI());
          //
          // Check if exists by ID or Document IRI
          //
          if (repo.existsOntology(ont.getOntologyID())) {
            JOptionPane.showMessageDialog(
                getWorkspace(),
                "An ontology with the same ID already exists in the hypergraph repository."
                    + "\r\n "
                    + ont.getOntologyID()
                    + "\r\n If you wish to replace, delete the old one now using: HypergraphDB/Delete",
                "Hypergraph Database Import - Failed",
                JOptionPane.ERROR_MESSAGE);
            return false;
          } else if (repo.existsOntologyByDocumentIRI(documentIri)) {
            JOptionPane.showMessageDialog(
                getWorkspace(),
                "An ontology with the same documentIRI already exists in the hypergraph repository."
                    + "\r\n "
                    + documentIri
                    + "\r\n If you wish to replace, delete the old one now using: HypergraphDB/Delete",
                "Hypergraph Database Import - Failed",
                JOptionPane.ERROR_MESSAGE);
            return false;
          } // else continue import
        }
        System.out.println("Saving with documentIRI: " + documentIri);
        // logger.info("Saving with documentIRI: " + documentIri);
        // + ont.getOntologyID().getOntologyIRI().getFragment());

        man.setOntologyDocumentIRI(ont, documentIri);
        getModelManager().save(ont);
        int durationSecs = (int) (System.currentTimeMillis() - startTime) / 1000;
        message =
            "Hypergraph Database Import Success.\n"
                + "Saving took "
                + durationSecs
                + " seconds for "
                + ont.getAxiomCount()
                + " Axioms. \n"
                + "You are still working with the in-memory ontology. \n "
                + "Do you wish to use the database backed ontology now?";
        int useHGOnto =
            JOptionPane.showConfirmDialog(
                getWorkspace(),
                message,
                "Hypergraph Database Import Success",
                JOptionPane.YES_NO_OPTION);
        addRecent(documentIri.toURI());
        if (useHGOnto == JOptionPane.YES_OPTION) {
          // load the ontology from hypergraph and close
          getModelManager().reload(ont);
        } else {
          man.setOntologyFormat(ont, oldFormat);
          man.setOntologyDocumentIRI(ont, oldDocumentIRI);
        }
        return true;
      }
    } else {
      // FILE BASED FORMAT
      File file = getSaveAsOWLFile(ont);
      if (file != null) {
        // Do the following only if not database backed.
        man.setOntologyFormat(ont, format);
        man.setOntologyDocumentIRI(ont, IRI.create(file));
        try {
          getModelManager().save(ont);
          addRecent(file.toURI());
        } finally {
          if (ont instanceof HGDBOntology) {
            man.setOntologyFormat(ont, oldFormat);
            man.setOntologyDocumentIRI(ont, oldDocumentIRI);
          }
        }
        return true;
      } else {
        System.err.println("No valid file specified for the save as operation - quitting");
        // logger.warn("No valid file specified for the save as operation - quitting");
        return false;
      }
    }
  }