org.apache.olingo.odata2.api.edm.EdmLiteral Java Examples

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmLiteral. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FunctionalVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Object visitLiteral(LiteralExpression literal, EdmLiteral edm_literal)
{
   try
   {
      // A literal is a Provider<?> (Functional Java)
      // Returns a Node<?> (Expression Tree)
      Object o = edm_literal.getType().valueOfString(
            edm_literal.getLiteral(),
            EdmLiteralKind.DEFAULT,
            null,
            edm_literal.getType().getDefaultType());
      return ExecutableExpressionTree.Node.createLeave(ConstantFactory.constantFactory(o));
   }
   catch (EdmException ex)
   {
      throw new RuntimeException(ex);
   }
}
 
Example #2
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public ODataResponse executeFunctionImport(GetFunctionImportUriInfo uri_info, String content_type)
      throws ODataException
{
   EdmFunctionImport function_import = uri_info.getFunctionImport();
   Map<String, EdmLiteral> params = uri_info.getFunctionImportParameters();
   EntityProviderWriteProperties entry_props =
         EntityProviderWriteProperties.serviceRoot(makeLink()).build();

   AbstractOperation op = Model.getServiceOperation(function_import.getName());

   fr.gael.dhus.database.object.User current_user =
         ApplicationContextProvider.getBean(SecurityService.class).getCurrentUser();
   if (!op.canExecute(current_user))
   {
      throw new NotAllowedException();
   }

   Object res = op.execute(params);

   return EntityProvider.writeFunctionImport(content_type, function_import, res, entry_props);
}
 
Example #3
Source File: JPAFunctionContext.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Object[] getArguments() throws EdmException {
  Map<String, EdmLiteral> edmArguments = functionView.getFunctionImportParameters();

  if (edmArguments == null) {
    return null;
  } else {
    Collection<String> paramNames = functionImport.getParameterNames();
    Object[] args = new Object[paramNames.size()];
    int i = 0;
    for (String paramName : functionImport.getParameterNames()) {
      EdmLiteral literal = edmArguments.get(paramName);
      EdmParameter parameter = functionImport.getParameter(paramName);
      JPAEdmMapping mappingValue = (JPAEdmMapping) parameter.getMapping();
      args[i++] = convertArgument(literal, parameter.getFacets(), mappingValue.getJPAType());
    }
    return args;
  }

}
 
Example #4
Source File: UriParserImpl.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private void handleOtherQueryParameters() throws UriSyntaxException, EdmException {
  final EdmFunctionImport functionImport = uriResult.getFunctionImport();
  if (functionImport != null) {
    for (final String parameterName : functionImport.getParameterNames()) {
      final EdmParameter parameter = functionImport.getParameter(parameterName);
      final String value = otherQueryParameters.remove(parameterName);

      if (value == null) {
        if (parameter.getFacets() == null || parameter.getFacets().isNullable() == null
            || parameter.getFacets().isNullable()) {
          continue;
        } else {
          throw new UriSyntaxException(UriSyntaxException.MISSINGPARAMETER);
        }
      }

      EdmLiteral uriLiteral = parseLiteral(value, (EdmSimpleType) parameter.getType(), parameter.getFacets());
      uriResult.addFunctionImportParameter(parameterName, uriLiteral);
    }
  }

  uriResult.setCustomQueryOptions(otherQueryParameters);
}
 
Example #5
Source File: UriParserImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EdmLiteral parseLiteral(final String value, final EdmSimpleType expectedType) throws UriSyntaxException {
  EdmLiteral literal;
  try {
    literal = simpleTypeFacade.parseUriLiteral(value);
  } catch (EdmLiteralException e) {
    throw convertEdmLiteralException(e);
  }

  if (expectedType.isCompatible(literal.getType())) {
    return literal;
  } else {
    throw new UriSyntaxException(UriSyntaxException.INCOMPATIBLELITERAL.addContent(value, expectedType));
  }
}
 
Example #6
Source File: ODataParser.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitLiteral(LiteralExpression literal, EdmLiteral edmLiteral) {
    try {
        final EdmSimpleType type = edmLiteral.getType();

        final Object value = type.valueOfString(edmLiteral.getLiteral(),
            EdmLiteralKind.DEFAULT, null, type.getDefaultType());

        return new TypedValue(type.getDefaultType(), edmLiteral.getLiteral(), value);
    } catch (EdmSimpleTypeException ex) {
        throw new SearchParseException("Failed to convert literal to a typed form: " + literal, ex);
    }
}
 
