Merge pull request #5 from pedem/main

I accidentally changed users, which forked the repro
This commit is contained in:
michaelpede 2021-08-18 14:56:03 -07:00 committed by GitHub
commit bd6ccf2116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 720 additions and 68 deletions

9
.env
View File

@ -2,7 +2,8 @@ COMPOSE__FILE=docker-compose.yml:./optional/docker-db-compose.yml
SQL_HOST=192.168.1.71 SQL_HOST=192.168.1.71
SQL_USER=mpede SQL_USER=mpede
SQL_PASSWORD=password SQL_PASSWORD=password
#SQL_DB_DRIVER=com.mysql.cj.jdbc.Driver SQL_DB_DRIVER=com.mysql.cj.jdbc.Driver
SQL_DB_DRIVER=org.postgresql.Driver #SQL_DB_DRIVER=org.postgresql.Driver
#SQL_CONNECTION_STR=jdbc:mysql://192.168.1.71/reso_data_dictionary_1_7?autoReconnect=true&maxReconnects=4&user=mpede&password=password SQL_CONNECTION_STR=jdbc:mysql://192.168.1.71/reso_data_dictionary_1_7?autoReconnect=true&maxReconnects=4&user=mpede&password=password
SQL_CONNECTION_STR=jdbc:postgresql://192.168.1.71:5432/reso_data_dictionary_1_7?autoReconnect=true&maxReconnects=4 #SQL_CONNECTION_STR=jdbc:postgresql://192.168.1.71:5432/reso_data_dictionary_1_7?autoReconnect=true&maxReconnects=4
CERT_REPORT_FILENAME=RESODataDictionary-1.7.metadata-report.json

2
.gitignore vendored
View File

