Skip to content

Commit a4f3d7e

Browse files
authored
Merge pull request #14 from efrain-mejia/fix/changes-from-issue-12-PR
replicating change request from issue 12 PR
2 parents 9bfce56 + d3fdf77 commit a4f3d7e

File tree

7 files changed

+58
-49
lines changed

7 files changed

+58
-49
lines changed

src/main/java/org/reso/service/data/GenericEntityCollectionProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ protected DataResult getDataFromSQL(EdmEntitySet edmEntitySet, UriInfo uriInfo,
337337
ResourceInfo resource) throws ODataApplicationException {
338338
ArrayList<FieldInfo> fields = resource.getFieldList();
339339
EntityCollection entCollection = new EntityCollection();
340-
List<Entity> productList = entCollection.getEntities();
340+
List<Entity> entityList = entCollection.getEntities();
341341
Bson mongoCriteria = null;
342342
int totalCount = 0;
343343

@@ -398,7 +398,7 @@ protected DataResult getDataFromSQL(EdmEntitySet edmEntitySet, UriInfo uriInfo,
398398
LOG.debug("Top: " + topNumber + ", Skip: " + skipNumber);
399399

400400
OrderByOption orderByOption = uriInfo.getOrderByOption();
401-
if (orderByOption != null) {
401+
if (orderByOption != null && orderByOption.getOrders() != null && !orderByOption.getOrders().isEmpty()) {
402402
List<OrderByItem> orderItemList = orderByOption.getOrders();
403403
final OrderByItem orderByItem = orderItemList.get(0);
404404
Expression expression = orderByItem.getExpression();
@@ -437,7 +437,7 @@ protected DataResult getDataFromSQL(EdmEntitySet edmEntitySet, UriInfo uriInfo,
437437

438438
while (resultSet.next()) {
439439
Entity ent = CommonDataProcessing.getEntityFromRow(resultSet, resource, selectLookup);
440-
productList.add(ent);
440+
entityList.add(ent);
441441
}
442442
statement.close();
443443
} catch (Exception e) {

src/main/java/org/reso/service/data/GenericEntityProcessor.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,20 @@ private HashMap<String, Object> getDataToHash(List<UriParameter> keyPredicates,
204204

205205
protected Entity getData(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicates, ResourceInfo resource,
206206
UriInfo uriInfo) {
207-
Entity product = null;
207+
Entity entity = null;
208208
String dbType = System.getenv().getOrDefault("DB_TYPE", "mongodb").toLowerCase();
209209

210210
try {
211211
if (dbType.equals("mongodb")) {
212-
product = getDataFromMongo(resource, keyPredicates, uriInfo);
212+
entity = getDataFromMongo(resource, keyPredicates, uriInfo);
213213
} else {
214-
product = getDataFromSQL(resource, keyPredicates, uriInfo);
214+
entity = getDataFromSQL(resource, keyPredicates, uriInfo);
215215
}
216216
} catch (Exception e) {
217217
LOG.error("Server Error occurred in reading " + resource.getResourceName(), e);
218218
}
219219

220-
return product;
220+
return entity;
221221
}
222222

223223
private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPredicates, UriInfo uriInfo) {
@@ -243,7 +243,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
243243

244244
List<FieldInfo> enumFields = CommonDataProcessing.gatherEnumFields(resource);
245245
HashMap<String, Object> enumValues = new HashMap<>();
246-
Entity product = null;
246+
Entity entity = null;
247247

248248
Document query = new Document();
249249
if (keyPredicates != null) {
@@ -270,7 +270,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
270270

271271
if (doc != null) {
272272
LOG.info("Found main document: {}", doc.toJson());
273-
product = CommonDataProcessing.getEntityFromDocument(doc, resource);
273+
entity = CommonDataProcessing.getEntityFromDocument(doc, resource);
274274
String resourceRecordKey = doc.getString(primaryFieldName);
275275
LOG.info("Resource Record Key: {}", resourceRecordKey);
276276

@@ -288,7 +288,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
288288
Document lookupDoc = lookupCursor.next();
289289
CommonDataProcessing.getEntityValues(lookupDoc, entities, enumFields);
290290
}
291-
CommonDataProcessing.setEntityEnums(enumValues, product, enumFields);
291+
CommonDataProcessing.setEntityEnums(enumValues, entity, enumFields);
292292
}
293293
}
294294

@@ -318,7 +318,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
318318
expandCollection = database.getCollection("media");
319319
break;
320320
case "ListAgent":
321-
Property listAgentKeyProp = product.getProperty("ListAgentKey");
321+
Property listAgentKeyProp = entity.getProperty("ListAgentKey");
322322
if (listAgentKeyProp != null && listAgentKeyProp.getValue() != null) {
323323
String listAgentKey = listAgentKeyProp.getValue().toString();
324324
LOG.info("Found ListAgentKey: {}", listAgentKey);
@@ -341,7 +341,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
341341
link.setTitle("ListAgent");
342342
link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE);
343343
link.setInlineEntity(memberEntity);
344-
product.getNavigationLinks().add(link);
344+
entity.getNavigationLinks().add(link);
345345
LOG.info("Added ListAgent link to property");
346346
} else {
347347
LOG.error("Member resource definition not found in resourceLookup");
@@ -383,7 +383,7 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
383383
} else {
384384
LOG.warn("No {} entities found for property {}", navigationName, resourceRecordKey);
385385
}
386-
product.getNavigationLinks().add(link);
386+
entity.getNavigationLinks().add(link);
387387
}
388388
}
389389
} else {
@@ -394,11 +394,11 @@ private Entity getDataFromMongo(ResourceInfo resource, List<UriParameter> keyPre
394394
e.printStackTrace();
395395
}
396396

397-
return product;
397+
return entity;
398398
}
399399

400400
private Entity getDataFromSQL(ResourceInfo resource, List<UriParameter> keyPredicates, UriInfo uriInfo) {
401-
Entity product = null;
401+
Entity entity = null;
402402
try (Connection connection = getMySQLConnection()) {
403403
String primaryFieldName = resource.getPrimaryKeyName();
404404
List<FieldInfo> enumFields = CommonDataProcessing.gatherEnumFields(resource);
@@ -422,7 +422,7 @@ private Entity getDataFromSQL(ResourceInfo resource, List<UriParameter> keyPredi
422422
ResultSet resultSet = statement.executeQuery(queryBuilder.toString())) {
423423

424424
if (resultSet.next()) {
425-
product = CommonDataProcessing.getEntityFromRow(resultSet, resource, null);
425+
entity = CommonDataProcessing.getEntityFromRow(resultSet, resource, null);
426426
String resourceRecordKey = resultSet.getString(primaryFieldName);
427427

428428
if (!enumFields.isEmpty()) {
@@ -437,7 +437,7 @@ private Entity getDataFromSQL(ResourceInfo resource, List<UriParameter> keyPredi
437437
while (enumResultSet.next()) {
438438
CommonDataProcessing.getEntityValues(enumResultSet, entities, enumFields);
439439
}
440-
CommonDataProcessing.setEntityEnums(enumValues, product, enumFields);
440+
CommonDataProcessing.setEntityEnums(enumValues, entity, enumFields);
441441
}
442442
}
443443

@@ -449,13 +449,13 @@ private Entity getDataFromSQL(ResourceInfo resource, List<UriParameter> keyPredi
449449
UriResourceNavigation expandNavigation = (UriResourceNavigation) expandPath;
450450
EdmNavigationProperty edmNavigationProperty = expandNavigation.getProperty();
451451
EntityCollection expandEntities = CommonDataProcessing.getExpandEntityCollection(
452-
connection, edmNavigationProperty, product, resource, resourceRecordKey);
452+
connection, edmNavigationProperty, entity, resource, resourceRecordKey);
453453

454454
Link link = new Link();
455455
link.setTitle(edmNavigationProperty.getName());
456456
link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE);
457457
link.setInlineEntitySet(expandEntities);
458-
product.getNavigationLinks().add(link);
458+
entity.getNavigationLinks().add(link);
459459
}
460460
}
461461
}
@@ -464,7 +464,7 @@ private Entity getDataFromSQL(ResourceInfo resource, List<UriParameter> keyPredi
464464
} catch (Exception e) {
465465
LOG.error("Error querying SQL database: " + e.getMessage(), e);
466466
}
467-
return product;
467+
return entity;
468468
}
469469

