diff --git a/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 4e4567beb..6cd0cad91 100755 --- a/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -83,7 +83,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); + EntityCollectionSerializerOptions opts = + EntityCollectionSerializerOptions.with().setId(id).contextURL(contextUrl).build(); SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts); // Finally: configure the response object: set the body, headers and status code diff --git a/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/data/Storage.java index d5d16c299..a8374b491 100755 --- a/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.uri.UriParameter; @@ -106,20 +109,36 @@ public class Storage { private void initSampleData(){ // add some sample product entities - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + final Entity e1 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); + e1.setId(createId("Products", 1)); + productList.add(e1); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + final Entity e2 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + e2.setId(createId("Products", 1)); + productList.add(e2); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + final Entity e3 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); + e3.setId(createId("Products", 1)); + productList.add(e3); + } + private URI createId(String entitySetName, Object id) { + try { + return new URI(entitySetName + "(" + String.valueOf(id) + ")"); + } catch (URISyntaxException e) { + throw new ODataRuntimeException("Unable to create id for entity: " + entitySetName, e); + } } } diff --git a/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 9b6e25abd..f02ebdd06 100755 --- a/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p2_readep/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -76,7 +76,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); + EntityCollectionSerializerOptions opts = + EntityCollectionSerializerOptions.with().setId(id).contextURL(contextUrl).build(); SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, opts); InputStream serializedContent = serializerResult.getContent(); diff --git a/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/data/Storage.java index ad5dd71c4..e078462cc 100755 --- a/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/data/Storage.java @@ -27,11 +27,14 @@ import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.uri.UriParameter; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -235,29 +238,39 @@ public class Storage { return false; } - private void initSampleData() { + private void initSampleData(){ // add some sample product entities - productList.add(new Entity() + final Entity e1 = new Entity() .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) - .addProperty( - new Property(null, "Description", ValueType.PRIMITIVE, - "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); + e1.setId(createId("Products", 1)); + productList.add(e1); - productList.add(new Entity() + final Entity e2 = new Entity() .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) - .addProperty( - new Property(null, "Description", ValueType.PRIMITIVE, - "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + e2.setId(createId("Products", 1)); + productList.add(e2); - productList.add(new Entity() + final Entity e3 = new Entity() .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) - .addProperty( - new Property(null, "Description", ValueType.PRIMITIVE, - "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); + e3.setId(createId("Products", 1)); + productList.add(e3); + } + private URI createId(String entitySetName, Object id) { + try { + return new URI(entitySetName + "(" + String.valueOf(id) + ")"); + } catch (URISyntaxException e) { + throw new ODataRuntimeException("Unable to create id for entity: " + entitySetName, e); + } } } diff --git a/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index bae1ec584..77614e3be 100755 --- a/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p3_write/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -80,7 +80,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); + EntityCollectionSerializerOptions opts = + EntityCollectionSerializerOptions.with().setId(id).contextURL(contextUrl).build(); SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts); // Finally: configure the response object: set the body, headers and status code diff --git a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/data/Storage.java index 2034fe6fd..9b2d342ab 100755 --- a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.server.api.uri.UriParameter; public class Storage { @@ -184,6 +187,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -192,6 +196,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Professional, 2.8GHz - 15 XGA - 8GB DDR3 RAM - 500GB")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -200,6 +205,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -208,6 +214,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "32 GB Digital Assitant with high-resolution color screen")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -216,6 +223,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -224,6 +232,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Optimum Hi-Resolution max. 1600 x 1200 @ 85Hz, Dot Pitch: 0.24mm")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); } @@ -234,19 +243,48 @@ public class Storage { entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebooks")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); entity = new Entity(); entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Organizers")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); entity = new Entity(); entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Monitors")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); } + private URI createId(Entity entity, String idPropertyName) { + return createId(entity, idPropertyName, null); + } + + 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); + } + } + + private String getEntitySetName(Entity entity) { + if(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_CATEGORIES_NAME; + } else if(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_PRODUCTS_NAME; + } + return entity.getType(); + } } diff --git a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 5a87c0d1e..b11d3a0d4 100755 --- a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -132,7 +132,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor // 3rd: create and configure a serializer ContextURL contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName(); + EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with() + .contextURL(contextUrl).setId(id).build(); EdmEntityType edmEntityType = responseEdmEntitySet.getEntityType(); ODataSerializer serializer = odata.createSerializer(responseFormat); diff --git a/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/data/Storage.java index 2aa9fac72..794940cd6 100755 --- a/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.uri.UriParameter; @@ -108,23 +111,36 @@ public class Storage { private void initSampleData(){ // add some sample product entities - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, - "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + final Entity e1 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); + e1.setId(createId("Products", 1)); + productList.add(e1); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, - "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + final Entity e2 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + e2.setId(createId("Products", 1)); + productList.add(e2); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, - "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + final Entity e3 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); + e3.setId(createId("Products", 1)); + productList.add(e3); + } + private URI createId(String entitySetName, Object id) { + try { + return new URI(entitySetName + "(" + String.valueOf(id) + ")"); + } catch (URISyntaxException e) { + throw new ODataRuntimeException("Unable to create id for entity: " + entitySetName, e); + } } } diff --git a/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index ee3558ec9..a33cd73a5 100755 --- a/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p5_queryoptions-tcs/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -133,8 +133,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); EntityCollectionSerializerOptions opts = - EntityCollectionSerializerOptions.with().contextURL(contextUrl).count(countOption).build(); + EntityCollectionSerializerOptions.with().contextURL(contextUrl).setId(id).count(countOption).build(); SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, returnEntityCollection, opts); InputStream serializedContent = serializerResult.getContent(); diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java index 2034fe6fd..4fd1ada79 100755 --- a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.server.api.uri.UriParameter; public class Storage { @@ -101,6 +104,7 @@ public class Storage { if (sourceEntityFqn.equals(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()) && relatedEntityFqn.equals(DemoEdmProvider.ET_CATEGORY_FQN)) { + navigationTargetEntityCollection.setId(createId(sourceEntity, "ID", DemoEdmProvider.NAV_TO_CATEGORY)); // relation Products->Category (result all categories) int productID = (Integer) sourceEntity.getProperty("ID").getValue(); if (productID == 1 || productID == 2) { @@ -112,6 +116,7 @@ public class Storage { } } else if (sourceEntityFqn.equals(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()) && relatedEntityFqn.equals(DemoEdmProvider.ET_PRODUCT_FQN)) { + navigationTargetEntityCollection.setId(createId(sourceEntity, "ID", DemoEdmProvider.NAV_TO_PRODUCTS)); // relation Category->Products (result all products) int categoryID = (Integer) sourceEntity.getProperty("ID").getValue(); if (categoryID == 1) { @@ -184,6 +189,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -192,6 +198,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Professional, 2.8GHz - 15 XGA - 8GB DDR3 RAM - 500GB")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -200,6 +207,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -208,6 +216,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "32 GB Digital Assitant with high-resolution color screen")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -216,6 +225,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); entity = new Entity(); @@ -224,6 +234,7 @@ public class Storage { entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Optimum Hi-Resolution max. 1600 x 1200 @ 85Hz, Dot Pitch: 0.24mm")); entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); productList.add(entity); } @@ -234,19 +245,48 @@ public class Storage { entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebooks")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); entity = new Entity(); entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Organizers")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); entity = new Entity(); entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Monitors")); entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()); + entity.setId(createId(entity, "ID")); categoryList.add(entity); } + private URI createId(Entity entity, String idPropertyName) { + return createId(entity, idPropertyName, null); + } + + 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); + } + } + + private String getEntitySetName(Entity entity) { + if(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_CATEGORIES_NAME; + } else if(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_PRODUCTS_NAME; + } + return entity.getType(); + } } diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java index 8ce9baced..4ffd461a2 100755 --- a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java @@ -54,6 +54,8 @@ public class DemoEdmProvider extends CsdlAbstractEdmProvider { // Entity Set Names public static final String ES_PRODUCTS_NAME = "Products"; public static final String ES_CATEGORIES_NAME = "Categories"; + public static final String NAV_TO_CATEGORY = "Category"; + public static final String NAV_TO_PRODUCTS = "Products"; @Override public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) { diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 14ac8cbce..eb6f23b6b 100755 --- a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -138,16 +138,19 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor Link link = new Link(); link.setTitle(navPropName); link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE); + link.setRel(Constants.NS_ASSOCIATION_LINK_REL + navPropName); if (edmNavigationProperty.isCollection()) { // in case of Categories/$expand=Products // fetch the data for the $expand (to-many navigation) from backend EntityCollection expandEntityCollection = storage.getRelatedEntityCollection(entity, expandEdmEntityType); link.setInlineEntitySet(expandEntityCollection); + link.setHref(expandEntityCollection.getId().toASCIIString()); } else { // in case of Products?$expand=Category // fetch the data for the $expand (to-one navigation) from backend // here we get the data for the expand Entity expandEntity = storage.getRelatedEntity(entity, expandEdmEntityType); link.setInlineEntity(expandEntity); + link.setHref(expandEntity.getId().toASCIIString()); } // set the link - containing the expanded data - to the current entity @@ -163,10 +166,12 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).selectList(selectList).build(); // adding the selectOption to the serializerOpts will actually tell the lib to do the job + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with() .contextURL(contextUrl) .select(selectOption) .expand(expandOption) + .setId(id) .build(); ODataSerializer serializer = odata.createSerializer(responseFormat); diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java index 6b623b08f..18372aa6b 100755 --- a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java @@ -133,17 +133,20 @@ public class DemoEntityProcessor implements EntityProcessor { Link link = new Link(); link.setTitle(navPropName); link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE); + link.setRel(Constants.NS_ASSOCIATION_LINK_REL + navPropName); if(edmNavigationProperty.isCollection()){ // in case of Categories(1)/$expand=Products // fetch the data for the $expand (to-many navigation) from backend // here we get the data for the expand EntityCollection expandEntityCollection = storage.getRelatedEntityCollection(entity, expandEdmEntityType); link.setInlineEntitySet(expandEntityCollection); + link.setHref(expandEntityCollection.getId().toASCIIString()); } else { // in case of Products(1)?$expand=Category // fetch the data for the $expand (to-one navigation) from backend // here we get the data for the expand Entity expandEntity = storage.getRelatedEntity(entity, expandEdmEntityType); link.setInlineEntity(expandEntity); + link.setHref(expandEntity.getId().toASCIIString()); } // set the link - containing the expanded data - to the current entity diff --git a/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/data/Storage.java index d5d16c299..a8374b491 100644 --- a/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.uri.UriParameter; @@ -106,20 +109,36 @@ public class Storage { private void initSampleData(){ // add some sample product entities - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + final Entity e1 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); + e1.setId(createId("Products", 1)); + productList.add(e1); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + final Entity e2 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + e2.setId(createId("Products", 1)); + productList.add(e2); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + final Entity e3 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); + e3.setId(createId("Products", 1)); + productList.add(e3); + } + private URI createId(String entitySetName, Object id) { + try { + return new URI(entitySetName + "(" + String.valueOf(id) + ")"); + } catch (URISyntaxException e) { + throw new ODataRuntimeException("Unable to create id for entity: " + entitySetName, e); + } } } diff --git a/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 5b5a5eb59..48f4660d0 100644 --- a/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p7_queryoptions-o/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -134,7 +134,9 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); + EntityCollectionSerializerOptions opts = + EntityCollectionSerializerOptions.with().setId(id).contextURL(contextUrl).build(); SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, opts); InputStream serializedContent = serializerResult.getContent(); diff --git a/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/data/Storage.java index d5d16c299..a8374b491 100644 --- a/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/data/Storage.java @@ -18,6 +18,8 @@ */ package myservice.mynamespace.data; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -31,6 +33,7 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.uri.UriParameter; @@ -106,20 +109,36 @@ public class Storage { private void initSampleData(){ // add some sample product entities - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + final Entity e1 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")); + e1.setId(createId("Products", 1)); + productList.add(e1); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + final Entity e2 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + e2.setId(createId("Products", 1)); + productList.add(e2); - productList.add(new Entity() - .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) - .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) - .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + final Entity e3 = new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")); + e3.setId(createId("Products", 1)); + productList.add(e3); + } + private URI createId(String entitySetName, Object id) { + try { + return new URI(entitySetName + "(" + String.valueOf(id) + ")"); + } catch (URISyntaxException e) { + throw new ODataRuntimeException("Unable to create id for entity: " + entitySetName, e); + } } } diff --git a/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java index 861b78658..140d0e760 100644 --- a/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java +++ b/samples/tutorials/p8_queryoptions-f/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -122,9 +122,12 @@ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor EdmEntityType edmEntityType = edmEntitySet.getEntityType(); ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); - EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); - SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, - entityCollection, opts); + final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName(); + EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with() + .contextURL(contextUrl).setId(id).build(); + SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, + opts); + InputStream serializedContent = serializerResult.getContent(); // 5th: configure the response object: set the body, headers and status code