[BAEL-1219] Code polishing
This commit is contained in:
+1
-1
@@ -31,7 +31,7 @@ public class CarModel {
|
||||
@NotNull
|
||||
private String sku;
|
||||
|
||||
@ManyToOne(optional=false, fetch= FetchType.LAZY)
|
||||
@ManyToOne(optional=false, fetch= FetchType.EAGER )
|
||||
@JoinColumn(name="maker_fk")
|
||||
private CarMaker maker;
|
||||
|
||||
|
||||
+13
-4
@@ -1,7 +1,9 @@
|
||||
package org.baeldung.examples.olingo4.edm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
@@ -33,11 +35,14 @@ public class JpaEdmProvider extends CsdlAbstractEdmProvider {
|
||||
private EdmTypeMapper typeMapper;
|
||||
|
||||
// Service Namespace
|
||||
public static final String NAMESPACE = "OData.Demo";
|
||||
public static final String NAMESPACE = "Baeldung.OData";
|
||||
|
||||
// EDM Container
|
||||
public static final String CONTAINER_NAME = "Cars";
|
||||
public static final FullQualifiedName CONTAINER = new FullQualifiedName(NAMESPACE, CONTAINER_NAME);
|
||||
|
||||
// Caches of OData types by it fully qualified name
|
||||
private Map<FullQualifiedName, CsdlEntityType> cdslName2Type = new HashMap<>();
|
||||
|
||||
public JpaEdmProvider(EntityManagerFactory emf, EdmTypeMapper mapper) {
|
||||
this.emf = emf;
|
||||
@@ -153,11 +158,13 @@ public class JpaEdmProvider extends CsdlAbstractEdmProvider {
|
||||
|
||||
@Override
|
||||
public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException {
|
||||
|
||||
CsdlEntityType result = cdslName2Type.get(entityTypeName);
|
||||
if ( result != null ) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Metamodel mm = emf.getMetamodel();
|
||||
|
||||
CsdlEntityType result = null;
|
||||
|
||||
result = mm.getEntities()
|
||||
.stream()
|
||||
.filter(et -> entityTypeName.equals(new FullQualifiedName(NAMESPACE, et.getName())))
|
||||
@@ -165,6 +172,8 @@ public class JpaEdmProvider extends CsdlAbstractEdmProvider {
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
// save for future use
|
||||
cdslName2Type.put(entityTypeName, result);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
+11
-6
@@ -5,6 +5,7 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
@@ -19,6 +20,7 @@ import org.apache.olingo.commons.api.data.Property;
|
||||
import org.apache.olingo.commons.api.data.ValueType;
|
||||
import org.apache.olingo.commons.api.edm.EdmEntitySet;
|
||||
import org.apache.olingo.commons.api.edm.EdmEntityType;
|
||||
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
|
||||
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
|
||||
import org.apache.olingo.commons.api.format.ContentType;
|
||||
import org.apache.olingo.commons.api.http.HttpHeader;
|
||||
@@ -35,6 +37,7 @@ import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions
|
||||
import org.apache.olingo.server.api.serializer.ODataSerializer;
|
||||
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.baeldung.examples.olingo4.repository.RepositoryRegistry;
|
||||
@@ -110,8 +113,7 @@ public class JpaEntityCollectionProcessor implements CountEntityCollectionProces
|
||||
Long count = getCount(edmEntitySet, uriInfo);
|
||||
|
||||
// Finally: configure the response object: set the body, headers and status code
|
||||
response.setContent(new ByteArrayInputStream(count.toString()
|
||||
.getBytes()));
|
||||
response.setContent(new ByteArrayInputStream(count.toString().getBytes()));
|
||||
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
|
||||
response.setHeader(HttpHeader.CONTENT_TYPE, "text/plain");
|
||||
|
||||
@@ -130,9 +132,9 @@ public class JpaEntityCollectionProcessor implements CountEntityCollectionProces
|
||||
EntityCollection result = new EntityCollection();
|
||||
|
||||
repo.findAll()
|
||||
.stream()
|
||||
.forEach((it) -> result.getEntities()
|
||||
.add(entityMapper.map2entity(edmEntitySet, it)));
|
||||
.stream()
|
||||
.forEach((it) -> result.getEntities()
|
||||
.add(entityMapper.map2entity(edmEntitySet, it)));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -144,7 +146,7 @@ public class JpaEntityCollectionProcessor implements CountEntityCollectionProces
|
||||
* @param uriInfo
|
||||
* @return
|
||||
*/
|
||||
private Long getCount(EdmEntitySet edmEntitySet, UriInfo uriInfo) {
|
||||
protected Long getCount(EdmEntitySet edmEntitySet, UriInfo uriInfo) {
|
||||
|
||||
EdmEntityType type = edmEntitySet.getEntityType();
|
||||
JpaRepository<?, ?> repo = (JpaRepository<?, ?>)repositoryRegistry.getRepositoryForEntity(type);
|
||||
@@ -152,5 +154,8 @@ public class JpaEntityCollectionProcessor implements CountEntityCollectionProces
|
||||
|
||||
return repo.count();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
+10
-1
@@ -63,13 +63,22 @@ public class JpaEntityMapper {
|
||||
}
|
||||
|
||||
|
||||
private Object getPropertyValue(Object entry, String name) {
|
||||
public Object getPropertyValue(Object entry, String name) {
|
||||
try {
|
||||
return PropertyUtils.getProperty(entry,name);
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
throw new ODataRuntimeException("[E141] Unable to read property from entity, property=" + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPropertyValue(Object entry, String name,Object value) {
|
||||
try {
|
||||
PropertyUtils.setProperty(entry,name,value);
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
throw new ODataRuntimeException("[E141] Unable to read property from entity, property=" + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private URI createId(String entitySetName, Object id) {
|
||||
try {
|
||||
|
||||
+101
-175
File diff suppressed because it is too large
Load Diff
+7
-5
@@ -5,23 +5,25 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.olingo.commons.api.edm.EdmEntityType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RepositoryRegistry {
|
||||
|
||||
private Map<String,EdmEntityRepository<?>> repositoriesByClassName = new HashMap<>();
|
||||
private Map<String,JpaRepository<?,?>> repositoriesByClassName = new HashMap<>();
|
||||
|
||||
public RepositoryRegistry(List<EdmEntityRepository<?>> allRepositories) {
|
||||
|
||||
allRepositories.stream().forEach((r) ->
|
||||
repositoriesByClassName.put(r.getEdmEntityName(),(EdmEntityRepository<?>)r));
|
||||
allRepositories.stream()
|
||||
.forEach((r) ->
|
||||
repositoriesByClassName.put(r.getEdmEntityName(),(JpaRepository<?,?>)r));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public EdmEntityRepository<?> getRepositoryForEntity(EdmEntityType entityType) {
|
||||
EdmEntityRepository<?> repo = repositoriesByClassName.get(entityType.getName());
|
||||
public JpaRepository<?,?> getRepositoryForEntity(EdmEntityType entityType) {
|
||||
JpaRepository<?,?> repo = repositoriesByClassName.get(entityType.getName());
|
||||
return repo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ server:
|
||||
spring:
|
||||
jpa:
|
||||
show-sql: true
|
||||
open-in-view: false
|
||||
open-in-view: true
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
|
||||
Reference in New Issue
Block a user