Skip to content

Commit

Permalink
Merge pull request #8435 from GlobalDataverseCommunityConsortium/IQSS…
Browse files Browse the repository at this point in the history
…/8286-enable_i18n_search_of_cvv_fields

IQSS/8286 enable i18n search of cvv fields
  • Loading branch information
kcondon authored Mar 23, 2022
2 parents f9ce365 + a59be6d commit c5d1df2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 42 deletions.
48 changes: 8 additions & 40 deletions src/main/java/edu/harvard/iq/dataverse/SettingsWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,37 +331,20 @@ public Boolean isHasDropBoxKey() {

public boolean isLocalesConfigured() {
if (configuredLocales == null) {
initLocaleSettings();
configuredLocales = new LinkedHashMap<>();
settingsService.initLocaleSettings(configuredLocales);
}
return configuredLocales.size() > 1;
}

public Map<String, String> getConfiguredLocales() {
if (configuredLocales == null) {
initLocaleSettings();
configuredLocales = new LinkedHashMap<>();
settingsService.initLocaleSettings(configuredLocales);
}
return configuredLocales;
}

private void initLocaleSettings() {

configuredLocales = new LinkedHashMap<>();

try {
JSONArray entries = new JSONArray(getValueForKey(SettingsServiceBean.Key.Languages, "[]"));
for (Object obj : entries) {
JSONObject entry = (JSONObject) obj;
String locale = entry.getString("locale");
String title = entry.getString("title");

configuredLocales.put(locale, title);
}
} catch (JSONException e) {
//e.printStackTrace();
// do we want to know? - probably not
}
}

public boolean isDoiInstallation() {
String protocol = getValueForKey(SettingsServiceBean.Key.Protocol);
if ("doi".equals(protocol)) {
Expand Down Expand Up @@ -490,31 +473,16 @@ public void validateEmbargoDate(FacesContext context, UIComponent component, Obj

public Map<String, String> getBaseMetadataLanguageMap(boolean refresh) {
if (languageMap == null || refresh) {
languageMap = new HashMap<String, String>();

/* If MetadataLanaguages is set, use it.
* If not, we can't assume anything and should avoid assuming a metadata language
*/
String mlString = getValueForKey(SettingsServiceBean.Key.MetadataLanguages,"");

if(mlString.isEmpty()) {
mlString="[]";
}
JsonReader jsonReader = Json.createReader(new StringReader(mlString));
JsonArray languages = jsonReader.readArray();
for(JsonValue jv: languages) {
JsonObject lang = (JsonObject) jv;
languageMap.put(lang.getString("locale"), lang.getString("title"));
}
languageMap = settingsService.getBaseMetadataLanguageMap(languageMap, true);
}
return languageMap;
}

public Map<String, String> getMetadataLanguages(DvObjectContainer target) {
Map<String,String> currentMap = new HashMap<String,String>();
currentMap.putAll(getBaseMetadataLanguageMap(true));
languageMap.put(DvObjectContainer.UNDEFINED_METADATA_LANGUAGE_CODE, getDefaultMetadataLanguageLabel(target));
return languageMap;
currentMap.putAll(getBaseMetadataLanguageMap(false));
currentMap.put(DvObjectContainer.UNDEFINED_METADATA_LANGUAGE_CODE, getDefaultMetadataLanguageLabel(target));
return currentMap;
}

private String getDefaultMetadataLanguageLabel(DvObjectContainer target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ public Future<String> indexDataverse(Dataverse dataverse, boolean processPaths)
solrInputDocument.addField(SearchFields.AFFILIATION, dataverse.getAffiliation());
solrInputDocument.addField(SearchFields.DATAVERSE_AFFILIATION, dataverse.getAffiliation());
}
Set<String> langs = settingsService.getConfiguredLanguages();
for (ControlledVocabularyValue dataverseSubject : dataverse.getDataverseSubjects()) {
String subject = dataverseSubject.getStrValue();
if (!subject.equals(DatasetField.NA_VALUE)) {
solrInputDocument.addField(SearchFields.DATAVERSE_SUBJECT, subject);
// Index in all used languages (display and metadata languages
for(String locale: langs) {
solrInputDocument.addField(SearchFields.DATAVERSE_SUBJECT, dataverseSubject.getLocaleStrValue(locale));
}
// collapse into shared "subject" field used as a facet
solrInputDocument.addField(SearchFields.SUBJECT, subject);
}
Expand Down Expand Up @@ -805,6 +809,7 @@ private String addOrUpdateDataset(IndexableDataset indexableDataset, Set<Long> d
solrInputDocument.addField(SearchFields.EXTERNAL_STATUS, datasetVersion.getExternalStatusLabel());
}

Set<String> langs = settingsService.getConfiguredLanguages();
Map<Long, JsonObject> cvocMap = datasetFieldService.getCVocConf(false);
for (DatasetField dsf : datasetVersion.getFlatDatasetFields()) {

Expand Down Expand Up @@ -892,7 +897,10 @@ private String addOrUpdateDataset(IndexableDataset indexableDataset, Set<Long> d
if (controlledVocabularyValue.getStrValue().equals(DatasetField.NA_VALUE)) {
continue;
}
solrInputDocument.addField(solrFieldSearchable, controlledVocabularyValue.getStrValue());
// Index in all used languages (display and metadata languages
for(String locale: langs) {
solrInputDocument.addField(solrFieldSearchable, controlledVocabularyValue.getLocaleStrValue(locale));
}
if (dsfType.getSolrField().isFacetable()) {
solrInputDocument.addField(solrFieldFacetable, controlledVocabularyValue.getStrValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.StringReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -730,5 +741,53 @@ public Set<Setting> listAll() {
return new HashSet<>(em.createNamedQuery("Setting.findAll", Setting.class).getResultList());
}

public Map<String, String> getBaseMetadataLanguageMap(Map<String,String> languageMap, boolean refresh) {
if (languageMap == null || refresh) {
languageMap = new HashMap<String, String>();

/* If MetadataLanaguages is set, use it.
* If not, we can't assume anything and should avoid assuming a metadata language
*/
String mlString = getValueForKey(SettingsServiceBean.Key.MetadataLanguages,"");

if(mlString.isEmpty()) {
mlString="[]";
}
JsonReader jsonReader = Json.createReader(new StringReader(mlString));
JsonArray languages = jsonReader.readArray();
for(JsonValue jv: languages) {
JsonObject lang = (JsonObject) jv;
languageMap.put(lang.getString("locale"), lang.getString("title"));
}
}
return languageMap;
}

public void initLocaleSettings(Map<String, String> configuredLocales) {

try {
JSONArray entries = new JSONArray(getValueForKey(SettingsServiceBean.Key.Languages, "[]"));
for (Object obj : entries) {
JSONObject entry = (JSONObject) obj;
String locale = entry.getString("locale");
String title = entry.getString("title");

configuredLocales.put(locale, title);
}
} catch (JSONException e) {
//e.printStackTrace();
// do we want to know? - probably not
}
}


public Set<String> getConfiguredLanguages() {
Set<String> langs = new HashSet<String>();
langs.addAll(getBaseMetadataLanguageMap(new HashMap<String, String>(), true).keySet());
Map<String, String> configuredLocales = new LinkedHashMap<>();
initLocaleSettings(configuredLocales);
langs.addAll(configuredLocales.keySet());
return langs;
}

}

0 comments on commit c5d1df2

Please sign in to comment.