Пример #1
0
  private void processMergeResourceRequest(VitroRequest vreq) {
    String uri1 = vreq.getParameter("uri1"); // get primary uri
    String uri2 = vreq.getParameter("uri2"); // get secondary uri
    String usePrimaryLabelOnlyStr = vreq.getParameter("usePrimaryLabelOnly");
    ;
    boolean usePrimaryLabelOnly =
        usePrimaryLabelOnlyStr != null && !usePrimaryLabelOnlyStr.isEmpty();

    if (uri1 != null) {
      JenaIngestUtils utils = new JenaIngestUtils();
      /*
       * get baseOnt and infOnt models
       */
      OntModel baseOntModel = ModelAccess.on(getServletContext()).getOntModel(FULL_ASSERTIONS);
      OntModel tboxOntModel = ModelAccess.on(getServletContext()).getOntModel(TBOX_UNION);

      /*
       * calling method that does the merge operation.
       */
      MergeResult result =
          utils.doMerge(uri1, uri2, baseOntModel, tboxOntModel, usePrimaryLabelOnly);

      vreq.getSession().setAttribute("leftoverModel", result.getLeftoverModel());
      vreq.setAttribute("result", result);
      vreq.setAttribute("title", "Merge Resources");
      vreq.setAttribute("bodyJsp", MERGE_RESULT);
    } else {
      vreq.setAttribute("title", "Merge Resources");
      vreq.setAttribute("bodyJsp", MERGE_RESOURCES);
    }
  }
Пример #2
0
 protected static ModelMaker getModelMaker(HttpServletRequest req) {
   ServletContext ctx = req.getSession().getServletContext();
   if (isUsingMainStoreForIngest(req)) {
     RDFService rdfService = ModelAccess.on(ctx).getRDFService(CONTENT);
     return new BlankNodeFilteringModelMaker(
         rdfService, ModelAccess.on(ctx).getModelMaker(CONTENT));
   } else {
     RDFService rdfService = ModelAccess.on(ctx).getRDFService(CONFIGURATION);
     return new BlankNodeFilteringModelMaker(
         rdfService, ModelAccess.on(ctx).getModelMaker(CONFIGURATION));
   }
 }
Пример #3
0
 private void processExecuteWorkflowRequest(VitroRequest vreq) {
   String workflowURIStr = vreq.getParameter("workflowURI");
   String workflowStepURIStr = vreq.getParameter("workflowStepURI");
   if (workflowURIStr != null && workflowStepURIStr != null) {
     doExecuteWorkflow(vreq);
     vreq.setAttribute("title", "IngestMenu");
     vreq.setAttribute("bodyJsp", INGEST_MENU_JSP);
   } else if (workflowURIStr != null) {
     // Select the workflow step at which to start
     OntModel jenaOntModel = (OntModel) getModel("vitro:jenaOntModel", vreq);
     vreq.setAttribute(
         "workflowSteps",
         new JenaIngestWorkflowProcessor(
                 jenaOntModel.getIndividual(workflowURIStr), getModelMaker(vreq))
             .getWorkflowSteps(null));
     vreq.setAttribute("title", "Choose Workflow Step");
     vreq.setAttribute("bodyJsp", WORKFLOW_STEP_JSP);
   } else {
     OntModel jenaOntModel = ModelAccess.on(getServletContext()).getOntModel();
     jenaOntModel.enterCriticalSection(Lock.READ);
     List<Individual> savedQueryList = new LinkedList<Individual>();
     try {
       Resource workflowClassRes = WorkflowOntology.Workflow;
       savedQueryList.addAll(jenaOntModel.listIndividuals(workflowClassRes).toList());
     } finally {
       jenaOntModel.leaveCriticalSection();
     }
     vreq.setAttribute("workflows", savedQueryList);
     vreq.setAttribute("title", "Execute Workflow");
     vreq.setAttribute("bodyJsp", EXECUTE_WORKFLOW_JSP);
   }
 }