@ -19,6 +19,7 @@ hs_err_pid*
/sql /sql
/web /web
/build /build
/.idea
# Project Genereated files # Project Genereated files
.pmd .pmd
@ -38,6 +39,7 @@ nb-configuration.xml
.externalToolBuilders .externalToolBuilders
maven-eclipse.xml maven-eclipse.xml
dependency-reduced-pom.xml dependency-reduced-pom.xml
src/main/java/org/reso/service/data/definition/custom/*.java
src/main/java/org/reso/service/data/definition/ContactListingNotesDefinition.java src/main/java/org/reso/service/data/definition/ContactListingNotesDefinition.java
src/main/java/org/reso/service/data/definition/ContactListingsDefinition.java src/main/java/org/reso/service/data/definition/ContactListingsDefinition.java
src/main/java/org/reso/service/data/definition/ContactsDefinition.java src/main/java/org/reso/service/data/definition/ContactsDefinition.java

View File

@ -5,40 +5,8 @@ HOME_DIR=`dirname ${REAL_VAR0}`
TEMP_DIR="${HOME_DIR}/temp" TEMP_DIR="${HOME_DIR}/temp"
SQL_DIR="${HOME_DIR}/sql" SQL_DIR="${HOME_DIR}/sql"
# Ensure we have directories set up docker build -t reso-builder -f docker/docker-builder .
[ -d "${TEMP_DIR}" ] && echo "temp directory found." || mkdir ${TEMP_DIR} docker run --name builder --mount type=bind,source="${HOME_DIR}",target=/usr/src/app -t reso-builder
[ -d "${SQL_DIR}" ] && echo "sql directory found." || mkdir ${SQL_DIR}
if [ -z "${SQL_HOST}" ]
then
# Get the Web API Commander, needed to generate the test database
if ! wget https://github.com/RESOStandards/web-api-commander/releases/download/current-version/web-api-commander.jar -O temp/web-api-commander.jar
then
echo "WGET not installed. trying CURL."
if ! curl -L https://github.com/RESOStandards/web-api-commander/releases/download/current-version/web-api-commander.jar --output temp/web-api-commander.jar
then
echo "CURL not installed. Exiting build."
exit
fi
fi
java -jar temp/web-api-commander.jar --generateReferenceDDL --useKeyNumeric > sql/reso-reference-ddl-dd-1.7.numeric-keys.sql
# The following lines should be independent of the SQL logic.
java -jar temp/web-api-commander.jar --generateResourceInfoModels
mv ResourceInfoModels/* src/main/java/org/reso/service/data/definition/
else
COMPOSE_FILE="docker-compose.yml"
fi
if ! mvn compile
then
echo "Maven could not be found."
exit
else
mvn package
fi
if ! docker-compose build if ! docker-compose build
then then

7
docker/docker-builder Normal file
View File

@ -0,0 +1,7 @@
FROM ubuntu:20.10
WORKDIR /usr/src/app
RUN apt-get update && apt-get install -y wget curl pip maven docker-compose
CMD ./docker/scripts/build.sh

42
docker/scripts/build.sh Normal file
View File

@ -0,0 +1,42 @@
#!/bin/bash
REAL_VAR0=`readlink -f $0`
HOME_DIR=`dirname ${REAL_VAR0}`
TEMP_DIR="${HOME_DIR}/temp"
SQL_DIR="${HOME_DIR}/sql"
# Ensure we have directories set up
[ -d "${TEMP_DIR}" ] && echo "temp directory found." || mkdir ${TEMP_DIR}
[ -d "${SQL_DIR}" ] && echo "sql directory found." || mkdir ${SQL_DIR}
if [ -z "${SQL_HOST}" ]
then
# Get the Web API Commander, needed to generate the test database
if ! wget https://github.com/RESOStandards/web-api-commander/releases/download/current-version/web-api-commander.jar -O temp/web-api-commander.jar
then
echo "WGET not installed. trying CURL."
if ! curl -L https://github.com/RESOStandards/web-api-commander/releases/download/current-version/web-api-commander.jar --output temp/web-api-commander.jar
then
echo "CURL not installed. Exiting build."
exit
fi
fi
java -jar temp/web-api-commander.jar --generateReferenceDDL --useKeyNumeric > sql/reso-reference-ddl-dd-1.7.numeric-keys.sql
# The following lines should be independent of the SQL logic.
java -jar temp/web-api-commander.jar --generateResourceInfoModels
mv ResourceInfoModels/* src/main/java/org/reso/service/data/definition/
else
COMPOSE_FILE="docker-compose.yml"
fi
if ! mvn compile
then
echo "Maven could not be found."
exit
else
mvn package
fi

View File

@ -103,6 +103,11 @@
<version>2.11.4</version> <version>2.11.4</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -85,7 +85,16 @@ public class GenericEntityCollectionProcessor implements EntityCollectionProcess
// 2nd: fetch the data from backend for this requested EntitySetName // 2nd: fetch the data from backend for this requested EntitySetName
// it has to be delivered as EntitySet object // it has to be delivered as EntitySet object
EntityCollection entitySet = getData(edmEntitySet, uriInfo, isCount, resource); EntityCollection entitySet;
if (resource.useCustomDatasource() )
{
entitySet = resource.getData(edmEntitySet, uriInfo, isCount);
}
else
{
entitySet = getData(edmEntitySet, uriInfo, isCount, resource);
}
// 3rd: create a serializer based on the requested format (json) // 3rd: create a serializer based on the requested format (json)
try try
@ -251,7 +260,7 @@ public class GenericEntityCollectionProcessor implements EntityCollectionProcess
if (uriResource instanceof UriResourcePrimitiveProperty) if (uriResource instanceof UriResourcePrimitiveProperty)
{ {
EdmProperty edmProperty = ((UriResourcePrimitiveProperty) uriResource).getProperty(); EdmProperty edmProperty = ((UriResourcePrimitiveProperty) uriResource).getProperty();
final String sortPropertyName = edmProperty.getName().toLowerCase(); final String sortPropertyName = edmProperty.getName(); // .toLowerCase();
queryString = queryString + " ORDER BY "+sortPropertyName; queryString = queryString + " ORDER BY "+sortPropertyName;
if(orderByItem.isDescending()) if(orderByItem.isDescending())
{ {

View File

@ -65,7 +65,15 @@ public class GenericEntityProcessor implements EntityProcessor
// 2. retrieve the data from backend // 2. retrieve the data from backend
List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates(); List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
Entity entity = getData(edmEntitySet, keyPredicates, resource); Entity entity;
if (resource.useCustomDatasource() )
{
entity = resource.getData(edmEntitySet, keyPredicates);
}
else
{
entity = getData(edmEntitySet, keyPredicates, resource);
}
// 3. serialize // 3. serialize
EdmEntityType entityType = edmEntitySet.getEntityType(); EdmEntityType entityType = edmEntitySet.getEntityType();
@ -103,7 +111,7 @@ public class GenericEntityProcessor implements EntityProcessor
for (final UriParameter key : keyPredicates) for (final UriParameter key : keyPredicates)
{ {
// key // key
String keyName = key.getName().toLowerCase(); String keyName = key.getName(); // .toLowerCase();
String keyValue = key.getText(); String keyValue = key.getText();
if (sqlCriteria==null) if (sqlCriteria==null)
{ {

View File

@ -0,0 +1,218 @@
package org.reso.service.data.definition;
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
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.EdmPrimitiveTypeKind;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriParameter;
import org.apache.olingo.server.api.uri.queryoption.FilterOption;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.reso.service.data.common.CommonDataProcessing;
import org.reso.service.data.meta.BreakdownOfFilterExpressionVisitor;
import org.reso.service.data.meta.FieldInfo;
import org.reso.service.data.meta.ResourceInfo;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
public class FieldDefinition extends ResourceInfo
{
private static String STANDARD_NAME = "RESO.OData.Metadata.StandardName";
private static ArrayList<FieldInfo> fieldList = null;
private ArrayList<ResourceInfo> resources;
public FieldDefinition()
{
this.tableName = "field"; // Never used
this.resourcesName = "Field";
this.resourceName = "Field";
this.primaryKeyName = "FieldKey";
}
public ArrayList<FieldInfo> getFieldList()
{
return FieldDefinition.getStaticFieldList();
}
public static ArrayList<FieldInfo> getStaticFieldList()
{
if (null!= FieldDefinition.fieldList)
{
return FieldDefinition.fieldList;
}
ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
FieldDefinition.fieldList = list;
FieldInfo fieldInfo = null;
fieldInfo = new FieldInfo("FieldKey", EdmPrimitiveTypeKind.String.getFullQualifiedName());
fieldInfo.addAnnotation("Field Key Field", "RESO.OData.Metadata.DisplayName");
list.add(fieldInfo);
fieldInfo = new FieldInfo("ResourceName", EdmPrimitiveTypeKind.String.getFullQualifiedName());
list.add(fieldInfo);
fieldInfo = new FieldInfo("FieldName", EdmPrimitiveTypeKind.String.getFullQualifiedName());
list.add(fieldInfo);
fieldInfo = new FieldInfo("DisplayName", EdmPrimitiveTypeKind.String.getFullQualifiedName());
list.add(fieldInfo);
fieldInfo = new FieldInfo("ModificationTimestamp", EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName());
list.add(fieldInfo);
return FieldDefinition.fieldList;
}
public Boolean useCustomDatasource() { return true; }
public Entity getData(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicates)
{
ArrayList<FieldInfo> fields = this.getFieldList();
Entity product = null;
Map<String, String> properties = System.getenv();
try {
String sqlCriteria = null;
// Statements allow to issue SQL queries to the database
Statement statement = null;
// Result set get the result of the SQL query
String queryString = null;
for (final UriParameter key : keyPredicates)
{
// key
String keyName = key.getName(); // .toLowerCase();
String keyValue = key.getText();
if (sqlCriteria==null)
{
sqlCriteria = keyName + " = " + keyValue;
}
else
{
sqlCriteria = sqlCriteria + " and " + keyName + " = " + keyValue;
}
}
queryString = "select * from " + this.getTableName();
if (null!=sqlCriteria && sqlCriteria.length()>0)
{
queryString = queryString + " WHERE " + sqlCriteria;
}
LOG.info("SQL Query: "+queryString);
ResultSet resultSet = statement.executeQuery(queryString);
String primaryFieldName = this.getPrimaryKeyName();
// add the lookups from the database.
while (resultSet.next())
{
Entity ent = CommonDataProcessing.getEntityFromRow(resultSet, this, null);
product = ent;
}
statement.close();
} catch (Exception e) {
LOG.error("Server Error occurred in reading "+this.getResourceName(), e);
return product;
}
return product;
}
public EntityCollection getData(EdmEntitySet edmEntitySet, UriInfo uriInfo, boolean isCount) throws ODataApplicationException
{
EntityCollection entCollection = new EntityCollection();
List<Entity> productList = entCollection.getEntities();
FilterOption filter = uriInfo.getFilterOption();
BreakdownOfFilterExpressionVisitor customExpression = new BreakdownOfFilterExpressionVisitor(this);
try
{
String criteria = filter.getExpression().accept(customExpression);
}
catch (ExpressionVisitException e)
{
LOG.error("Server Error occurred in reading "+this.getResourceName(), e);
return entCollection;
}
HashMap<String,String> reps = customExpression.getRepresentations();
String resourceName = reps.remove("ResourceName");
resourceName = resourceName.substring(1,resourceName.length()-1);
ResourceInfo res = null;
for (ResourceInfo defn :resources)
{
if (resourceName.equals(defn.getResourcesName()))
{
res = defn;
break;
}
}
ArrayList<FieldInfo> resourceFieldList = res.getFieldList();
for (FieldInfo field: resourceFieldList)
{
HashMap<String,Object> entityValues = new HashMap<>();
entityValues.put("FieldKey", field.getFieldName());
entityValues.put("FieldName", field.getFieldName());
entityValues.put("ResourceName", resourceName);
entityValues.put("DisplayName", field.getFieldName());
Date date = new Date();
entityValues.put("ModificationTimestamp", date);
boolean match = true;
for (String key: reps.keySet() )
{
String toMatch = reps.get(key);
toMatch = toMatch.substring(1,toMatch.length()-1);
String value = entityValues.get(key).toString();
if (!toMatch.equals(value))
{
match = false;
break;
}
}
if (match)
{
Entity ent = new Entity();
ent.addProperty(new Property(null, "FieldKey", ValueType.PRIMITIVE, field.getFieldName()));
ent.addProperty(new Property(null, "FieldName", ValueType.PRIMITIVE, field.getFieldName()));
ent.addProperty(new Property(null, "ResourceName", ValueType.PRIMITIVE, resourceName));
String displayName = field.getFieldName();
ent.addProperty(new Property(null, "DisplayName", ValueType.PRIMITIVE, displayName));
ent.addProperty(new Property(null, "ModificationTimestamp", ValueType.PRIMITIVE, date ));
productList.add(ent);
}
}
return entCollection;
}
public void addResources(ArrayList<ResourceInfo> resources)
{
this.resources = resources;
}
}

View File

@ -0,0 +1,179 @@
package org.reso.service.data.meta;
import org.apache.olingo.commons.api.edm.EdmEnumType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourcePrimitiveProperty;
import org.apache.olingo.server.api.uri.queryoption.expression.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class BreakdownOfFilterExpressionVisitor implements ExpressionVisitor<String>
{
private static final Logger LOG = LoggerFactory.getLogger(MySQLFilterExpressionVisitor.class);
private static final Map<BinaryOperatorKind, String> BINARY_OPERATORS = new HashMap<BinaryOperatorKind, String>() {{
put(BinaryOperatorKind.ADD, " + ");
put(BinaryOperatorKind.AND, " AND ");
put(BinaryOperatorKind.DIV, " / ");
put(BinaryOperatorKind.EQ, " = ");
put(BinaryOperatorKind.GE, " >= ");
put(BinaryOperatorKind.GT, " > ");
put(BinaryOperatorKind.LE, " <= ");
put(BinaryOperatorKind.LT, " < ");
put(BinaryOperatorKind.MOD, " % ");
put(BinaryOperatorKind.MUL, " * ");
put(BinaryOperatorKind.NE, " <> ");
put(BinaryOperatorKind.OR, " OR ");
put(BinaryOperatorKind.SUB, " - ");
}};
private HashMap<String,String> representations = new HashMap<>();
private ResourceInfo resourceInfo;
public BreakdownOfFilterExpressionVisitor(ResourceInfo resourceInfo) {
this.resourceInfo = resourceInfo;
}
public HashMap<String,String> getRepresentations()
{
return representations;
}
@Override
public String visitBinaryOperator(BinaryOperatorKind operator, String left, String right)
throws ExpressionVisitException, ODataApplicationException
{
String strOperator = BINARY_OPERATORS.get(operator);
if (strOperator == null) {
throw new ODataApplicationException("Unsupported binary operation: " + operator.name(),
operator == BinaryOperatorKind.HAS ?
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode() :
HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
}
// ResourceName eq 'Property' becomes (=, ResourceName, Property)
if ( "=".equals(strOperator.trim() ) ) // We only want the equals operator from the filter. Assume all are AND ops for now if there are multiples.
{ // @TODO: Make this filter more robust for queries
representations.put(left,right);
}
return left + strOperator + right;
}
// @TODO I'm unsure where this would be called.
@Override public String visitBinaryOperator(BinaryOperatorKind operator, String s, List<String> list)
throws ExpressionVisitException, ODataApplicationException
{
String strOperator = BINARY_OPERATORS.get(operator);
throw new ODataApplicationException("Unsupported binary operation: " + operator.name(),
operator == BinaryOperatorKind.HAS ?
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode() :
HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitUnaryOperator(UnaryOperatorKind operator, String operand)
throws ExpressionVisitException, ODataApplicationException {
switch (operator) {
case NOT:
return "NOT " + operand;
case MINUS:
return "-" + operand;
}
throw new ODataApplicationException("Wrong unary operator: " + operator,
HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitMethodCall(MethodKind methodCall, List<String> parameters)
throws ExpressionVisitException, ODataApplicationException {
if (parameters.isEmpty() && methodCall.equals(MethodKind.NOW)) {
return "CURRENT_DATE";
}
String firsEntityParam = parameters.get(0);
switch (methodCall) {
case CONTAINS:
return firsEntityParam + " LIKE '%" + extractFromStringValue(parameters.get(1)) + "%'";
case STARTSWITH:
return firsEntityParam + " LIKE '" + extractFromStringValue(parameters.get(1)) + "%'";
case ENDSWITH:
return firsEntityParam + " LIKE '%" + extractFromStringValue(parameters.get(1));
case DAY:
return "DAY(" + firsEntityParam + ")";
case MONTH:
return "MONTH(" + firsEntityParam + ")";
case YEAR:
return "YEAR(" + firsEntityParam + ")";
}
throw new ODataApplicationException("Method call " + methodCall + " not implemented",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
String literalAsString = literal.getText();
if (literal.getType() == null) {
literalAsString = "NULL";
}
return literalAsString;
}
@Override
public String visitMember(Member member) throws ExpressionVisitException, ODataApplicationException {
List<UriResource> resources = member.getResourcePath().getUriResourceParts();
UriResource first = resources.get(0);
// TODO: Enum and ComplexType; joins
if (resources.size() == 1 && first instanceof UriResourcePrimitiveProperty) {
UriResourcePrimitiveProperty primitiveProperty = (UriResourcePrimitiveProperty) first;
return primitiveProperty.getProperty().getName();
} else {
throw new ODataApplicationException("Only primitive properties are implemented in filter expressions",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
}
@Override
public String visitEnum(EdmEnumType type, List<String> enumValues)
throws ExpressionVisitException, ODataApplicationException {
throw new ODataApplicationException("Enums are not implemented", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(),
Locale.ENGLISH);
}
@Override
public String visitLambdaExpression(String lambdaFunction, String lambdaVariable, Expression expression)
throws ExpressionVisitException, ODataApplicationException {
throw new ODataApplicationException("Lambda expressions are not implemented",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitAlias(String aliasName) throws ExpressionVisitException, ODataApplicationException {
throw new ODataApplicationException("Aliases are not implemented",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitTypeLiteral(EdmType type) throws ExpressionVisitException, ODataApplicationException {
throw new ODataApplicationException("Type literals are not implemented",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
@Override
public String visitLambdaReference(String variableName) throws ExpressionVisitException, ODataApplicationException {
throw new ODataApplicationException("Lambda references are not implemented",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
private String extractFromStringValue(String val) {
return val.substring(1, val.length() - 1);
}
}

View File

@ -0,0 +1,170 @@
package org.reso.service.data.meta;
import com.google.gson.stream.JsonReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class AnnotationObject
{
private static Map<String, Object> FIELD_PROPERTIES = Stream.of(
new AbstractMap.SimpleEntry<>("term", String.class),
new AbstractMap.SimpleEntry<>("value", String.class) )
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
class FieldObject
{
private static Map<String, Object> FIELD_PROPERTIES = Stream.of(
new AbstractMap.SimpleEntry<>("resourceName", String.class),
new AbstractMap.SimpleEntry<>("fieldName", String.class),
new AbstractMap.SimpleEntry<>("type", String.class),
new AbstractMap.SimpleEntry<>("nullable", Boolean.class),
new AbstractMap.SimpleEntry<>("scale", Number.class),
new AbstractMap.SimpleEntry<>("precision", Number.class),
new AbstractMap.SimpleEntry<>("isCollection", Boolean.class),
new AbstractMap.SimpleEntry<>("unicode", Boolean.class) )
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
private JsonReader reader;
private HashMap<String,Object> properties = new HashMap<>();
public FieldObject(JsonReader reader)
{
this.reader = reader;
this.readObject();
}
private void readObject()
{
try
{
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (FIELD_PROPERTIES.containsKey(name))
{
Object classType = FIELD_PROPERTIES.get(name);
if (classType.equals(String.class))
{
properties.put(name,reader.nextString() );
}
else if (classType.equals(Boolean.class))
{
properties.put(name,reader.nextBoolean() );
}
else if (classType.equals(Number.class))
{
properties.put(name,reader.nextLong() );
}
} else if (name.equals("annotations")) {
// read array
reader.beginArray();
while (reader.hasNext()) {
reader.skipValue();
//fields.add( this.readField() );
}
reader.endArray();
} else {
reader.skipValue(); //avoid some unhandle events
}
}
reader.endObject();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class DefinitionBuilder
{
private static Map<String, Boolean> HEADER_FIELDS = Stream.of(
new AbstractMap.SimpleEntry<>("description", true),
new AbstractMap.SimpleEntry<>("generatedOn", true),
new AbstractMap.SimpleEntry<>("version", true))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
private String fileName;
private JsonReader reader;
public DefinitionBuilder(String fileName)
{
this.fileName = fileName;
this.openFile();
}
public void openFile()
{
try
{
reader = new JsonReader(new FileReader(fileName));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
private FieldObject readField()
{
FieldObject fo = new FieldObject(reader);
return fo;
}
public List<ResourceInfo> readResources()
{
List<ResourceInfo> resources = new ArrayList<>();
ArrayList<FieldObject> fields = new ArrayList();
try
{
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (HEADER_FIELDS.containsKey(name))
{
String headerValue = reader.nextString();
} else if (name.equals("fields")) {
// read array
reader.beginArray();
while (reader.hasNext()) {
fields.add( this.readField() );
}
reader.endArray();
} else {
reader.skipValue(); //avoid some unhandle events
}
}
reader.endObject();
}
catch (IOException e)
{
e.printStackTrace();
}
return resources;
}
}

View File

@ -41,7 +41,8 @@ public class FieldInfo
public String getFieldName() public String getFieldName()
{ {
return fieldName.toLowerCase(); return fieldName;
//return fieldName.toLowerCase(); // For PostgreSQL. Not currently supported, but here if you want to play with it.
} }
public String getODATAFieldName() public String getODATAFieldName()
{ {

View File

@ -2,9 +2,14 @@ package org.reso.service.data.meta;
import org.apache.olingo.commons.api.data.Entity; import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ValueType; import org.apache.olingo.commons.api.data.ValueType;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriParameter;
import org.reso.service.data.common.CommonDataProcessing; import org.reso.service.data.common.CommonDataProcessing;
import org.reso.service.servlet.RESOservlet; import org.reso.service.servlet.RESOservlet;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -12,6 +17,7 @@ import org.slf4j.LoggerFactory;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class ResourceInfo public class ResourceInfo
{ {
@ -52,6 +58,8 @@ public class ResourceInfo
return null; return null;
} }
public Boolean useCustomDatasource() { return false; }
public FullQualifiedName getFqn(String namespace) public FullQualifiedName getFqn(String namespace)
{ {
if (this.fqn==null) if (this.fqn==null)
@ -71,10 +79,10 @@ public class ResourceInfo
String pkColumnName = pkColumns.getString("COLUMN_NAME"); String pkColumnName = pkColumns.getString("COLUMN_NAME");
Integer pkPosition = pkColumns.getInt("KEY_SEQ"); Integer pkPosition = pkColumns.getInt("KEY_SEQ");
LOG.debug(""+pkColumnName+" is the "+pkPosition+". column of the primary key of the table "+tableName); LOG.debug(""+pkColumnName+" is the "+pkPosition+". column of the primary key of the table "+tableName);
primaryKey = pkColumnName.toLowerCase(); primaryKey = pkColumnName; //.toLowerCase(); // lowercase only needed for PostgreSQL
} }
String[] splitKey = primaryKey.split("numeric"); String[] splitKey = primaryKey.split("Numeric");
if (splitKey.length>=1) if (splitKey.length>=1)
primaryKey = splitKey[0]; primaryKey = splitKey[0];
@ -90,4 +98,14 @@ public class ResourceInfo
this.primaryKeyName = primaryKey; this.primaryKeyName = primaryKey;
} }
public Entity getData(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicates)
{
return null;
}
public EntityCollection getData(EdmEntitySet edmEntitySet, UriInfo uriInfo, boolean isCount) throws ODataApplicationException
{
return null;
}
} }

View File

@ -9,9 +9,10 @@ import org.apache.olingo.server.api.ServiceMetadata;
import org.reso.service.data.GenericEntityCollectionProcessor; import org.reso.service.data.GenericEntityCollectionProcessor;
import org.reso.service.data.GenericEntityProcessor; import org.reso.service.data.GenericEntityProcessor;
import org.reso.service.data.definition.LookupDefinition; import org.reso.service.data.definition.LookupDefinition;
import org.reso.service.data.meta.DefinitionBuilder;
import org.reso.service.data.definition.FieldDefinition;
import org.reso.service.data.meta.ResourceInfo; import org.reso.service.data.meta.ResourceInfo;
import org.reso.service.edmprovider.RESOedmProvider; import org.reso.service.edmprovider.RESOedmProvider;
import org.reso.service.security.providers.BasicAuthProvider;
import org.reso.service.security.Validator; import org.reso.service.security.Validator;
import org.reso.service.security.providers.BearerAuthProvider; import org.reso.service.security.providers.BearerAuthProvider;
import org.reso.service.servlet.util.ClassLoader; import org.reso.service.servlet.util.ClassLoader;
@ -23,10 +24,8 @@ import java.io.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.util.ArrayList; import java.sql.SQLException;
import java.util.HashMap; import java.util.*;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.*; import javax.servlet.http.*;
@ -71,6 +70,9 @@ public class RESOservlet extends HttpServlet
String dbConnString = env.get("SQL_CONNECTION_STR"); String dbConnString = env.get("SQL_CONNECTION_STR");
String dbDriverStr = env.get("SQL_DB_DRIVER"); String dbDriverStr = env.get("SQL_DB_DRIVER");
String definitionFile = env.get("CERT_REPORT_FILENAME");
if (dbConnString!=null) if (dbConnString!=null)
{ {
String[] dbConnSplitStr = dbConnString.split(":"); String[] dbConnSplitStr = dbConnString.split(":");
@ -81,23 +83,13 @@ public class RESOservlet extends HttpServlet
} }
else LOG.info("DB String unknown form: "+dbConnString); else LOG.info("DB String unknown form: "+dbConnString);
} }
LOG.info("DB String empty");
try { try {
Class.forName(dbDriverStr).newInstance(); Class.forName(dbDriverStr).newInstance();
LOG.debug("looking to connect to " + dbConnString); LOG.debug("looking to connect to " + dbConnString);
if (this.dbType.equals("mysql")) connect = DriverManager
{ .getConnection(dbConnString,dbUser,dbPwd);
connect = DriverManager
.getConnection(dbConnString);
}
else if (this.dbType.equals("postgresql"))
{
connect = DriverManager
.getConnection(dbConnString,dbUser,dbPwd);
}
} catch (Exception e) { } catch (Exception e) {
@ -116,10 +108,25 @@ public class RESOservlet extends HttpServlet
ArrayList<ResourceInfo> resources = new ArrayList<>(); ArrayList<ResourceInfo> resources = new ArrayList<>();
// We are going to use a custom field definition to query Fields
FieldDefinition fieldDefinition = new FieldDefinition();
resources.add(fieldDefinition);
fieldDefinition.addResources(resources);
resourceLookup.put(fieldDefinition.getResourceName(), fieldDefinition);
// If there is a Certification metadata report file, import it for class definitions.
if (definitionFile!=null && false)
{
DefinitionBuilder definitionBuilder = new DefinitionBuilder(definitionFile);
List<ResourceInfo> loadedResources = definitionBuilder.readResources();
}
// Get all classes with constructors with 0 parameters. LookupDefinition should not work. // Get all classes with constructors with 0 parameters. LookupDefinition should not work.
try try
{ {
Class[] classList = ClassLoader.getClasses("org.reso.service.data.definition"); Class[] classList = ClassLoader.getClasses("org.reso.service.data.definition.custom");
for (Class classProto: classList) for (Class classProto: classList)
{ {
Constructor ctor = null; Constructor ctor = null;
@ -133,10 +140,17 @@ public class RESOservlet extends HttpServlet
{ {
ctor.setAccessible(true); ctor.setAccessible(true);
ResourceInfo resource = (ResourceInfo)ctor.newInstance(); ResourceInfo resource = (ResourceInfo)ctor.newInstance();
resources.add(resource);
resourceLookup.put(resource.getResourceName(), resource);
resource.findPrimaryKey(this.connect); try
{
resource.findPrimaryKey(this.connect);
resources.add(resource);
resourceLookup.put(resource.getResourceName(), resource);
}
catch (Exception e)
{
LOG.error("Error with: "+resource.getResourceName()+" - "+e.getMessage());
}
} }
} }
} }
@ -145,7 +159,17 @@ public class RESOservlet extends HttpServlet
LOG.error(e.getMessage()); LOG.error(e.getMessage());
} }
// ResourceInfo defn = new LookupDefinition(); ResourceInfo defn = new LookupDefinition();
try
{
defn.findPrimaryKey(this.connect);
resources.add(defn);
resourceLookup.put(defn.getResourceName(), defn);
}
catch (Exception e)
{
LOG.error(e.getMessage());
}
ServiceMetadata edm = odata.createServiceMetadata(edmProvider, new ArrayList<EdmxReference>()); ServiceMetadata edm = odata.createServiceMetadata(edmProvider, new ArrayList<EdmxReference>());