Getting ready for slight refactor
This commit is contained in:
parent
708c5420d2
commit
6029925ff7
|
@ -0,0 +1,8 @@
|
||||||
|
COMPOSE_FILE=docker-compose.yml:./optional/docker-db-compose.yml
|
||||||
|
SQL_HOST=docker-mysql
|
||||||
|
SQL_USER=root
|
||||||
|
SQL_PASSWORD=root
|
||||||
|
SQL_DB_DRIVER=com.mysql.cj.jdbc.Driver
|
||||||
|
SQL_CONNECTION_STR=jdbc:mysql://docker-mysql/reso_data_dictionary_1_7?autoReconnect=true&maxReconnects=4
|
||||||
|
#&user=root&password=root
|
||||||
|
CERT_REPORT_FILENAME=RESODataDictionary-1.7.metadata-report.json
|
|
@ -280,13 +280,38 @@ public class GenericEntityCollectionProcessor implements EntityCollectionProcess
|
||||||
entCollection.setCount(size);
|
entCollection.setCount(size);
|
||||||
return entCollection;
|
return entCollection;
|
||||||
}
|
}
|
||||||
|
ArrayList<String> resourceRecordKeys = new ArrayList<>();
|
||||||
|
|
||||||
// add the lookups from the database.
|
// add the lookups from the database.
|
||||||
while (resultSet.next())
|
while (resultSet.next())
|
||||||
{
|
{
|
||||||
Entity ent = CommonDataProcessing.getEntityFromRow(resultSet,resource,selectLookup);
|
Entity ent = CommonDataProcessing.getEntityFromRow(resultSet,resource,selectLookup);
|
||||||
|
resourceRecordKeys.add( ent.getProperty(primaryFieldName).getValue().toString() );
|
||||||
productList.add(ent);
|
productList.add(ent);
|
||||||
}
|
}
|
||||||
|
List<FieldInfo> enumFields = CommonDataProcessing.gatherEnumFields(resource);
|
||||||
|
|
||||||
|
if (productList.size()>0 && resourceRecordKeys.size()>0 && enumFields.size()>0)
|
||||||
|
{
|
||||||
|
queryString = "select * from lookup_value";
|
||||||
|
queryString = queryString + " WHERE ResourceRecordKey in (\"" + String.join("','", resourceRecordKeys ) + "\")";
|
||||||
|
|
||||||
|
LOG.info("SQL Query: "+queryString);
|
||||||
|
resultSet = statement.executeQuery(queryString);
|
||||||
|
|
||||||
|
// add the lookups from the database.
|
||||||
|
HashMap<String, HashMap<String, Object>> entities = new HashMap<>();
|
||||||
|
while (resultSet.next())
|
||||||
|
{
|
||||||
|
CommonDataProcessing.getEntityValues(resultSet, entities, enumFields);
|
||||||
|
}
|
||||||
|
for (Entity product :productList)
|
||||||
|
{
|
||||||
|
String key = product.getProperty(primaryFieldName).getValue().toString();
|
||||||
|
HashMap<String, Object> enumValues = entities.get(key);
|
||||||
|
CommonDataProcessing.setEntityEnums(enumValues,product,enumFields);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,14 @@ package org.reso.service.data;
|
||||||
import org.apache.olingo.commons.api.data.*;
|
import org.apache.olingo.commons.api.data.*;
|
||||||
import org.apache.olingo.commons.api.edm.EdmEntitySet;
|
import org.apache.olingo.commons.api.edm.EdmEntitySet;
|
||||||
import org.apache.olingo.commons.api.edm.EdmEntityType;
|
import org.apache.olingo.commons.api.edm.EdmEntityType;
|
||||||
|
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
|
||||||
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
|
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
|
||||||
import org.apache.olingo.commons.api.format.ContentType;
|
import org.apache.olingo.commons.api.format.ContentType;
|
||||||
import org.apache.olingo.commons.api.http.HttpHeader;
|
import org.apache.olingo.commons.api.http.HttpHeader;
|
||||||
import org.apache.olingo.commons.api.http.HttpStatusCode;
|
import org.apache.olingo.commons.api.http.HttpStatusCode;
|
||||||
import org.apache.olingo.server.api.*;
|
import org.apache.olingo.server.api.*;
|
||||||
|
import org.apache.olingo.server.api.deserializer.DeserializerResult;
|
||||||
|
import org.apache.olingo.server.api.deserializer.ODataDeserializer;
|
||||||
import org.apache.olingo.server.api.processor.EntityProcessor;
|
import org.apache.olingo.server.api.processor.EntityProcessor;
|
||||||
import org.apache.olingo.server.api.serializer.EntitySerializerOptions;
|
import org.apache.olingo.server.api.serializer.EntitySerializerOptions;
|
||||||
import org.apache.olingo.server.api.serializer.ODataSerializer;
|
import org.apache.olingo.server.api.serializer.ODataSerializer;
|
||||||
|
@ -22,6 +25,7 @@ import org.reso.service.data.meta.EnumFieldInfo;
|
||||||
import org.reso.service.data.meta.EnumValueInfo;
|
import org.reso.service.data.meta.EnumValueInfo;
|
||||||
import org.reso.service.data.meta.FieldInfo;
|
import org.reso.service.data.meta.FieldInfo;
|
||||||
import org.reso.service.data.meta.ResourceInfo;
|
import org.reso.service.data.meta.ResourceInfo;
|
||||||
|
import org.reso.service.data.meta.builder.FieldObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -30,6 +34,7 @@ import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -160,10 +165,13 @@ public class GenericEntityProcessor implements EntityProcessor
|
||||||
resultSet = statement.executeQuery(queryString);
|
resultSet = statement.executeQuery(queryString);
|
||||||
|
|
||||||
// add the lookups from the database.
|
// add the lookups from the database.
|
||||||
|
HashMap<String, HashMap<String, Object>> entities = new HashMap<>();
|
||||||
HashMap<String, Object> enumValues = new HashMap<>();
|
HashMap<String, Object> enumValues = new HashMap<>();
|
||||||
|
entities.put(resourceRecordKeys.get(0), enumValues);
|
||||||
|
|
||||||
while (resultSet.next())
|
while (resultSet.next())
|
||||||
{
|
{
|
||||||
CommonDataProcessing.getEntityValues(resultSet, enumValues, enumFields);
|
CommonDataProcessing.getEntityValues(resultSet, entities, enumFields);
|
||||||
}
|
}
|
||||||
CommonDataProcessing.setEntityEnums(enumValues,product,enumFields);
|
CommonDataProcessing.setEntityEnums(enumValues,product,enumFields);
|
||||||
|
|
||||||
|
@ -189,7 +197,107 @@ public class GenericEntityProcessor implements EntityProcessor
|
||||||
@Override public void createEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType requestFormat, ContentType responseFormat)
|
@Override public void createEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType requestFormat, ContentType responseFormat)
|
||||||
throws ODataApplicationException, ODataLibraryException
|
throws ODataApplicationException, ODataLibraryException
|
||||||
{
|
{
|
||||||
|
// 1. retrieve the Entity Type
|
||||||
|
List<UriResource> 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();
|
||||||
|
|
||||||
|
ResourceInfo resource = this.resourceList.get(resourcePaths.get(0).toString());
|
||||||
|
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
|
||||||
|
|
||||||
|
// 2. create the data in backend
|
||||||
|
// 2.1. retrieve the payload from the POST request for the entity to create and deserialize it
|
||||||
|
InputStream requestInputStream = request.getBody();
|
||||||
|
ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
|
||||||
|
DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
|
||||||
|
Entity requestEntity = result.getEntity();
|
||||||
|
// 2.2 do the creation in backend, which returns the newly created entity
|
||||||
|
//Entity createdEntity = storage.createEntityData(edmEntitySet, requestEntity);
|
||||||
|
HashMap<String, Object> mappedObj = CommonDataProcessing.translateEntityToMap(requestEntity);
|
||||||
|
String primaryFieldName = resource.getPrimaryKeyName();
|
||||||
|
List<FieldInfo> enumFields = CommonDataProcessing.gatherEnumFields(resource);
|
||||||
|
ArrayList<Object> enumValues = new ArrayList<>();
|
||||||
|
for (FieldInfo field: enumFields)
|
||||||
|
{
|
||||||
|
if (field.isCollection())
|
||||||
|
{
|
||||||
|
String fieldName = field.getFieldName();
|
||||||
|
Object value = mappedObj.remove(fieldName);
|
||||||
|
enumValues.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
saveData(resource, mappedObj);
|
||||||
|
|
||||||
|
// 3. serialize the response (we have to return the created entity)
|
||||||
|
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
|
||||||
|
// expand and select currently not supported
|
||||||
|
EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();
|
||||||
|
|
||||||
|
ODataSerializer serializer = this.odata.createSerializer(responseFormat);
|
||||||
|
SerializerResult serializedResponse = serializer.entity(serviceMetadata, edmEntityType, requestEntity, options);
|
||||||
|
//SerializerResult serializedResponse = serializer.entity(serviceMetadata, edmEntityType, createdEntity, options);
|
||||||
|
|
||||||
|
//4. configure the response object
|
||||||
|
response.setContent(serializedResponse.getContent());
|
||||||
|
response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
|
||||||
|
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); }
|
||||||
|
|
||||||
|
|
||||||
|
private void saveData(ResourceInfo resource, HashMap<String, Object> mappedObj)
|
||||||
|
{
|
||||||
|
String queryString = "insert into " + resource.getTableName();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Statement statement = connect.createStatement();
|
||||||
|
ArrayList<String> columnNames = new ArrayList<>();
|
||||||
|
ArrayList<String> columnValues = new ArrayList<>();
|
||||||
|
|
||||||
|
ArrayList<FieldInfo> fieldList = resource.getFieldList();
|
||||||
|
HashMap<String, FieldInfo> fieldLookup = new HashMap<>();
|
||||||
|
|
||||||
|
for (FieldInfo field: fieldList)
|
||||||
|
{
|
||||||
|
fieldLookup.put(field.getFieldName(),field);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String,Object> entrySet: mappedObj.entrySet())
|
||||||
|
{
|
||||||
|
String key = entrySet.getKey();
|
||||||
|
Object value = entrySet.getValue();
|
||||||
|
columnNames.add(key);
|
||||||
|
|
||||||
|
FieldInfo field = fieldLookup.get(key);
|
||||||
|
|
||||||
|
if (value==null)
|
||||||
|
{
|
||||||
|
columnValues.add("NULL");
|
||||||
|
}
|
||||||
|
else if (field.getType().equals(EdmPrimitiveTypeKind.String.getFullQualifiedName()))
|
||||||
|
{
|
||||||
|
columnValues.add('"'+value.toString()+'"');
|
||||||
|
}
|
||||||
|
else if (field.getType().equals(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName()))
|
||||||
|
{
|
||||||
|
columnValues.add('"'+value.toString()+'"');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
columnValues.add(value.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
queryString = queryString+" ("+String.join(",",columnNames)+") values ("+String.join(",",columnValues)+")";
|
||||||
|
|
||||||
|
//boolean success = statement.execute(queryString);
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
LOG.error(e.getMessage());
|
||||||
|
}
|
||||||
|
// Result set get the result of the SQL query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -150,13 +150,20 @@ public class CommonDataProcessing
|
||||||
return ent;
|
return ent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void getEntityValues(ResultSet resultSet,HashMap<String, Object> entity, List<FieldInfo> enumFields) throws SQLException
|
public static void getEntityValues(ResultSet resultSet,HashMap<String, HashMap<String, Object>> entities, List<FieldInfo> enumFields) throws SQLException
|
||||||
{
|
{
|
||||||
HashMap<String, EnumFieldInfo> enumFieldLookup = new HashMap<>();
|
HashMap<String, EnumFieldInfo> enumFieldLookup = new HashMap<>();
|
||||||
EnumFieldInfo field = null;
|
EnumFieldInfo field = null;
|
||||||
|
|
||||||
String value = resultSet.getString("LookupKey");
|
String value = resultSet.getString("LookupKey");
|
||||||
String fieldName = resultSet.getString("FieldName");
|
String fieldName = resultSet.getString("FieldName");
|
||||||
|
String resourceRecordKey = resultSet.getString("ResourceRecordKey");
|
||||||
|
HashMap<String, Object> entity = entities.get(resourceRecordKey);
|
||||||
|
if (entity==null)
|
||||||
|
{
|
||||||
|
entity = new HashMap<>();
|
||||||
|
entities.put(resourceRecordKey, entity);
|
||||||
|
}
|
||||||
|
|
||||||
for (FieldInfo f : enumFields)
|
for (FieldInfo f : enumFields)
|
||||||
{
|
{
|
||||||
|
@ -253,6 +260,22 @@ public class CommonDataProcessing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashMap<String,Object> translateEntityToMap(Entity entity)
|
||||||
|
{
|
||||||
|
HashMap<String,Object> result = new HashMap<>();
|
||||||
|
|
||||||
|
List<Property> properties = entity.getProperties();
|
||||||
|
|
||||||
|
for (Property property: properties)
|
||||||
|
{
|
||||||
|
String name = property.getName();
|
||||||
|
Object value = property.getValue();
|
||||||
|
result.put(name,value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static ArrayList<HashMap<String,Object>> loadAllResource(Connection connect, ResourceInfo resource)
|
public static ArrayList<HashMap<String,Object>> loadAllResource(Connection connect, ResourceInfo resource)
|
||||||
{
|
{
|
||||||
ArrayList<FieldInfo> fields = resource.getFieldList();
|
ArrayList<FieldInfo> fields = resource.getFieldList();
|
||||||
|
|
Loading…
Reference in New Issue