private Object initializeFunctionObject(Function function) throws ODataEdmException { Object functionOperationObject; try { functionOperationObject = function.getJavaClass().newInstance(); } catch (ReflectiveOperationException e) { throw new ODataEdmException("Error during initialization of OData Function instance: " + function.getName()); } if (!(functionOperationObject instanceof Operation)) { throw new ODataEdmException("The initialized OData Function with name: " + function.getName() + " does not implement Operation interface"); } return functionOperationObject; }
public static Object parseEnumValue(String value, EnumType enumType) throws ODataException { EnumMember member = enumType.getMember(value); if (member == null) { member = enumType.getMember(Long.valueOf(value)); } if (member == null) { throw new ODataUnmarshallingException("Enum member not found in entity data model, type: " + enumType + " for value: " + value); } @SuppressWarnings("unchecked") Class<? extends Enum> javaType = (Class<? extends Enum>) enumType.getJavaType(); try { return Enum.valueOf(javaType, member.getName()); } catch (IllegalArgumentException e) { throw new ODataEdmException("Java enum type does not match what is registered in the entity data model", e); } } }
public static EntityType getEntityType(EntityDataModel entityDataModel, Object entity) throws ODataEdmException { final Type type = entityDataModel.getType(entity.getClass()); if (type == null) { String msg = String.format("Given entity %s is not found in entity data model", entity); throw new ODataEdmException(msg); } return (EntityType) type; } }
/** * Get the entity key for a given entity by inspecting the Entity Data Model. * * @param entityDataModel The Entity Data Model. * @param entity The given entity. * @return The String representation of the entity key. * @throws ODataEdmException If unable to format entity key */ public static String formatEntityKey(EntityDataModel entityDataModel, Object entity) throws ODataEdmException { Key entityKey = getAndCheckEntityType(entityDataModel, entity.getClass()).getKey(); List<PropertyRef> keyPropertyRefs = entityKey.getPropertyRefs(); try { if (keyPropertyRefs.size() == 1) { return getKeyValueFromPropertyRef(entityDataModel, entity, keyPropertyRefs.get(0)); } else if (keyPropertyRefs.size() > 1) { List<String> processedKeys = new ArrayList<>(); for (PropertyRef propertyRef : keyPropertyRefs) { processedKeys.add(String.format("%s=%s", propertyRef.getPath(), getKeyValueFromPropertyRef(entityDataModel, entity, propertyRef))); } return processedKeys.stream().map(Object::toString).collect(Collectors.joining(",")); } else { LOG.error("Not possible to retrieve entity key for entity " + entity); throw new ODataEdmException("Entity key is not found for " + entity); } } catch (IllegalAccessException e) { LOG.error("Not possible to retrieve entity key for entity " + entity); throw new ODataEdmException("Not possible to retrieve entity key for entity " + entity, e); } }
/** * Write a feed {@code <link>} element. * * @throws XMLStreamException If unable to write to stream */ void writeFeedLink(Object entity, NavigationProperty property) throws XMLStreamException, ODataEdmException { xmlWriter.writeStartElement(ATOM_LINK); xmlWriter.writeAttribute(REL, SELF); if (entity == null) { if (ODataUriUtil.isActionCallUri(oDataUri) || ODataUriUtil.isFunctionCallUri(oDataUri)) { Option<TargetType> targetTypeOption = ODataUriUtil.resolveTargetType(oDataUri, entityDataModel); if (targetTypeOption.isDefined()) { TargetType targetType = targetTypeOption.get(); String entitySetName = EntityDataModelUtil.getEntitySetByEntityTypeName(entityDataModel, targetType.typeName()).getName(); xmlWriter.writeAttribute(TITLE, entitySetName); xmlWriter.writeAttribute(HREF, entitySetName); } else { throw new ODataEdmException("Failed to resolve entity target type"); } } else { xmlWriter.writeAttribute(TITLE, getEntitySetName(oDataUri).get()); xmlWriter.writeAttribute(HREF, getEntitySetName(oDataUri).get()); } } else { xmlWriter.writeAttribute(TITLE, property.getName()); xmlWriter.writeAttribute(HREF, String.format("%s/%s", getEntityWithKey(entity), property.getName())); } xmlWriter.writeEndElement(); }
/** * Get the entity key for a given entity by inspecting the Entity Data Model. * * @param entityDataModel The Entity Data Model. * @param entity The given entity. * @return The String representation of the entity key. * @throws ODataEdmException If unable to format entity key */ public static String formatEntityKey(EntityDataModel entityDataModel, Object entity) throws ODataEdmException { Key entityKey = getAndCheckEntityType(entityDataModel, entity.getClass()).getKey(); List<PropertyRef> keyPropertyRefs = entityKey.getPropertyRefs(); try { if (keyPropertyRefs.size() == 1) { return getKeyValueFromPropertyRef(entityDataModel, entity, keyPropertyRefs.get(0)); } else if (keyPropertyRefs.size() > 1) { List<String> processedKeys = new ArrayList<>(); for (PropertyRef propertyRef : keyPropertyRefs) { processedKeys.add(String.format("%s=%s", propertyRef.getPath(), getKeyValueFromPropertyRef(entityDataModel, entity, propertyRef))); } return processedKeys.stream().map(Object::toString).collect(Collectors.joining(",")); } else { LOG.error("Not possible to retrieve entity key for entity " + entity); throw new ODataEdmException("Entity key is not found for " + entity); } } catch (IllegalAccessException e) { LOG.error("Not possible to retrieve entity key for entity " + entity); throw new ODataEdmException("Not possible to retrieve entity key for entity " + entity, e); } }
/** * Write a feed {@code <link>} element. * * @throws XMLStreamException If unable to write to stream */ void writeFeedLink(Object entity, NavigationProperty property) throws XMLStreamException, ODataEdmException { xmlWriter.writeStartElement(ATOM_LINK); xmlWriter.writeAttribute(REL, SELF); if (entity == null) { if (ODataUriUtil.isActionCallUri(oDataUri) || ODataUriUtil.isFunctionCallUri(oDataUri)) { Option<TargetType> targetTypeOption = ODataUriUtil.resolveTargetType(oDataUri, entityDataModel); if (targetTypeOption.isDefined()) { TargetType targetType = targetTypeOption.get(); String entitySetName = EntityDataModelUtil.getEntitySetByEntityTypeName(entityDataModel, targetType.typeName()).getName(); xmlWriter.writeAttribute(TITLE, entitySetName); xmlWriter.writeAttribute(HREF, entitySetName); } else { throw new ODataEdmException("Failed to resolve entity target type"); } } else { xmlWriter.writeAttribute(TITLE, getEntitySetName(oDataUri).get()); xmlWriter.writeAttribute(HREF, getEntitySetName(oDataUri).get()); } } else { xmlWriter.writeAttribute(TITLE, property.getName()); xmlWriter.writeAttribute(HREF, String.format("%s/%s", getEntityWithKey(entity), property.getName())); } xmlWriter.writeEndElement(); }
public static Object parseEnumValue(String value, EnumType enumType) throws ODataException { EnumMember member = enumType.getMember(value); if (member == null) { member = enumType.getMember(Long.valueOf(value)); } if (member == null) { throw new ODataUnmarshallingException("Enum member not found in entity data model, type: " + enumType + " for value: " + value); } @SuppressWarnings("unchecked") Class<? extends Enum> javaType = (Class<? extends Enum>) enumType.getJavaType(); try { return Enum.valueOf(javaType, member.getName()); } catch (IllegalArgumentException e) { throw new ODataEdmException("Java enum type does not match what is registered in the entity data model", e); } } }
private Object initializeFunctionObject(Function function) throws ODataEdmException { Object functionOperationObject; try { functionOperationObject = function.getJavaClass().newInstance(); } catch (ReflectiveOperationException e) { throw new ODataEdmException("Error during initialization of OData Function instance: " + function.getName()); } if (!(functionOperationObject instanceof Operation)) { throw new ODataEdmException("The initialized OData Function with name: " + function.getName() + " does not implement Operation interface"); } return functionOperationObject; }