}
return query;
}
private static Constraint parseConstraint(JSONObject jConstraint, NamespacePrefixService nsPrefixService) throws JSONException {
final Constraint constraint;
if(jConstraint.has("type") && !jConstraint.isNull("type")) {
String type = jConstraint.getString("type");
//Event that internally "reference" is not part of the
//ConstraintType enum it is still present in the serialisation
//ant the Java API (see ReferenceConstraint class)
//Value constraints with the dataType Reference and AnyURI are
//considered to represent reference constraints
if(type.equals("reference")){
constraint = parseReferenceConstraint(jConstraint,nsPrefixService);
} else if (type.equals(ConstraintType.value.name())){
constraint = parseValueConstraint(jConstraint, nsPrefixService);
} else if (type.equals(ConstraintType.text.name())){
constraint = parseTextConstraint(jConstraint);
} else if (type.equals(ConstraintType.range.name())){
constraint = parseRangeConstraint(jConstraint,nsPrefixService);
} else if(type.equals(ConstraintType.similarity.name())){
constraint = parseSimilarityConstraint(jConstraint, nsPrefixService);
} else {
log.warn(String.format("Unknown Constraint Type %s. Supported values are %s",
Arrays.asList("reference",ConstraintType.values())));
StringBuilder message = new StringBuilder();
message.append("Parsed Constraint uses an unknown value for 'type'!\n");
message.append("Supported values: ");
message.append(ConstraintType.values());
message.append('\n');
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
} else {
log.warn(String.format("Earch Constraint MUST HAVE the \"type\" key set to one of the values %s",
Arrays.asList("reference",ConstraintType.values())));
StringBuilder message = new StringBuilder();
message.append("Parsed Constraint does not define a value for the field 'type'!\n");
message.append("Supported values: ");
message.append(ConstraintType.values());
message.append('\n');
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
//finally parse the optional boost
if(jConstraint.has("boost")){
double boost = jConstraint.optDouble("boost");
if(boost == Double.NaN || boost <= 0){
StringBuilder message = new StringBuilder("The Boost of a Constraint " +
"MUST BE a double AND >= 0 (parsed: '");
message.append(jConstraint.get("boost")).append("')!");
log.warn(message.toString());
throw new IllegalArgumentException(message.toString());
} else {
constraint.setBoost(boost);
}
} //else no boost defined
return constraint;
}