Example #7
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> mapFunctionParameters(final Map<String, EdmLiteral> functionImportParameters)
    throws EdmSimpleTypeException {
  if (functionImportParameters == null) {
    return Collections.emptyMap();
  } else {
    Map<String, Object> parameterMap = new HashMap<String, Object>();
    for (final String parameterName : functionImportParameters.keySet()) {
      final EdmLiteral literal = functionImportParameters.get(parameterName);
      final EdmSimpleType type = literal.getType();
      parameterMap.put(parameterName, type.valueOfString(literal.getLiteral(), EdmLiteralKind.DEFAULT, null, type
          .getDefaultType()));
    }
    return parameterMap;
  }
}
 
Example #8
Source File: EdmSimpleTypeFacadeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public static void parse(final String literal, final EdmSimpleType expectedType) throws EdmLiteralException {
  final EdmLiteral uriLiteral = EdmSimpleTypeKind.parseUriLiteral(literal);
  assertNotNull(uriLiteral);
  if (!expectedType.equals(EdmNull.getInstance())) {
    assertNotNull(uriLiteral.getLiteral());
    assertTrue(uriLiteral.getLiteral().length() > 0);
  }
  assertNotNull(uriLiteral.getType());
  assertEquals(expectedType, uriLiteral.getType());
}
 
Example #9
Source File: UriInfoImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public void addFunctionImportParameter(final String name, final EdmLiteral value) {
  if (functionImportParameters.equals(Collections.EMPTY_MAP)) {
    functionImportParameters = new HashMap<String, EdmLiteral>();
  }

  functionImportParameters.put(name, value);
}
 
Example #10
Source File: UriParserImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EdmLiteral parseLiteral(String value, EdmSimpleType expectedType, EdmFacets facets) 
    throws UriSyntaxException {
  EdmLiteral literal;
  try {
    literal = simpleTypeFacade.parseUriLiteral(value, facets);
  } catch (EdmLiteralException e) {
    throw convertEdmLiteralException(e);
  }

  if (expectedType.isCompatible(literal.getType())) {
    return literal;
  } else {
    throw new UriSyntaxException(UriSyntaxException.INCOMPATIBLELITERAL.addContent(value, expectedType));
  }
}
 
Example #11
Source File: PropertyExpressionImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public PropertyExpressionImpl(final String uriLiteral, final EdmLiteral edmLiteral) {
  this.uriLiteral = uriLiteral;

  this.edmLiteral = edmLiteral;
  if (edmLiteral != null) {
    edmType = edmLiteral.getType();
  }
}
 
Example #12
Source File: Tokenizer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private boolean checkForBoolean(final int oldPosition, final String rem_expr) {
  boolean isBoolean = false;
  if ("true".equals(rem_expr) || "false".equals(rem_expr)) {
    curPosition = curPosition + rem_expr.length();
    tokens.appendEdmTypedToken(oldPosition, TokenKind.SIMPLE_TYPE, rem_expr, new EdmLiteral(EdmSimpleTypeFacadeImpl
        .getEdmSimpleType(EdmSimpleTypeKind.Boolean), rem_expr));
    isBoolean = true;
  }
  return isBoolean;
}
 
Example #13
Source File: Tokenizer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private boolean checkForLiteral(final int oldPosition, final char curCharacter, final String rem_expr) {
  final Matcher matcher = OTHER_LIT.matcher(rem_expr);
  boolean isLiteral = false;
  if (matcher.lookingAt()) {
    String token = matcher.group();
    try {
      EdmLiteral edmLiteral = typeDectector.parseUriLiteral(token);
      curPosition = curPosition + token.length();
      // It is a simple type.
      tokens.appendEdmTypedToken(oldPosition, TokenKind.SIMPLE_TYPE, token, edmLiteral);
      isLiteral = true;
    } catch (EdmLiteralException e) {
      // We treat it as normal untyped literal.

      // The '-' is checked here (and not in the switch statement) because it may be
      // part of a negative number.
      if (curCharacter == '-') {
        curPosition = curPosition + 1;
        tokens.appendToken(oldPosition, TokenKind.SYMBOL, curCharacter);
        isLiteral = true;
      } else {
        curPosition = curPosition + token.length();
        tokens.appendToken(oldPosition, TokenKind.LITERAL, token);
        isLiteral = true;
      }
    }
  }
  return isLiteral;
}
 
Example #14
Source File: TokenList.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Append UriLiteral Token to tokens parameter
 * @param position Position of parsed token
 * @param kind Kind of parsed token
 * @param javaLiteral EdmLiteral of parsed token containing type and value of UriLiteral
 */
public void appendEdmTypedToken(final int position, final TokenKind kind, final String uriLiteral,
    final EdmLiteral javaLiteral) {
  Token token = new Token(kind, position, uriLiteral, javaLiteral);
  tokens.add(token);
  return;
}
 
