From b477bde07b190717b0e6ef443efcef1ca1b3d57e Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Mon, 27 Jul 2015 09:57:17 +0200 Subject: [PATCH] [OLINGO-713] Added tutorial project for system query options - expand/select --- .../pom.xml | 2 +- .../myservice/mynamespace/data/Storage.java | 0 .../mynamespace/service/DemoEdmProvider.java | 0 .../DemoEntityCollectionProcessor.java | 0 .../service/DemoEntityProcessor.java | 0 .../service/DemoPrimitiveProcessor.java | 0 .../java/myservice/mynamespace/util/Util.java | 0 .../mynamespace/web/DemoServlet.java | 0 .../src/main/webapp/WEB-INF/web.xml | 0 .../src/main/webapp/index.jsp | 0 samples/tutorials/p6_queryoptions-es/pom.xml | 80 ++++++ .../myservice/mynamespace/data/Storage.java | 252 ++++++++++++++++++ .../mynamespace/service/DemoEdmProvider.java | 211 +++++++++++++++ .../DemoEntityCollectionProcessor.java | 160 +++++++++++ .../service/DemoEntityProcessor.java | 235 ++++++++++++++++ .../service/DemoPrimitiveProcessor.java | 150 +++++++++++ .../java/myservice/mynamespace/util/Util.java | 149 +++++++++++ .../mynamespace/web/DemoServlet.java | 75 ++++++ .../src/main/webapp/WEB-INF/web.xml | 34 +++ .../src/main/webapp/index.jsp | 43 +++ samples/tutorials/pom.xml | 3 +- 21 files changed, 1392 insertions(+), 2 deletions(-) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/pom.xml (97%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/data/Storage.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/util/Util.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/java/myservice/mynamespace/web/DemoServlet.java (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/webapp/WEB-INF/web.xml (100%) rename samples/tutorials/{p5_queryoptions => p5_queryoptions-tis}/src/main/webapp/index.jsp (100%) create mode 100755 samples/tutorials/p6_queryoptions-es/pom.xml create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/web/DemoServlet.java create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/webapp/WEB-INF/web.xml create mode 100755 samples/tutorials/p6_queryoptions-es/src/main/webapp/index.jsp diff --git a/samples/tutorials/p5_queryoptions/pom.xml b/samples/tutorials/p5_queryoptions-tis/pom.xml similarity index 97% rename from samples/tutorials/p5_queryoptions/pom.xml rename to samples/tutorials/p5_queryoptions-tis/pom.xml index 117f59da5..db1e18c4b 100755 --- a/samples/tutorials/p5_queryoptions/pom.xml +++ b/samples/tutorials/p5_queryoptions-tis/pom.xml @@ -23,7 +23,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 my.group.id - DemoService-QueryOptions + DemoService-QueryOptions-TIS war 0.0.1 diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/data/Storage.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/data/Storage.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/data/Storage.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/util/Util.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/util/Util.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/util/Util.java diff --git a/samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/web/DemoServlet.java b/samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/web/DemoServlet.java similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/java/myservice/mynamespace/web/DemoServlet.java rename to samples/tutorials/p5_queryoptions-tis/src/main/java/myservice/mynamespace/web/DemoServlet.java diff --git a/samples/tutorials/p5_queryoptions/src/main/webapp/WEB-INF/web.xml b/samples/tutorials/p5_queryoptions-tis/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/webapp/WEB-INF/web.xml rename to samples/tutorials/p5_queryoptions-tis/src/main/webapp/WEB-INF/web.xml diff --git a/samples/tutorials/p5_queryoptions/src/main/webapp/index.jsp b/samples/tutorials/p5_queryoptions-tis/src/main/webapp/index.jsp similarity index 100% rename from samples/tutorials/p5_queryoptions/src/main/webapp/index.jsp rename to samples/tutorials/p5_queryoptions-tis/src/main/webapp/index.jsp diff --git a/samples/tutorials/p6_queryoptions-es/pom.xml b/samples/tutorials/p6_queryoptions-es/pom.xml new file mode 100755 index 000000000..bb095adc7 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/pom.xml @@ -0,0 +1,80 @@ + + + + 4.0.0 + my.group.id + DemoService-QueryOptions-ES + war + 0.0.1 + + ${project.artifactId}-Webapp + + + DemoService + + + + 2.5 + 4.0.0-beta-03 + 1.7.7 + + + + + javax.servlet + servlet-api + ${javax.version} + provided + + + + org.apache.olingo + odata-server-api + ${odata.version} + + + org.apache.olingo + odata-server-core + ${odata.version} + runtime + + + + org.apache.olingo + odata-commons-api + ${odata.version} + + + org.apache.olingo + odata-commons-core + ${odata.version} + + + + org.slf4j + slf4j-simple + ${slf4j.version} + runtime + + + 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 new file mode 100755 index 000000000..2034fe6fd --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/data/Storage.java @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.data; + +import java.util.ArrayList; +import java.util.List; + +import myservice.mynamespace.service.DemoEdmProvider; +import myservice.mynamespace.util.Util; + +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +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.edm.FullQualifiedName; +import org.apache.olingo.server.api.uri.UriParameter; + +public class Storage { + + // represent our database + private List productList; + private List categoryList; + + public Storage() { + + productList = new ArrayList(); + categoryList = new ArrayList(); + + // creating some sample data + initProductSampleData(); + initCategorySampleData(); + } + + /* PUBLIC FACADE */ + + public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet) { + EntityCollection entitySet = null; + + if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) { + entitySet = getProducts(); + } else if (edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) { + entitySet = getCategories(); + } + + return entitySet; + } + + public Entity readEntityData(EdmEntitySet edmEntitySet, List keyParams) { + Entity entity = null; + + EdmEntityType edmEntityType = edmEntitySet.getEntityType(); + + if (edmEntityType.getName().equals(DemoEdmProvider.ET_PRODUCT_NAME)) { + entity = getProduct(edmEntityType, keyParams); + } else if (edmEntityType.getName().equals(DemoEdmProvider.ET_CATEGORY_NAME)) { + entity = getCategory(edmEntityType, keyParams); + } + + return entity; + } + + // Navigation + + public Entity getRelatedEntity(Entity entity, EdmEntityType relatedEntityType) { + EntityCollection collection = getRelatedEntityCollection(entity, relatedEntityType); + if (collection.getEntities().isEmpty()) { + return null; + } + return collection.getEntities().get(0); + } + + public Entity getRelatedEntity(Entity entity, EdmEntityType relatedEntityType, List keyPredicates) { + + EntityCollection relatedEntities = getRelatedEntityCollection(entity, relatedEntityType); + return Util.findEntity(relatedEntityType, relatedEntities, keyPredicates); + } + + public EntityCollection getRelatedEntityCollection(Entity sourceEntity, EdmEntityType targetEntityType) { + EntityCollection navigationTargetEntityCollection = new EntityCollection(); + + FullQualifiedName relatedEntityFqn = targetEntityType.getFullQualifiedName(); + String sourceEntityFqn = sourceEntity.getType(); + + if (sourceEntityFqn.equals(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()) + && relatedEntityFqn.equals(DemoEdmProvider.ET_CATEGORY_FQN)) { + // relation Products->Category (result all categories) + int productID = (Integer) sourceEntity.getProperty("ID").getValue(); + if (productID == 1 || productID == 2) { + navigationTargetEntityCollection.getEntities().add(categoryList.get(0)); + } else if (productID == 3 || productID == 4) { + navigationTargetEntityCollection.getEntities().add(categoryList.get(1)); + } else if (productID == 5 || productID == 6) { + navigationTargetEntityCollection.getEntities().add(categoryList.get(2)); + } + } else if (sourceEntityFqn.equals(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString()) + && relatedEntityFqn.equals(DemoEdmProvider.ET_PRODUCT_FQN)) { + // relation Category->Products (result all products) + int categoryID = (Integer) sourceEntity.getProperty("ID").getValue(); + if (categoryID == 1) { + // the first 2 products are notebooks + navigationTargetEntityCollection.getEntities().addAll(productList.subList(0, 2)); + } else if (categoryID == 2) { + // the next 2 products are organizers + navigationTargetEntityCollection.getEntities().addAll(productList.subList(2, 4)); + } else if (categoryID == 3) { + // the first 2 products are monitors + navigationTargetEntityCollection.getEntities().addAll(productList.subList(4, 6)); + } + } + + if (navigationTargetEntityCollection.getEntities().isEmpty()) { + return null; + } + + return navigationTargetEntityCollection; + } + + /* INTERNAL */ + + private EntityCollection getProducts() { + EntityCollection retEntitySet = new EntityCollection(); + + for (Entity productEntity : this.productList) { + retEntitySet.getEntities().add(productEntity); + } + + return retEntitySet; + } + + private Entity getProduct(EdmEntityType edmEntityType, List keyParams) { + + // the list of entities at runtime + EntityCollection entityCollection = getProducts(); + + /* generic approach to find the requested entity */ + return Util.findEntity(edmEntityType, entityCollection, keyParams); + } + + private EntityCollection getCategories() { + EntityCollection entitySet = new EntityCollection(); + + for (Entity categoryEntity : this.categoryList) { + entitySet.getEntities().add(categoryEntity); + } + + return entitySet; + } + + private Entity getCategory(EdmEntityType edmEntityType, List keyParams) { + + // the list of entities at runtime + EntityCollection entitySet = getCategories(); + + /* generic approach to find the requested entity */ + return Util.findEntity(edmEntityType, entitySet, keyParams); + } + + /* HELPER */ + + private void initProductSampleData() { + + Entity entity = new Entity(); + + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")); + 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()); + productList.add(entity); + + entity = new Entity(); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Professional 17")); + 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()); + productList.add(entity); + + entity = new Entity(); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")); + entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")); + entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + productList.add(entity); + + entity = new Entity(); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 4)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Comfort Easy")); + entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, + "32 GB Digital Assitant with high-resolution color screen")); + entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString()); + productList.add(entity); + + entity = new Entity(); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 5)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")); + 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()); + productList.add(entity); + + entity = new Entity(); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 6)); + entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Flat Basic")); + 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()); + productList.add(entity); + } + + private void initCategorySampleData() { + + Entity entity = new Entity(); + + 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()); + 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()); + 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()); + categoryList.add(entity); + } + +} 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 new file mode 100755 index 000000000..7b1fef806 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.commons.api.ODataException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainer; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityType; +import org.apache.olingo.commons.api.edm.provider.CsdlNavigationProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlNavigationPropertyBinding; +import org.apache.olingo.commons.api.edm.provider.CsdlProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef; +import org.apache.olingo.commons.api.edm.provider.CsdlSchema; + +public class DemoEdmProvider extends CsdlAbstractEdmProvider { + + // Service Namespace + public static final String NAMESPACE = "OData.Demo"; + + // EDM Container + public static final String CONTAINER_NAME = "Container"; + public static final FullQualifiedName CONTAINER = new FullQualifiedName(NAMESPACE, CONTAINER_NAME); + + // Entity Types Names + public static final String ET_PRODUCT_NAME = "Product"; + public static final FullQualifiedName ET_PRODUCT_FQN = new FullQualifiedName(NAMESPACE, ET_PRODUCT_NAME); + + public static final String ET_CATEGORY_NAME = "Category"; + public static final FullQualifiedName ET_CATEGORY_FQN = new FullQualifiedName(NAMESPACE, ET_CATEGORY_NAME); + + // Entity Set Names + public static final String ES_PRODUCTS_NAME = "Products"; + public static final String ES_CATEGORIES_NAME = "Categories"; + + @Override + public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException { + + // this method is called for each EntityType that are configured in the Schema + CsdlEntityType entityType = null; + + if (entityTypeName.equals(ET_PRODUCT_FQN)) { + // create EntityType properties + CsdlProperty id = new CsdlProperty().setName("ID") + .setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName()); + CsdlProperty name = new CsdlProperty().setName("Name") + .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()); + CsdlProperty description = new CsdlProperty().setName("Description") + .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()); + + // create PropertyRef for Key element + CsdlPropertyRef propertyRef = new CsdlPropertyRef(); + propertyRef.setName("ID"); + + // navigation property: many-to-one, null not allowed (product must have a category) + CsdlNavigationProperty navProp = new CsdlNavigationProperty().setName("Category") + .setType(ET_CATEGORY_FQN).setNullable(false).setPartner("Products"); + List navPropList = new ArrayList(); + navPropList.add(navProp); + + // configure EntityType + entityType = new CsdlEntityType(); + entityType.setName(ET_PRODUCT_NAME); + entityType.setProperties(Arrays.asList(id, name, description)); + entityType.setKey(Arrays.asList(propertyRef)); + entityType.setNavigationProperties(navPropList); + + } else if (entityTypeName.equals(ET_CATEGORY_FQN)) { + // create EntityType properties + CsdlProperty id = new CsdlProperty().setName("ID") + .setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName()); + CsdlProperty name = new CsdlProperty().setName("Name") + .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()); + + // create PropertyRef for Key element + CsdlPropertyRef propertyRef = new CsdlPropertyRef(); + propertyRef.setName("ID"); + + // navigation property: one-to-many + CsdlNavigationProperty navProp = new CsdlNavigationProperty().setName("Products") + .setType(ET_PRODUCT_FQN).setCollection(true).setPartner("Category"); + List navPropList = new ArrayList(); + navPropList.add(navProp); + + // configure EntityType + entityType = new CsdlEntityType(); + entityType.setName(ET_CATEGORY_NAME); + entityType.setProperties(Arrays.asList(id, name)); + entityType.setKey(Arrays.asList(propertyRef)); + entityType.setNavigationProperties(navPropList); + } + + return entityType; + + } + + @Override + public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) throws ODataException { + + CsdlEntitySet entitySet = null; + + if (entityContainer.equals(CONTAINER)) { + + if (entitySetName.equals(ES_PRODUCTS_NAME)) { + + entitySet = new CsdlEntitySet(); + entitySet.setName(ES_PRODUCTS_NAME); + entitySet.setType(ET_PRODUCT_FQN); + + // navigation + CsdlNavigationPropertyBinding navPropBinding = new CsdlNavigationPropertyBinding(); + navPropBinding.setTarget("Categories"); // the target entity set, where the navigation property points to + navPropBinding.setPath("Category"); // the path from entity type to navigation property + List navPropBindingList = new ArrayList(); + navPropBindingList.add(navPropBinding); + entitySet.setNavigationPropertyBindings(navPropBindingList); + + } else if (entitySetName.equals(ES_CATEGORIES_NAME)) { + + entitySet = new CsdlEntitySet(); + entitySet.setName(ES_CATEGORIES_NAME); + entitySet.setType(ET_CATEGORY_FQN); + + // navigation + CsdlNavigationPropertyBinding navPropBinding = new CsdlNavigationPropertyBinding(); + navPropBinding.setTarget("Products"); // the target entity set, where the navigation property points to + navPropBinding.setPath("Products"); // the path from entity type to navigation property + List navPropBindingList = new ArrayList(); + navPropBindingList.add(navPropBinding); + entitySet.setNavigationPropertyBindings(navPropBindingList); + } + } + + return entitySet; + } + + @Override + public CsdlEntityContainerInfo getEntityContainerInfo(FullQualifiedName entityContainerName) throws ODataException { + + // This method is invoked when displaying the service document at + // e.g. http://localhost:8080/DemoService/DemoService.svc + if (entityContainerName == null || entityContainerName.equals(CONTAINER)) { + CsdlEntityContainerInfo entityContainerInfo = new CsdlEntityContainerInfo(); + entityContainerInfo.setContainerName(CONTAINER); + return entityContainerInfo; + } + + return null; + } + + @Override + public List getSchemas() throws ODataException { + // create Schema + CsdlSchema schema = new CsdlSchema(); + schema.setNamespace(NAMESPACE); + + // add EntityTypes + List entityTypes = new ArrayList(); + entityTypes.add(getEntityType(ET_PRODUCT_FQN)); + entityTypes.add(getEntityType(ET_CATEGORY_FQN)); + schema.setEntityTypes(entityTypes); + + // add EntityContainer + schema.setEntityContainer(getEntityContainer()); + + // finally + List schemas = new ArrayList(); + schemas.add(schema); + + return schemas; + } + + @Override + public CsdlEntityContainer getEntityContainer() throws ODataException { + + // create EntitySets + List entitySets = new ArrayList(); + entitySets.add(getEntitySet(CONTAINER, ES_PRODUCTS_NAME)); + entitySets.add(getEntitySet(CONTAINER, ES_CATEGORIES_NAME)); + + // create EntityContainer + CsdlEntityContainer entityContainer = new CsdlEntityContainer(); + entityContainer.setName(CONTAINER_NAME); + entityContainer.setEntitySets(entitySets); + + return entityContainer; + } +} 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 new file mode 100755 index 000000000..60f8cd28a --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.service; + +import java.util.List; + +import myservice.mynamespace.data.Storage; + +import org.apache.olingo.commons.api.Constants; +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.Link; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmNavigationProperty; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.commons.api.format.ODataFormat; +import org.apache.olingo.commons.api.http.HttpHeader; +import org.apache.olingo.commons.api.http.HttpStatusCode; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataApplicationException; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.processor.EntityCollectionProcessor; +import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.serializer.SerializerException; +import org.apache.olingo.server.api.serializer.SerializerResult; +import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceEntitySet; +import org.apache.olingo.server.api.uri.UriResourceNavigation; +import org.apache.olingo.server.api.uri.queryoption.ExpandItem; +import org.apache.olingo.server.api.uri.queryoption.ExpandOption; +import org.apache.olingo.server.api.uri.queryoption.SelectOption; + +public class DemoEntityCollectionProcessor implements EntityCollectionProcessor { + + private OData odata; + private ServiceMetadata srvMetadata; + // our database-mock + private Storage storage; + + public DemoEntityCollectionProcessor(Storage storage) { + this.storage = storage; + } + + public void init(OData odata, ServiceMetadata serviceMetadata) { + this.odata = odata; + this.srvMetadata = serviceMetadata; + } + + /* + * This method is invoked when a collection of entities has to be read. In + * our example, this can be either a "normal" read operation, or a + * navigation: + * + * Example for "normal" read entity set operation: + * http://localhost:8080/DemoService/DemoService.svc/Categories + * + * Example for navigation + * http://localhost:8080/DemoService/DemoService.svc/Categories(3)/Products + */ + public void readEntityCollection(ODataRequest request, + ODataResponse response, UriInfo uriInfo, ContentType responseFormat) + throws ODataApplicationException, SerializerException { + + // 1st retrieve the requested EdmEntitySet from the uriInfo + List resourcePaths = uriInfo.getUriResourceParts(); + // in our example, the first segment is the EntitySet + UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); + EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet(); + + // 2nd: fetch the data from backend for this requested EntitySetName + EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet); + + // 3rd: apply system query options + SelectOption selectOption = uriInfo.getSelectOption(); + ExpandOption expandOption = uriInfo.getExpandOption(); + + // handle $expand + // in our example: http://localhost:8080/DemoService/DemoService.svc/Categories/$expand=Products + // or http://localhost:8080/DemoService/DemoService.svc/Products?$expand=Category + if (expandOption != null) { + + // retrieve the EdmNavigationProperty from the expand expression + // Note: in our example, we have only one NavigationProperty, so we can directly access it + ExpandItem expandItem = expandOption.getExpandItems().get(0); + // can be 'Category' or 'Products' + UriResource uriResource = expandItem.getResourcePath().getUriResourceParts().get(0); + // we don't need to handle error cases, as it is done in the Olingo library + if (uriResource instanceof UriResourceNavigation) { + EdmNavigationProperty edmNavigationProperty = ((UriResourceNavigation) uriResource).getProperty(); + String navPropName = edmNavigationProperty.getName(); + EdmEntityType expandEdmEntityType = edmNavigationProperty.getType(); + + List entityList = entityCollection.getEntities(); + for (Entity entity : entityList) { + Link link = new Link(); + link.setTitle(navPropName); + link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE); + + 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); + } 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); + } + + // set the link - containing the expanded data - to the current entity + entity.getNavigationLinks().add(link); + } + } + } + + // 4th: serialize + EdmEntityType edmEntityType = edmEntitySet.getEntityType(); + // we need the property names of the $select, in order to build the context URL + String selectList = odata.createUriHelper().buildContextURLSelectList(edmEntityType, expandOption, selectOption); + ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).selectList(selectList).build(); + + // adding the selectOption to the serializerOpts will actually tell the lib to do the job + EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with() + .contextURL(contextUrl) + .select(selectOption) + .expand(expandOption) + .build(); + + ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(responseFormat)); + SerializerResult serializerResult = serializer.entityCollection(srvMetadata, edmEntityType, entityCollection, opts); + + // 5th: configure the response object: set the body, headers and status code + response.setContent(serializerResult.getContent()); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); + } + +} 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 new file mode 100755 index 000000000..f9bcb2b37 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java @@ -0,0 +1,235 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.service; + +import java.util.List; +import java.util.Locale; + +import myservice.mynamespace.data.Storage; + +import org.apache.olingo.commons.api.Constants; +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.ContextURL.Suffix; +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.Link; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmNavigationProperty; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.commons.api.format.ODataFormat; +import org.apache.olingo.commons.api.http.HttpHeader; +import org.apache.olingo.commons.api.http.HttpStatusCode; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataApplicationException; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.deserializer.DeserializerException; +import org.apache.olingo.server.api.processor.EntityProcessor; +import org.apache.olingo.server.api.serializer.EntitySerializerOptions; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.serializer.SerializerException; +import org.apache.olingo.server.api.serializer.SerializerResult; +import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.UriParameter; +import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceEntitySet; +import org.apache.olingo.server.api.uri.UriResourceNavigation; +import org.apache.olingo.server.api.uri.queryoption.ExpandItem; +import org.apache.olingo.server.api.uri.queryoption.ExpandOption; +import org.apache.olingo.server.api.uri.queryoption.SelectOption; + +public class DemoEntityProcessor implements EntityProcessor { + + private OData odata; + private ServiceMetadata srvMetadata; + private Storage storage; + + public DemoEntityProcessor(Storage storage) { + this.storage = storage; + } + + public void init(OData odata, ServiceMetadata serviceMetadata) { + this.odata = odata; + this.srvMetadata = serviceMetadata; + } + + +// /** +// * DUMMY example implementation +// * */ +// public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) +// throws ODataApplicationException, SerializerException { +// +// // 1. Analyze the URI +// EdmEntitySet edmEntitySet = ((UriResourceEntitySet)uriInfo.getUriResourceParts().get(0)).getEntitySet(); +// // get the system query option $expand +// ExpandOption expandOption = uriInfo.getExpandOption(); +// +// // 2. get the data. +// +// // Note: this is FAKE implementation +// // used for following request: +// // http://localhost:8080/DemoService/DemoService.svc/Products(1)?$expand=Category +// +// // create hard-coded product entity and set a hard-coded category as inlineEntity for the expand +// Entity fakeProductEntity = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 11)) +// .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Gamer Mouse")); +// fakeProductEntity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "High end gaming mouse")); +// +// // create hard-coded category entity (the target of the $expand) +// Entity fakeCategoryEntity = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 22)) +// .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Mice")); +// +// // create navigation link from product to category +// Link fakeLink = new Link(); +// fakeLink.setTitle("Category"); // hard-code the name of the navigation property as declared in EdmProvider +// fakeLink.setInlineEntity(fakeCategoryEntity); // the entity which will be expanded +// +// //add the link to the product entity +// fakeProductEntity.getNavigationLinks().add(fakeLink); +// +// // END FAKE +// +// +// // 3. serialize +// ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build(); +// // $expand info is added to the serializer options +// EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).expand(expandOption).build(); +// ODataSerializer serializer = this.odata.createSerializer(ODataFormat.fromContentType(responseFormat)); +// SerializerResult serializerResult = serializer.entity(srvMetadata, edmEntitySet.getEntityType(), fakeProductEntity, options); +// +// //4. configure the response object +// response.setContent(serializerResult.getContent()); +// response.setStatusCode(HttpStatusCode.OK.getStatusCode()); +// response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); +// } + + + + public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) + throws ODataApplicationException, SerializerException { + + // 1. retrieve the Entity Type + List resourcePaths = uriInfo.getUriResourceParts(); + // Note: only in our example we can assume that the first segment is the EntitySet + UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); + EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet(); + + // 2. retrieve the data from backend + List keyPredicates = uriResourceEntitySet.getKeyPredicates(); + Entity entity = storage.readEntityData(edmEntitySet, keyPredicates); + + // 3. apply system query options + + // handle $select + SelectOption selectOption = uriInfo.getSelectOption(); + // in our example, we don't have performance issues, so we can rely upon the handling in the Olingo lib + // nothing else to be done + + // handle $expand + ExpandOption expandOption = uriInfo.getExpandOption(); + // in our example: http://localhost:8080/DemoService/DemoService.svc/Categories(1)/$expand=Products + // or http://localhost:8080/DemoService/DemoService.svc/Products(1)?$expand=Category + if(expandOption != null){ + + // retrieve the EdmNavigationProperty from the expand expression + // Note: in our example, we have only one NavigationProperty, so we can directly access it + ExpandItem expandItem = expandOption.getExpandItems().get(0); + // can be 'Category' or 'Products', no path supported + UriResource uriResource = expandItem.getResourcePath().getUriResourceParts().get(0); + // we don't need to handle error cases, as it is done in the Olingo library + if(uriResource instanceof UriResourceNavigation){ + EdmNavigationProperty edmNavigationProperty = ((UriResourceNavigation)uriResource).getProperty(); + EdmEntityType expandEdmEntityType = edmNavigationProperty.getType(); + String navPropName = edmNavigationProperty.getName(); + + // build the inline data + Link link = new Link(); + link.setTitle(navPropName); + link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE); + + 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); + }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); + } + + // set the link - containing the expanded data - to the current entity + entity.getNavigationLinks().add(link); + } + } + + + + // 4. serialize + EdmEntityType edmEntityType = edmEntitySet.getEntityType(); + // we need the property names of the $select, in order to build the context URL + String selectList = odata.createUriHelper().buildContextURLSelectList(edmEntityType, expandOption, selectOption); + ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet) + .selectList(selectList) + .suffix(Suffix.ENTITY).build(); + + // make sure that $expand and $select are considered by the serializer + // adding the selectOption to the serializerOpts will actually tell the lib to do the job + EntitySerializerOptions opts = EntitySerializerOptions.with() + .contextURL(contextUrl) + .select(selectOption) + .expand(expandOption) + .build(); + + ODataSerializer serializer = this.odata.createSerializer(ODataFormat.fromContentType(responseFormat)); + SerializerResult serializerResult = serializer.entity(srvMetadata, edmEntityType, entity, opts); + + //5. configure the response object + response.setContent(serializerResult.getContent()); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); + } + + + + /* + * These processor methods are not handled in this tutorial + */ + + public void createEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, + ContentType requestFormat, ContentType responseFormat) + throws ODataApplicationException, DeserializerException, SerializerException { + throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } + + public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, + ContentType requestFormat, ContentType responseFormat) + throws ODataApplicationException, DeserializerException, SerializerException { + throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } + + public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo) + throws ODataApplicationException { + throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } +} diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java new file mode 100755 index 000000000..c32c5c24a --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.service; + +import java.io.InputStream; +import java.util.List; +import java.util.Locale; + +import myservice.mynamespace.data.Storage; + +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.commons.api.format.ODataFormat; +import org.apache.olingo.commons.api.http.HttpHeader; +import org.apache.olingo.commons.api.http.HttpStatusCode; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataApplicationException; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.deserializer.DeserializerException; +import org.apache.olingo.server.api.processor.PrimitiveProcessor; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; +import org.apache.olingo.server.api.serializer.SerializerException; +import org.apache.olingo.server.api.serializer.SerializerResult; +import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.UriParameter; +import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceEntitySet; +import org.apache.olingo.server.api.uri.UriResourceProperty; + +public class DemoPrimitiveProcessor implements PrimitiveProcessor { + + private OData odata; + private Storage storage; + + public DemoPrimitiveProcessor(Storage storage) { + this.storage = storage; + } + + public void init(OData odata, ServiceMetadata serviceMetadata) { + this.odata = odata; + + } + + /* + * In our example, the URL would be: http://localhost:8080/DemoService/DemoService.svc/Products(1)/Name + * and the response: + * { + * + * @odata.context: "$metadata#Products/Name", + * value: "Notebook Basic 15" + * } + */ + 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 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 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 + ODataFormat format = ODataFormat.fromContentType(responseFormat); + ODataSerializer serializer = odata.createSerializer(format); + + ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build(); + PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build(); + // 3.2. serialize + SerializerResult serializerResult = serializer.primitive(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()); + } + } + + /* + * These processor methods are not handled in this tutorial + */ + + public void updatePrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType requestFormat, + ContentType responseFormat) + throws ODataApplicationException, DeserializerException, SerializerException { + throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } + + public void deletePrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo) + throws ODataApplicationException { + throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } +} diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java new file mode 100755 index 000000000..f16360fc6 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.util; + +import java.util.List; +import java.util.Locale; + +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.edm.EdmBindingTarget; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmNavigationProperty; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.edm.EdmType; +import org.apache.olingo.commons.api.http.HttpStatusCode; +import org.apache.olingo.server.api.ODataApplicationException; +import org.apache.olingo.server.api.uri.UriParameter; + +public class Util { + + public static Entity findEntity(EdmEntityType edmEntityType, EntityCollection entitySet, + List keyParams) { + + List entityList = entitySet.getEntities(); + + // loop over all entities in order to find that one that matches + // all keys in request e.g. contacts(ContactID=1, CompanyID=1) + for (Entity entity : entityList) { + boolean foundEntity = entityMatchesAllKeys(edmEntityType, entity, keyParams); + if (foundEntity) { + return entity; + } + } + + return null; + } + + public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity rt_entity, + List keyParams) { + + // loop over all keys + for (final UriParameter key : keyParams) { + // key + String keyName = key.getName(); + String keyText = key.getText(); + + // note: below line doesn't consider: keyProp can be part of a complexType in V4 + // in such case, it would be required to access it via getKeyPropertyRef() + // but since this isn't the case in our model, we ignore it in our implementation + EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName); + // Edm: we need this info for the comparison below + Boolean isNullable = edmKeyProperty.isNullable(); + Integer maxLength = edmKeyProperty.getMaxLength(); + Integer precision = edmKeyProperty.getPrecision(); + Boolean isUnicode = edmKeyProperty.isUnicode(); + Integer scale = edmKeyProperty.getScale(); + // get the EdmType in order to compare + EdmType edmType = edmKeyProperty.getType(); + // if(EdmType instanceof EdmPrimitiveType) // do we need this? + EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType; + + // Runtime data: the value of the current entity + // don't need to check for null, this is done in FWK + Object valueObject = rt_entity.getProperty(keyName).getValue(); + // TODO if the property is a complex type + + // now need to compare the valueObject with the keyText String + // this is done using the type.valueToString + String valueAsString = null; + try { + valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, + maxLength, precision, scale, isUnicode); + } catch (EdmPrimitiveTypeException e) { + return false; // TODO proper Exception handling + } + + if (valueAsString == null) { + return false; + } + + boolean matches = valueAsString.equals(keyText); + // if any of the key properties is not found in the entity, we don't need to search further + if (!matches) { + return false; + } + // if the given key value is found in the current entity, continue with the next key + } + + return true; + } + + /** + * Example: + * For the following navigation: DemoService.svc/Categories(1)/Products + * we need the EdmEntitySet for the navigation property "Products" + * + * This is defined as follows in the metadata: + * + * + * + * + * + * + * The "Target" attribute specifies the target EntitySet + * Therefore we need the startEntitySet "Categories" in order to retrieve the target EntitySet "Products" + */ + public static EdmEntitySet getNavigationTargetEntitySet(EdmEntitySet startEntitySet, + EdmNavigationProperty edmNavigationProperty) + throws ODataApplicationException { + + EdmEntitySet navigationTargetEntitySet = null; + + String navPropName = edmNavigationProperty.getName(); + EdmBindingTarget edmBindingTarget = startEntitySet.getRelatedBindingTarget(navPropName); + if (edmBindingTarget == null) { + throw new ODataApplicationException("Not supported.", + HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } + + if (edmBindingTarget instanceof EdmEntitySet) { + navigationTargetEntitySet = (EdmEntitySet) edmBindingTarget; + } else { + throw new ODataApplicationException("Not supported.", + HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); + } + + return navigationTargetEntitySet; + } + +} diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/web/DemoServlet.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/web/DemoServlet.java new file mode 100755 index 000000000..5c828e5d3 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/web/DemoServlet.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package myservice.mynamespace.web; + +import java.io.IOException; +import java.util.ArrayList; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import myservice.mynamespace.data.Storage; +import myservice.mynamespace.service.DemoEdmProvider; +import myservice.mynamespace.service.DemoEntityCollectionProcessor; +import myservice.mynamespace.service.DemoEntityProcessor; +import myservice.mynamespace.service.DemoPrimitiveProcessor; + +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataHttpHandler; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.edmx.EdmxReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DemoServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class); + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + HttpSession session = req.getSession(true); + Storage storage = (Storage) session.getAttribute(Storage.class.getName()); + if (storage == null) { + storage = new Storage(); + session.setAttribute(Storage.class.getName(), storage); + } + + // create odata handler and configure it with EdmProvider and Processor + OData odata = OData.newInstance(); + ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList()); + ODataHttpHandler handler = odata.createHandler(edm); + handler.register(new DemoEntityCollectionProcessor(storage)); + handler.register(new DemoEntityProcessor(storage)); + handler.register(new DemoPrimitiveProcessor(storage)); + + // let the handler do the work + handler.process(req, resp); + } catch (RuntimeException e) { + LOG.error("Server Error occurred in DemoServlet", e); + throw new ServletException(e); + } + + } + +} diff --git a/samples/tutorials/p6_queryoptions-es/src/main/webapp/WEB-INF/web.xml b/samples/tutorials/p6_queryoptions-es/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 000000000..bc216cb97 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,34 @@ + + + + + + DemoServlet + myservice.mynamespace.web.DemoServlet + 1 + + + + DemoServlet + /DemoService.svc/* + + diff --git a/samples/tutorials/p6_queryoptions-es/src/main/webapp/index.jsp b/samples/tutorials/p6_queryoptions-es/src/main/webapp/index.jsp new file mode 100755 index 000000000..cee3a3190 --- /dev/null +++ b/samples/tutorials/p6_queryoptions-es/src/main/webapp/index.jsp @@ -0,0 +1,43 @@ + + + +

Hello World!

+OData Olingo V4 Demo Service - Expand and Select +

Sample Links

+ + + + + diff --git a/samples/tutorials/pom.xml b/samples/tutorials/pom.xml index 111e2000e..cfa48a40f 100644 --- a/samples/tutorials/pom.xml +++ b/samples/tutorials/pom.xml @@ -39,7 +39,8 @@ p2_readep p3_write p4_navigation - p5_queryoptions + p5_queryoptions-tis + p6_queryoptions-es