package com.lightinit.hsdatashow.common;

import com.lightinit.hsdatashow.json.pojo.solr.CoreOverViewResponseEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/12/7.
 */
public class SolrUtils {
    public static CoreOverViewResponseEntity parse(String service, String core){
        CoreOverViewResponseEntity result=null;
        if(StringUtils.isEmpty(service) || StringUtils.isEmpty(core))
            return result;
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("wt","json"));
        nameValuePairs.add(new BasicNameValuePair("show","index"));
        nameValuePairs.add(new BasicNameValuePair("numTerms","0"));
        nameValuePairs.add(new BasicNameValuePair("_",String.valueOf(System.currentTimeMillis())));
        String responseBody= HttpTookitUtils.doGet(service+"/"+core+"/admin/luke",nameValuePairs, CharEncoding.UTF8,"application/json");
        if(StringUtils.isEmpty(responseBody)==false) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                result = objectMapper.readValue(responseBody, CoreOverViewResponseEntity.class);
            } catch (JsonParseException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