Пример #4
0
 private void doDetachModel(String modelName) {
   Model m = attachedModels.get(modelName);
   if (m == null) {
     return;
   }
   ModelAccess.on(getServletContext()).getOntModel(TBOX_ASSERTIONS).removeSubModel(m);
   attachedModels.remove(modelName);
   log.info("Detached " + modelName + " (" + m.hashCode() + ") from webapp");
 }
Пример #5
0
 private long doExecuteSparql(VitroRequest vreq) {
   OntModel jenaOntModel = ModelAccess.on(getServletContext()).getOntModel();
   OntModel source = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
   String[] sourceModel = vreq.getParameterValues("sourceModelName");
   for (int i = 0; i < sourceModel.length; i++) {
     Model m = getModel(sourceModel[i], vreq);
     source.addSubModel(m);
   }
   Model destination = getModel(vreq.getParameter("destinationModelName"), vreq);
   String sparqlQueryStr = vreq.getParameter("sparqlQueryStr");
   String savedQueryURIStr = vreq.getParameter("savedQuery");
   String queryStr;
   if (savedQueryURIStr.length() == 0) {
     log.debug("Using entered query");
     queryStr = sparqlQueryStr;
   } else {
     Property queryStrProp = ResourceFactory.createProperty(SPARQL_QUERYSTR_PROP);
     jenaOntModel.enterCriticalSection(Lock.READ);
     try {
       Individual ind = jenaOntModel.getIndividual(savedQueryURIStr);
       log.debug("Using query " + savedQueryURIStr);
       queryStr = ((Literal) ind.getPropertyValue(queryStrProp)).getLexicalForm();
       queryStr =
           StringEscapeUtils.unescapeHtml(
               queryStr); // !!! We need to turn off automatic HTML-escaping for data property
                          // editing.
     } finally {
       jenaOntModel.leaveCriticalSection();
     }
   }
   Model tempModel = ModelFactory.createDefaultModel();
   Query query = SparqlQueryUtils.create(queryStr);
   QueryExecution qexec = QueryExecutionFactory.create(query, source);
   try {
     qexec.execConstruct(tempModel);
   } catch (QueryExecException qee) {
     qexec.execDescribe(tempModel);
   }
   destination.enterCriticalSection(Lock.WRITE);
   try {
     if (destination instanceof OntModel) {
       ((OntModel) destination).getBaseModel().notifyEvent(new EditEvent(null, true));
     } else {
       destination.notifyEvent(new EditEvent(null, true));
     }
     destination.add(tempModel);
   } finally {
     if (destination instanceof OntModel) {
       ((OntModel) destination).getBaseModel().notifyEvent(new EditEvent(null, false));
     } else {
       destination.notifyEvent(new EditEvent(null, false));
     }
     destination.leaveCriticalSection();
   }
   return tempModel.size();
 }
Пример #6
0
 private void doAttachModel(String modelName, ModelMaker modelMaker) {
   if (attachedModels.containsKey(modelName)) {
     doDetachModel(modelName);
   }
   Model m = ModelFactory.createDefaultModel();
   m.add(modelMaker.getModel(modelName));
   ModelAccess.on(getServletContext()).getOntModel(TBOX_ASSERTIONS).addSubModel(m);
   attachedModels.put(modelName, m);
   log.info("Attached " + modelName + " (" + m.hashCode() + ") to webapp");
 }
Пример #7
0
  private Object getApplicationName() {
    String name = "";
    try {
      WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory();
      ApplicationBean app = wadf.getApplicationDao().getApplicationBean();
      name = app.getApplicationName();
    } catch (Exception e) {
      // deal with problems below
    }

    if ((name != null) && (!name.isEmpty())) {
      return name;
    } else {
      return getContextPath();
    }
  }
