Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Avoid query length limit
Browse files Browse the repository at this point in the history
  • Loading branch information
hvarg committed Sep 8, 2021
1 parent 81d0350 commit 4dcc33f
Showing 1 changed file with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -787,34 +788,64 @@ public List<String> isFileListOnWings (String username, String domain, Set<Strin
List<String> returnValue = new ArrayList<String>();
String filetype = this.internal_server + "/export/users/" + username + "/" + domain + "/data/ontology.owl#File";
String fileprefix = "<" + this.internal_server + "/export/users/" + username + "/" + domain + "/data/library.owl#";



// Only checking that the filename is already on WINGs
String query = "SELECT DISTINCT ?value WHERE {\n"
// This will fail if the query is too long, only ask 20 at one time.
String queryStart = "SELECT DISTINCT ?value WHERE {\n"
+ " ?value a <" + filetype + "> .\n"
+ " VALUES ?value {\n";
for (String file: filelist) query += fileprefix + file + ">\n";
query += " }\n}";
String queryEnd = " }\n}";

String pageid = "sparql";
List<NameValuePair> formdata = new ArrayList<NameValuePair>();
formdata.add(new BasicNameValuePair("query", query));
formdata.add(new BasicNameValuePair("format", "json"));
String resultjson = get(username, pageid, formdata);
if (resultjson == null || resultjson.equals(""))
return returnValue;
List<Set<String>> grouped = new ArrayList<Set<String>>();
int size = filelist.size();
if (size <= 20) {
grouped.add(filelist);
} else {
int i = 0;
Set<String> cur = new HashSet<String>();
for (String file: filelist) {
cur.add(file);
i++;
if (i == 20) {
grouped.add(cur);
cur = new HashSet<String>();
i = 0;
}
}
if (i != 0) { // That means the last one wasnt added yet
grouped.add(cur);
}
}

JsonParser jsonParser = new JsonParser();
JsonObject result = jsonParser.parse(resultjson).getAsJsonObject();
JsonArray qbindings = result.get("results").getAsJsonObject().get("bindings").getAsJsonArray();

for (JsonElement qbinding : qbindings) {
JsonObject qb = qbinding.getAsJsonObject();
if (qb.get("value") == null)
continue;
String fileurl = qb.get("value").getAsJsonObject().get("value").getAsString();
String name = fileurl.replaceAll("^.*\\#", "");
returnValue.add(name);
for (Set<String> group: grouped) {
String query = queryStart;
for (String file: group) query += fileprefix + file + ">\n";
query += queryEnd;

//Doing the query
String pageid = "sparql";
List<NameValuePair> formdata = new ArrayList<NameValuePair>();
formdata.add(new BasicNameValuePair("query", query));
formdata.add(new BasicNameValuePair("format", "json"));
String resultjson = get(username, pageid, formdata);
if (resultjson == null || resultjson.equals(""))
return returnValue;

JsonParser jsonParser = new JsonParser();
JsonObject result = jsonParser.parse(resultjson).getAsJsonObject();
JsonArray qbindings = result.get("results").getAsJsonObject().get("bindings").getAsJsonArray();

for (JsonElement qbinding : qbindings) {
JsonObject qb = qbinding.getAsJsonObject();
if (qb.get("value") == null)
continue;
String fileurl = qb.get("value").getAsJsonObject().get("value").getAsString();
String name = fileurl.replaceAll("^.*\\#", "");
returnValue.add(name);
}
}

return returnValue;

}
Expand Down

0 comments on commit 4dcc33f

Please sign in to comment.