private static Map<String, Object> getKeyValues(Object entity, EntityType entityType) throws ODataProcessorException { Map<String, Object> keyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { try { Field keyField = entity.getClass().getDeclaredField(propertyRef.getPath()); keyField.setAccessible(true); keyValues.put(propertyRef.getPath(), keyField.get(entity)); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ODataProcessorException(PROCESSOR_ERROR, "Not possible to extract the key/values from the entity", e); } } return keyValues; }
private static Map<String, Object> getKeyValues(Object entity, EntityType entityType) throws ODataProcessorException { Map<String, Object> keyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { try { Field keyField = entity.getClass().getDeclaredField(propertyRef.getPath()); keyField.setAccessible(true); keyValues.put(propertyRef.getPath(), keyField.get(entity)); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ODataProcessorException(PROCESSOR_ERROR, "Not possible to extract the key/values from the entity", e); } } return keyValues; }
private static String getKeyValueFromPropertyRef(EntityDataModel entityDataModel, Object entity, PropertyRef propertyRef) throws IllegalAccessException, ODataEdmException { EntityType entityType = getAndCheckEntityType(entityDataModel, entity.getClass()); Field field = entityType.getStructuralProperty(propertyRef.getPath()).getJavaField(); field.setAccessible(true); Object value = field.get(entity); if (value instanceof String) { return String.format("'%s'", ((String) value).replaceAll("'", "''")); } else if (value instanceof Period) { return String.format("duration'%s'", value.toString()); } else { return value != null ? value.toString() : null; } }
private static String getKeyValueFromPropertyRef(EntityDataModel entityDataModel, Object entity, PropertyRef propertyRef) throws IllegalAccessException, ODataEdmException { EntityType entityType = getAndCheckEntityType(entityDataModel, entity.getClass()); Field field = entityType.getStructuralProperty(propertyRef.getPath()).getJavaField(); field.setAccessible(true); Object value = field.get(entity); if (value instanceof String) { return String.format("'%s'", ((String) value).replaceAll("'", "''")); } else if (value instanceof Period) { return String.format("duration'%s'", value.toString()); } else { return value != null ? value.toString() : null; } }
private Map<String, Object> getKeyValues(Object entity, EntityType entityType) throws ODataProcessorException { Map<String, Object> keyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { try { String keyFieldName = entityType.getStructuralProperty(propertyRef.getPath()).getJavaField().getName(); Field keyField = entity.getClass().getDeclaredField(keyFieldName); keyField.setAccessible(true); keyValues.put(keyFieldName, keyField.get(entity)); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ODataProcessorException(PROCESSOR_ERROR, "Not possible to extract the key/values from the entity", e); } } return keyValues; }
private Map<String, Object> getKeyValues(Object entity, EntityType entityType) throws ODataProcessorException { Map<String, Object> keyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { try { String keyFieldName = entityType.getStructuralProperty(propertyRef.getPath()).getJavaField().getName(); Field keyField = entity.getClass().getDeclaredField(keyFieldName); keyField.setAccessible(true); keyValues.put(keyFieldName, keyField.get(entity)); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ODataProcessorException(PROCESSOR_ERROR, "Not possible to extract the key/values from the entity", e); } } return keyValues; }
/** * 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); } }
/** * Gets the values of the properties that part of the key of an entity type. * * @param entityType The entity type. * @param entity The entity. * @return A {@code Map} containing the values of the key properties of the entity, mapped by property name. */ public static Map<String, Object> getKeyPropertyValues(EntityType entityType, Object entity) { Map<String, Object> keyPropertyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { String propertyName = propertyRef.getPath(); Object propertyValue = getPropertyValue(entityType.getStructuralProperty(propertyName), entity); keyPropertyValues.put(propertyName, propertyValue); } return keyPropertyValues; }
/** * Gets the values of the properties that part of the key of an entity type. * * @param entityType The entity type. * @param entity The entity. * @return A {@code Map} containing the values of the key properties of the entity, mapped by property name. */ public static Map<String, Object> getKeyPropertyValues(EntityType entityType, Object entity) { Map<String, Object> keyPropertyValues = new HashMap<>(); for (PropertyRef propertyRef : entityType.getKey().getPropertyRefs()) { String propertyName = propertyRef.getPath(); Object propertyValue = getPropertyValue(entityType.getStructuralProperty(propertyName), entity); keyPropertyValues.put(propertyName, propertyValue); } return keyPropertyValues; }
/** * 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); } }
for (PropertyRef propertyRef : type.getKey().getPropertyRefs()) { xmlWriter.writeStartElement(PROPERTY_REF); xmlWriter.writeAttribute(NAME, propertyRef.getPath()); xmlWriter.writeEndElement();
for (PropertyRef propertyRef : type.getKey().getPropertyRefs()) { xmlWriter.writeStartElement(PROPERTY_REF); xmlWriter.writeAttribute(NAME, propertyRef.getPath()); xmlWriter.writeEndElement();