From bd28def9a6a177a7c060fbc759dce333e6f7b14b Mon Sep 17 00:00:00 2001 From: michaelpede Date: Thu, 1 Apr 2021 00:14:28 -0700 Subject: [PATCH] SQL read and enum exploration. --- .env | 4 +- Dockerfile | 5 +- build.sh | 1 - docker-compose.yml | 6 +- optional/docker-db-compose.yml | 3 +- pom.xml | 7 ++ .../data/LookupEntityCollectionProcessor.java | 85 ++++++++++++++----- .../edmprovider/LookupEdmProvider.java | 7 ++ .../org/reso/service/servlet/RESOservlet.java | 24 +++++- 9 files changed, 113 insertions(+), 29 deletions(-) diff --git a/.env b/.env index 318092a..b8f782c 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ COMPOSE_FILE=docker-compose.yml:./optional/docker-db-compose.yml -WAIT_HOSTS=mysql:3306 \ No newline at end of file +SQL_HOST=docker-mysql +SQL_USER=root +SQL_PASSWORD=root diff --git a/Dockerfile b/Dockerfile index df82b1d..2ba3c07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,6 @@ FROM tomcat:latest -COPY ./target/RESOservice-1.0.war /usr/local/tomcat/webapps/ +#Not needed while volume mapped for development +#COPY ./target/RESOservice-1.0.war /usr/local/tomcat/webapps/ -CMD ["catalina.sh", "run"] \ No newline at end of file +CMD ["catalina.sh", "run"] diff --git a/build.sh b/build.sh index 2aa91ab..12ae197 100644 --- a/build.sh +++ b/build.sh @@ -4,4 +4,3 @@ mvn compile mvn package docker-compose build -rm -rf temp/spring-server-generated diff --git a/docker-compose.yml b/docker-compose.yml index bdb4537..05cd43a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,10 @@ services: ports: - 8080:8080 environment: - - WAIT_HOSTS + - SQL_HOST + - SQL_USER + - SQL_PASSWORD + volumes: + - ./target:/usr/local/tomcat/webapps # depends_on: # - docker-mysql diff --git a/optional/docker-db-compose.yml b/optional/docker-db-compose.yml index 3e05f50..fec8bb6 100644 --- a/optional/docker-db-compose.yml +++ b/optional/docker-db-compose.yml @@ -4,8 +4,9 @@ services: restart: always container_name: docker-mysql image: mysql + command: --default-authentication-plugin=mysql_native_password environment: - MYSQL_DATABASE: odata_manager + MYSQL_DATABASE: reso_data_dictionary_1_7 MYSQL_ROOT_PASSWORD: root MYSQL_ROOT_HOST: '%' volumes: diff --git a/pom.xml b/pom.xml index cebec99..b4e881e 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,13 @@ provided + + mysql + mysql-connector-java + 8.0.23 + runtime + + org.apache.olingo odata-server-api diff --git a/src/main/java/org/reso/service/data/LookupEntityCollectionProcessor.java b/src/main/java/org/reso/service/data/LookupEntityCollectionProcessor.java index cedbf2b..af5a892 100644 --- a/src/main/java/org/reso/service/data/LookupEntityCollectionProcessor.java +++ b/src/main/java/org/reso/service/data/LookupEntityCollectionProcessor.java @@ -17,16 +17,33 @@ import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriResource; import org.apache.olingo.server.api.uri.UriResourceEntitySet; import org.reso.service.edmprovider.LookupEdmProvider; +import org.reso.service.servlet.RESOservlet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.List; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Date; +import java.util.Map; + public class LookupEntityCollectionProcessor implements EntityCollectionProcessor { private OData odata; private ServiceMetadata serviceMetadata; + private Connection connect = null; + private Statement statement = null; + private PreparedStatement preparedStatement = null; + private ResultSet resultSet = null; + private static final Logger LOG = LoggerFactory.getLogger(LookupEntityCollectionProcessor.class); public void init(OData odata, ServiceMetadata serviceMetadata) { this.odata = odata; @@ -71,30 +88,54 @@ public class LookupEntityCollectionProcessor implements EntityCollectionProcesso if(LookupEdmProvider.ES_LOOKUPS_NAME.equals(edmEntitySet.getName())) { List productList = lookupsCollection.getEntities(); - // add some sample product entities - final Entity e1 = new Entity() - .addProperty(new Property(null, "LookupKey", ValueType.PRIMITIVE, "ABC123")) - .addProperty(new Property(null, "LookupName", ValueType.PRIMITIVE, "CountyOrParish")) - .addProperty(new Property(null, "LookupValue", ValueType.PRIMITIVE, - "Los Angeles County")); - e1.setId(createId("Lookups", 1)); - productList.add(e1); + Map properties = System.getenv(); - final Entity e2 = new Entity() - .addProperty(new Property(null, "LookupKey", ValueType.PRIMITIVE, "BCD124")) - .addProperty(new Property(null, "LookupName", ValueType.PRIMITIVE, "CountyOrParish")) - .addProperty(new Property(null, "LookupValue", ValueType.PRIMITIVE, - "Ventura County")); - e2.setId(createId("Lookups", 1)); - productList.add(e2); + try { - final Entity e3 = new Entity() - .addProperty(new Property(null, "LookupKey", ValueType.PRIMITIVE, "CDE125")) - .addProperty(new Property(null, "LookupName", ValueType.PRIMITIVE, "CountyOrParish")) - .addProperty(new Property(null, "LookupValue", ValueType.PRIMITIVE, - "Contra Costa County")); - e3.setId(createId("Lookups", 1)); - productList.add(e3); + String mysqlHost = properties.get("SQL_HOST"); + String mysqlUser = properties.get("SQL_USER"); + String mysqlPwd = properties.get("SQL_PASSWORD"); + + LOG.info("looking to connect to jdbc:mysql://"+mysqlHost+"/reso_data_dictionary_1_7"); + connect = DriverManager + .getConnection("jdbc:mysql://"+mysqlHost+"/reso_data_dictionary_1_7?" + + "user="+mysqlUser+"&password="+mysqlPwd); + + // Statements allow to issue SQL queries to the database + statement = connect.createStatement(); + // Result set get the result of the SQL query + resultSet = statement.executeQuery("select * from lookup"); + + // add the lookups from the database. + while (resultSet.next()) + { + String lookupKey = resultSet.getString("LookupKey"); + Entity ent = new Entity() + .addProperty(new Property(null, "LookupKey", ValueType.PRIMITIVE, lookupKey)) + .addProperty(new Property(null, "LookupName", ValueType.PRIMITIVE, resultSet.getString("LookupName"))) + .addProperty(new Property(null, "LookupValue", ValueType.PRIMITIVE,resultSet.getString("LookupValue"))) + .addProperty(new Property(null, "StandardLookupValue", ValueType.PRIMITIVE,resultSet.getString("StandardLookupValue"))) + .addProperty(new Property(null, "LegacyOdataValue", ValueType.PRIMITIVE,resultSet.getString("LegacyOdataValue"))) + .addProperty(new Property(null, "ModificationTimestamp", ValueType.PRIMITIVE,resultSet.getDate("ModificationTimestamp"))); + + ent.setId(createId("Lookups", lookupKey)); + productList.add(ent); + } + + statement.close(); + + } catch (Exception e) { + LOG.error("Server Error occurred in reading Lookups", e); + return lookupsCollection; + } finally { + try + { + connect.close(); + } catch (Exception e) { + LOG.error("Server Error closing connection", e); + } + return lookupsCollection; + } } return lookupsCollection; diff --git a/src/main/java/org/reso/service/edmprovider/LookupEdmProvider.java b/src/main/java/org/reso/service/edmprovider/LookupEdmProvider.java index 7831656..3352701 100644 --- a/src/main/java/org/reso/service/edmprovider/LookupEdmProvider.java +++ b/src/main/java/org/reso/service/edmprovider/LookupEdmProvider.java @@ -102,6 +102,13 @@ public class LookupEdmProvider extends CsdlAbstractEdmProvider List schemas = new ArrayList(); schemas.add(schema); + CsdlEnumType type = new CsdlEnumType(); + type.setMembers(new ArrayList()); + type.setName("EnumTest"); + type.setUnderlyingType(EdmPrimitiveTypeKind.Int64.getFullQualifiedName()); + + schema.getEnumTypes().add(type); + return schemas; } diff --git a/src/main/java/org/reso/service/servlet/RESOservlet.java b/src/main/java/org/reso/service/servlet/RESOservlet.java index a7f80e3..85967f6 100644 --- a/src/main/java/org/reso/service/servlet/RESOservlet.java +++ b/src/main/java/org/reso/service/servlet/RESOservlet.java @@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory; import java.io.*; import java.util.ArrayList; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.*; @@ -20,6 +21,27 @@ public class RESOservlet extends HttpServlet private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(RESOservlet.class); + + @Override public void init() throws ServletException + { + super.init(); + + Map env = System.getenv(); + for (String envName : env.keySet()) { + LOG.debug( String.format("ENV VAR: %s=%s%n", + envName, + env.get(envName)) + ); + } + + try { + Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); + } catch (Exception e) { + LOG.error("Server Error occurred in connecting to the database", e); + } + } + + protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { try { // create odata handler and configure it with CsdlEdmProvider and Processor @@ -32,7 +54,7 @@ public class RESOservlet extends HttpServlet handler.process(req, resp); } catch (RuntimeException e) { - LOG.error("Server Error occurred in ExampleServlet", e); + LOG.error("Server Error occurred in RESOservlet", e); throw new ServletException(e); } }