/** * Get the Entity Set of a given entity through the Entity Data Model. * * @param entityDataModel The Entity Data Model. * @param entity The given entity. * @return The Entity Set. * @throws ODataEdmException If unable to get entity set */ public static EntitySet getEntitySetByEntity(EntityDataModel entityDataModel, Object entity) throws ODataEdmException { return getEntitySetByEntityTypeName(entityDataModel, getAndCheckEntityType(entityDataModel, entity.getClass()).getFullyQualifiedName()); }
LOG.debug("Writing type {} of type {}", type.getName(), type.getMetaType()); try { xmlWriter.writeStartElement(ENTITY_TYPE); if (!isNullOrEmpty(type.getBaseTypeName())) { xmlWriter.writeAttribute(BASE_TYPE, type.getBaseTypeName()); if (type.isOpen()) { xmlWriter.writeAttribute(OPEN_TYPE, Boolean.toString(type.isOpen())); if (type.hasStream()) { xmlWriter.writeAttribute(HAS_STREAM, Boolean.toString(type.hasStream())); xmlWriter.writeAttribute(NAME, type.getName()); if (type.isAbstract()) { xmlWriter.writeAttribute(ABSTRACT, "true"); for (PropertyRef propertyRef : type.getKey().getPropertyRefs()) { xmlWriter.writeStartElement(PROPERTY_REF); xmlWriter.writeAttribute(NAME, propertyRef.getPath()); xmlWriter.writeEndElement(); } catch (XMLStreamException e) { throw new ODataRenderException("Error while writing entity type: " + type.getName(), e);
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; }
/** * Write a {@code <link>} element for a given entity. * * @param entity The given entity. * @throws XMLStreamException If unable to write to stream * @throws ODataEdmException if unable to check entity types */ public void writeEntryEntityLink(Object entity) throws XMLStreamException, ODataEdmException { EntityType entityType = getAndCheckEntityType(entityDataModel, entity.getClass()); xmlWriter.writeStartElement(ATOM_LINK); if (entityType.isReadOnly()) { xmlWriter.writeAttribute(REL, SELF); } else { xmlWriter.writeAttribute(REL, EDIT); } xmlWriter.writeAttribute(TITLE, entityType.getName()); xmlWriter.writeAttribute(HREF, getEntityWithKey(entity)); xmlWriter.writeEndElement(); }
public void addEntityType(Class<?> cls, EntityType entityType) { EdmSingleton singletonAnno = cls.getAnnotation(EdmSingleton.class); if (singletonAnno == null) { return; } String singletonName = singletonAnno.name(); if (isNullOrEmpty(singletonName)) { singletonName = singletonAnno.value(); if (isNullOrEmpty(singletonName)) { // Use simple name of entity type if no name for the singleton is specified singletonName = entityType.getName(); } } String entityTypeName = entityType.getFullyQualifiedName(); LOG.debug("Creating Singleton named: {} for entity type: {}", singletonName, entityTypeName); builders.put(entityTypeName, new SingletonImpl.Builder() .setName(singletonName) .setTypeName(entityTypeName)); }
/** * Gets the names of the properties that are part of the key of an entity type. * * @param entityType The entity type. * @return A {@code Set} containing the names of the key properties of the entity type. */ public static Set<String> getKeyPropertyNames(EntityType entityType) { Set<String> keyPropertyNames = entityType.getKey().getPropertyRefs().stream() .map(PropertyRef::getPath).collect(Collectors.toSet()); return keyPropertyNames; }
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 Object processEntity(Element entryElement) throws ODataException { if (!entryElement.getNodeName().equals(ATOM_ENTRY)) { throw new ODataUnmarshallingException("Expected <entry> as the root element, but found: " + entryElement.getNodeName()); } EntityType entityType = getEntityType(entryElement); Object entity; try { Class<?> javaType = entityType.getJavaType(); LOG.debug("Creating new instance of type: {}", javaType.getName()); entity = javaType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new ODataUnmarshallingException("Error while instantiating entity of type: " + entityType.getFullyQualifiedName(), e); } setEntityProperties(entity, entityType, entryElement); setEntityNavigationProperties(entity, entityType, entryElement); ensureNonNullableCollectionArePresent(entityType); return entity; }
public void addEntityType(Class<?> cls, EntityType entityType) { EdmSingleton singletonAnno = cls.getAnnotation(EdmSingleton.class); if (singletonAnno == null) { return; } String singletonName = singletonAnno.name(); if (isNullOrEmpty(singletonName)) { singletonName = singletonAnno.value(); if (isNullOrEmpty(singletonName)) { // Use simple name of entity type if no name for the singleton is specified singletonName = entityType.getName(); } } String entityTypeName = entityType.getFullyQualifiedName(); LOG.debug("Creating Singleton named: {} for entity type: {}", singletonName, entityTypeName); builders.put(entityTypeName, new SingletonImpl.Builder() .setName(singletonName) .setTypeName(entityTypeName)); }
/** * Gets the names of the properties that are part of the key of an entity type. * * @param entityType The entity type. * @return A {@code Set} containing the names of the key properties of the entity type. */ public static Set<String> getKeyPropertyNames(EntityType entityType) { Set<String> keyPropertyNames = entityType.getKey().getPropertyRefs().stream() .map(PropertyRef::getPath).collect(Collectors.toSet()); return keyPropertyNames; }
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 Object processEntity(Element entryElement) throws ODataException { if (!entryElement.getNodeName().equals(ATOM_ENTRY)) { throw new ODataUnmarshallingException("Expected <entry> as the root element, but found: " + entryElement.getNodeName()); } EntityType entityType = getEntityType(entryElement); Object entity; try { Class<?> javaType = entityType.getJavaType(); LOG.debug("Creating new instance of type: {}", javaType.getName()); entity = javaType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new ODataUnmarshallingException("Error while instantiating entity of type: " + entityType.getFullyQualifiedName(), e); } setEntityProperties(entity, entityType, entryElement); setEntityNavigationProperties(entity, entityType, entryElement); ensureNonNullableCollectionArePresent(entityType); return entity; }
/** * Write a {@code <link>} element for a given entity. * * @param entity The given entity. * @throws XMLStreamException If unable to write to stream * @throws ODataEdmException if unable to check entity types */ public void writeEntryEntityLink(Object entity) throws XMLStreamException, ODataEdmException { EntityType entityType = getAndCheckEntityType(entityDataModel, entity.getClass()); xmlWriter.writeStartElement(ATOM_LINK); if (entityType.isReadOnly()) { xmlWriter.writeAttribute(REL, SELF); } else { xmlWriter.writeAttribute(REL, EDIT); } xmlWriter.writeAttribute(TITLE, entityType.getName()); xmlWriter.writeAttribute(HREF, getEntityWithKey(entity)); xmlWriter.writeEndElement(); }
/** * Get the Entity Set of a given entity through the Entity Data Model. * * @param entityDataModel The Entity Data Model. * @param entity The given entity. * @return The Entity Set. * @throws ODataEdmException If unable to get entity set */ public static EntitySet getEntitySetByEntity(EntityDataModel entityDataModel, Object entity) throws ODataEdmException { return getEntitySetByEntityTypeName(entityDataModel, getAndCheckEntityType(entityDataModel, entity.getClass()).getFullyQualifiedName()); }
LOG.debug("Writing type {} of type {}", type.getName(), type.getMetaType()); try { xmlWriter.writeStartElement(ENTITY_TYPE); if (!isNullOrEmpty(type.getBaseTypeName())) { xmlWriter.writeAttribute(BASE_TYPE, type.getBaseTypeName()); if (type.isOpen()) { xmlWriter.writeAttribute(OPEN_TYPE, Boolean.toString(type.isOpen())); if (type.hasStream()) { xmlWriter.writeAttribute(HAS_STREAM, Boolean.toString(type.hasStream())); xmlWriter.writeAttribute(NAME, type.getName()); if (type.isAbstract()) { xmlWriter.writeAttribute(ABSTRACT, "true"); for (PropertyRef propertyRef : type.getKey().getPropertyRefs()) { xmlWriter.writeStartElement(PROPERTY_REF); xmlWriter.writeAttribute(NAME, propertyRef.getPath()); xmlWriter.writeEndElement(); } catch (XMLStreamException e) { throw new ODataRenderException("Error while writing entity type: " + type.getName(), e);
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; }
public void addEntityType(Class<?> cls, EntityType entityType) { EdmEntitySet entitySetAnno = cls.getAnnotation(EdmEntitySet.class); if (entitySetAnno == null) { return; } String entitySetName = entitySetAnno.name(); if (isNullOrEmpty(entitySetName)) { entitySetName = entitySetAnno.value(); if (isNullOrEmpty(entitySetName)) { // Use automatically pluralized simple name of entity type if no name for the entity set is specified entitySetName = pluralize(entityType.getName()); } } String entityTypeName = entityType.getFullyQualifiedName(); LOG.trace("Creating EntitySet named: {} for entity type: {}", entitySetName, entityTypeName); builders.put(entityTypeName, new EntitySetImpl.Builder() .setName(entitySetName) .setTypeName(entityTypeName) .setIsIncludedInServiceDocument(entitySetAnno.includedInServiceDocument())); }
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 String processNavigationLink(Object entity, EntityType entityType, Element linkElement) throws ODataException { String propertyName = linkElement.getAttribute(REL).substring(getODataNavLinkRelationNSPrefix().length()); LOG.debug("Found link element for navigation property: {}", propertyName); StructuralProperty property = entityType.getStructuralProperty(propertyName); if (!(property instanceof NavigationProperty)) { throw new ODataUnmarshallingException("The request contains a navigation link '" + propertyName + "' but the entity type '" + entityType + "' does not contain a navigation property " + "with this name."); } processReferencedEntity(entity, entityType, linkElement); return propertyName; }
private EntitySet getEntitySet(Object entity) { String entityTypeName = getEntityType(entity).getFullyQualifiedName(); for (EntitySet eSet : entityDataModel.getEntityContainer().getEntitySets()) { if (eSet.getTypeName().equals(entityTypeName)) { return eSet; } } return null; }