import org.json.simple.JSONArray; //导入方法依赖的package包/类
/**
* Update a label object
*
* @param label
* - the label to be updated. The URI, language and value of this
* label must be defined. The value and language code must match
* the values in the Ontology.
* @param newLabelLanguage
* - the new language for the label
* @param newLabelValue
* - the new value for the label
* @throws OEClientException - an error has occurred contacting the server
*/
@SuppressWarnings({ "unchecked" })
public void updateLabel(Label label, String newLabelLanguage, String newLabelValue) throws OEClientException {
logger.info("updateLabel entry: {}", label.getUri());
if (label.getUri() == null) {
throw new OEClientException("Attemping to update label with null URI");
}
String processedLabelUri = FmtUtils.stringForURI(label.getUri());
String escapedLabelUri = getEscapedUri(processedLabelUri);
String url = String.format("%s/%s", getModelURL(), escapedLabelUri);
logger.info("updateLabel - URL: {}", url);
Invocation.Builder invocationBuilder = getInvocationBuilder(url);
JSONArray operationList = new JSONArray();
JSONObject testOperation = new JSONObject();
JSONObject replaceOperation = new JSONObject();
testOperation.put("op", "test");
testOperation.put("path", "@graph/0/skosxl:literalForm/0");
JSONObject oldLabelObject = new JSONObject();
oldLabelObject.put("@value", label.getValue());
oldLabelObject.put("@language", label.getLanguageCode());
testOperation.put("value", oldLabelObject);
replaceOperation.put("op", "replace");
replaceOperation.put("path", "@graph/0/skosxl:literalForm/0");
JSONObject newLabelObject = new JSONObject();
newLabelObject.put("@value", newLabelValue);
newLabelObject.put("@language", newLabelLanguage);
replaceOperation.put("value", newLabelObject);
operationList.add(testOperation);
operationList.add(replaceOperation);
String conceptSchemePayload = operationList.toJSONString();
Invocation invocation = invocationBuilder.build("PATCH",
Entity.entity(conceptSchemePayload, "application/json-patch+json"));
Response response = invocation.invoke();
if (response.getStatus() == 200) {
label.setValue(newLabelValue);
label.setLanguage(newLabelLanguage);
return;
}
throw new OEClientException(
String.format("%s Response recieved\n%s", response.getStatus(), response.getEntity().toString()));
}