Skip to content

Commit

Permalink
chore(emm): Clean up generics
Browse files Browse the repository at this point in the history
  • Loading branch information
olovy committed Nov 21, 2024
1 parent a0d1c57 commit f027bfd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
37 changes: 23 additions & 14 deletions emm/src/main/java/whelk/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import static whelk.util.Jackson.mapper;

Expand Down Expand Up @@ -70,22 +79,22 @@ public static void sendDumpResponse(Whelk whelk, String apiBaseUrl, HttpServletR
}

private static void sendDumpIndexResponse(String apiBaseUrl, HttpServletResponse res) throws IOException {
HashMap responseObject = new HashMap();
var responseObject = new LinkedHashMap<>();

ArrayList<Map> categoriesList = new ArrayList<>();
var categoriesList = new ArrayList<>();

HashMap allCategory = new HashMap();
var allCategory = new LinkedHashMap<>();
allCategory.put("url", apiBaseUrl+"?dump=all&offset=0");
allCategory.put("description", "This category represents the whole collection, without reservations.");
categoriesList.add(allCategory);

HashMap libraryCategory = new HashMap();
var libraryCategory = new LinkedHashMap<>();
libraryCategory.put("url", apiBaseUrl+"?dump=itemAndInstance:X&offset=0");
libraryCategory.put("description", "These categories represent the Items and Instances held by a particular library. " +
"The relevant library-code (sigel) for which you want data must replace the X in the category URL.");
categoriesList.add(libraryCategory);

HashMap typesCategory = new HashMap();
var typesCategory = new LinkedHashMap<>();
typesCategory.put("url", apiBaseUrl+"?dump=type:X&offset=0");
typesCategory.put("description", "These categories represent the set of entities of a certain type, including subtypes. " +
"For example the type Agent would include both Persons and Organizations etc. The X in the URL must be replaced " +
Expand Down Expand Up @@ -166,7 +175,7 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String
}

private static void sendFormattedResponse(Whelk whelk, String apiBaseUrl, String dump, ArrayList<String> recordIdsOnPage, HttpServletResponse res, long nextLineOffset, Long totalEntityCount, Instant dumpCreationTime) throws IOException{
HashMap responseObject = new HashMap();
var responseObject = new LinkedHashMap<>();

responseObject.put("creationTime", ZonedDateTime.ofInstant(dumpCreationTime, ZoneOffset.UTC).toString());
if (totalEntityCount == null)
Expand All @@ -180,8 +189,8 @@ private static void sendFormattedResponse(Whelk whelk, String apiBaseUrl, String
responseObject.put("next", apiBaseUrl+"?dump="+dump+"&offset="+nextLineOffset);
}

ArrayList<Map> entitesList = new ArrayList<>(EmmChangeSet.TARGET_HITS_PER_PAGE);
responseObject.put("entities", entitesList);
var entitiesList = new ArrayList<>(EmmChangeSet.TARGET_HITS_PER_PAGE);
responseObject.put("entities", entitiesList);
Map<String, Document> idsAndRecords = whelk.bulkLoad(recordIdsOnPage);
for (Document doc : idsAndRecords.values()) {

Expand All @@ -199,15 +208,15 @@ private static void sendFormattedResponse(Whelk whelk, String apiBaseUrl, String
logger.warn("Bad instance? " + itemOf);
continue;
}
ArrayList itemOfPath = new ArrayList();
var itemOfPath = new ArrayList<>();
itemOfPath.add("@graph"); itemOfPath.add(1); itemOfPath.add("itemOf"); // unggh..
doc._set(itemOfPath, instance.getThing(), doc.data);
entitesList.add(doc.getThing());
entitiesList.add(doc.getThing());
}

// For normal categories
else {
entitesList.add(doc.getThing());
entitiesList.add(doc.getThing());
}

}
Expand Down
20 changes: 12 additions & 8 deletions emm/src/main/java/whelk/EmmChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import static whelk.util.Jackson.mapper;
Expand All @@ -28,31 +33,30 @@ static void sendChangeSet(Whelk whelk, HttpServletResponse res, String until, St
return;
}

// THIS SHIT is so painful in Java :(
HashMap responseObject = new HashMap();
ArrayList contexts = new ArrayList();
var responseObject = new LinkedHashMap<>();
var contexts = new ArrayList<>();
contexts.add("https://www.w3.org/ns/activitystreams");
contexts.add("https://emm-spec.org/1.0/context.json");
responseObject.put("@context", contexts);
responseObject.put("type", "OrderedCollectionPage");
responseObject.put("id", apiBaseUrl+"?until="+until);
HashMap partOf = new HashMap();
var partOf = new LinkedHashMap<>();
partOf.put("type", "OrderedCollection");
partOf.put("id", apiBaseUrl);
responseObject.put("partOf", partOf);
responseObject.put("next", apiBaseUrl+"?until="+nextTimeStamp.getTime());
List orderedItems = new ArrayList();
var orderedItems = new ArrayList<>();
responseObject.put("orderedItems", orderedItems);

for (EmmActivity activityInList : activitiesOnPage) {
HashMap activityInStream = new HashMap();
var activityInStream = new LinkedHashMap<>();
activityInStream.put("type", switch (activityInList.activityType) {
case CREATE -> "create";
case UPDATE -> "update";
case DELETE -> "delete";
});
activityInStream.put("published", ZonedDateTime.ofInstant(activityInList.modificationTime.toInstant(), ZoneOffset.UTC).toString());
HashMap activityObject = new HashMap();
var activityObject = new HashMap<>();
activityInStream.put("object", activityObject);
activityObject.put("id", activityInList.uri);
activityObject.put("type", activityInList.entityType);
Expand Down
18 changes: 7 additions & 11 deletions emm/src/main/java/whelk/EmmServlet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package whelk;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.util.LinkedHashMap;

import static whelk.util.Jackson.mapper;

Expand Down Expand Up @@ -45,15 +41,15 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) {

// Send an Entry-Point reply
if (until == null) {
HashMap responseObject = new HashMap();
ArrayList contexts = new ArrayList();
var responseObject = new LinkedHashMap<>();
var contexts = new ArrayList<>();
contexts.add("https://www.w3.org/ns/activitystreams");
contexts.add("https://emm-spec.org/1.0/context.json");
responseObject.put("@context", contexts);
responseObject.put("type", "OrderedCollection");
responseObject.put("id", apiBaseUrl);
responseObject.put("url", apiBaseUrl+"?dump=index");
HashMap first = new HashMap();
var first = new LinkedHashMap<>();
first.put("type", "OrderedCollectionPage");
first.put("id", apiBaseUrl+"?until="+System.currentTimeMillis());
responseObject.put("first", first);
Expand Down

0 comments on commit f027bfd

Please sign in to comment.