Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retain language tags on editing data properties with backend editor #315

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class DynamicFieldRow {

private int id = -1;
private String value = null;
private String language = "";
private Map parameterMap = null;

public int getId() {
Expand All @@ -34,4 +35,13 @@ public void setParameterMap(Map parameterMap) {
this.parameterMap = parameterMap;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
if (language != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if clause is a little bit strange here. I understand that null value might include some issue later, but I think you used StringUtils.isNotBlank wherever you invoked this method. I believe this might be removed.

this.language = language;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
import edu.cornell.mannlib.vedit.beans.DynamicField;
import edu.cornell.mannlib.vedit.beans.DynamicFieldRow;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import edu.cornell.mannlib.vedit.tags.EditTag;

public class DynamicFieldsTag extends EditTag {

private static final String LANGUAGE = "language";

private char PATH_SEP = File.separatorChar;

public final String MARKUP_FILE_PATH = "templates"+PATH_SEP+"edit"+PATH_SEP+"specific"+PATH_SEP;
Expand Down Expand Up @@ -175,7 +179,12 @@ public int doEndTag() throws JspException {
String key = (String) paramIt.next();
String value = (String) row.getParameterMap().get(key);
byte[] valueInBase64 = Base64.encodeBase64(value.getBytes());
taName.append(key).append(":").append(new String(valueInBase64)).append(";");
taName.append(key).append(":").append(new String(valueInBase64));
if (StringUtils.isNotBlank(row.getLanguage())) {
byte[] encodedLang = Base64.encodeBase64(row.getLanguage().getBytes());
taName.append(":").append(LANGUAGE).append(":").append(new String(encodedLang));
}
taName.append(";");
}
if (row.getValue().length() > 0) {
String templateWithVars = templateMarkup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ public static Map<String, String> beanParamMapFromString(String params) {
for (String aParam : param) {
String[] p = aParam.split(":");
beanParamMap.put(p[0], new String(Base64.decodeBase64(p[1].getBytes())));
if (p.length > 3) {
beanParamMap.put(p[2], new String(Base64.decodeBase64(p[3].getBytes())));
}
}
return beanParamMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

import edu.cornell.mannlib.vitro.webapp.utils.JSPPageHandler;
import org.apache.commons.collections4.map.ListOrderedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ctc.wstx.util.StringUtil;

import edu.cornell.mannlib.vedit.beans.DynamicField;
import edu.cornell.mannlib.vedit.beans.DynamicFieldRow;
import edu.cornell.mannlib.vedit.beans.EditProcessObject;
Expand Down Expand Up @@ -250,7 +253,12 @@ public void doPost (HttpServletRequest request, HttpServletResponse response) {
//TODO: UGH
//row.setId(existingValue.getId());
row.setParameterMap(parameterMap);
row.setValue(existingValue.getData());
String value = existingValue.getData();
row.setValue(value);
String language = existingValue.getLanguage();
if (!StringUtils.isBlank(language)) {
row.setLanguage(language);
}
if (dynamo.getRowList() == null)
dynamo.setRowList(new ArrayList());
dynamo.getRowList().add(row);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

public class IndividualDataPropertyStatementProcessor implements ChangeListener {

private static final Log log = LogFactory.getLog(IndividualDataPropertyStatementProcessor.class.getName());
private static final String LANGUAGE = "language";
private static final Log log = LogFactory.getLog(IndividualDataPropertyStatementProcessor.class.getName());

public void doInserted(Object newObj, EditProcessObject epo) {
processDataprops(epo);
Expand Down Expand Up @@ -53,6 +54,9 @@ private void processDataprops (EditProcessObject epo) {
try {
Map beanParamMap = FormUtils.beanParamMapFromString(keyArg[3]);
String dataPropertyURI = (String) beanParamMap.get("DatatypePropertyURI");
if (beanParamMap.containsKey(LANGUAGE)) {
dataPropertyStmt.setLanguage((String) beanParamMap.get(LANGUAGE));
}
if (!deletedDataPropertyURIs.contains(dataPropertyURI)) {
deletedDataPropertyURIs.add(dataPropertyURI);
dataPropertyStatementDao.deleteDataPropertyStatementsForIndividualByDataProperty(((Individual) epo.getNewBean()).getURI(), dataPropertyURI);
Expand Down