Skip to content

Commit

Permalink
Refactor into searchDocuments #27
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Sep 26, 2022
1 parent aa59387 commit 9a34808
Showing 1 changed file with 31 additions and 100 deletions.
131 changes: 31 additions & 100 deletions src/main/java/org/icatproject/icat/client/ICAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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<String, String> credentials) throws IcatException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -447,7 +447,7 @@ public Session login(String plugin, Map<String, String> 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
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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"));
Expand Down Expand Up @@ -593,15 +593,15 @@ 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<ParameterForLucene> parameters, String userFullName, 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", "Investigation");
gen.write("target", target);
if (user != null) {
gen.write("user", user);
}
Expand Down Expand Up @@ -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<ParameterForLucene> 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<ParameterForLucene> parameters, int maxResults) throws IcatException {
URIBuilder uriBuilder = getUriBuilder("lucene/data");
Expand Down Expand Up @@ -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<ParameterForLucene> 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<ParameterForLucene> 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<ParameterForLucene> parameters) {
Expand Down Expand Up @@ -779,7 +747,7 @@ private void writeParameters(JsonGenerator gen, List<ParameterForLucene> 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"));
Expand Down Expand Up @@ -846,7 +814,8 @@ List<String> 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<NameValuePair> formparams = new ArrayList<>();
formparams.add(new BasicNameValuePair("sessionId", sessionId));
Expand Down Expand Up @@ -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<ParameterForLucene> 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<ParameterForLucene> 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<String, String> keys) throws IcatException {
Expand Down

0 comments on commit 9a34808

Please sign in to comment.