Skip to content

Commit

Permalink
Merge pull request DANS-KNAW#61 from IQSS/develop
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
lubitchv authored Mar 24, 2022
2 parents b9b2efe + c5d1df2 commit 21bc2f9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 43 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;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package edu.harvard.iq.dataverse.export;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import edu.harvard.iq.dataverse.ControlledVocabularyValue;
import edu.harvard.iq.dataverse.DatasetFieldType;
import edu.harvard.iq.dataverse.DatasetFieldType.FieldType;
Expand All @@ -17,10 +21,15 @@
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -42,7 +51,10 @@ public class DDIExporterTest {
private static final SettingsServiceBean settingsService = Mockito.mock(SettingsServiceBean.class);
private static final LicenseServiceBean licenseService = Mockito.mock(LicenseServiceBean.class);
private static final MockDatasetFieldSvc datasetFieldTypeSvc = new MockDatasetFieldSvc();
private static final Gson gson = new Gson();
private static final Gson gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDateTime>) (JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) -> {
Instant instant = Instant.ofEpochMilli(json.getAsJsonPrimitive().getAsLong());
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}).create();

/*
* Setup and teardown mocks for BrandingUtil for atomicity.
Expand Down

0 comments on commit 21bc2f9

Please sign in to comment.