Example #15
Source File: JsonVisitor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitLiteral(final LiteralExpression literal, final EdmLiteral edmLiteral) {
  try {
    StringWriter writer = new StringWriter();
    JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);
    jsonStreamWriter.beginObject().namedStringValueRaw("nodeType", literal.getKind().toString()).separator()
        .namedStringValueRaw("type", getType(literal)).separator().namedStringValue("value", edmLiteral.getLiteral())
        .endObject();
    writer.flush();
    return writer.toString();
  } catch (final IOException e) {
    return null;
  }
}
 
Example #16
Source File: EdmSimpleTypeFacadeImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static EdmLiteral createEdmLiteral(final EdmSimpleTypeKind typeKind, final String literal,
    final int prefixLength, final int suffixLength) throws EdmLiteralException {
  final EdmSimpleType type = getEdmSimpleType(typeKind);
  if (type.validate(literal, EdmLiteralKind.URI, null)) {
    return new EdmLiteral(type, literal.substring(prefixLength, literal.length() - suffixLength));
  } else {
    throw new EdmLiteralException(EdmLiteralException.LITERALFORMAT.addContent(literal));
  }
}
 
Example #17
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ODataResponse executeFunctionImportValue(GetFunctionImportUriInfo uri_info, String content_type)
      throws ODataException
{
   EdmFunctionImport function_import = uri_info.getFunctionImport();
   Map<String, EdmLiteral> params = uri_info.getFunctionImportParameters();

   // FIXME: returned type might not be a simple type ...
   EdmSimpleType type = (EdmSimpleType) function_import.getReturnType().getType();

   AbstractOperation op = Model.getServiceOperation(function_import.getName());

   fr.gael.dhus.database.object.User current_user =
         ApplicationContextProvider.getBean(SecurityService.class).getCurrentUser();
   if (!op.canExecute(current_user))
   {
      throw new NotAllowedException();
   }

   Object res = op.execute(params);

   /* To handle binary results (NYI):
   if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
      response = EntityProvider.writeBinary(
         ((BinaryData) data).getMimeType(),    // BinaryData is an meta-object holding the data
         ((BinaryData) data).getData()         // and its mime type
       );
   }//*/
   final String value = type.valueToString(res, EdmLiteralKind.DEFAULT, null);

   return EntityProvider.writeText(value == null ? "" : value);
}
 
Example #18
Source File: JPAFunctionContext.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object convertArgument(final EdmLiteral edmLiteral, final EdmFacets facets, final Class<?> targetType)
    throws EdmSimpleTypeException {
  Object value = null;
  if (edmLiteral != null) {
    EdmSimpleType edmType = edmLiteral.getType();
    value = edmType.valueOfString(edmLiteral.getLiteral(), EdmLiteralKind.DEFAULT, facets, targetType);
  }
  return value;
}
 
Example #19
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> mapFunctionParameters(final Map<String, EdmLiteral> functionImportParameters)
    throws EdmSimpleTypeException {
  if (functionImportParameters == null) {
    return Collections.emptyMap();
  } else {
    Map<String, Object> parameterMap = new HashMap<String, Object>();
    for (final Entry<String, EdmLiteral> parameter : functionImportParameters.entrySet()) {
      final EdmLiteral literal = parameter.getValue();
      final EdmSimpleType type = literal.getType();
      parameterMap.put(parameter.getKey(), type.valueOfString(literal.getLiteral(), EdmLiteralKind.DEFAULT, null, type
          .getDefaultType()));
    }
    return parameterMap;
  }
}
 
Example #20
Source File: LiteralExpressionImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public LiteralExpressionImpl(final String uriLiteral, final EdmLiteral javaLiteral) {
  this.uriLiteral = uriLiteral;
  edmLiteral = javaLiteral;
  edmType = edmLiteral.getType();
}
 
Example #21
Source File: SQLVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Object visitLiteral(LiteralExpression literal, EdmLiteral edm_literal)
{
   Object result;
   Class type = edm_literal.getType().getDefaultType();

   if (type.equals(Boolean.class))
   {
      result = Boolean.valueOf(edm_literal.getLiteral());
   }
   else if (type.equals(Byte.class)    || type.equals(Short.class) ||
            type.equals(Integer.class) || type.equals(Long.class))
   {
      result = Long.valueOf(edm_literal.getLiteral());
   }
   else if (type.equals(Double.class) || type.equals(BigDecimal.class))
   {
      result = Double.valueOf(edm_literal.getLiteral());
   }
   else if (type.equals(String.class))
   {
      result = edm_literal.getLiteral();
   }
   else if (type.equals(Calendar.class))
   {
      SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
      try
      {
         result = sdf1.parse(edm_literal.getLiteral());
      }
      catch (ParseException e)
      {
         try
         {
            result = sdf2.parse(edm_literal.getLiteral());
         }
         catch (ParseException e1)
         {
            throw new IllegalArgumentException("Invalid date format");
         }
      }
   }
   else
   {
      throw new IllegalArgumentException("Type " + edm_literal.getType() +
            " is not supported by the service");
   }

   return result;
}
 
