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

reference static serviceSettings in a static manner... #5192

Merged
merged 9 commits into from
Oct 19, 2018
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 @@ -65,6 +65,8 @@ public static synchronized ExportService getInstance() {

public static synchronized ExportService getInstance(SettingsServiceBean settingsService) {
ExportService.settingsService = settingsService;
// We pass settingsService into the JsonPrinter so it can check the :ExcludeEmailFromExport setting in calls to JsonPrinter.jsonAsDatasetDto().
JsonPrinter.setSettingsService(settingsService);
if (service == null) {
service = new ExportService();
}
Expand Down Expand Up @@ -154,6 +156,7 @@ public void exportAllFormats(Dataset dataset) throws ExportException {
if (releasedVersion == null) {
throw new ExportException("No released version for dataset " + dataset.getGlobalId().toString());
}

final JsonObjectBuilder datasetAsJsonBuilder = JsonPrinter.jsonAsDatasetDto(releasedVersion);
JsonObject datasetAsJson = datasetAsJsonBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.auto.service.AutoService;
import edu.harvard.iq.dataverse.DatasetVersion;
import edu.harvard.iq.dataverse.export.spi.Exporter;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.export.ExportException;
import edu.harvard.iq.dataverse.util.bagit.OREMap;
import java.io.OutputStream;
Expand All @@ -23,7 +24,7 @@ public class OAI_OREExporter implements Exporter {
public void exportDataset(DatasetVersion version, JsonObject json, OutputStream outputStream)
throws ExportException {
try {
new OREMap(version).writeOREMap(outputStream);
new OREMap(version, ExportService.settingsService.isTrueForKey(SettingsServiceBean.Key.ExcludeEmailFromExport, false)).writeOREMap(outputStream);
} catch (Exception e) {
logger.severe(e.getMessage());
e.printStackTrace();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/util/bagit/OREMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public class OREMap {
public static final String NAME = "OREMap";
private Map<String, String> localContext = new TreeMap<String, String>();
private DatasetVersion version;

public OREMap(DatasetVersion version) {
private boolean excludeEmail = false;

public OREMap(DatasetVersion version, boolean excludeEmail) {
this.version = version;
this.excludeEmail = excludeEmail;
}

public void writeOREMap(OutputStream outputStream) throws Exception {
Expand All @@ -64,6 +66,9 @@ public JsonObject getOREMap() throws Exception {
for (DatasetField field : fields) {
if (!field.isEmpty()) {
DatasetFieldType dfType = field.getDatasetFieldType();
if(excludeEmail && DatasetFieldType.FieldType.EMAIL.equals(dfType.getFieldType())) {
continue;
}
JsonLDTerm fieldName = getTermFor(dfType);
if (fieldName.inNamespace()) {
localContext.putIfAbsent(fieldName.getNamespace().getPrefix(), fieldName.getNamespace().getUrl());
Expand All @@ -83,6 +88,9 @@ public JsonObject getOREMap() throws Exception {

for (DatasetField dsf : dscv.getChildDatasetFields()) {
DatasetFieldType dsft = dsf.getDatasetFieldType();
if(excludeEmail && DatasetFieldType.FieldType.EMAIL.equals(dsft.getFieldType())) {
continue;
}
// which may have multiple values
if (!dsf.isEmpty()) {
// Add context entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ public class JsonPrinter {

private static final Logger logger = Logger.getLogger(JsonPrinter.class.getCanonicalName());

static SettingsServiceBean settingsService;
static SettingsServiceBean settingsService = null;

public JsonPrinter(SettingsServiceBean settingsService) {
this.settingsService = settingsService;
// Passed to DatasetFieldWalker so it can check the :ExcludeEmailFromExport setting
public static void setSettingsService(SettingsServiceBean ssb) {
settingsService = ssb;
}

public JsonPrinter() {
this(null);

}

public static final BriefJsonPrinter brief = new BriefJsonPrinter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ public void setUp() {
t.setParentDatasetFieldType(compoundSingleType);
}
compoundSingleType.setChildDatasetFieldTypes(childTypes);
// settingsSvc = new JsonParserTest.MockSettingsSvc();
// jsonPrinter = new JsonPrinter(settingsSvc);
}

@Test
Expand Down Expand Up @@ -197,9 +195,9 @@ public void testDatasetContactOutOfBoxNoPrivacy() {
fields.add(datasetContactField);

SettingsServiceBean nullServiceBean = null;
JsonPrinter jsonPrinter = new JsonPrinter(nullServiceBean);

JsonObject jsonObject = jsonPrinter.json(block, fields).build();
JsonPrinter.setSettingsService(nullServiceBean);
JsonObject jsonObject = JsonPrinter.json(block, fields).build();
assertNotNull(jsonObject);

System.out.println("json: " + JsonUtil.prettyPrint(jsonObject.toString()));
Expand All @@ -208,7 +206,7 @@ public void testDatasetContactOutOfBoxNoPrivacy() {
assertEquals("Bar University", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals("foo@bar.com", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail").getString("value"));

JsonObject byBlocks = jsonPrinter.jsonByBlocks(fields).build();
JsonObject byBlocks = JsonPrinter.jsonByBlocks(fields).build();

System.out.println("byBlocks: " + JsonUtil.prettyPrint(byBlocks.toString()));
assertEquals("Foo Bar", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
Expand Down Expand Up @@ -238,7 +236,7 @@ public void testDatasetContactWithPrivacy() {
datasetContactField.setDatasetFieldCompoundValues(vals);
fields.add(datasetContactField);

JsonPrinter jsonPrinter = new JsonPrinter(new MockSettingsSvc());
JsonPrinter.setSettingsService(new MockSettingsSvc());

JsonObject jsonObject = JsonPrinter.json(block, fields).build();
assertNotNull(jsonObject);
Expand All @@ -249,7 +247,7 @@ public void testDatasetContactWithPrivacy() {
assertEquals("Bar University", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals(null, jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail"));

JsonObject byBlocks = jsonPrinter.jsonByBlocks(fields).build();
JsonObject byBlocks = JsonPrinter.jsonByBlocks(fields).build();

System.out.println("byBlocks: " + JsonUtil.prettyPrint(byBlocks.toString()));
assertEquals("Foo Bar", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
Expand Down