package helper.oai;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import models.DublinCoreData;
import models.Globals;
import models.Node;

public class WglMapper {

	OaiDcMapper dcMapper;

	public WglMapper(Node node) {
		dcMapper = new OaiDcMapper(node);
	}

	public DublinCoreData getData() {
		DublinCoreData data = dcMapper.getData();
		data.setWglContributor(getWglContributor(dcMapper.getNode()));
		return data;
	}

	private List<String> getWglContributor(Node node) {

		List<String> result = new ArrayList<>();
		JsonNode n = new ObjectMapper().valueToTree(node.getLd());
		JsonNode collectionOneArray = n.at("/collectionOne");
		for (JsonNode item : collectionOneArray) {
			String id = item.at("/@id").asText("no Value found");
			result.add(Globals.wglContributor.acronymOf(id));
		}

		JsonNode institutionArray = n.at("/institution");
		for (JsonNode item : institutionArray) {
			String id = item.at("/@id").asText("no Value found");
			result.add(Globals.wglContributor.acronymOf(id));
		}

		return result;
	}

}

@@ -578,17 +578,16 @@
 node) %7B%0A
-%0A
 %09%09List%3CS
@@ -897,19 +897,45 @@
 ));%0A%09%09%7D%0A
+%09%09if (result.isEmpty()) %7B
 %0A%09%09
+%09
 JsonNode
@@ -972,24 +972,25 @@
 titution%22);%0A
+%09
 %09%09for (JsonN
@@ -1015,32 +1015,33 @@
 tionArray) %7B%0A%09%09%09
+%09
 String id = item
@@ -1070,32 +1070,33 @@
  Value found%22);%0A
+%09
 %09%09%09result.add(Gl
@@ -1134,18 +1134,22 @@
 id));%0A%09%09
+%09
 %7D%0A
+%09%09%7D
 %0A%09%09retur

use collectionOne only - use institution if collectionOne is not present