Пример #8
0
 private void processExecuteSparqlRequest(VitroRequest vreq) {
   String sparqlQueryStr = vreq.getParameter("sparqlQueryStr");
   OntModel jenaOntModel = ModelAccess.on(getServletContext()).getOntModel();
   jenaOntModel.enterCriticalSection(Lock.READ);
   List<Individual> savedQueryList = new LinkedList<Individual>();
   try {
     Resource sparqlConstructClassRes = ResourceFactory.createResource(SPARQL_CONSTRUCT_CLASS);
     savedQueryList.addAll(jenaOntModel.listIndividuals(sparqlConstructClassRes).toList());
   } finally {
     jenaOntModel.leaveCriticalSection();
   }
   /*ass92*/
   OntologyDao daoObj = vreq.getUnfilteredWebappDaoFactory().getOntologyDao();
   List<Ontology> ontologiesObj = daoObj.getAllOntologies();
   List<String> prefixList = new ArrayList<>();
   if (ontologiesObj != null && ontologiesObj.size() > 0) {
     Iterator<Ontology> ontItr = ontologiesObj.iterator();
     while (ontItr.hasNext()) {
       Ontology ont = ontItr.next();
       prefixList.add(ont.getPrefix() == null ? "(not yet specified)" : ont.getPrefix());
       prefixList.add(ont.getURI() == null ? "" : ont.getURI());
     }
   } else {
     prefixList.add("<strong>" + "No Ontologies added" + "</strong>");
     prefixList.add("<strong>" + "Load Ontologies" + "</strong>");
   }
   vreq.setAttribute("prefixList", prefixList);
   /*complete*/
   if (sparqlQueryStr != null) {
     String validationMessage = "";
     if (vreq.getParameterValues("sourceModelName") == null) {
       validationMessage += "<p>Please select one or more source models.</p>";
     }
     if (vreq.getParameter("destinationModelName") == null) {
       validationMessage += "<p>Please select a destination model</p>";
     }
     if (validationMessage.length() > 0) {
       vreq.setAttribute("validationMessage", validationMessage);
     } else {
       long constructedStmtCount = 0;
       try {
         constructedStmtCount = doExecuteSparql(vreq);
       } catch (QueryParseException qpe) {
         String errorMsg = "<p>Unable to parse query:</p>";
         if (qpe.getMessage() != null) {
           errorMsg += "<p>" + qpe.getMessage() + "</p>";
         }
         vreq.setAttribute("errorMsg", errorMsg);
       } catch (InconsistentOntologyException ioe) {
         String errorMsg = "<p>Inconsistent source ontology:</p>";
         if (ioe.getMessage() != null) {
           errorMsg += "<p>" + ioe.getMessage() + "</p>";
         }
         vreq.setAttribute("errorMsg", errorMsg);
       }
       vreq.setAttribute("constructedStmtCount", constructedStmtCount);
     }
     vreq.setAttribute("savedQueries", savedQueryList);
     vreq.setAttribute("title", "SPARQL CONSTRUCT result");
     vreq.setAttribute("bodyJsp", EXECUTE_SPARQL_JSP);
   } else {
     vreq.setAttribute("savedQueries", savedQueryList);
     vreq.setAttribute("title", "Execute SPARQL Construct");
     vreq.setAttribute("bodyJsp", EXECUTE_SPARQL_JSP);
   }
 }
Пример #9
0
 private void processContentModelsRequest(VitroRequest vreq) {
   ModelMaker vsmm = ModelAccess.on(getServletContext()).getModelMaker(CONTENT);
   vreq.getSession().setAttribute(WHICH_MODEL_MAKER, CONTENT);
   showModelList(vreq, vsmm, CONTENT);
 }
