package de.ddb.pdc.metadata;

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

import org.springframework.web.client.RestTemplate;
/**
 * 
 * Implementation of a {@link MetaFetcher}
 *
 */
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[] getSearchResult(String query, int maxCount) {
    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 getMetaData(DDBItem ddbItem) {

  }

  /**
   * @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.setImgUrl(URL + (String) results.get(idx).get("thumbnail"));
      ddbItems[idx] = ddbItem;
    }

    return ddbItems;
  }
}

@@ -84,16 +84,75 @@
 shMap;%0A%0A
+import org.springframework.web.client.RestClientException;%0A
 import o
@@ -203,12 +203,8 @@
 /**%0A
- * %0A
  * I
@@ -224,16 +224,11 @@
  of 
-a %7B@link
+the
  Met
@@ -239,12 +239,18 @@
 cher
-%7D%0A *
+ Interface
 %0A */
@@ -833,13 +833,8 @@
 hKey
- = %22%22
 ;%0A%0A 
@@ -1105,23 +1105,22 @@
 m%5B%5D 
-getS
+s
 earch
-Result
+ForItems
 (Str
@@ -1143,16 +1143,43 @@
 axCount)
+ throws RestClientException
  %7B%0A    R
@@ -1582,16 +1582,18 @@
 oid 
-g
+f
 et
+ch
 Meta
-D
+d
 ata(
@@ -1608,16 +1608,43 @@
 ddbItem)
+ throws RestClientException
  %7B%0A%0A  %7D%0A
@@ -2392,17 +2392,19 @@
 em.setIm
-g
+age
 Url(URL 

rename the methods like interface and make change from the comments