Skip to content

Commit

Permalink
fix: replace label in backend editor in default language. Avoid side …
Browse files Browse the repository at this point in the history
…effect of removing all other labels.
  • Loading branch information
litvinovg committed Jul 5, 2022
1 parent 33607bc commit c544982
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Set;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -286,15 +287,14 @@ public int updateIndividual(Individual ent, OntModel ontModel) {
ontModel.getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(),true,ent.getURI()));
org.apache.jena.ontology.Individual ind = ontModel.getIndividual(ent.getURI());
if (ind != null) {
if (ent.getName() != null && ( (ind.getLabel(getDefaultLanguage())==null) || (ind.getLabel(getDefaultLanguage())!=null && ent.getName()!=null && !ent.getName().equals(ind.getLabel(getDefaultLanguage())) ) ) ) {

// removal of existing values done this odd way to trigger
// the change listeners
Model temp = ModelFactory.createDefaultModel();
temp.add(ontModel.listStatements(ind, RDFS.label, (RDFNode) null));
ontModel.remove(temp);

ind.setLabel(ent.getName(), getDefaultLanguage());

String newLabel = ent.getName();
String oldLabel = ind.getLabel(getDefaultLanguage());
if ( newLabel != null && !newLabel.equals(oldLabel) ) {
if (oldLabel == null) {
oldLabel = "";
}
replaceOldLabelWithNewInDefaultLanguage(ontModel, ind, newLabel, oldLabel);
}
Set<String> oldTypeURIsSet = new HashSet<String>();
for (Iterator<Resource> typeIt = ind.listRDFTypes(true); typeIt.hasNext();) {
Expand Down Expand Up @@ -347,6 +347,23 @@ public int updateIndividual(Individual ent, OntModel ontModel) {
}
}

private void replaceOldLabelWithNewInDefaultLanguage(OntModel ontModel, org.apache.jena.ontology.Individual ind,
final String newLabel, final String oldLabel) {
Model temp = ModelFactory.createDefaultModel();
StmtIterator statements = ontModel.listStatements(ind, RDFS.label, (RDFNode) null);
while (statements.hasNext()) {
Statement statement = (Statement) statements.next();
Literal object = statement.getLiteral();
String lexicalForm = object.getLexicalForm();
String language = object.getLanguage();
if (oldLabel.equals(lexicalForm) && language.equals(getDefaultLanguage()) ) {
temp.add(statement);
}
}
ontModel.remove(temp);
ind.addLabel(newLabel, getDefaultLanguage());
}

public void markModified(Individual ind) {
markModified(ind,getOntModel());
}
Expand Down

0 comments on commit c544982

Please sign in to comment.