From 6029925ff7694a5b1cd5d540819095de28f82299 Mon Sep 17 00:00:00 2001 From: michaelpede Date: Mon, 25 Oct 2021 11:10:26 -0700 Subject: [PATCH] Getting ready for slight refactor --- env-default | 8 ++ .../GenericEntityCollectionProcessor.java | 25 ++++ .../service/data/GenericEntityProcessor.java | 110 +++++++++++++++++- .../data/common/CommonDataProcessing.java | 25 +++- 4 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 env-default diff --git a/env-default b/env-default new file mode 100644 index 0000000..3fb29f0 --- /dev/null +++ b/env-default @@ -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 \ No newline at end of file diff --git a/src/main/java/org/reso/service/data/GenericEntityCollectionProcessor.java b/src/main/java/org/reso/service/data/GenericEntityCollectionProcessor.java index 77371fe..1a20adf 100644 --- a/src/main/java/org/reso/service/data/GenericEntityCollectionProcessor.java +++ b/src/main/java/org/reso/service/data/GenericEntityCollectionProcessor.java @@ -280,13 +280,38 @@ public class GenericEntityCollectionProcessor implements EntityCollectionProcess entCollection.setCount(size); return entCollection; } + ArrayList resourceRecordKeys = new ArrayList<>(); // add the lookups from the database. while (resultSet.next()) { Entity ent = CommonDataProcessing.getEntityFromRow(resultSet,resource,selectLookup); + resourceRecordKeys.add( ent.getProperty(primaryFieldName).getValue().toString() ); productList.add(ent); } + List 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> entities = new HashMap<>(); + while (resultSet.next()) + { + CommonDataProcessing.getEntityValues(resultSet, entities, enumFields); + } + for (Entity product :productList) + { + String key = product.getProperty(primaryFieldName).getValue().toString(); + HashMap enumValues = entities.get(key); + CommonDataProcessing.setEntityEnums(enumValues,product,enumFields); + } + } statement.close(); diff --git a/src/main/java/org/reso/service/data/GenericEntityProcessor.java b/src/main/java/org/reso/service/data/GenericEntityProcessor.java index 3b1d2ba..a808d6c 100644 --- a/src/main/java/org/reso/service/data/GenericEntityProcessor.java +++ b/src/main/java/org/reso/service/data/GenericEntityProcessor.java @@ -4,11 +4,14 @@ package org.reso.service.data; import org.apache.olingo.commons.api.data.*; import org.apache.olingo.commons.api.edm.EdmEntitySet; 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.format.ContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; 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.serializer.EntitySerializerOptions; 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.FieldInfo; import org.reso.service.data.meta.ResourceInfo; +import org.reso.service.data.meta.builder.FieldObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,6 +34,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.sql.Connection; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.*; @@ -160,10 +165,13 @@ public class GenericEntityProcessor implements EntityProcessor resultSet = statement.executeQuery(queryString); // add the lookups from the database. + HashMap> entities = new HashMap<>(); HashMap enumValues = new HashMap<>(); + entities.put(resourceRecordKeys.get(0), enumValues); + while (resultSet.next()) { - CommonDataProcessing.getEntityValues(resultSet, enumValues, enumFields); + CommonDataProcessing.getEntityValues(resultSet, entities, 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) throws ODataApplicationException, ODataLibraryException { + // 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(); + 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 mappedObj = CommonDataProcessing.translateEntityToMap(requestEntity); + String primaryFieldName = resource.getPrimaryKeyName(); + List enumFields = CommonDataProcessing.gatherEnumFields(resource); + ArrayList 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 mappedObj) + { + String queryString = "insert into " + resource.getTableName(); + try + { + Statement statement = connect.createStatement(); + ArrayList columnNames = new ArrayList<>(); + ArrayList columnValues = new ArrayList<>(); + + ArrayList fieldList = resource.getFieldList(); + HashMap fieldLookup = new HashMap<>(); + + for (FieldInfo field: fieldList) + { + fieldLookup.put(field.getFieldName(),field); + } + + for (Map.Entry 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 } diff --git a/src/main/java/org/reso/service/data/common/CommonDataProcessing.java b/src/main/java/org/reso/service/data/common/CommonDataProcessing.java index 5c65bff..4227182 100644 --- a/src/main/java/org/reso/service/data/common/CommonDataProcessing.java +++ b/src/main/java/org/reso/service/data/common/CommonDataProcessing.java @@ -150,13 +150,20 @@ public class CommonDataProcessing return ent; } - public static void getEntityValues(ResultSet resultSet,HashMap entity, List enumFields) throws SQLException + public static void getEntityValues(ResultSet resultSet,HashMap> entities, List enumFields) throws SQLException { HashMap enumFieldLookup = new HashMap<>(); EnumFieldInfo field = null; String value = resultSet.getString("LookupKey"); String fieldName = resultSet.getString("FieldName"); + String resourceRecordKey = resultSet.getString("ResourceRecordKey"); + HashMap entity = entities.get(resourceRecordKey); + if (entity==null) + { + entity = new HashMap<>(); + entities.put(resourceRecordKey, entity); + } for (FieldInfo f : enumFields) { @@ -253,6 +260,22 @@ public class CommonDataProcessing } } + public static HashMap translateEntityToMap(Entity entity) + { + HashMap result = new HashMap<>(); + + List properties = entity.getProperties(); + + for (Property property: properties) + { + String name = property.getName(); + Object value = property.getValue(); + result.put(name,value); + } + + return result; + } + public static ArrayList> loadAllResource(Connection connect, ResourceInfo resource) { ArrayList fields = resource.getFieldList();