[BAEL-1219] OLingo2 code

This commit is contained in:
Philippe 2019-04-04 00:09:38 -03:00
parent bac1443897
commit 881580f2fe
10 changed files with 641 additions and 44 deletions

View File

@ -1,24 +0,0 @@
package org.baeldung.examples.olingo2;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import org.apache.olingo.odata2.core.rest.ODataRootLocator;
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class CarsODataApplication extends ODataApplication {
private static final Logger log = LoggerFactory.getLogger(CarsODataApplication.class);
public CarsODataApplication() {
super();
log.info("[I17] Creating CarsODataApplication...");
}
}

View File

@ -1,37 +1,292 @@
package org.baeldung.examples.olingo2;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.SynchronizationType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.metamodel.Metamodel;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.stereotype.Component;
/**
* OData ServiceFactory that
* ODataJPAServiceFactory implementation for our sample domain
* @author Philippe
*
*/
@Component
public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
private EntityManager em;
private static final Logger log = LoggerFactory.getLogger(CarsODataJPAServiceFactory.class);
public CarsODataJPAServiceFactory(EntityManager em) {
this.em = em;
public CarsODataJPAServiceFactory() {
// Enable detailed error messages (useful for debugging)
setDetailErrors(true);
}
/**
* This method will be called by Olingo on every request to
* initialize the ODataJPAContext that will be used.
*/
@Override
public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
ODataJPAContext oDataJPAContext = getODataJPAContext();
oDataJPAContext.setEntityManagerFactory(em.getEntityManagerFactory());
oDataJPAContext.setPersistenceUnitName("default");
log.info("[I32] >>> initializeODataJPAContext()");
ODataJPAContext ctx = getODataJPAContext();
return oDataJPAContext;
// Here we're passing the EM that was created by the EntityManagerFilter (see JerseyConfig)
ctx.setEntityManager(new EntityManagerWrapper(EntityManagerHolder.getCurrentEntityManager()));
ctx.setPersistenceUnitName("default");
// We're managing the EM's lifecycle, so we must inform Olingo that it should not
// try to manage transactions and/or persistence sessions
ctx.setContainerManaged(true);
return ctx;
}
static class EntityManagerWrapper implements EntityManager {
private EntityManager delegate;
public void persist(Object entity) {
log.info("[I68] persist: entity.class=" + entity.getClass().getSimpleName());
delegate.persist(entity);
//delegate.flush();
}
public <T> T merge(T entity) {
log.info("[I74] merge: entity.class=" + entity.getClass().getSimpleName());
return delegate.merge(entity);
}
public void remove(Object entity) {
log.info("[I78] remove: entity.class=" + entity.getClass().getSimpleName());
delegate.remove(entity);
}
public <T> T find(Class<T> entityClass, Object primaryKey) {
return delegate.find(entityClass, primaryKey);
}
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) {
return delegate.find(entityClass, primaryKey, properties);
}
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
return delegate.find(entityClass, primaryKey, lockMode);
}
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
return delegate.find(entityClass, primaryKey, lockMode, properties);
}
public <T> T getReference(Class<T> entityClass, Object primaryKey) {
return delegate.getReference(entityClass, primaryKey);
}
public void flush() {
delegate.flush();
}
public void setFlushMode(FlushModeType flushMode) {
delegate.setFlushMode(flushMode);
}
public FlushModeType getFlushMode() {
return delegate.getFlushMode();
}
public void lock(Object entity, LockModeType lockMode) {
delegate.lock(entity, lockMode);
}
public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
delegate.lock(entity, lockMode, properties);
}
public void refresh(Object entity) {
delegate.refresh(entity);
}
public void refresh(Object entity, Map<String, Object> properties) {
delegate.refresh(entity, properties);
}
public void refresh(Object entity, LockModeType lockMode) {
delegate.refresh(entity, lockMode);
}
public void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties) {
delegate.refresh(entity, lockMode, properties);
}
public void clear() {
delegate.clear();
}
public void detach(Object entity) {
delegate.detach(entity);
}
public boolean contains(Object entity) {
return delegate.contains(entity);
}
public LockModeType getLockMode(Object entity) {
return delegate.getLockMode(entity);
}
public void setProperty(String propertyName, Object value) {
delegate.setProperty(propertyName, value);
}
public Map<String, Object> getProperties() {
return delegate.getProperties();
}
public Query createQuery(String qlString) {
return delegate.createQuery(qlString);
}
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
return delegate.createQuery(criteriaQuery);
}
public Query createQuery(CriteriaUpdate updateQuery) {
return delegate.createQuery(updateQuery);
}
public Query createQuery(CriteriaDelete deleteQuery) {
return delegate.createQuery(deleteQuery);
}
public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
return delegate.createQuery(qlString, resultClass);
}
public Query createNamedQuery(String name) {
return delegate.createNamedQuery(name);
}
public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
return delegate.createNamedQuery(name, resultClass);
}
public Query createNativeQuery(String sqlString) {
return delegate.createNativeQuery(sqlString);
}
public Query createNativeQuery(String sqlString, Class resultClass) {
return delegate.createNativeQuery(sqlString, resultClass);
}
public Query createNativeQuery(String sqlString, String resultSetMapping) {
return delegate.createNativeQuery(sqlString, resultSetMapping);
}
public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
return delegate.createNamedStoredProcedureQuery(name);
}
public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
return delegate.createStoredProcedureQuery(procedureName);
}
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
return delegate.createStoredProcedureQuery(procedureName, resultClasses);
}
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
return delegate.createStoredProcedureQuery(procedureName, resultSetMappings);
}
public void joinTransaction() {
delegate.joinTransaction();
}
public boolean isJoinedToTransaction() {
return delegate.isJoinedToTransaction();
}
public <T> T unwrap(Class<T> cls) {
return delegate.unwrap(cls);
}
public Object getDelegate() {
return delegate.getDelegate();
}
public void close() {
log.info("[I229] close");
delegate.close();
}
public boolean isOpen() {
boolean isOpen = delegate.isOpen();
log.info("[I236] isOpen: " + isOpen);
return isOpen;
}
public EntityTransaction getTransaction() {
log.info("[I240] getTransaction()");
return delegate.getTransaction();
}
public EntityManagerFactory getEntityManagerFactory() {
return delegate.getEntityManagerFactory();
}
public CriteriaBuilder getCriteriaBuilder() {
return delegate.getCriteriaBuilder();
}
public Metamodel getMetamodel() {
return delegate.getMetamodel();
}
public <T> EntityGraph<T> createEntityGraph(Class<T> rootType) {
return delegate.createEntityGraph(rootType);
}
public EntityGraph<?> createEntityGraph(String graphName) {
return delegate.createEntityGraph(graphName);
}
public EntityGraph<?> getEntityGraph(String graphName) {
return delegate.getEntityGraph(graphName);
}
public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass) {
return delegate.getEntityGraphs(entityClass);
}
public EntityManagerWrapper(EntityManager delegate) {
this.delegate = delegate;
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.examples.olingo2;
import javax.persistence.EntityManager;
public class EntityManagerHolder {
private static ThreadLocal<EntityManager> currentEntityManager = new ThreadLocal<>();
public static void setCurrentEntityManager(EntityManager em) {
currentEntityManager.set(em);
}
public static EntityManager getCurrentEntityManager() {
return currentEntityManager.get();
}
}

View File

@ -1,41 +1,111 @@
package org.baeldung.examples.olingo2;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletContext;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
import org.apache.olingo.odata2.api.ODataServiceFactory;
import org.apache.olingo.odata2.core.rest.ODataRootLocator;
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Jersey JAX-RS configuration
* @author Philippe
*
*/
@Component
@ApplicationPath("/odata")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(CarsODataApplication delegate,ServletContext servletContext,CarsODataJPAServiceFactory serviceFactory) {
public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) {
delegate
ODataApplication app = new ODataApplication();
app
.getClasses()
.forEach( c -> {
// Avoid using the default Locator
// Avoid using the default RootLocator, as we want
// a Spring Managed one
if ( !ODataRootLocator.class.isAssignableFrom(c)) {
register(c);
}
});
register(new CustomLocator(serviceFactory));
register(new CarsRootLocator(serviceFactory));
register( new EntityManagerFilter(emf));
}
/**
* This filter handles the EntityManager transaction lifecycle.
* @author Philippe
*
*/
@Provider
public static class EntityManagerFilter implements ContainerRequestFilter, ContainerResponseFilter {
private static final Logger log = LoggerFactory.getLogger(EntityManagerFilter.class);
private final EntityManagerFactory emf;
public EntityManagerFilter(EntityManagerFactory emf) {
this.emf = emf;
}
@Override
public void filter(ContainerRequestContext ctx) throws IOException {
log.info("[I60] >>> filter");
EntityManager em = this.emf.createEntityManager();
EntityManagerHolder.setCurrentEntityManager(em);
// Start a new transaction unless we have a simple GET
if ( !"GET".equalsIgnoreCase(ctx.getMethod())) {
em.getTransaction().begin();
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
log.info("[I68] <<< filter");
EntityManager em = EntityManagerHolder.getCurrentEntityManager();
if ( em != null && !"GET".equalsIgnoreCase(requestContext.getMethod())) {
EntityTransaction t = em.getTransaction();
if ( t.isActive()) {
if ( !t.getRollbackOnly()) {
t.commit();
}
}
}
em.close();
}
}
@Path("/")
public static class CustomLocator extends ODataRootLocator {
public static class CarsRootLocator extends ODataRootLocator {
private CarsODataJPAServiceFactory serviceFactory;
public CustomLocator(CarsODataJPAServiceFactory serviceFactory) {
public CarsRootLocator(CarsODataJPAServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}

View File

@ -1,5 +1,6 @@
package org.baeldung.examples.olingo2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@ -7,9 +8,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
@SpringBootApplication
public class Olingo2SampleApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
new Olingo2SampleApplication()
.configure(new SpringApplicationBuilder(Olingo2SampleApplication.class))
.run(args);
public static void main(String[] args) {
SpringApplication.run(Olingo2SampleApplication.class);
}
}

View File

@ -5,6 +5,8 @@ import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@ -18,13 +20,16 @@ import lombok.Data;
public class CarMaker {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(name="name")
private String name;
@OneToMany(mappedBy="maker",cascade=CascadeType.ALL)
@OneToMany(mappedBy="maker",
orphanRemoval = true,
cascade=CascadeType.ALL)
private List<CarModel> models;

View File

@ -2,17 +2,24 @@ package org.baeldung.examples.olingo2.domain;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Entity
@Data
@Table(name="car_model")
public class CarModel {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@NotNull
@ -24,7 +31,8 @@ public class CarModel {
@NotNull
private String sku;
@ManyToOne
@ManyToOne(optional=false, fetch= FetchType.LAZY)
@JoinColumn(name="maker_fk")
private CarMaker maker;
}

View File

@ -5,5 +5,6 @@ spring:
jpa:
show-sql: true
open-in-view: false
hibernate:
ddl-auto: update

View File

@ -1,3 +1,12 @@
insert into car_maker(id,name) values (1,'Special Motors');
insert into car_maker(id,name) values (2,'BWM');
insert into car_maker(id,name) values (3,'Dolores');
insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018);
insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008);
insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008);
insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009);
insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008);
alter sequence hibernate_sequence restart with 100;

View File

@ -0,0 +1,256 @@
{
"info": {
"_postman_id": "afa8e1e5-ab0e-4f1d-8b99-b7d1f091f975",
"name": "OLingo2 - Cars",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "GET Metadata",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": "http://localhost:8080/odata/$metadata"
},
"response": []
},
{
"name": "GET All CarMakers",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": "http://localhost:8080/odata/CarMakers"
},
"response": []
},
{
"name": "GET Makers with Pagination",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:8080/odata/CarMakers?$top=1&$orderby=Name&$skip=3",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"odata",
"CarMakers"
],
"query": [
{
"key": "$top",
"value": "1"
},
{
"key": "$orderby",
"value": "Name"
},
{
"key": "$skip",
"value": "3"
}
]
}
},
"response": []
},
{
"name": "GET Makers and Models",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:8080/odata/CarMakers?$expand=CarModelDetails",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"odata",
"CarMakers"
],
"query": [
{
"key": "$expand",
"value": "CarModelDetails"
}
]
}
},
"response": []
},
{
"name": "GET Makers with filter",
"request": {
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:8080/odata/CarMakers?$filter=Name eq 'BWM'&$expand=CarModelDetails",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"odata",
"CarMakers"
],
"query": [
{
"key": "$filter",
"value": "Name eq 'BWM'"
},
{
"key": "$expand",
"value": "CarModelDetails"
}
]
}
},
"response": []
},
{
"name": "Create CarMaker",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/atom+xml",
"type": "text"
},
{
"key": "Accept",
"value": "application/atom+xml",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"\r\n xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" \r\n xmlns=\"http://www.w3.org/2005/Atom\">\r\n <title type=\"text\"></title> \r\n <updated>2019-04-02T21:36:47Z</updated>\r\n <author> \r\n <name /> \r\n </author> \r\n <category term=\"default.CarMaker\"\r\n scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" /> \r\n <content type=\"application/xml\"> \r\n <m:properties>\r\n <d:Name>Lucien</d:Name>\r\n </m:properties> \r\n </content> \r\n</entry>"
},
"url": "http://localhost:8080/odata/CarMakers"
},
"response": []
},
{
"name": "Create CarModel",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/atom+xml"
},
{
"key": "Accept",
"type": "text",
"value": "application/atom+xml"
}
],
"body": {
"mode": "raw",
"raw": "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"\r\n xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" \r\n xmlns=\"http://www.w3.org/2005/Atom\">\r\n <title type=\"text\"></title> \r\n <updated>2019-04-02T21:36:47Z</updated>\r\n <author> \r\n <name /> \r\n </author> \r\n <link href=\"CarModels(1L)/CarMakerDetails\" \r\n rel=\"http://schemas.microsoft.com/ado/2007/08/dataservices/related/CarMakerDetails\" \r\n title=\"CarMakerDetails\" \r\n type=\"application/atom+xml;type=entry\"></link> \r\n <category term=\"default.CarModel\"\r\n scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" /> \r\n <content type=\"application/xml\"> \r\n <m:properties>\r\n\t <d:Name>Tata</d:Name>\r\n\t <d:Sku>TT101</d:Sku>\r\n\t <d:Year>2018</d:Year>\r\n </m:properties> \r\n </content> \r\n</entry>"
},
"url": "http://localhost:8080/odata/CarModels"
},
"response": []
},
{
"name": "Update CarMaker",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/atom+xml"
},
{
"key": "Accept",
"type": "text",
"value": "application/atom+xml"
}
],
"body": {
"mode": "raw",
"raw": "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"\r\n xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" \r\n xmlns=\"http://www.w3.org/2005/Atom\">\r\n <title type=\"text\"></title> \r\n <updated>2019-04-02T21:36:47Z</updated>\r\n <author> \r\n <name /> \r\n </author> \r\n <category term=\"default.CarMaker\"\r\n scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" /> \r\n <content type=\"application/xml\"> \r\n <m:properties>\r\n <d:Id>5</d:Id>\r\n <d:Name>KaiserWagen</d:Name>\r\n </m:properties> \r\n </content> \r\n</entry>"
},
"url": "http://localhost:8080/odata/CarMakers(5L)"
},
"response": []
},
{
"name": "All CarModels",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": "http://localhost:8080/odata/CarModels"
},
"response": []
},
{
"name": "Delete CarModel",
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/atom+xml"
},
{
"key": "Accept",
"type": "text",
"value": "application/atom+xml"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": "http://localhost:8080/odata/CarModels(100L)"
},
"response": []
}
]
}