package de.ddb.pdc.metadata;

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
class RDFItem {

  @JsonProperty("ProvidedCHO")
  private Map<String, Object> providedCHO;
  
  @JsonProperty("Agent")
  private Object agents;
  
  @JsonProperty("Aggregation")
  private Map<String, Object> aggregation;

  public int getPublishYear() {
    try {
      String publishedYear = (String) providedCHO.get("issued");
      if (publishedYear == null) {
        // The item is missing the publishing date.
        return -1;
      }
      return Integer.parseInt(publishedYear.split(",")[0]);
    } catch (NumberFormatException e) {
      throw new RuntimeException(e);
    }
  }

  public String getInstitution() {
    return (String) aggregation.get("provider");
  }

  public List<String> getAuthorIds() {
    List<String> authorIds = new ArrayList<>();
    for (Map agent : getAgents()) {
      String about = agent.get("@about").toString();
      if (about.startsWith("http://d-nb.info/")) {
        authorIds.add(about);
      }
    }
    return authorIds;
  }

  private List<Map> getAgents() {
    if (agents instanceof List) {
      // Multiple agents
      return (List) agents;
    } else {
      // A single agent
      List<Map> agentList = new ArrayList<>();
      agentList.add((Map) agents);
      return agentList;
    }
  }
}

@@ -47,24 +47,56 @@
 .ArrayList;%0A
+import java.util.LinkedHashMap;%0A
 import java.
@@ -246,16 +246,80 @@
 perty;%0A%0A
+/**%0A * class for rdf item from edm item of aip json request%0A */%0A
 @JsonIgn
@@ -576,16 +576,87 @@
 ation;%0A%0A
+  /**%0A   * @return a 4 digit int if a issued year is found or -1%0A   */%0A
   public
@@ -864,24 +864,25 @@
 -1;%0A      %7D%0A
+%0A
       return
@@ -903,35 +903,48 @@
 Int(
-publishedYear.split(%22,%22)%5B0%5D
+Utility.useRegex(publishedYear,%22%5C%5Cd%7B4%7D%22)
 );%0A 
@@ -992,103 +992,195 @@
     
-throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A  public String getInstitution() %7B%0A    return (String
+return -1;%0A    %7D%0A  %7D%0A%0A  /**%0A   * @return  institution from the dataProvider item of aggregation%0A   */%0A  public String getInstitution() %7B%0A    List%3CObject%3E dataProvider = (ArrayList%3CObject%3E
 ) ag
@@ -1192,15 +1192,26 @@
 tion
+%0A      
 .get(%22
-p
+dataP
 rovi
@@ -1215,26 +1215,118 @@
 ovider%22);%0A  
-%7D%0A
+  return dataProvider.get(0).toString();%0A  %7D%0A%0A  /**%0A   * @return  list of all author ids%0A   */
 %0A  public Li
@@ -1612,16 +1612,78 @@
 s;%0A  %7D%0A%0A
+  /**%0A   * @return  list of all agents item in rdf item%0A   */%0A
   privat

add documentation and change getInstitution to the right value from aggregation dataProvider