Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 74 additions & 26 deletions src/main/java/de/nrw/hbz/genericSipLoader/impl/KtblLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import de.nrw.hbz.genericSipLoader.util.JsonFileLoader;
import de.nrw.hbz.genericSipLoader.util.PropertiesLoader;
import de.nrw.hbz.genericSipLoader.util.ZipExtractor;
import de.nrw.hbz.genericSipLoader.util.md.AdHocUriProvider;

import java.nio.file.Path;

Expand Down Expand Up @@ -107,12 +108,9 @@ public void uploadJsonFile(File file, String parentId) {
client.postJsonFile(parentId, file);
}



/**
* Method aims to persist a complete ktbl
* ResearchData item into appropriate to.science Objects with respect to each
* objects relation to each other
* Method aims to persist a complete ktbl ResearchData item into appropriate
* to.science Objects with respect to each objects relation to each other
*
* @param fList list of file names found by FileScanner
*/
Expand All @@ -130,7 +128,7 @@ public void persistKtblRD(Set<String> fList) {
*/

Iterator<String> fIt = ((TreeSet<String>) fList).descendingIterator();

// Step 1: find json files and persist an empty ktblResearchData object for each
// one
while (fIt.hasNext()) {
Expand All @@ -151,11 +149,13 @@ public void persistKtblRD(Set<String> fList) {
ktblDataId.put(fileName, pId);
}
}

// Step 2: Modify json files to add
logger.debug("search for associatedDataSets");
addRelatededDataSetMD(ktblDataId);


createMissingAdHocUris(ktblDataId);

// Step 3: Upload modified json files into empty ktblResearchData objects
Set<String> kSet = ktblDataId.keySet();
Iterator<String> kIt = kSet.iterator();
Expand All @@ -167,7 +167,7 @@ public void persistKtblRD(Set<String> fList) {
File jsonFile = new File(fileName);
uploadJsonFile(jsonFile, parentId);
logger.info("Uploaded JSON-File: " + fileName);

File zipFile = new File(fileName.replace(".json", ".zip"));
if (zipFile.exists()) {
String partId = createToScienceObject("file", parentId);
Expand All @@ -189,19 +189,66 @@ public void persistKtblRD(Set<String> fList) {
FileUtil.removeWorkDir(basePath, workDir);
}

/**
* For sanity reasons provide adHocUris whenever an @id is missing or empty
*
* @param ktblDataId
*/
private void createMissingAdHocUris(LinkedHashMap<String, String> ktblDataId) {
// TODO Auto-generated method stub

Set<String> keySet = ktblDataId.keySet();
Iterator<String> kIt = keySet.iterator();

while (kIt.hasNext()) {
String fileName = kIt.next();
JsonFileLoader jFl = new JsonFileLoader();
JSONObject ktblJSONObj = jFl.loadJsonObject(fileName);

String[] fields2check = new String[] { "other", "contributor", "creator", "publisher", "subject" };

for (String field : fields2check) {
if (ktblJSONObj.has(field)) {
JSONArray fieldArray = ktblJSONObj.getJSONArray(field);
for (int i = 0; i < fieldArray.length(); i++) {
JSONObject fieldObj = fieldArray.getJSONObject(i);
if (! fieldObj.has("@id") && fieldObj.has("prefLabel")) {
String adHocUri = new AdHocUriProvider("ktbl").encode(fieldObj.getString("prefLabel"));
fieldObj.put("@id", adHocUri);
logger.info("added adHocUri: " + adHocUri);
// fieldArray.put(i, fieldObj);
}
if(fieldObj.has("@id") && fieldObj.has("prefLabel")) {
if(fieldObj.getString("@id").equals("")){
String adHocUri = new AdHocUriProvider("ktbl").encode(fieldObj.getString("prefLabel"));
fieldObj.put("@id", adHocUri);
logger.info("added adHocUri: " + adHocUri);
// fieldArray.put(i, fieldObj);
}
}
}
// ktblJSONObj.put(field, fieldArray);
}
}
FileUtil.saveStringToResultFile(fileName, ktblJSONObj.toString(2));

}

}

private void addChildsToParent(LinkedHashMap<String, String> ktblDataId) {

TreeSet<String> pIdSet = (TreeSet<String>) ktblDataId.keySet();
NavigableSet<String> descSet = pIdSet.descendingSet();
String parentId = descSet.pollFirst();

Iterator<String> childPidIt = descSet.iterator();

while (childPidIt.hasNext()) {
String childId = childPidIt.next();
createToScienceObject("part", parentId);
}

}

/**
Expand All @@ -219,7 +266,7 @@ public void addRelatededDataSetMD(LinkedHashMap<String, String> ktblDataId) {
ArrayList<Hashtable<String, String>> relatedDataset = new ArrayList<>();

ArrayList<String> pids = new ArrayList<>();

Set<String> keySet = ktblDataId.keySet();
Iterator<String> pIt = keySet.iterator();

Expand All @@ -228,7 +275,7 @@ public void addRelatededDataSetMD(LinkedHashMap<String, String> ktblDataId) {
String key = pIt.next();
localFileList.add(key);
pids.add(ktblDataId.get(key));

logger.debug("id " + ktblDataId.get(key));

JsonFileLoader jFl = new JsonFileLoader();
Expand All @@ -238,17 +285,17 @@ public void addRelatededDataSetMD(LinkedHashMap<String, String> ktblDataId) {

if (ktblJSONObj.has("relatedDatasets")) {

logger.debug("found related Datasets");
logger.info("found related Datasets");
ktblRelDat = ktblJSONObj.getJSONArray("relatedDatasets");

logger.info("relatedDataset array has: " + ktblRelDat.length() + " items");
logger.info("localFileList has: " + localFileList.size() + " items");

Hashtable<String, String> related = null;

Iterator<Object> kIt = ktblRelDat.iterator();

int j = localFileList.size() -2 ;
int j = localFileList.size() - 2;
while (kIt.hasNext()) {

if (localFileList != null && localFileList.size() > 0) {
Expand All @@ -267,7 +314,6 @@ public void addRelatededDataSetMD(LinkedHashMap<String, String> ktblDataId) {
}
}


}

}
Expand All @@ -279,30 +325,32 @@ public void addRelatededDataSetMD(LinkedHashMap<String, String> ktblDataId) {

String jsonFileName = localFileList.get(i);
logger.info("load file: " + jsonFileName);

JsonFileLoader jFl = new JsonFileLoader();
JSONObject ktblJSONObj = jFl.loadJsonObject(jsonFileName);
JSONArray tosRelDat = new JSONArray();
JSONObject relDat = null;

logger.info("relations count: " + relatedDataset.size());

if (jsonFileName != localFileList.get(localFileList.size() - 1)) {
relDat = new JSONObject();
relDat.put("prefLabel", "Parent publication");
relDat.put("@id", client.getResouceUri() + ktblDataId.get(localFileList.get(localFileList.size() -1 )));
tosRelDat.put(relDat);
relDat.put("@id", client.getResouceUri() + ktblDataId.get(localFileList.get(localFileList.size() - 1)));
tosRelDat.put(relDat);
}

for (int k = 0; k < relatedDataset.size(); k++) {
if (relatedDataset.size() -1 - i != k) {
if (relatedDataset.size() - 1 - i != k) {
relDat = new JSONObject();
relDat.put("prefLabel", relatedDataset.get(k).get("prefLabel"));
relDat.put("@id", relatedDataset.get(k).get("@id"));
tosRelDat.put(relDat);
}
}
}
ktblJSONObj.put("relatedDatasets", tosRelDat);
ktblJSONObj.put("associatedDataset", tosRelDat);

logger.debug(ktblJSONObj.toString(2));

FileUtil.saveStringToResultFile(jsonFileName, ktblJSONObj.toString(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void loadProperties() {
e.printStackTrace();
}
} else {
apiProps = new PropertiesLoader().getApiProperties();
apiProps = new PropertiesLoader("ktbl-api.properties").getApiProperties();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class PropertiesLoader {
private static Properties apiProps = new Properties();
private Hashtable<String, String> config = new Hashtable<>();

public PropertiesLoader() {
loadProperties();
}

public PropertiesLoader(String propertiesFileName) {
loadProperties(propertiesFileName);
}

private void loadProperties() {
InputStream propStream = null;

Expand Down Expand Up @@ -55,6 +63,27 @@ private void loadProperties() {
}
}

private void loadProperties(String propertiesFileName) {
InputStream propStream = null;

propStream = this.getClass().getClassLoader()
.getResourceAsStream(propertiesFileName);

if (propStream != null) {
try {
apiProps.load(propStream);
} catch (IOException e) {
e.printStackTrace();
}

Enumeration<Object> eProps = apiProps.keys();
while (eProps.hasMoreElements()) {
String key = (String) eProps.nextElement();
config.put(key, apiProps.getProperty(key));
}
}
}

public Hashtable<String, String> getProperties() {
loadProperties();
return config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package de.nrw.hbz.genericSipLoader.util.md;

import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Properties;

import de.nrw.hbz.genericSipLoader.util.PropertiesLoader;
/**
*
*/
public class AdHocUriProvider {

private Properties apiProperties;

public AdHocUriProvider(String apiName) {

PropertiesLoader propertiesLoader = new PropertiesLoader(apiName + "-api.properties");
propertiesLoader.getApiProperties();
apiProperties = propertiesLoader.getApiProperties();
}


public String encode(String label) {

String adHocUri = apiProperties.getProperty("protocol")
+ "://"
+ apiProperties.getProperty("host") + "/adhoc/uri/";
byte[] bytes = label.getBytes();

Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(bytes);

adHocUri = adHocUri + result;

return adHocUri;
}
}
34 changes: 34 additions & 0 deletions src/test/java/genericSipLoader/TestAdHocProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
*
*/
package genericSipLoader;

import java.util.Properties;
import de.nrw.hbz.genericSipLoader.util.PropertiesLoader;
import de.nrw.hbz.genericSipLoader.util.md.AdHocUriProvider;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
*
*/
public class TestAdHocProvider {

@Test
public void getApiProperties() {
String propertiesFileName = "ktbl-api.properties";
Properties apiProp = new PropertiesLoader(propertiesFileName).getApiProperties();
System.out.println(apiProp.getProperty("host"));
assertEquals("frl.publisso.de", apiProp.getProperty("host"));
}

@Test
public void getAdHocUri() {
AdHocUriProvider aHP = new AdHocUriProvider("ktbl");
String adHocUri = aHP.encode("Goethe, Johann W.");
System.out.println(adHocUri);
assertEquals("https://frl.publisso.de/adhoc/uri/R29ldGhlLCBKb2hhbm4gVy4=", adHocUri);
}


}
Loading