Exemplo n.º 1
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);
   }
 }
  private String addVClassDataToResultsList(
      WebappDaoFactory wadf, VClass vcw, int position, String ontologyUri, int counter) {
    String tempString = "";
    if (ontologyUri == null
        || ((vcw.getNamespace() != null) && (vcw.getNamespace().equals(ontologyUri)))) {
      // first if statement ensures that the first class begins with correct format
      if (counter < 1 && position < 1) {
        tempString += "{ \"name\": ";
      } else if (position == previous_posn) {
        tempString += "}, { \"name\": ";
      } else if (position > previous_posn) {
        tempString += " { \"name\": ";
      } else if (position < previous_posn) {
        tempString += "}, { \"name\": ";
      }
      try {
        tempString +=
            JSONUtils.quote(
                    "<a href='vclassEdit?uri="
                        + URLEncoder.encode(vcw.getURI(), "UTF-8")
                        + "'>"
                        + vcw.getPickListName()
                        + "</a>")
                + ", ";
      } catch (Exception e) {
        tempString +=
            JSONUtils.quote(((vcw.getPickListName() == null) ? "" : vcw.getPickListName())) + ", ";
      }

      String shortDef = ((vcw.getShortDef() == null) ? "" : vcw.getShortDef());
      tempString += "\"data\": { \"shortDef\": " + JSONUtils.quote(shortDef) + ", ";

      // Get group name if it exists
      VClassGroupDao groupDao = wadf.getVClassGroupDao();
      String groupURI = vcw.getGroupURI();
      String groupName = null;
      VClassGroup classGroup = null;
      if (groupURI != null) {
        classGroup = groupDao.getGroupByURI(groupURI);
        if (classGroup != null) {
          groupName = classGroup.getPublicName();
        }
      }
      tempString +=
          "\"classGroup\": " + JSONUtils.quote((groupName == null) ? "" : groupName) + ", ";
      // Get ontology name
      OntologyDao ontDao = wadf.getOntologyDao();
      String ontName = vcw.getNamespace();
      Ontology ont = ontDao.getOntologyByURI(ontName);
      if (ont != null && ont.getName() != null) {
        ontName = ont.getName();
      }
      tempString +=
          "\"ontology\": "
              + JSONUtils.quote((ontName == null) ? "" : ontName)
              + "}, \"children\": [";

      previous_posn = position;
    }
    return tempString;
  }
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse response) {
    if (!isAuthorizedToDisplayPage(req, response, new Actions(new EditOntology()))) {
      return;
    }

    VitroRequest request = new VitroRequest(req);

    EditProcessObject epo = super.createEpo(request);
    request.setAttribute("epoKey", epo.getKey());

    OntologyDao oDao = request.getFullWebappDaoFactory().getOntologyDao();
    Ontology o = null;
    if (request.getParameter("uri") == null) {
      log.error("doPost() expects non-null uri parameter");
    } else {
      o = oDao.getOntologyByURI(request.getParameter("uri"));
      if (o == null) {
        if (!VitroVocabulary.vitroURI.equals(request.getParameter("uri"))) {
          log.debug(
              "doPost(): no ontology object found for the namespace "
                  + request.getParameter("uri"));
        }
      } else {
        request.setAttribute("Ontology", o);
      }
    }
    ArrayList<String> results = new ArrayList<String>();
    results.add("Ontology");
    results.add("Namespace");
    results.add("Prefix");
    String name = o == null ? "" : (o.getName() == null) ? "" : o.getName();
    results.add(name);
    String namespace = o == null ? "" : (o.getURI() == null) ? "" : o.getURI();
    results.add(namespace);
    String prefix = o == null ? "" : (o.getPrefix() == null) ? "" : o.getPrefix();
    results.add(prefix);
    request.setAttribute("results", results);
    request.setAttribute("columncount", 3);
    request.setAttribute("suppressquery", "true");

    epo.setDataAccessObject(oDao);
    FormObject foo = new FormObject();
    HashMap<String, List<Option>> OptionMap = new HashMap<String, List<Option>>();

    HashMap formSelect =
        new HashMap(); // tells the JSP what select lists are populated, and thus should be
                       // displayed
    request.setAttribute("formSelect", formSelect);

    // add the options
    foo.setOptionLists(OptionMap);
    epo.setFormObject(foo);

    // funky hack because Ontology.getURI() will append a hash for a hash namespace
    // See OntologyDaoJena.ontologyFromOntologyResource() comments
    String realURI = OntologyDaoJena.adjustOntologyURI(o.getURI());
    request.setAttribute("realURI", realURI);
    request.setAttribute("exportURL", request.getContextPath() + Controllers.EXPORT_RDF);

    RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP);
    request.setAttribute("epoKey", epo.getKey());
    request.setAttribute("bodyJsp", "/templates/edit/specific/ontologies_edit.jsp");
    request.setAttribute("title", "Ontology Control Panel");
    request.setAttribute(
        "css",
        "<link rel=\"stylesheet\" type=\"text/css\" href=\""
            + request.getAppBean().getThemeDir()
            + "css/edit.css\"/>");

    try {
      rd.forward(request, response);
    } catch (Exception e) {
      log.error("OntologyEditController could not forward to view.");
      log.error(e.getMessage());
      log.error(e.getStackTrace());
    }
  }