package de.ddb.pdc.metadata;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
/**
 * Implementation of the MetaFetcher Interface
 */
public class MetaFetcherImpl implements MetaFetcher {

  private static final String URL = 
    "https://www.deutsche-digitale-bibliothek.de";
  private static final String APIURL = 
    "https://api.deutsche-digitale-bibliothek.de";
  private static final String SEARCH = "/search?";
  private static final String ITEMS = "/items/";
  private static final String AUTH = "oauth_consumer_key=";
  private static final String SORTROWS = "&sort=RELEVANCE&rows=";
  private static final String QUERY = "&query=";
  private static final String EDM = "/edm?";

  private String authKey;

  /** 
   * @param authKey authentication Key for the DDB API
   */
  public MetaFetcherImpl(String authKey) {
    this.authKey = authKey;
  }

  /**
   * @param query 
   * @param maxCount
   * @return array of DDBItems with the search result
   */
  public DDBItem[] searchForItems(String query, int maxCount) throws RestClientException {
    RestTemplate restTemplate = new RestTemplate();
    String modifiedQuery = query.replace(" ", "+");
    String url = APIURL + SEARCH + AUTH + authKey + SORTROWS + maxCount + QUERY
      + modifiedQuery;
    JSONResult jsonResult = restTemplate.getForObject(url, JSONResult.class);
    return getDDBItems(jsonResult);
  }


  /**
   * @param ddbItem for the id to get the meta data
   */
  public void fetchMetadata(DDBItem ddbItem) throws RestClientException {

  }

  /**
   * @param jsonresult
   * @return array filled with search results
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  private DDBItem[] getDDBItems(JSONResult jsonresult) {
    ArrayList<LinkedHashMap> results = (ArrayList<LinkedHashMap>) jsonresult
      .getResults().get(0).get("docs");
    int maxResults = results.size();
    DDBItem[] ddbItems = new DDBItem[maxResults];
    
    for (int idx = 0; idx < maxResults; idx++) {
      DDBItem ddbItem = new DDBItem((String) results.get(idx).get("id"));
      ddbItem.setTitle(MetaFetcherUtil.deleteMatch((String) results.get(idx)
        .get("title")));
      ddbItem.setSubtitle(MetaFetcherUtil.deleteMatch((String) results
        .get(idx).get("subtitle")));
      ddbItem.setImageUrl(URL + (String) results.get(idx).get("thumbnail"));
      ddbItems[idx] = ddbItem;
    }

    return ddbItems;
  }
}

@@ -27,69 +27,8 @@
 a;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.LinkedHashMap;%0A%0A
 impo
@@ -478,57 +478,8 @@
 ?%22;%0A
-  private static final String ITEMS = %22/items/%22;%0A
   pr
@@ -652,53 +652,8 @@
 y=%22;
-%0A  private static final String EDM = %22/edm?%22;
 %0A%0A  
@@ -1233,29 +1233,25 @@
     
-JSON
 Result
- jsonResult
+sOfJSON roj
  = r
@@ -1280,26 +1280,29 @@
 ct(url, 
-JSON
 Result
+sOfJSON
 .class);
@@ -1329,18 +1329,11 @@
 ems(
-jsonResult
+roj
 );%0A 
@@ -1566,57 +1566,8 @@
  */%0A
-  @SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)%0A
   pr
@@ -1598,178 +1598,66 @@
 ems(
-JSON
 Result
- jsonresult) %7B%0A    ArrayList%3CLinkedHashMap%3E results = (ArrayList%3CLinkedHashMap%3E) jsonresult%0A      .getResults().get(0).get(%22docs%22);%0A    int maxResults = results
+sOfJSON roj) %7B%0A    int maxResults = roj.getResults()
 .siz
@@ -1719,18 +1719,8 @@
     
-%0A    for (
 int 
@@ -1731,207 +1731,198 @@
 = 0;
- idx %3C maxResults; idx++) %7B%0A      DDBItem ddbItem = new DDBItem((String) results.get(idx).get(%22id%22));%0A      ddbItem.setTitle(MetaFetcherUtil.deleteMatch((String) results.get(idx)%0A        
+%0A    for (SearchResultItem rsi : roj.getResults()) %7B%0A      DDBItem ddbItem = new DDBItem(rsi.getId());%0A      System.out.println(rsi.getId());%0A      ddbItem.setTitle(deleteMatch(rsi
 .get
-(%22t
+T
 itle
-%22
+(
 )));
@@ -1952,85 +1952,36 @@
 tle(
-MetaFetcherUtil.deleteMatch((String) results%0A        .get(idx)
+deleteMatch(rsi
 .get
-(%22s
+S
 ubtitle
-%22
+(
 )));
@@ -2017,49 +2017,149 @@
 L + 
-(String) results.get(idx).get(%22thumbnail%22
+rsi.getThumbnail());%0A      ddbItem.setCategory(rsi.getCategory());%0A      ddbItem.setMedia(rsi.getMedia());%0A      ddbItem.setType(rsi.getType(
 ));%0A
@@ -2189,16 +2189,29 @@
 dbItem;%0A
+      idx++;%0A
     %7D%0A%0A 
@@ -2230,14 +2230,280 @@
 bItems;%0A
+  %7D %0A  %0A  /**%0A   * @param string%0A   * @return string with replaced match string%0A   * Method use to remove the match-tag of the search strings%0A   */%0A  public static String deleteMatch(String string) %7B%0A    return string.replace(%22%3Cmatch%3E%22, %22%22).replace(%22%3C/match%3E%22, %22%22);%0A
   %7D%0A%7D%0A

optimize MetaFetcherImpl with new JSON result class