470470
private URI createId(String entitySetName, Object id) {

src/main/java/org/reso/service/data/common/CommonDataProcessing.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public static HashMap<String, Object> translateEntityToMap(Entity entity) {
323323
* @return A List of HashMap representations of the entries
324324
*/
325325
public static ArrayList<HashMap<String, Object>> loadAllResource(Connection connect, ResourceInfo resource) {
326-
ArrayList<HashMap<String, Object>> productList = new ArrayList<>();
326+
ArrayList<HashMap<String, Object>> entityList = new ArrayList<>();
327327

328328
try {
329329
Statement statement = connect.createStatement();
@@ -334,17 +334,17 @@ public static ArrayList<HashMap<String, Object>> loadAllResource(Connection conn
334334

335335
while (resultSet.next()) {
336336
HashMap<String, Object> ent = CommonDataProcessing.getObjectFromRow(resultSet, resource, null);
337-
productList.add(ent);
337+
entityList.add(ent);
338338
}
339339

340340
statement.close();
341341

342342
} catch (Exception e) {
343343
LOG.error("Server Error occurred in reading " + resource.getResourceName(), e);
344-
return productList;
344+
return entityList;
345345
}
346346

347-
return productList;
347+
return entityList;
348348
}
349349

350350
/**

src/main/java/org/reso/service/data/definition/FieldDefinition.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public Entity getData(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicate
9191
public EntityCollection getData(EdmEntitySet edmEntitySet, UriInfo uriInfo, boolean isCount)
9292
throws ODataApplicationException {
9393
EntityCollection entityCollection = new EntityCollection();
94-
List<Entity> productList = entityCollection.getEntities();
94+
List<Entity> entityList = entityCollection.getEntities();
9595

9696
FilterOption filter = uriInfo.getFilterOption();
9797
BreakdownOfFilterExpressionVisitor customExpression = new BreakdownOfFilterExpressionVisitor(this);
@@ -164,7 +164,7 @@ public EntityCollection getData(EdmEntitySet edmEntitySet, UriInfo uriInfo, bool
164164
entity.addProperty(new Property(null, "ModificationTimestamp", ValueType.PRIMITIVE,
165165
entityValues.get("ModificationTimestamp")));
166166

167-
productList.add(entity);
167+
entityList.add(entity);
168168
}
169169
}
170170
}
@@ -244,7 +244,12 @@ private Object convertToType(Class<?> type, String value) {
244244
@SuppressWarnings("unchecked")
245245
private int compare(Object entityValue, Object filterValue) {
246246
if (entityValue instanceof Comparable && filterValue instanceof Comparable) {
247-
return ((Comparable<Object>) entityValue).compareTo(filterValue);
247+
if (entityValue.getClass().isInstance(filterValue)) {
248+
return ((Comparable<Object>) entityValue).compareTo(filterValue);
249+
} else {
250+
throw new IllegalArgumentException("Cannot compare values of different types: " +
251+
entityValue.getClass().getName() + " vs. " + filterValue.getClass().getName());
252+
}
248253
}
249254
throw new IllegalArgumentException("Values are not comparable: " + entityValue + ", " + filterValue);
250255
}

src/main/java/org/reso/service/data/meta/EnumFieldInfo.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ public Object getValueOf(String enumStringValue) {
122122
Object value = valueLookup.get(enumStringValue);
123123
if (value == null) {
124124
long bitValue = 1;
125-
long gitValue = 0;
126-
for (EnumValueInfo val : values) {
125+
for (int i = 0; i < values.size(); i++) {
126+
EnumValueInfo val = values.get(i);
127127
if (isFlags) {
128-
valueLookup.put(val.getValue(), bitValue);
129-
bitValue = bitValue * 2;
128+
valueLookup.put(val.getValue(), bitValue);
129+
bitValue *= 2;
130130
} else {
131-
valueLookup.put(val.getValue(), gitValue);
132-
gitValue = gitValue + 1;
131+
valueLookup.put(val.getValue(), (long) i);
133132
}
134133
}
135134
value = valueLookup.get(enumStringValue);

src/main/java/org/reso/service/data/meta/EnumValueInfo.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.reso.service.data.definition.LookupDefinition;
77

88
import java.util.ArrayList;
9+
import java.util.Optional;
910

1011
public class EnumValueInfo
1112
{
@@ -23,11 +24,12 @@ public String getValue()
2324
return value;
2425
}
2526

26-
public String getKey(String fieldName)
27-
{
28-
29-
return LookupDefinition.getReverseLookupCache().get(fieldName).get(value);
30-
}
27+
public String getKey(String fieldName) {
28+
return Optional.ofNullable(LookupDefinition.getReverseLookupCache())
29+
.map(cache -> cache.get(fieldName))
30+
.map(innerMap -> innerMap.get(value))
31+
.orElse(null);
32+
}
3133

3234
public void addAnnotation(String annotation, String term)
3335
{

src/main/java/org/reso/service/data/meta/MySQLFilterExpressionVisitor.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public String visitBinaryOperator(BinaryOperatorKind operator, String left, Stri
7070
FieldInfo field = resourceInfo.getFieldList().stream()
7171
.filter(fieldInfo -> fieldInfo.getFieldName().equals(parts[1])).findFirst().get();
7272
boolean isSingle = !field.isCollection() && !field.isFlags();
73-
String unformatted = " EXISTS (SELECT 1 FROM lookup_value AS v JOIN lookup AS l ON l.LookupKey = v.LookupKey WHERE v.FieldName = 'COLUMN_NAME' AND v.ResourceRecordKey = TABLE_NAME.PRIMARY_KEY_NAME AND l.LegacyODataValue = "
73+
String unformatted = " EXISTS (SELECT 1 FROM lookup_value AS v JOIN lookup AS l ON l.LookupKey = v.LookupKey WHERE v.FieldName = 'COLUMN_NAME' AND v.ResourceRecordKey = TABLE_NAME.PRIMARY_KEY_NAME AND l.LegacyOdataValue = "
7474
+ right + ")";
7575
return unformatted.replaceAll("COLUMN_NAME", parts[1]).replaceAll("TABLE_NAME", parts[0])
7676
.replaceAll("PRIMARY_KEY_NAME", resourceInfo.getPrimaryKeyName());
@@ -108,23 +108,26 @@ public String visitMethodCall(MethodKind methodCall, List<String> parameters)
108108
if (parameters.isEmpty() && methodCall.equals(MethodKind.NOW)) {
109109
return "CURRENT_DATE";
110110
}
111-
String firsEntityParam = parameters.get(0);
111+
String firstEntityParam = parameters.get(0);
112+
String secondEntityParam = parameters.size() > 1 ? extractFromStringValue(parameters.get(1)) : null;
113+
112114
switch (methodCall) {
113115
case CONTAINS:
114-
return firsEntityParam + " LIKE '%" + extractFromStringValue(parameters.get(1)) + "%'";
116+
return String.format("%s LIKE '%%%s%%'", firstEntityParam, secondEntityParam);
115117
case STARTSWITH:
116-
return firsEntityParam + " LIKE '" + extractFromStringValue(parameters.get(1)) + "%'";
118+
return String.format("%s LIKE '%s%%'", firstEntityParam, secondEntityParam);
117119
case ENDSWITH:
118-
return firsEntityParam + " LIKE '%" + extractFromStringValue(parameters.get(1)) + "'";
120+
return String.format("%s LIKE '%%%s'", firstEntityParam, secondEntityParam);
119121
case DAY:
120-
return "DAY(" + firsEntityParam + ")";
122+
return String.format("DAY(%s)", firstEntityParam);
121123
case MONTH:
122-
return "MONTH(" + firsEntityParam + ")";
124+
return String.format("MONTH(%s)", firstEntityParam);
123125
case YEAR:
124-
return "YEAR(" + firsEntityParam + ")";
126+
return String.format("YEAR(%s)", firstEntityParam);
127+
default:
128+
throw new ODataApplicationException("Method call " + methodCall + " not implemented",
129+
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
125130
}
126-
throw new ODataApplicationException("Method call " + methodCall + " not implemented",
127-
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
128131
}
129132

130133
@Override
@@ -206,7 +209,7 @@ public String visitLambdaExpression(String lambdaFunction, String lambdaVariable
206209
+ n + " value " + x + ")";
207210
}
208211
return n + " EXISTS (SELECT 1 FROM lookup_value AS v JOIN lookup AS l ON l.LookupKey = v.LookupKey WHERE v.FieldName = 'COLUMN_NAME' AND v.ResourceRecordKey = TABLE_NAME.PRIMARY_KEY_NAME AND "
209-
+ n + " l.LegacyODataValue " + x + ")";
212+
+ n + " l.LegacyOdataValue " + x + ")";
210213
}
211214

212215
@Override

0 commit comments

Comments
 (0)