SQL read and enum exploration.

This commit is contained in:
michaelpede 2021-04-01 00:14:28 -07:00
parent 7d462cbc10
commit bd28def9a6
9 changed files with 113 additions and 29 deletions

4
.env
View File

@ -1,2 +1,4 @@
COMPOSE_FILE=docker-compose.yml:./optional/docker-db-compose.yml
WAIT_HOSTS=mysql:3306
SQL_HOST=docker-mysql
SQL_USER=root
SQL_PASSWORD=root

View File

@ -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"]

View File

@ -4,4 +4,3 @@ mvn compile
mvn package
docker-compose build
rm -rf temp/spring-server-generated

View File

@ -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

View File

@ -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:

View File

@ -26,6 +26,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>odata-server-api</artifactId>

View File

@ -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<Entity> 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<String, String> 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;

View File

@ -102,6 +102,13 @@ public class LookupEdmProvider extends CsdlAbstractEdmProvider
List<CsdlSchema> schemas = new ArrayList<CsdlSchema>();
schemas.add(schema);
CsdlEnumType type = new CsdlEnumType();
type.setMembers(new ArrayList<CsdlEnumMember>());
type.setName("EnumTest");
type.setUnderlyingType(EdmPrimitiveTypeKind.Int64.getFullQualifiedName());
schema.getEnumTypes().add(type);
return schemas;
}

View File

@ -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<String, String> 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);
}
}