package com.github.olingo.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name="MOTHER")
public class Mother {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mother mother = (Mother) o;
return Objects.equals(id, mother.id) &&
Objects.equals(name, mother.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
package com.github.olingo.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name="FATHER")
public class Father {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Father father = (Father) o;
return Objects.equals(id, father.id) &&
Objects.equals(name, father.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
package com.github.olingo.example.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
public class ChildPK implements Serializable {
@Column(name = "FATHER_ID", nullable = false)
private Long fatherId;
@Column(name = "MOTHER_ID", nullable = false)
private Long motherId;
public ChildPK() {
}
public ChildPK(Father father, Mother mother) {
this.fatherId = father.getId();
this.motherId = mother.getId();
}
public Long getFatherId() {
return fatherId;
}
public void setFatherId(Long fatherId) {
this.fatherId = fatherId;
}
public Long getMotherId() {
return motherId;
}
public void setMotherId(Long motherId) {
this.motherId = motherId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChildPK childPK = (ChildPK) o;
return Objects.equals(fatherId, childPK.fatherId) &&
Objects.equals(motherId, childPK.motherId);
}
@Override
public int hashCode() {
return Objects.hash(fatherId, motherId);
}
}
package com.github.olingo.example.entity;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name="CHILD")
public class Child {
@EmbeddedId
private ChildPK childPK = new ChildPK();
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId("fatherId")
private Father father;
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId("motherId")
private Mother mother;
private String name;
public Child() {}
public Child(Father father, Mother mother) {
this.childPK = new ChildPK(father, mother);
}
public ChildPK getChildPK() {
return childPK;
}
public void setChildPK(ChildPK childPK) {
this.childPK = childPK;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
public Mother getMother() {
return mother;
}
public void setMother(Mother mother) {
this.mother = mother;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Child child = (Child) o;
return Objects.equals(childPK, child.childPK) &&
Objects.equals(father, child.father) &&
Objects.equals(mother, child.mother) &&
Objects.equals(name, child.name);
}
@Override
public int hashCode() {
return Objects.hash(childPK, father, mother, name);
}
}
package com.github.olingo.example.service;
import org.springframework.stereotype.Component;
@Component
public class OdataJpaServiceFactory extends CustomODataServiceFactory{
//need this wrapper class for the spring framework, otherwise we face issues when auto wiring directly the CustomODataServiceFactory
}
package com.github.olingo.example.service;
import com.github.olingo.example.config.JerseyConfig;
import org.apache.olingo.odata2.api.ODataService;
import org.apache.olingo.odata2.api.ODataServiceFactory;
import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAAccessFactory;
import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAFactory;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
public class CustomODataServiceFactory extends ODataServiceFactory {
private ODataJPAContext oDataJPAContext;
private ODataContext oDataContext;
@Override
public final ODataService createService(final ODataContext context) throws ODataException {
oDataContext = context;
oDataJPAContext = initializeODataJPAContext();
validatePreConditions();
ODataJPAFactory factory = ODataJPAFactory.createFactory();
ODataJPAAccessFactory accessFactory = factory.getODataJPAAccessFactory();
if (oDataJPAContext.getODataContext() == null) {
oDataJPAContext.setODataContext(context);
}
ODataSingleProcessor oDataSingleProcessor = new CustomODataJpaProcessor(
oDataJPAContext);
EdmProvider edmProvider = accessFactory.createJPAEdmProvider(oDataJPAContext);
return createODataSingleProcessorService(edmProvider, oDataSingleProcessor);
}
private void validatePreConditions() throws ODataJPARuntimeException {
if (oDataJPAContext.getEntityManager() == null) {
throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.ENTITY_MANAGER_NOT_INITIALIZED, null);
}
}
public final ODataJPAContext getODataJPAContext()
throws ODataJPARuntimeException {
if (oDataJPAContext == null) {
oDataJPAContext = ODataJPAFactory.createFactory()
.getODataJPAAccessFactory().createODataJPAContext();
}
if (oDataContext != null)
oDataJPAContext.setODataContext(oDataContext);
return oDataJPAContext;
}
protected ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
ODataJPAContext oDataJPAContext = this.getODataJPAContext();
ODataContext oDataContext = oDataJPAContext.getODataContext();
HttpServletRequest request = (HttpServletRequest) oDataContext.getParameter(
ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
EntityManager entityManager = (EntityManager) request
.getAttribute(JerseyConfig.EntityManagerFilter.EM_REQUEST_ATTRIBUTE);
oDataJPAContext.setEntityManager(entityManager);
oDataJPAContext.setPersistenceUnitName("default");
oDataJPAContext.setContainerManaged(true);
return oDataJPAContext;
}
}
package com.github.olingo.example.service;
import org.apache.olingo.odata2.api.edm.EdmException;
import org.apache.olingo.odata2.api.ep.EntityProviderException;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
import org.apache.olingo.odata2.api.processor.ODataResponse;
import org.apache.olingo.odata2.api.uri.info.*;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPADefaultProcessor;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.util.List;
public class CustomODataJpaProcessor extends ODataJPADefaultProcessor {
private Logger logger = LoggerFactory.getLogger(getClass());
public CustomODataJpaProcessor(ODataJPAContext oDataJPAContext) {
super(oDataJPAContext);
}
@Override
public ODataResponse readEntitySet(final GetEntitySetUriInfo uriParserResultView, final String contentType) throws ODataJPAModelException, ODataJPARuntimeException, EdmException {
logger.info("READ: Entity Set {} called", uriParserResultView.getTargetEntitySet().getName());
try {
List<Object> jpaEntities = jpaProcessor.process(uriParserResultView);
return responseBuilder.build(uriParserResultView, jpaEntities, contentType);
} finally {
this.close();
}
}
@Override
public ODataResponse readEntity(final GetEntityUriInfo uriParserResultView, final String contentType) throws ODataJPAModelException, ODataJPARuntimeException, ODataNotFoundException, EdmException {
ODataResponse response = null;
if (uriParserResultView.getKeyPredicates().size() > 1) {
logger.info("READ: Entity {} called with key {} and key {}", uriParserResultView.getTargetEntitySet().getName(), uriParserResultView.getKeyPredicates().get(0).getLiteral(), uriParserResultView.getKeyPredicates().get(1).getLiteral());
} else {
logger.info("READ: Entity {} called with key {}", uriParserResultView.getTargetEntitySet().getName(), uriParserResultView.getKeyPredicates().get(0).getLiteral());
}
try {
Object readEntity = jpaProcessor.process(uriParserResultView);
response = responseBuilder.build(uriParserResultView, readEntity, contentType);
} finally {
this.close();
}
return response;
}
@Override
public ODataResponse createEntity(final PostUriInfo uriParserResultView, final InputStream content, final String requestContentType, final String contentType) throws ODataJPAModelException, ODataJPARuntimeException, ODataNotFoundException, EdmException, EntityProviderException {
logger.info("POST: Entity {} called", uriParserResultView.getTargetEntitySet().getName());
ODataResponse response = null;
try {
Object createdEntity = jpaProcessor.process(uriParserResultView, content, requestContentType);
response = responseBuilder.build(uriParserResultView, createdEntity, contentType);
} finally {
this.close();
}
return response;
}
@Override
public ODataResponse updateEntity(final PutMergePatchUriInfo uriParserResultView, final InputStream content,
final String requestContentType, final boolean merge, final String contentType) throws ODataException, ODataJPAModelException, ODataJPARuntimeException, ODataNotFoundException {
logger.info("PUT: Entity {} called with key {}", uriParserResultView.getTargetEntitySet().getName(), uriParserResultView.getKeyPredicates().get(0).getLiteral());
ODataResponse response = null;
try {
Object updatedEntity = jpaProcessor.process(uriParserResultView, content, requestContentType);
response = responseBuilder.build(uriParserResultView, updatedEntity);
} finally {
this.close();
}
return response;
}
@Override
public ODataResponse deleteEntity(DeleteUriInfo uriParserResultView, String contentType) throws ODataException {
logger.info("DELETE: Entity {} called with key {}", uriParserResultView.getTargetEntitySet().getName(), uriParserResultView.getKeyPredicates().get(0).getLiteral());
ODataResponse oDataResponse = null;
try {
this.oDataJPAContext.setODataContext(this.getContext());
Object deletedEntity = this.jpaProcessor.process(uriParserResultView, contentType);
oDataResponse = this.responseBuilder.build(uriParserResultView, deletedEntity);
} finally {
this.close();
}
return oDataResponse;
}
}
package com.github.olingo.example.config;
import com.github.olingo.example.service.OdataJpaServiceFactory;
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.springframework.stereotype.Component;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.http.HttpServletRequest;
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.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Component
@ApplicationPath("/odata")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(OdataJpaServiceFactory serviceFactory, EntityManagerFactory entityManagerFactory) {
ODataApplication oDataApplication = new ODataApplication();
oDataApplication
.getClasses()
.forEach( c -> {
if ( !ODataRootLocator.class.isAssignableFrom(c)) {
register(c);
}
});
register(new ODataServiceRootLocator(serviceFactory));
register(new EntityManagerFilter(entityManagerFactory));
}
@Path("/")
public static class ODataServiceRootLocator extends ODataRootLocator {
private OdataJpaServiceFactory serviceFactory;
@Inject
public ODataServiceRootLocator (OdataJpaServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}
@Override
public ODataServiceFactory getServiceFactory() {
return this.serviceFactory;
}
}
@Provider
public static class EntityManagerFilter implements ContainerRequestFilter,
ContainerResponseFilter {
public static final String EM_REQUEST_ATTRIBUTE =
EntityManagerFilter.class.getName() + "_ENTITY_MANAGER";
private final EntityManagerFactory entityManagerFactory;
@Context
private HttpServletRequest httpRequest;
public EntityManagerFilter(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
EntityManager entityManager = this.entityManagerFactory.createEntityManager();
httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE, entityManager);
if (!"GET".equalsIgnoreCase(containerRequestContext.getMethod())) {
entityManager.getTransaction().begin();
}
}
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
EntityManager entityManager = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);
if (!"GET".equalsIgnoreCase(requestContext.getMethod())) {
EntityTransaction entityTransaction = entityManager.getTransaction(); //we do not commit because it's just a READ
if (entityTransaction.isActive() && !entityTransaction.getRollbackOnly()) {
entityTransaction.commit();
}
}
entityManager.close();
}
}
}
SAP S/4HANA Cloud Development Financials
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
2 | |
2 |