public static FieldQuery fromJSON(String jsonQueryString,MediaType acceptedMediaType) throws JSONException,WebApplicationException{
if(jsonQueryString == null){
throw new IllegalArgumentException("The parsed JSON object MUST NOT be NULL!");
}
JSONObject jQuery = new JSONObject(jsonQueryString);
FieldQuery query;
if(jQuery.has("ldpath")){ //STANBOL-417: support for using LDPath as select
LDPathFieldQueryImpl ldPathQuery = new LDPathFieldQueryImpl();
ldPathQuery.setLDPathSelect(jQuery.getString("ldpath"));
query = ldPathQuery;
} else {
query = new FieldQueryImpl();
}
if(!jQuery.has("constraints")){
StringBuilder message = new StringBuilder();
message.append("The parsed Field Query MUST contain at least a single 'constraints'\n");
message.append("Parsed Query:\n");
message.append(jQuery.toString(4));
log.warn(message.toString());
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(
message.toString()).header(HttpHeaders.ACCEPT, acceptedMediaType.toString())
.build());
}
JSONArray constraints = jQuery.getJSONArray("constraints");
//collect all parsing Errors to report a complete set of all errors
boolean parsingError = false;
StringBuilder parsingErrorMessages = new StringBuilder();
parsingErrorMessages.append("Constraint parsing Errors:\n");
for(int i=0;i<constraints.length();i++){
JSONObject jConstraint = constraints.getJSONObject(i);
if(jConstraint.has("field")){
String field = jConstraint.getString("field");
field = NamespaceEnum.getFullName(field);
//check if there is already a constraint for that field
if(field == null || field.isEmpty()){
// log.warn("The value of the key \"field\" MUST NOT be NULL nor emtpy!");
// log.warn("Constraint:\n {}",jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingErrorMessages.append(
"Each Field Query Constraint MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else if(query.isConstrained(field)){
// log.warn("Multiple constraints for field {} in parsed FieldQuery!",field);
parsingErrorMessages.append('\n');
parsingErrorMessages.append(
"The parsed Query defines multiple constraints fr the field '"
+field+"'!\n");
parsingErrorMessages.append("FieldQuery allows only a single Constraint for a field\n");
parsingErrorMessages.append("Parsed Constraints:\n");
parsingErrorMessages.append(constraints.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else {
try {
query.setConstraint(field, parseConstraint(jConstraint));
} catch (IllegalArgumentException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append(e.getMessage());
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
} else { //empty field
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Constraints MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
if(parsingError){
String message = parsingErrorMessages.toString();
log.warn(message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(
message).header(HttpHeaders.ACCEPT, acceptedMediaType.toString())
.build());
}
//parse selected fields
JSONArray selected = jQuery.optJSONArray("selected");
if(selected != null){
for(int i=0;i<selected.length();i++){
String selectedField = selected.getString(i);
selectedField = NamespaceEnum.getFullName(selectedField);
if(selectedField != null && !selectedField.isEmpty()){
query.addSelectedField(selectedField);
}
}
} //else no selected fields -> funny but maybe someone do need only the ids
//parse limit and offset
if(jQuery.has("limit") && !jQuery.isNull("limit")){
try {
query.setLimit(jQuery.getInt("limit"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"limit\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));
parsingErrorMessages.append('\n');
parsingError = true;
}
}
if(jQuery.has("offset") && !jQuery.isNull("offset")){
try {
query.setOffset(jQuery.getInt("offset"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"offset\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));