Newer
Older
CosmosServer / src / main / java / com / example / cosmos_serversb / models / Shops.java
t-kume on 14 Nov 2019 4 KB 細かな修正
package com.example.cosmos_serversb.models;

import com.example.common.LogUtils;
import com.example.cosmos_serversb.entities.*;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
//import org.hibernate.Session;
//import org.hibernate.SessionFactory;

import javax.inject.Singleton;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

@Singleton
public class Shops {

    private static Shops theInstance = null;
    private static ArrayList<Shop> shops = new ArrayList<>();
    //private static SessionFactory sessionFactory;
    private static String baseURI="http://nitta-lab-www.is.konan-u.ac.jp/";
    private static String AppName="cosmos";

    private Shops() {
        //sessionFactory = SessionFactoryManager.getInstance().getSessionFactory();
    }

    public static Shops getInstance() {
        if (theInstance == null) {
            theInstance = new Shops();
        }
        return theInstance;
    }

    public static String getResult(String urlString) {
        String result = "";
        try {
            URL url = new URL(urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.connect();
            String tmp = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            while ((tmp = in.readLine()) != null) {
                result += tmp;
            }
            in.close();
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static JsonNode getJsonNode(String jsonString) {
        JsonNode head = null;
        try {
            JsonFactory jfactory = new JsonFactory();
            JsonParser parser = jfactory.createJsonParser(jsonString);
            ObjectMapper mapper = new ObjectMapper();
            head = mapper.readTree(parser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return head;
    }

    private static JsonNode yolpConnection(String leftLongitude, String leftLatitude, String rightLongitude, String rightLatitude, int start, String gc) {
        String Result;
        JsonNode json;
        String URL = "https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLongitude + "," + leftLatitude + "," + rightLongitude + "," + rightLatitude + "&gc=" + gc + "&start=" + start + "&results=100&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-";
        Result = getResult(URL);
        json = getJsonNode(Result);
        return json;
    }

    private static ArrayList<Shop> getShopsByCode(String leftLongitude, String leftLatitude, String rightLongitude, String rightLatitude, String gc) {
        JsonNode jnode;
        int start = 1;
        int count = 100;
        int roop = 1;

        for(int i = 0; i < roop; i++) {
            jnode = yolpConnection(leftLongitude, leftLatitude, rightLongitude, rightLatitude, start, gc);
            String total = jnode.get("ResultInfo").get("Total").asText();

            if (Integer.parseInt(total) - (start - 1) < 100) {
                count = Integer.parseInt(total) - (start - 1);
            } else {
                if(Integer.parseInt(total) % count > 0) {
                    roop = Integer.parseInt(total) / count + 1;
                } else {
                    roop = Integer.parseInt(total) / count;
                }
            }
            start += 100;


            for (int j = 0; j < count; j++) {
                String genre = jnode.get("Feature").get(j).get("Property").get("Genre").get(0).get("Name").asText();
                String code = jnode.get("Feature").get(j).get("Property").get("Genre").get(0).get("Code").asText();
                String location = jnode.get("Feature").get(j).get("Geometry").get("Coordinates").asText();
                String[] split = location.split(",");
                Shop shop = new Shop(genre, Integer.parseInt(code), Double.parseDouble(split[1]), Double.parseDouble(split[0]));
                shops.add(shop);
            }
        }
        return shops;
    }

    public static ArrayList<Shop> getShop(String leftLongitude, String leftLatitude, String rightLongitude, String rightLatitude) {
        if(shops != null) {
            shops.clear();
        }

        shops = getShopsByCode(leftLongitude, leftLatitude, rightLongitude, rightLatitude, "0203");
        shops = getShopsByCode(leftLongitude, leftLatitude, rightLongitude, rightLatitude, "0205");

        return shops;
    }
}