Example #22
Source File: VisitorTool.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitLiteral(final LiteralExpression literal, final EdmLiteral edmLiteral) {
  return "" + literal.getUriLiteral() + "";
}
 
Example #23
Source File: EdmSimpleTypeFacadeTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void parseIncompatibleLiteralContent(final String literal, final EdmSimpleTypeKind typeKind)
    throws EdmLiteralException {
  final EdmLiteral uriLiteral = EdmSimpleTypeKind.parseUriLiteral(literal);
  assertFalse(typeKind.getEdmSimpleTypeInstance().isCompatible(uriLiteral.getType()));
}
 
Example #24
Source File: Sparql.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Object execute(Map<String, EdmLiteral> parameters) throws ODataException
{
   EdmLiteral query_lit = parameters.remove("query");
   // Olingo2 checks for presence of non-nullable parameters for us!
   String query_s = query_lit.getLiteral();
   Query query = QueryFactory.create(query_s);
   if (!(query.isSelectType() || query.isDescribeType()))
   {
      throw new InvalidOperationException(query.getQueryType());
   }

   DrbCortexModel cortexmodel;
   try
   {
      cortexmodel = DrbCortexModel.getDefaultModel();
   }
   catch (IOException ex)
   {
      throw new RuntimeException(ex);
   }

   Model model = cortexmodel.getCortexModel().getOntModel();

   QueryExecution qexec = null;
   // FIXME: QueryExecution in newer versions of Jena (post apache incubation) implement AutoClosable.
   try
   {
      qexec = QueryExecutionFactory.create(query, model);
      if (query.isSelectType())
      {
         ResultSet results = qexec.execSelect();
         return ResultSetFormatter.asXMLString(results);
      }
      else
      {
         Model description = qexec.execDescribe();
         // newer version of Jena have the RIOT package for I/O
         StringWriter strwrt = new StringWriter();
         Abbreviated abb = new Abbreviated();
         abb.write(description, strwrt, null);
         return strwrt.toString();
      }
   }
   finally
   {
      if (qexec != null)
      {
         qexec.close();
      }
   }
}
 
Example #25
Source File: UriInfoImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, EdmLiteral> getFunctionImportParameters() {
  return functionImportParameters;
}
 
Example #26
Source File: AdaptableUriInfo.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Map<String, EdmLiteral> getFunctionImportParameters()
{
   return Map.class.cast(get("getFunctionImportParameters"));
}
 
Example #27
Source File: Token.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public EdmLiteral getJavaLiteral() {
  return javaLiteral;
}
 
Example #28
Source File: EdmSimpleTypeFacadeImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public EdmLiteral parseUriLiteral(String uriLiteral, EdmFacets facets) throws EdmLiteralException {
  this.facets = facets;
  return parseUriLiteral(uriLiteral);
}
 
Example #29
Source File: Tokenizer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
/**
 * Read up to single ' and move pointer to the following char and tries a type detection
 * @param curCharacter
 * @param token
 * @throws ExpressionParserException
 * @throws TokenizerException
 */
private void readLiteral(char curCharacter, String token) throws ExpressionParserException, TokenizerException {
  int offsetPos = -token.length();
  int oldPosition = curPosition;
  token = token + Character.toString(curCharacter);
  curPosition = curPosition + 1;

  boolean wasApostroph = false; // leading ' does not count
  while (curPosition < expressionLength) {
    curCharacter = expression.charAt(curPosition);

    if (curCharacter != '\'') {
      if (wasApostroph == true) {
        break;
      }

      token = token + curCharacter;
      wasApostroph = false;
    } else {
      if (wasApostroph) {
        wasApostroph = false; // a double ' is a normal character '
      } else {
        wasApostroph = true;
      }
      token = token + curCharacter;
    }
    curPosition = curPosition + 1;
  }

  if (!wasApostroph) {
    // Exception tested within TestPMparseFilterString
    throw FilterParserExceptionImpl.createTOKEN_UNDETERMINATED_STRING(oldPosition, expression);
  }

  try {
    EdmLiteral edmLiteral = typeDectector.parseUriLiteral(token);
    tokens.appendEdmTypedToken(oldPosition + offsetPos, TokenKind.SIMPLE_TYPE, token, edmLiteral);
  } catch (EdmLiteralException ex) {
    throw TokenizerException.createTYPEDECTECTION_FAILED_ON_STRING(ex, oldPosition, token);
  }
}
 
Example #30
Source File: JPAFunctionContextTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Map<String, EdmLiteral> getFunctionImportParameters() {
  Map<String, EdmLiteral> paramMap = new HashMap<String, EdmLiteral>();
  paramMap.put(PARAMNAME, new EdmLiteral(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance(), PARAMVALUE));
  return paramMap;
}