From 9a34808dd1cf7114c74c80068f63f4039e6a94c3 Mon Sep 17 00:00:00 2001 From: Patrick Austin Date: Mon, 26 Sep 2022 11:43:19 +0100 Subject: [PATCH] Refactor into searchDocuments #27 --- .../org/icatproject/icat/client/ICAT.java | 131 +++++------------- 1 file changed, 31 insertions(+), 100 deletions(-) diff --git a/src/main/java/org/icatproject/icat/client/ICAT.java b/src/main/java/org/icatproject/icat/client/ICAT.java index 0b8802e..5bcf592 100644 --- a/src/main/java/org/icatproject/icat/client/ICAT.java +++ b/src/main/java/org/icatproject/icat/client/ICAT.java @@ -65,10 +65,10 @@ public class ICAT { * URI * * @param urlString - * The URI of a server in the form https://example.com:443. + * The URI of a server in the form https://example.com:443. * * @throws URISyntaxException - * If the urlString is not a valid URI + * If the urlString is not a valid URI */ public ICAT(String urlString) throws URISyntaxException { this.uri = new URI(urlString); @@ -382,13 +382,13 @@ void importMetaData(String sessionId, Path path, DuplicateAction duplicate, Attr * See whether or not someone is logged in. * * @param userName - * which must include mnemonic if the authenticator plugin is - * configured to return them. + * which must include mnemonic if the authenticator plugin is + * configured to return them. * * @return true if at least one session exists else false. * * @throws IcatException - * For various ICAT errors + * For various ICAT errors */ public boolean isLoggedIn(String userName) throws IcatException { URI uri = getUri(getUriBuilder("user/" + userName)); @@ -407,13 +407,13 @@ public boolean isLoggedIn(String userName) throws IcatException { * Login to a RESTful ICAT instance and return a Session * * @param plugin - * The mnemonic of the authentication plugin + * The mnemonic of the authentication plugin * @param credentials - * A map holding credential key/value pairs + * A map holding credential key/value pairs * @return A RESTful ICAT Session * * @throws IcatException - * For various ICAT errors + * For various ICAT errors */ public Session login(String plugin, Map credentials) throws IcatException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -447,7 +447,7 @@ public Session login(String plugin, Map credentials) throws Icat * No check is made on the validity of the sessionId. * * @param sessionId - * the sessionId to hold in the session. + * the sessionId to hold in the session. * * @return the new session */ @@ -503,7 +503,7 @@ String search(String sessionId, String query) throws IcatException { * @return the version of the ICAT server * * @throws IcatException - * For various ICAT errors + * For various ICAT errors */ @Deprecated public String getApiVersion() throws IcatException { @@ -525,7 +525,7 @@ public String getApiVersion() throws IcatException { * @return the version of the ICAT server * * @throws IcatException - * For various ICAT errors + * For various ICAT errors */ public String getVersion() throws IcatException { URI uri = getUri(getUriBuilder("version")); @@ -593,7 +593,7 @@ String searchInvestigations(String sessionId, String user, String text, Date low } } - String searchInvestigations(String sessionId, String user, String text, Date lower, Date upper, + private String searchDocuments(String target, String sessionId, String user, String text, Date lower, Date upper, List parameters, String userFullName, String searchAfter, int maxCount, String sort, JsonArray facets) throws IcatException { URIBuilder uriBuilder = getUriBuilder("search/documents"); @@ -601,7 +601,7 @@ String searchInvestigations(String sessionId, String user, String text, Date low ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (JsonGenerator gen = Json.createGenerator(baos)) { gen.writeStartObject(); - gen.write("target", "Investigation"); + gen.write("target", target); if (user != null) { gen.write("user", user); } @@ -643,6 +643,13 @@ String searchInvestigations(String sessionId, String user, String text, Date low } } + String searchInvestigations(String sessionId, String user, String text, Date lower, Date upper, + List parameters, String userFullName, String searchAfter, int maxCount, String sort, + JsonArray facets) throws IcatException { + return searchDocuments("Investigation", sessionId, user, text, lower, upper, parameters, userFullName, + searchAfter, maxCount, sort, facets); + } + String searchDatasets(String sessionId, String user, String text, Date lower, Date upper, List parameters, int maxResults) throws IcatException { URIBuilder uriBuilder = getUriBuilder("lucene/data"); @@ -685,49 +692,10 @@ String searchDatasets(String sessionId, String user, String text, Date lower, Da } String searchDatasets(String sessionId, String user, String text, Date lower, Date upper, - List parameters, String searchAfter, int maxCount, String sort, JsonArray facets) throws IcatException { - URIBuilder uriBuilder = getUriBuilder("search/documents"); - uriBuilder.setParameter("sessionId", sessionId); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos)) { - gen.writeStartObject(); - gen.write("target", "Dataset"); - if (user != null) { - gen.write("user", user); - } - if (text != null) { - gen.write("text", text); - } - if (lower != null) { - gen.write("lower", DateTools.dateToString(lower, Resolution.MINUTE)); - } - if (upper != null) { - gen.write("upper", DateTools.dateToString(upper, Resolution.MINUTE)); - } - if (parameters != null && !parameters.isEmpty()) { - writeParameters(gen, parameters); - } - if (facets != null) { - gen.write("facets", facets); - } - gen.writeEnd(); - } - - uriBuilder.setParameter("query", baos.toString()); - uriBuilder.setParameter("search_after", searchAfter); - uriBuilder.setParameter("maxCount", Integer.toString(maxCount)); - uriBuilder.setParameter("sort", sort); - URI uri = getUri(uriBuilder); - - try (CloseableHttpClient httpclient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet(uri); - try (CloseableHttpResponse response = httpclient.execute(httpGet)) { - return getString(response); - } - } catch (IOException e) { - throw new IcatException(IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage()); - - } + List parameters, String searchAfter, int maxCount, String sort, JsonArray facets) + throws IcatException { + return searchDocuments("Dataset", sessionId, user, text, lower, upper, parameters, null, searchAfter, maxCount, + sort, facets); } private void writeParameters(JsonGenerator gen, List parameters) { @@ -779,7 +747,7 @@ private void writeParameters(JsonGenerator gen, List paramet * @return the json string * * @throws IcatException - * For various ICAT errors + * For various ICAT errors */ public String getProperties() throws IcatException { URI uri = getUri(getUriBuilder("properties")); @@ -846,7 +814,8 @@ List luceneGetPopulating(String sessionId) throws IcatException { } } - void lucenePopulate(String sessionId, String entityName, Long minId, Long maxId, Boolean delete) throws IcatException { + void lucenePopulate(String sessionId, String entityName, Long minId, Long maxId, Boolean delete) + throws IcatException { URI uri = getUri(getUriBuilder("lucene/db/" + entityName)); List formparams = new ArrayList<>(); formparams.add(new BasicNameValuePair("sessionId", sessionId)); @@ -908,48 +877,10 @@ String searchDatafiles(String sessionId, String user, String text, Date lower, D } String searchDatafiles(String sessionId, String user, String text, Date lower, Date upper, - List parameters, String searchAfter, int maxCount, String sort, JsonArray facets) throws IcatException { - URIBuilder uriBuilder = getUriBuilder("search/documents"); - uriBuilder.setParameter("sessionId", sessionId); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos)) { - gen.writeStartObject(); - gen.write("target", "Datafile"); - if (user != null) { - gen.write("user", user); - } - if (text != null) { - gen.write("text", text); - } - if (lower != null) { - gen.write("lower", DateTools.dateToString(lower, Resolution.MINUTE)); - } - if (upper != null) { - gen.write("upper", DateTools.dateToString(upper, Resolution.MINUTE)); - } - if (parameters != null && !parameters.isEmpty()) { - writeParameters(gen, parameters); - } - if (facets != null) { - gen.write("facets", facets); - } - gen.writeEnd(); - } - - uriBuilder.setParameter("query", baos.toString()); - uriBuilder.setParameter("search_after", searchAfter); - uriBuilder.setParameter("maxCount", Integer.toString(maxCount)); - uriBuilder.setParameter("sort", sort); - URI uri = getUri(uriBuilder); - - try (CloseableHttpClient httpclient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet(uri); - try (CloseableHttpResponse response = httpclient.execute(httpGet)) { - return getString(response); - } - } catch (IOException e) { - throw new IcatException(IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage()); - } + List parameters, String searchAfter, int maxCount, String sort, JsonArray facets) + throws IcatException { + return searchDocuments("Datafile", sessionId, user, text, lower, upper, parameters, null, searchAfter, maxCount, + sort, facets); } long cloneEntity(String sessionId, String name, long id, Map keys) throws IcatException {