From 9a9257c0b0cd9af949f97ef599707acdd97a58b5 Mon Sep 17 00:00:00 2001
From: "Stefan E. Funk" <funk@sub.uni-goettingen.de>
Date: Wed, 21 Dec 2022 19:20:02 +0100
Subject: [PATCH] fix: add reducing singleField again.... had to understand it
 first

---
 .../textgrid/middleware/OaipmhUtilities.java  | 84 +++++++++++++++----
 1 file changed, 69 insertions(+), 15 deletions(-)

diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OaipmhUtilities.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OaipmhUtilities.java
index 142f456e..638a05b3 100644
--- a/oaipmh-core/src/main/java/info/textgrid/middleware/OaipmhUtilities.java
+++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OaipmhUtilities.java
@@ -396,8 +396,11 @@ public class OaipmhUtilities {
    * @param resultFromGetRequestInES
    * @param fields
    * @return Returns ONE or MORE params as a list!
+   * @deprecated
    */
-  public static List<String> fieldLoader(JSONObject resultFromGetRequestInES, String[] fields) {
+  @Deprecated
+  public static List<String> fieldLoader(final JSONObject resultFromGetRequestInES,
+      final String[] fields) {
 
     log.fine("JSON: " + (resultFromGetRequestInES == null ? "NULL" : resultFromGetRequestInES));
     log.fine("FIELDS: " + fields);
@@ -418,32 +421,63 @@ public class OaipmhUtilities {
       try {
         // Loop the splitted field components, such as "edition", "source", etcpp.
         for (int i = 0; i < fieldPathForESIndex.length; i++) {
-          // Case 1: We have only one field component.
+
+          String fieldComponent = fieldPathForESIndex[i];
+
+          log.fine("i=" + i);
+          log.fine("fieldPathLength=" + fieldPathForESIndex.length);
+
+          // Case 1: Reduce JSON object.
           if (i < fieldPathForESIndex.length - 1) {
-            String singleFieldComponent = fieldPathForESIndex[i];
 
-            log.fine("case 1: we have only one field component: " + singleFieldComponent);
+            log.fine(
+                "case 1: " + i + "<" + (fieldPathForESIndex.length - 1) + " : reduce singlePath");
 
             singlePath = singlePath.getJSONObject(fieldPathForESIndex[i]);
-            fieldResults.add(singlePath.toString());
           }
-          // Case 2: We have only one field component??
+          // Case 2: We have one field component.
           else if (fieldPathForESIndex.length == 1) {
-            String singleFieldComponent = fieldPathForESIndex[i];
 
-            log.fine("case 2: we have only one field component: " + singleFieldComponent);
+            log.fine("case 2: we have only one field component in field " + field);
+
+            // Examine JSONArray, take String if not applicable.
+            try {
+              JSONArray singlePathArray = singlePath.getJSONArray(fieldComponent);
+
+              log.fine("get json array: " + singlePathArray.toString(2));
+
+              for (int j = 0; j < singlePathArray.length(); j++) {
+                fieldResults.add(singlePathArray.getString(j));
+              }
+            } catch (JSONException e) {
+              String singlePathValue = singlePath.getString(fieldComponent);
+
+              log.fine("get string: " + singlePathValue);
 
-            JSONObject resultiDingsda =
-                resultFromGetRequestInES.getJSONObject(singleFieldComponent);
-            fieldResults.add(resultiDingsda.toString());
+              fieldResults.add(singlePathValue);
+            }
           }
-          // Case 3: We have got more field components.
+          // // Case 2: We have only one field component??
+          // else if (fieldPathForESIndex.length == 1) {
+          // String singleFieldComponent = fieldPathForESIndex[i];
+          //
+          // log.fine("case 2: we have only one field component: " + singleFieldComponent);
+          //
+          // JSONObject resultiDingsda =
+          // resultFromGetRequestInES.getJSONObject(singleFieldComponent);
+          // fieldResults.add(resultiDingsda.toString());
+          // }
+
+          // Case 2: We have got more field components.
           else {
-            String res = singlePath.get(fieldPathForESIndex[i]).toString();
 
-            log.fine("case 3: we have " + fieldPathForESIndex.length + " components in " + field);
+            log.fine("case 2: we have " + fieldPathForESIndex.length + " components in " + field);
+
+            // JSONArray pathArray = resultFromGetRequestInES.getJSONArray(fieldComponent);
+
+            String res = singlePath.get(fieldPathForESIndex[i]).toString();
 
-            // Examine JSONArray.
+            // Examine JSONArray, take String if not applicable.
             if (res.startsWith("[")) {
 
               log.fine("json array found: " + res);
@@ -489,19 +523,39 @@ public class OaipmhUtilities {
     String[] fieldPathForESIndex = field.split(ES_DIVIDER_REGEXP);
     JSONObject singlePath = resultFromGetRequestInES;
     try {
+
+      log.fine("kakki 1");
+
       for (int i = 0; i < fieldPathForESIndex.length; i++) {
+
+        log.fine("kakki 2");
+
         if (i < fieldPathForESIndex.length - 1) {
+
+          log.fine(i + "<" + (fieldPathForESIndex.length - 1) + " : reduce singlePath");
+
           singlePath = singlePath.getJSONObject(fieldPathForESIndex[i]);
+
+          log.fine("kakki 3");
+
         } else if (fieldPathForESIndex.length == 1) {
           fieldResults = resultFromGetRequestInES.get(fieldPathForESIndex[i]).toString();
+
+          log.fine("kakki 4");
+
         } else {
           fieldResults = singlePath.get(fieldPathForESIndex[i]).toString();
+
+          log.fine("kakki 5");
+
         }
       }
     } catch (JSONException e) {
       log.fine("IGNORING JSON ERROR: " + e.getMessage());
     }
 
+    log.fine("kakki 6");
+
     return fieldResults;
   }
 
-- 
GitLab