Пример #10
0
  private String doRename(String oldNamespace, String newNamespace) {
    String uri = null;
    String result = null;
    Integer counter = 0;
    Boolean namespacePresent = false;
    RDFService rdfService = ModelAccess.on(getServletContext()).getRDFService();
    try {
      Model baseOntModel =
          RDFServiceGraph.createRDFServiceModel(new RDFServiceGraph(rdfService, ABOX_ASSERTIONS));
      OntModel ontModel = ModelAccess.on(getServletContext()).getOntModel();
      List<String> urisToChange = new LinkedList<String>();
      ontModel.enterCriticalSection(Lock.READ);
      try {
        Iterator<Individual> indIter = ontModel.listIndividuals();
        while (indIter.hasNext()) {
          Individual ind = indIter.next();
          String namespace = ind.getNameSpace();
          if (namespace != null) {
            if (oldNamespace.equals(namespace)) {
              uri = ind.getURI();
              urisToChange.add(uri);
              namespacePresent = true;
            }
          }
        }
      } finally {
        ontModel.leaveCriticalSection();
      }
      if (!namespacePresent) {
        result = "no resources renamed";
        return result;
      }
      for (String oldURIStr : urisToChange) {
        long time1 = System.currentTimeMillis();
        Resource res = baseOntModel.getResource(oldURIStr);
        long time2 = System.currentTimeMillis();
        String newURIStr = null;
        Pattern p = Pattern.compile(oldNamespace);
        String candidateString = res.getURI();
        Matcher matcher = p.matcher(candidateString);
        newURIStr = matcher.replaceFirst(newNamespace);
        long time3 = System.currentTimeMillis();
        log.debug("time to get new uri: " + Long.toString(time3 - time2));
        log.debug("Renaming " + oldURIStr + " to " + newURIStr);

        String whereClause =
            "} WHERE { \n"
                + "  GRAPH <"
                + ABOX_ASSERTIONS
                + "> { \n"
                + "   { <"
                + oldURIStr
                + "> ?p <"
                + oldURIStr
                + "> } \n "
                + "     UNION \n"
                + "   { <"
                + oldURIStr
                + "> ?q ?o } \n "
                + "     UNION \n"
                + "   { ?s ?r <"
                + oldURIStr
                + "> } \n"
                + "  } \n"
                + "}";

        String removeQuery =
            "CONSTRUCT { \n"
                + "   <"
                + oldURIStr
                + "> ?p <"
                + oldURIStr
                + "> . \n "
                + "   <"
                + oldURIStr
                + "> ?q ?o . \n "
                + "   ?s ?r <"
                + oldURIStr
                + "> \n"
                + whereClause;

        String addQuery =
            "CONSTRUCT { \n"
                + "   <"
                + newURIStr
                + "> ?p <"
                + newURIStr
                + "> . \n "
                + "   <"
                + newURIStr
                + "> ?q ?o . \n "
                + "   ?s ?r <"
                + newURIStr
                + "> \n"
                + whereClause;
        try {
          ChangeSet cs = rdfService.manufactureChangeSet();
          cs.addAddition(
              rdfService.sparqlConstructQuery(addQuery, RDFService.ModelSerializationFormat.N3),
              RDFService.ModelSerializationFormat.N3,
              ABOX_ASSERTIONS);
          cs.addRemoval(
              rdfService.sparqlConstructQuery(removeQuery, RDFService.ModelSerializationFormat.N3),
              RDFService.ModelSerializationFormat.N3,
              ABOX_ASSERTIONS);
          rdfService.changeSetUpdate(cs);
        } catch (RDFServiceException e) {
          throw new RuntimeException(e);
        }

        long time4 = System.currentTimeMillis();
        log.debug(" time to rename : " + Long.toString(time4 - time3));
        log.debug(" time for one resource: " + Long.toString(time4 - time1));
        counter++;
      }
      result = counter.toString() + " resources renamed";
      return result;
    } finally {
      if (rdfService != null) {
        rdfService.close();
      }
    }
  }
Пример #11
0
 protected WebappDaoFactory getWebappDaoFactory() {
   return ModelAccess.on(getServletContext()).getWebappDaoFactory(ASSERTIONS_ONLY);
 }