Exemple #1
0
 private Object maxOfList(Node lst, RuleContext context) {
   java.util.List<Node> l = Util.convertList(lst, context);
   Number max = null;
   XSDDateTime maxDate = null;
   for (int i = 0; l != null && i < l.size(); i++) {
     Node elt = (Node) l.get(i);
     if (elt != null && elt.isLiteral()) {
       Object v1 = elt.getLiteralValue();
       if (v1 instanceof Number) {
         if (max == null || max.doubleValue() < ((Number) v1).doubleValue()) {
           max = (Number) v1;
         }
       } else if (v1 instanceof XSDDateTime) {
         if (maxDate == null || maxDate.compareTo((XSDDateTime) v1) < 0) {
           maxDate = (XSDDateTime) v1;
         }
       } else {
         throw new BuiltinException(
             this, context, "Element of list input to max not a number: " + v1.toString());
       }
     } else {
       throw new BuiltinException(
           this, context, "Element of list input to max not a Literal: " + elt.toString());
     }
   }
   if (maxDate != null) {
     return maxDate;
   }
   return max;
 }
  /**
   * Set the cache control and last modified HTTP headers from data in the graph
   *
   * @param httpHeaders
   * @param rdf
   */
  public static void setCachingHeaders(
      final MultivaluedMap<String, Object> httpHeaders, final Dataset rdf, final UriInfo uriInfo) {

    final List<PathSegment> segments = uriInfo.getPathSegments();
    if (!segments.isEmpty() && segments.get(0).getPath().startsWith("tx:")) {
      // Do not set caching headers if we are in a transaction
      return;
    }

    httpHeaders.put(CACHE_CONTROL, of((Object) "max-age=0", (Object) "must-revalidate"));

    LOGGER.trace(
        "Attempting to discover the last-modified date of the node for the resource in question...");
    final Iterator<Quad> iterator =
        rdf.asDatasetGraph().find(ANY, getDatasetSubject(rdf), lastModifiedPredicate, ANY);

    if (!iterator.hasNext()) {
      return;
    }

    final Object dateObject = iterator.next().getObject().getLiteralValue();

    if (!(dateObject instanceof XSDDateTime)) {
      LOGGER.debug("Found last-modified date, but it was not an XSDDateTime: {}", dateObject);

      return;
    }

    final XSDDateTime lastModified = (XSDDateTime) dateObject;
    LOGGER.debug("Found last-modified date: {}", lastModified);
    final String lastModifiedAsRdf2822 =
        RFC2822DATEFORMAT.print(new DateTime(lastModified.asCalendar()));
    httpHeaders.put(LAST_MODIFIED, of((Object) lastModifiedAsRdf2822));
  }
  /**
   * the last three can be null log( message, level, null, null, null); log(defaultlogprefix,
   * message, level, null, null, null);
   *
   * @param logPrefix
   * @param message
   * @param level
   * @param dc_creator
   * @param resourceURI
   * @param logger
   * @return
   */
  public static OntModel log(
      String logPrefix,
      String message,
      RLOGIndividuals level,
      String dc_creator,
      String resourceURI,
      Logger logger) {
    OntModel model =
        ModelFactory.createOntologyModel(
            OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
    model.setNsPrefix("rlog", "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/rlog#");

    // urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6
    //        Resource r = model.createResource(logPrefix + level.name() + "_" + UUID.randomUUID() +
    // "_" + System.currentTimeMillis());
    Resource r = model.createResource(logPrefix + UUID.randomUUID());
    r.addProperty(RDF.type, model.createResource(RLOGOntClasses.Entry.getUri()));
    r.addProperty(
        RLOGObjectProperties.level.getObjectProperty(model), model.createResource(level.getUri()));
    r.addProperty(
        RLOGDatatypeProperties.message.getDatatypeProperty(model), model.createLiteral(message));
    XSDDateTime date = new XSDDateTime(Calendar.getInstance());
    r.addProperty(
        RLOGDatatypeProperties.date.getDatatypeProperty(model),
        date.toString(),
        date.getNarrowedDatatype());

    if (dc_creator != null) {
      r.addProperty(DC.creator, model.createLiteral(dc_creator));
    }

    if (resourceURI != null) {
      r.addProperty(
          RLOGObjectProperties.resource.getObjectProperty(model),
          model.createResource(resourceURI));
    }

    if (logger != null) {
      r.addProperty(RLOGDatatypeProperties.className.getDatatypeProperty(model), logger.getName());
      switch (level) {
        case TRACE:
          logger.trace(message);
          break;
        case DEBUG:
          logger.debug(message);
          break;
        case INFO:
          logger.info(message);
          break;
        case WARN:
          logger.warn(message);
          break;
        case ERROR:
          logger.error(message);
          break;
        case FATAL:
          logger.error(message);
          break;
      }
    }

    return model;
  }