Skip to content

Commit 7479ca6

Browse files
committed
Updated output in the list() method to avoid duplicate fields in WebServices
1 parent 3f09a2e commit 7479ca6

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

src/javaxt/express/WebService.java

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public ServiceResponse getServiceResponse(ServiceRequest request, Database datab
176176
if (Modifier.isPrivate(m.getModifiers())) continue;
177177

178178
if (m.getReturnType().equals(ServiceResponse.class)){
179-
179+
180180
Class<?>[] params = m.getParameterTypes();
181181
if (params.length>0){
182182
if (ServiceRequest.class.isAssignableFrom(params[0])){
@@ -486,25 +486,57 @@ private ServiceResponse list(Class c, ServiceRequest request, Database database)
486486
while (rs.next()){
487487
if (x>0) csv.append("\r\n");
488488

489+
490+
//Add header row as needed
489491
if (x==0){
490492
int i = 0;
493+
HashSet<String> fieldNames = new HashSet<>();
491494
for (javaxt.sql.Field field : rs.getFields()){
495+
String fieldName = field.getName().toLowerCase();
496+
497+
if (fieldNames.contains(fieldName)) continue;
498+
fieldNames.add(fieldName);
499+
492500
if (i>0) csv.append(",");
493-
csv.append(field.getName());
501+
csv.append(fieldName);
494502
i++;
495503
}
496504
csv.append("\r\n");
497505
}
498506

507+
508+
//Add data row
499509
int i = 0;
510+
HashSet<String> fieldNames = new HashSet<>();
500511
for (javaxt.sql.Field field : rs.getFields()){
512+
String fieldName = field.getName().toLowerCase();
513+
514+
if (fieldNames.contains(fieldName)) continue;
515+
fieldNames.add(fieldName);
516+
501517
if (i>0) csv.append(",");
502518
javaxt.sql.Value value = field.getValue();
519+
503520
if (!value.isNull()){
504521
String val = value.toString();
505-
if (val.contains("\"")) val = "\"" + val + "\"";
522+
523+
524+
//Update spatial data as needed
525+
fieldName = StringUtils.underscoreToCamelCase(fieldName);
526+
if (spatialFields.contains(fieldName)){
527+
if (database.getDriver().equals("PostgreSQL")){
528+
val = createGeom(val.toString()).toString();
529+
}
530+
}
531+
532+
533+
if (val.contains("\"") || val.contains(",")){
534+
val = "\"" + val + "\"";
535+
}
536+
506537
csv.append(val);
507538
}
539+
508540
i++;
509541
}
510542

@@ -518,18 +550,32 @@ private ServiceResponse list(Class c, ServiceRequest request, Database database)
518550

519551
}
520552
else if (format.equals("json")){
521-
522-
StringBuilder json = new StringBuilder("[");
553+
StringBuilder arr = new StringBuilder("[");
523554

524555
long x = 0;
525556
while (rs.next()){
526-
if (x>0) json.append(",");
527-
json.append(DbUtils.getJson(rs));
557+
JSONObject json = DbUtils.getJson(rs);
558+
559+
//Update spatial data as needed
560+
for (String fieldName : json.keySet()){
561+
JSONValue val = json.get(fieldName);
562+
if (!val.isNull()){
563+
if (spatialFields.contains(fieldName)){
564+
if (database.getDriver().equals("PostgreSQL")){
565+
val = new JSONValue(createGeom(val.toString()));
566+
json.set(fieldName, val);
567+
}
568+
}
569+
}
570+
}
571+
572+
if (x>0) arr.append(",");
573+
arr.append(json);
528574
x++;
529575
}
530-
json.append("]");
576+
arr.append("]");
531577

532-
response = new ServiceResponse(json.toString());
578+
response = new ServiceResponse(arr.toString());
533579
response.setContentType("application/json");
534580

535581
}
@@ -542,20 +588,28 @@ else if (format.equals("json")){
542588
while (rs.next()){
543589
JSONArray row = new JSONArray();
544590

545-
JSONObject record = DbUtils.getJson(rs);
591+
HashSet<String> fieldNames = new HashSet<>();
546592
for (javaxt.sql.Field field : rs.getFields()){
547593
String fieldName = field.getName().toLowerCase();
548594
fieldName = StringUtils.underscoreToCamelCase(fieldName);
595+
596+
if (fieldNames.contains(fieldName)) continue;
597+
fieldNames.add(fieldName);
549598
if (x==0) cols.add(fieldName);
550599

551-
JSONValue val = record.get(fieldName);
600+
JSONObject f = field.toJson();
601+
JSONValue val = f.get("value");
602+
603+
604+
//Update spatial data as needed
552605
if (!val.isNull()){
553606
if (spatialFields.contains(fieldName)){
554607
if (database.getDriver().equals("PostgreSQL")){
555608
val = new JSONValue(createGeom(val.toString()));
556609
}
557610
}
558611
}
612+
559613
row.add(val);
560614
}
561615

@@ -869,7 +923,7 @@ else if (e instanceof IllegalArgumentException){
869923
//**************************************************************************
870924
//** createGeom
871925
//**************************************************************************
872-
/** Used to create a geometry from a EWKT formatted string returned from
926+
/** Used to create a JTS Geometry from a EWKT formatted string returned from
873927
* PostgreSQL/PostGIS
874928
*/
875929
public Object createGeom(String hex) throws Exception {

0 commit comments

Comments
 (0)