Java Code Examples for org.apache.olingo.commons.api.data.Entity#getProperty()
The following examples show how to use
org.apache.olingo.commons.api.data.Entity#getProperty() .
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: ODataAdapter.java From micro-integrator with Apache License 2.0 | 6 votes |
/** * This method returns matched entity list, where it uses in getEntity method to get the matched entity. * * @param entityType EdmEntityType * @param param UriParameter * @param entityList List of entities * @return list of entities * @throws ODataApplicationException * @throws ODataServiceFault */ private List<Entity> getMatch(EdmEntityType entityType, UriParameter param, List<Entity> entityList) throws ODataApplicationException, ODataServiceFault { ArrayList<Entity> list = new ArrayList<>(); for (Entity entity : entityList) { EdmProperty property = (EdmProperty) entityType.getProperty(param.getName()); EdmType type = property.getType(); if (type.getKind() == EdmTypeKind.PRIMITIVE) { Object match = readPrimitiveValue(property, param.getText()); Property entityValue = entity.getProperty(param.getName()); if (match != null) { if (match.equals(entityValue.asPrimitive())) { list.add(entity); } } else { if (null == entityValue.asPrimitive()) { list.add(entity); } } } else { throw new ODataServiceFault("Complex elements are not supported, couldn't compare complex objects."); } } return list; }
Example 2
Source File: ODataAdapter.java From micro-integrator with Apache License 2.0 | 6 votes |
/** * This method return the entity collection which are able to navigate from the parent entity (source) using uri navigation properties. * <p/> * In this method we check the parent entities primary keys and return the entity according to the values. * we use ODataDataHandler, navigation properties to get particular foreign keys. * * @param metadata Service Metadata * @param parentEntity parentEntity * @param navigation UriResourceNavigation * @return EntityCollection * @throws ODataServiceFault */ private EntityCollection getNavigableEntitySet(ServiceMetadata metadata, Entity parentEntity, EdmNavigationProperty navigation, String url) throws ODataServiceFault, ODataApplicationException { EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType())); String linkName = navigation.getName(); List<Property> properties = new ArrayList<>(); Map<String, EdmProperty> propertyMap = new HashMap<>(); for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(type.getName()) .getNavigationKeys(linkName)) { Property property = parentEntity.getProperty(keys.getPrimaryKey()); if (property != null && !property.isNull()) { propertyMap.put(keys.getForeignKey(), (EdmProperty) type.getProperty(property.getName())); property.setName(keys.getForeignKey()); properties.add(property); } } if(!properties.isEmpty()) { return createEntityCollectionFromDataEntryList(linkName, dataHandler .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), url); } return null; }
Example 3
Source File: Storage.java From syndesis with Apache License 2.0 | 6 votes |
private Entity createProduct(Entity entity) { // the ID of the newly created product entity is generated automatically int newId = 1; while (productIdExists(newId)) { newId++; } Property idProperty = entity.getProperty(PRODUCT_ID); if (idProperty != null) { idProperty.setValue(ValueType.PRIMITIVE, Integer.valueOf(newId)); } else { // as of OData v4 spec, the key property can be omitted from the POST request body entity.getProperties().add(new Property(null, PRODUCT_ID, ValueType.PRIMITIVE, newId)); } entity.setId(createId(ES_PRODUCTS_NAME, newId)); this.productList.add(entity); return entity; }
Example 4
Source File: Storage.java From olingo-odata4 with Apache License 2.0 | 6 votes |
private Entity createEntity(EdmEntityType edmEntityType, Entity entity, List<Entity> entityList) { // the ID of the newly created entity is generated automatically int newId = 1; while (entityIdExists(newId, entityList)) { newId++; } Property idProperty = entity.getProperty("ID"); if (idProperty != null) { idProperty.setValue(ValueType.PRIMITIVE, Integer.valueOf(newId)); } else { // as of OData v4 spec, the key property can be omitted from the POST request body entity.getProperties().add(new Property(null, "ID", ValueType.PRIMITIVE, newId)); } entity.setId(createId(entity, "ID")); entityList.add(entity); return entity; }
Example 5
Source File: DataProvider.java From olingo-odata4 with Apache License 2.0 | 6 votes |
private Object findPropertyRefValue(Entity entity, EdmKeyPropertyRef refType) { final int INDEX_ERROR_CODE = -1; final String propertyPath = refType.getName(); String tmpPropertyName; int lastIndex; int index = propertyPath.indexOf('/'); if (index == INDEX_ERROR_CODE) { index = propertyPath.length(); } tmpPropertyName = propertyPath.substring(0, index); //get first property Property prop = entity.getProperty(tmpPropertyName); //get following properties while (index < propertyPath.length()) { lastIndex = ++index; index = propertyPath.indexOf('/', index+1); if (index == INDEX_ERROR_CODE) { index = propertyPath.length(); } tmpPropertyName = propertyPath.substring(lastIndex, index); prop = findProperty(tmpPropertyName, prop.asComplex().getValue()); } return prop.getValue(); }
Example 6
Source File: ODataJsonDeserializerEntityTest.java From olingo-odata4 with Apache License 2.0 | 6 votes |
@Test public void eTMixEnumDefCollCompTest() throws Exception { InputStream stream = getFileAsStream("EntityETMixEnumDefCollComp.json"); final Entity entity = deserialize(stream, "ETMixEnumDefCollComp", ContentType.JSON); assertEquals(6, entity.getProperties().size()); Property enumProperty = entity.getProperty("PropertyEnumString"); assertNotNull(enumProperty); assertEquals((short) 2, enumProperty.getValue()); Property defProperty = entity.getProperty("PropertyDefString"); assertNotNull(defProperty); assertEquals("string", defProperty.getValue()); Property complexProperty = entity.getProperty("PropertyCompMixedEnumDef"); List<Property> value = complexProperty.asComplex().getValue(); assertEquals((short) 2, value.get(0).getValue()); defProperty = ((ComplexValue) entity.getProperty("CollPropertyCompMixedEnumDef").asCollection().get(1)) .getValue().get(2); assertEquals("string", defProperty.getValue()); stream.close(); }
Example 7
Source File: Storage.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private void updateProduct(EdmEntityType edmEntityType, List<UriParameter> keyParams, Entity entity, HttpMethod httpMethod) throws ODataApplicationException { Entity productEntity = getProduct(edmEntityType, keyParams); if (productEntity == null) { throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH); } // loop over all properties and replace the values with the values of the given payload // Note: ignoring ComplexType, as we don't have it in our odata model List<Property> existingProperties = productEntity.getProperties(); for (Property existingProp : existingProperties) { String propName = existingProp.getName(); // ignore the key properties, they aren't updateable if (isKey(edmEntityType, propName)) { continue; } Property updateProperty = entity.getProperty(propName); // the request payload might not consider ALL properties, so it can be null if (updateProperty == null) { // if a property has NOT been added to the request payload // depending on the HttpMethod, our behavior is different if (httpMethod.equals(HttpMethod.PATCH)) { // as of the OData spec, in case of PATCH, the existing property is not touched continue; // do nothing } else if (httpMethod.equals(HttpMethod.PUT)) { // as of the OData spec, in case of PUT, the existing property is set to null (or to default value) existingProp.setValue(existingProp.getValueType(), null); continue; } } // change the value of the properties existingProp.setValue(existingProp.getValueType(), updateProperty.getValue()); } }
Example 8
Source File: Storage.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private URI createId(Entity entity, String idPropertyName, String navigationName) { try { StringBuilder sb = new StringBuilder(getEntitySetName(entity)).append("("); final Property property = entity.getProperty(idPropertyName); sb.append(property.asPrimitive()).append(")"); if (navigationName != null) { sb.append("/").append(navigationName); } return new URI(sb.toString()); } catch (URISyntaxException e) { throw new ODataRuntimeException("Unable to create (Atom) id for entity: " + entity, e); } }
Example 9
Source File: Storage.java From syndesis with Apache License 2.0 | 5 votes |
private void updateProduct(EdmEntityType edmEntityType, List<UriParameter> keyParams, Entity entity, HttpMethod httpMethod) throws ODataApplicationException { Entity productEntity = getProduct(edmEntityType, keyParams); if (productEntity == null) { throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH); } // loop over all properties and replace the values with the values of the given payload // Note: ignoring ComplexType, as we don't have it in our odata model List<Property> existingProperties = productEntity.getProperties(); for (Property existingProp : existingProperties) { String propName = existingProp.getName(); // ignore the key properties, they aren't updateable if (isKey(edmEntityType, propName)) { continue; } Property updateProperty = entity.getProperty(propName); // the request payload might not consider ALL properties, so it can be null if (updateProperty == null) { // if a property has NOT been added to the request payload // depending on the HttpMethod, our behavior is different if (httpMethod.equals(HttpMethod.PATCH)) { // as of the OData spec, in case of PATCH, the existing property is not touched continue; // do nothing } else if (httpMethod.equals(HttpMethod.PUT)) { // as of the OData spec, in case of PUT, the existing property is set to null (or to default value) existingProp.setValue(existingProp.getValueType(), null); continue; } } // change the value of the properties existingProp.setValue(existingProp.getValueType(), updateProperty.getValue()); } }
Example 10
Source File: UriHelperImpl.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private Object findPropertyRefValue(Entity entity, EdmKeyPropertyRef refType) throws SerializerException { final int INDEX_ERROR_CODE = -1; final String propertyPath = refType.getName(); String tmpPropertyName; int lastIndex; int index = propertyPath.indexOf('/'); if (index == INDEX_ERROR_CODE) { index = propertyPath.length(); } tmpPropertyName = propertyPath.substring(0, index); //get first property Property prop = entity.getProperty(tmpPropertyName); //get following properties while (index < propertyPath.length()) { lastIndex = ++index; index = propertyPath.indexOf('/', index+1); if (index == INDEX_ERROR_CODE) { index = propertyPath.length(); } tmpPropertyName = propertyPath.substring(lastIndex, index); prop = findProperty(tmpPropertyName, prop.asComplex().getValue()); } if (prop == null) { throw new SerializerException("Key Value Cannot be null for property: " + propertyPath, SerializerException.MessageKeys.NULL_PROPERTY, propertyPath); } return prop.getValue(); }
Example 11
Source File: Services.java From olingo-odata4 with Apache License 2.0 | 5 votes |
@POST @Path("/ComputerDetail({entityId})/ResetComputerDetailsSpecifications") public Response actionResetComputerDetailsSpecifications( @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept, @PathParam("entityId") final String entityId, @QueryParam("$format") @DefaultValue(StringUtils.EMPTY) final String format, final String argument) { final Map.Entry<Accept, AbstractUtilities> utils = getUtilities(accept, format); if (utils.getKey() == Accept.XML || utils.getKey() == Accept.TEXT) { throw new UnsupportedMediaTypeException("Unsupported media type"); } try { final Map.Entry<String, InputStream> entityInfo = xml.readEntity("ComputerDetail", entityId, Accept.ATOM); final InputStream entity = entityInfo.getValue(); final ResWrap<Entity> container = atomDeserializer.toEntity(entity); final Entity param = xml.readEntity(utils.getKey(), IOUtils.toInputStream(argument, Constants.ENCODING)); Property property = param.getProperty("specifications"); container.getPayload().getProperty("SpecificationsBag").setValue(property.getValueType(), property.getValue()); property = param.getProperty("purchaseTime"); container.getPayload().getProperty("PurchaseDate").setValue(property.getValueType(), property.getValue()); final FSManager fsManager = FSManager.instance(); fsManager.putInMemory(xml.writeEntity(Accept.ATOM, container), fsManager.getAbsolutePath(Commons.getEntityBasePath("ComputerDetail", entityId) + Constants.get( ConstantKey.ENTITY), Accept.ATOM)); return utils.getValue().createResponse(null, null, null, utils.getKey(), Response.Status.NO_CONTENT); } catch (Exception e) { return xml.createFaultResponse(accept, e); } }
Example 12
Source File: Storage.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private URI createId(Entity entity, String idPropertyName, String navigationName) { try { StringBuilder sb = new StringBuilder(getEntitySetName(entity)).append("("); final Property property = entity.getProperty(idPropertyName); sb.append(property.asPrimitive()).append(")"); if(navigationName != null) { sb.append("/").append(navigationName); } return new URI(sb.toString()); } catch (URISyntaxException e) { throw new ODataRuntimeException("Unable to create (Atom) id for entity: " + entity, e); } }
Example 13
Source File: DemoPrimitiveProcessor.java From olingo-odata4 with Apache License 2.0 | 4 votes |
public void readPrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException { // 1. Retrieve info from URI // 1.1. retrieve the info about the requested entity set List<UriResource> resourceParts = uriInfo.getUriResourceParts(); // Note: only in our example we can rely that the first segment is the EntitySet UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); EdmEntitySet edmEntitySet = uriEntityset.getEntitySet(); // the key for the entity List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates(); // 1.2. retrieve the requested (Edm) property // the last segment is the Property UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); EdmProperty edmProperty = uriProperty.getProperty(); String edmPropertyName = edmProperty.getName(); // in our example, we know we have only primitive types in our model EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); // 2. retrieve data from backend // 2.1. retrieve the entity data, for which the property has to be read Entity entity = storage.readEntityData(edmEntitySet, keyPredicates); if (entity == null) { // Bad request throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT); } // 2.2. retrieve the property data from the entity Property property = entity.getProperty(edmPropertyName); if (property == null) { throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT); } // 3. serialize Object value = property.getValue(); if (value != null) { // 3.1. configure the serializer ODataSerializer serializer = odata.createSerializer(responseFormat); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build(); PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build(); // 3.2. serialize SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options); InputStream propertyStream = serializerResult.getContent(); //4. configure the response object response.setContent(propertyStream); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); } else { // in case there's no value for the property, we can skip the serialization response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); } }
Example 14
Source File: DemoPrimitiveProcessor.java From olingo-odata4 with Apache License 2.0 | 4 votes |
public void readPrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException { // 1. Retrieve info from URI // 1.1. retrieve the info about the requested entity set List<UriResource> resourceParts = uriInfo.getUriResourceParts(); // Note: only in our example we can rely that the first segment is the EntitySet UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); EdmEntitySet edmEntitySet = uriEntityset.getEntitySet(); // the key for the entity List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates(); // 1.2. retrieve the requested (Edm) property // the last segment is the Property UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1); EdmProperty edmProperty = uriProperty.getProperty(); String edmPropertyName = edmProperty.getName(); // in our example, we know we have only primitive types in our model EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); // 2. retrieve data from backend // 2.1. retrieve the entity data, for which the property has to be read Entity entity = storage.readEntityData(edmEntitySet, keyPredicates); if (entity == null) { // Bad request throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH); } // 2.2. retrieve the property data from the entity Property property = entity.getProperty(edmPropertyName); if (property == null) { throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH); } // 3. serialize Object value = property.getValue(); if (value != null) { // 3.1. configure the serializer ODataSerializer serializer = odata.createSerializer(responseFormat); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build(); PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build(); // 3.2. serialize SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options); InputStream propertyStream = serializerResult.getContent(); // 4. configure the response object response.setContent(propertyStream); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); } else { // in case there's no value for the property, we can skip the serialization response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); } }
Example 15
Source File: ODataJsonDeserializerEntityTest.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Test public void derivedEntityESCompCollDerivedEmptyNull() throws Exception { final String payload = "{\n" + " \"@odata.context\": \"$metadata#ESCompCollDerived/$entity\",\n" + " \"@odata.metadataEtag\":\"W/metadataETag\",\n" + " \"@odata.etag\":\"W/32767\",\n" + " \"PropertyInt16\":32767,\n" + " \"PropertyCompAno\":null,\n"+ " \"[email protected]\": \"#Collection(olingo.odata.test1.CTTwoPrimAno)\",\n" + " \"CollPropertyCompAno\": [\n" + " {\n" + " \"@odata.type\": \"#olingo.odata.test1.CTBaseAno\",\n" + " \"PropertyString\": null,\n" + " \"AdditionalPropString\": null\n" + " },\n" + " {\n" + " }\n" + " ]\n" + " \n" + "}"; final Entity entity = deserialize(payload, "ETDeriveCollComp"); Assert.assertNotNull(entity); List<Property> properties = entity.getProperties(); assertNotNull(properties); assertEquals(3, properties.size()); assertEquals("PropertyInt16=32767", properties.get(0).toString()); assertEquals("PropertyCompAno=null", properties.get(1).toString()); for (Property prop : properties) { assertNotNull(prop); if (prop.isCollection()) { Property property = entity.getProperty("CollPropertyCompAno"); assertEquals(ValueType.COLLECTION_COMPLEX, property.getValueType()); assertTrue(property.getValue() instanceof List); List<? extends Object> asCollection = property.asCollection(); assertEquals(2, asCollection.size()); for (Object arrayElement : asCollection) { assertTrue(arrayElement instanceof ComplexValue); List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue(); if(castedArrayElement.size() == 2){ assertEquals(2, castedArrayElement.size()); assertEquals("AdditionalPropString=null", castedArrayElement.get(1).toString()); }else{ assertEquals(0, castedArrayElement.size()); } } } } }
Example 16
Source File: ODataXmlDeserializerTest.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Test public void entityCompAllPrim() throws Exception { final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompAllPrim"); String payload = "<?xml version='1.0' encoding='UTF-8'?>" + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" " + "xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\" " + "xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" " + "metadata:etag=\"W/"32767"\">" + "<atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" " + "term=\"#olingo.odata.test1.ETCompAllPrim\"/>" + "<atom:content type=\"application/xml\">" + "<metadata:properties>" + "<data:PropertyInt16>32767</data:PropertyInt16>" + "<data:PropertyComp metadata:type=\"#olingo.odata.test1.CTAllPrim\">" + "<data:PropertyString>First Resource - first</data:PropertyString>" + "<data:PropertyBinary>ASNFZ4mrze8=</data:PropertyBinary>" + "<data:PropertyBoolean>true</data:PropertyBoolean>" + "<data:PropertyByte>255</data:PropertyByte>" + "<data:PropertyDate>2012-10-03</data:PropertyDate>" + "<data:PropertyDateTimeOffset>2012-10-03T07:16:23.1234567Z</data:PropertyDateTimeOffset>" + "<data:PropertyDecimal>34.27</data:PropertyDecimal>" + "<data:PropertySingle>1.79E20</data:PropertySingle>" + "<data:PropertyDouble>-1.79E19</data:PropertyDouble>" + "<data:PropertyDuration>PT6S</data:PropertyDuration>" + "<data:PropertyGuid>01234567-89ab-cdef-0123-456789abcdef</data:PropertyGuid>" + "<data:PropertyInt16>32767</data:PropertyInt16>" + "<data:PropertyInt32>2147483647</data:PropertyInt32>" + "<data:PropertyInt64>9223372036854775807</data:PropertyInt64>" + "<data:PropertySByte>127</data:PropertySByte>" + "<data:PropertyTimeOfDay>01:00:01</data:PropertyTimeOfDay>" + "</data:PropertyComp>" + "</metadata:properties>" + "</atom:content>" + "</atom:entry>"; Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), edmEntitySet.getEntityType()).getEntity(); Assert.assertEquals("olingo.odata.test1.ETCompAllPrim",result.getType()); Assert.assertEquals(2, result.getProperties().size()); Assert.assertEquals(0, result.getNavigationLinks().size()); Assert.assertEquals((short) 32767, result.getProperty("PropertyInt16").asPrimitive()); Assert.assertNotNull(result.getProperty("PropertyComp")); Property comp = result.getProperty("PropertyComp"); Assert.assertEquals("olingo.odata.test1.CTAllPrim", comp.getType()); ComplexValue cv = comp.asComplex(); Assert.assertEquals(16, cv.getValue().size()); Assert.assertEquals((short) 32767, getCVProperty(cv, "PropertyInt16").asPrimitive()); Assert.assertEquals("First Resource - first", getCVProperty(cv, "PropertyString").asPrimitive()); Assert.assertEquals((short) 255, getCVProperty(cv, "PropertyByte").asPrimitive()); Assert.assertEquals((byte) 127, getCVProperty(cv, "PropertySByte").asPrimitive()); Assert.assertEquals(2147483647, getCVProperty(cv, "PropertyInt32").asPrimitive()); Assert.assertEquals(9223372036854775807L, getCVProperty(cv, "PropertyInt64").asPrimitive()); }
Example 17
Source File: AbstractUtilities.java From olingo-odata4 with Apache License 2.0 | 4 votes |
public String getDefaultEntryKey(final String entitySetName, final Entity entity) throws IOException { try { String res; if ("OrderDetails".equals(entitySetName)) { int productID; if (entity.getProperty("OrderID") == null || entity.getProperty("ProductID") == null) { if (Commons.SEQUENCE.containsKey(entitySetName)) { productID = Commons.SEQUENCE.get(entitySetName) + 1; res = "OrderID=1" + ",ProductID=" + String.valueOf(productID); } else { throw new IOException(String.format("Unable to retrieve entity key value for %s", entitySetName)); } } else { productID = (Integer) entity.getProperty("OrderID").asPrimitive(); res = "OrderID=" + entity.getProperty("OrderID").asPrimitive() + ",ProductID=" + entity.getProperty("ProductID").asPrimitive(); } Commons.SEQUENCE.put(entitySetName, productID); } else if ("Message".equals(entitySetName)) { int messageId; if (entity.getProperty("MessageId") == null || entity.getProperty("FromUsername") == null) { if (Commons.SEQUENCE.containsKey(entitySetName)) { messageId = Commons.SEQUENCE.get(entitySetName) + 1; res = "FromUsername=1" + ",MessageId=" + String.valueOf(messageId); } else { throw new IOException(String.format("Unable to retrieve entity key value for %s", entitySetName)); } } else { messageId = (Integer) entity.getProperty("MessageId").asPrimitive(); res = "FromUsername=" + entity.getProperty("FromUsername").asPrimitive() + ",MessageId=" + entity.getProperty("MessageId").asPrimitive(); } Commons.SEQUENCE.put(entitySetName, messageId); } else if ("PersonDetails".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "PersonID"); } else if ("Order".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "OrderId"); } else if ("Product".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "ProductId"); } else if ("Orders".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "OrderID"); } else if ("Customer".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "CustomerId"); } else if ("Customers".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "PersonID"); } else if ("Person".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "PersonId"); } else if ("ComputerDetail".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "ComputerDetailId"); } else if ("AllGeoTypesSet".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "Id"); } else if ("CustomerInfo".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "CustomerInfoId"); } else if ("Car".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "VIN"); } else if ("RowIndex".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "Id"); } else if ("Login".equals(entitySetName)) { res = (String) entity.getProperty("Username").asPrimitive(); } else if ("Products".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "ProductID"); } else if ("ProductDetails".equals(entitySetName)) { int productId; int productDetailId; if (entity.getProperty("ProductID") == null || entity.getProperty("ProductDetailID") == null) { if (Commons.SEQUENCE.containsKey(entitySetName) && Commons.SEQUENCE.containsKey("Products")) { productId = Commons.SEQUENCE.get("Products") + 1; productDetailId = Commons.SEQUENCE.get(entitySetName) + 1; res = "ProductID=" + String.valueOf(productId) + ",ProductDetailID=" + String.valueOf(productDetailId); } else { throw new IOException(String.format("Unable to retrieve entity key value for %s", entitySetName)); } Commons.SEQUENCE.put(entitySetName, productDetailId); } else { productId = (Integer) entity.getProperty("ProductID").asPrimitive(); productDetailId = (Integer) entity.getProperty("ProductDetailID").asPrimitive(); res = "ProductID=" + entity.getProperty("ProductID").asPrimitive() + ",ProductDetailID=" + entity.getProperty("ProductDetailID").asPrimitive(); } Commons.SEQUENCE.put(entitySetName, productDetailId); Commons.SEQUENCE.put("Products", productId); } else if ("PaymentInstrument".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "PaymentInstrumentID"); } else if ("Advertisements".equals(entitySetName)) { res = UUID.randomUUID().toString(); } else if ("People".equals(entitySetName)) { res = getDefaultEntryKey(entitySetName, entity, "PersonID"); } else { throw new IOException(String.format("EntitySet '%s' not found", entitySetName)); } return res; } catch (Exception e) { throw new NotFoundException(e); } }
Example 18
Source File: ODataXmlDeserializerTest.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Test public void derivedEntityESCompCollDerivedNullEmpty() throws Exception { final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollDerived"); final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<a:entry xmlns:a=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" m:context=\"$metadata#ESCompCollDerived/$entity\"\n" + "m:metadata-etag=\"W/"1c2796fd-da13-4741-9da2-99cac365f296"\">\n" + " <a:id>ESCompCollDerived(32767)</a:id>\n" + " <a:title/>\n" + " <a:summary/>\n" + " <a:updated>2017-07-18T13:18:13Z</a:updated>\n" + " <a:author>\n" + " <a:name/>\n" + " </a:author>\n" + " <a:link rel=\"edit\" href=\"ESCompCollDerived(32767)\"/>\n" + " <a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" + " term=\"#olingo.odata.test1.ETDeriveCollComp\"/>\n" + " <a:content type=\"application/xml\">\n" + " <m:properties>\n" + " <d:PropertyInt16 m:type=\"Int16\">32767</d:PropertyInt16>\n" + " <d:CollPropertyCompAno>\n" + " <m:element m:type=\"#olingo.odata.test1.CTBaseAno\">\n" + " <d:PropertyString/>\n" + " <d:AdditionalPropString/>\n" + " </m:element>\n" + " <m:element m:type=\"#olingo.odata.test1.CTBaseAno\">\n" + " </m:element>\n" + " </d:CollPropertyCompAno>\n" + " </m:properties>\n" + " </a:content>\n" + "</a:entry>\n" ; Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), edmEntitySet.getEntityType()).getEntity(); Assert.assertEquals(2, result.getProperties().size()); Assert.assertEquals(0, result.getNavigationLinks().size()); Property comp = result.getProperty("CollPropertyCompAno"); Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrimAno)", comp.getType()); List<? extends Object> cv = comp.asCollection(); Assert.assertEquals(2, cv.size()); for (Object arrayElement : cv) { assertTrue(arrayElement instanceof ComplexValue); List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue(); if(castedArrayElement.size()>0){ assertEquals(2, castedArrayElement.size()); assertNull(castedArrayElement.get(0).getValue()); } } }
Example 19
Source File: ODataXmlDeserializerTest.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Test public void entityMixPrimCollComp() throws Exception { final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp"); final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" + " xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + " xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" \n" + " metadata:metadata-etag=\"WmetadataETag\">\n" + " <atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" + " term=\"#olingo.odata.test1.ETMixPrimCollComp\" />\n" + " <atom:content type=\"application/xml\">\n" + " <metadata:properties>\n" + " <data:PropertyInt16>32767</data:PropertyInt16>\n" + " <data:CollPropertyString type=\"#Collection(String)\">\n" + " <metadata:element>[email protected]</metadata:element>\n" + " <metadata:element>[email protected]</metadata:element>\n" + " <metadata:element>[email protected]</metadata:element>\n" + " </data:CollPropertyString>\n" + " <data:PropertyComp metadata:type=\"#olingo.odata.test1.CTTwoPrim\">\n" + " <data:PropertyInt16>111</data:PropertyInt16>\n" + " <data:PropertyString>TEST A</data:PropertyString>\n" + " </data:PropertyComp>\n" + " <data:CollPropertyComp metadata:type=\"#Collection(olingo.odata.test1.CTTwoPrim)\">\n" + " <metadata:element>\n" + " <data:PropertyInt16>123</data:PropertyInt16>\n" + " <data:PropertyString>TEST 1</data:PropertyString>\n" + " </metadata:element>\n" + " <metadata:element>\n" + " <data:PropertyInt16>456</data:PropertyInt16>\n" + " <data:PropertyString>TEST 2</data:PropertyString>\n" + " </metadata:element>\n" + " <metadata:element>\n" + " <data:PropertyInt16>789</data:PropertyInt16>\n" + " <data:PropertyString>TEST 3</data:PropertyString>\n" + " </metadata:element>\n" + " </data:CollPropertyComp>\n" + " </metadata:properties>\n" + " </atom:content>\n" + "</atom:entry>\n"; Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), edmEntitySet.getEntityType()).getEntity(); Assert.assertEquals(4, result.getProperties().size()); Assert.assertEquals(0, result.getNavigationLinks().size()); Assert.assertEquals(Arrays.asList("[email protected]", "[email protected]", "[email protected]"), result.getProperty("CollPropertyString").getValue()); Property comp = result.getProperty("PropertyComp"); Assert.assertEquals("olingo.odata.test1.CTTwoPrim", comp.getType()); ComplexValue cv = comp.asComplex(); Assert.assertEquals(2, cv.getValue().size()); Assert.assertEquals((short) 111, getCVProperty(cv, "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST A", getCVProperty(cv, "PropertyString").asPrimitive()); comp = result.getProperty("CollPropertyComp"); Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrim)", comp.getType()); List<?> properties = comp.asCollection(); Assert.assertEquals(3, properties.size()); Assert.assertEquals((short) 123, getCVProperty((ComplexValue) properties.get(0), "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST 1", getCVProperty((ComplexValue) properties.get(0), "PropertyString").asPrimitive()); Assert.assertEquals((short) 789, getCVProperty((ComplexValue) properties.get(2), "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST 3", getCVProperty((ComplexValue) properties.get(2), "PropertyString").asPrimitive()); }
Example 20
Source File: ODataXmlDeserializerTest.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Test public void derivedEntityMixPrimCollComp() throws Exception { final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp"); final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" + " xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + " xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" \n" + " metadata:metadata-etag=\"WmetadataETag\">\n" + " <atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" + " term=\"#olingo.odata.test1.ETMixPrimCollComp\" />\n" + " <atom:content type=\"application/xml\">\n" + " <metadata:properties>\n" + " <data:PropertyInt16>32767</data:PropertyInt16>\n" + " <data:CollPropertyString type=\"#Collection(String)\">\n" + " <metadata:element>[email protected]</metadata:element>\n" + " <metadata:element>[email protected]</metadata:element>\n" + " <metadata:element>[email protected]</metadata:element>\n" + " </data:CollPropertyString>\n" + " <data:PropertyComp metadata:type=\"#olingo.odata.test1.CTBase\">\n" + " <data:PropertyInt16>111</data:PropertyInt16>\n" + " <data:PropertyString>TEST A</data:PropertyString>\n" + " <data:AdditionalPropString>Additional</data:AdditionalPropString>\n" + " </data:PropertyComp>\n" + " <data:CollPropertyComp metadata:type=\"#Collection(olingo.odata.test1.CTTwoPrim)\">\n" + " <metadata:element metadata:type=\"olingo.odata.test1.CTBase\">\n" + " <data:PropertyInt16>123</data:PropertyInt16>\n" + " <data:PropertyString>TEST 1</data:PropertyString>\n" + " <data:AdditionalPropString>Additional test</data:AdditionalPropString>\n" + " </metadata:element>\n" + " <metadata:element>\n" + " <data:PropertyInt16>456</data:PropertyInt16>\n" + " <data:PropertyString>TEST 2</data:PropertyString>\n" + " </metadata:element>\n" + " <metadata:element>\n" + " <data:PropertyInt16>789</data:PropertyInt16>\n" + " <data:PropertyString>TEST 3</data:PropertyString>\n" + " </metadata:element>\n" + " </data:CollPropertyComp>\n" + " </metadata:properties>\n" + " </atom:content>\n" + "</atom:entry>\n"; Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), edmEntitySet.getEntityType()).getEntity(); Assert.assertEquals(4, result.getProperties().size()); Assert.assertEquals(0, result.getNavigationLinks().size()); Assert.assertEquals(Arrays.asList("[email protected]", "[email protected]", "[email protected]"), result.getProperty("CollPropertyString").getValue()); Property comp = result.getProperty("PropertyComp"); Assert.assertEquals("olingo.odata.test1.CTBase", comp.getType()); ComplexValue cv = comp.asComplex(); Assert.assertEquals(3, cv.getValue().size()); Assert.assertEquals((short) 111, getCVProperty(cv, "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST A", getCVProperty(cv, "PropertyString").asPrimitive()); Assert.assertEquals("Additional", getCVProperty(cv, "AdditionalPropString").asPrimitive()); comp = result.getProperty("CollPropertyComp"); Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrim)", comp.getType()); List<?> properties = comp.asCollection(); Assert.assertEquals(3, properties.size()); Assert.assertEquals((short) 123, getCVProperty((ComplexValue) properties.get(0), "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST 1", getCVProperty((ComplexValue) properties.get(0), "PropertyString").asPrimitive()); Assert.assertEquals("Additional test", getCVProperty((ComplexValue) properties.get(0), "AdditionalPropString") .asPrimitive()); Assert.assertEquals((short) 789, getCVProperty((ComplexValue) properties.get(2), "PropertyInt16").asPrimitive()); Assert.assertEquals("TEST 3", getCVProperty((ComplexValue) properties.get(2), "PropertyString").asPrimitive()); }