Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.openapidatadb.controller;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import lombok.RequiredArgsConstructor;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @RequiredArgsConstructor
- public class MainController {
- @PostMapping("/")
- public void start()
- throws IOException, InvocationTargetException, NoSuchMethodException,
- IllegalAccessException {
- List<String> jsons = readTxtFilesFromDirectory("./src/main/resources");
- for (int i = jsons.size()-1; i >= 0; i--) {
- makeInsertSql(jsons.get(i));
- }
- }
- private List<String> readTxtFilesFromDirectory(String directoryPath) throws IOException {
- List<String> fileContents = new ArrayList<>();
- Files.walk(Paths.get(directoryPath))
- .filter(Files::isRegularFile)
- .filter(path -> path.toString().endsWith(".txt"))
- .forEach(path -> {
- try {
- fileContents.addAll(Files.readAllLines(path));
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- return fileContents;
- }
- public void makeInsertSql(String json)
- throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
- StringBuilder sb = new StringBuilder();
- JSONObject base = new JSONObject(json);
- Object detailsItems = base.getJSONObject("more_detail_body").get("items");
- JSONArray details = new JSONArray("[]");
- if (detailsItems instanceof JSONObject) {
- details = ((JSONObject) detailsItems).getJSONArray("item");
- }
- if (details.length() == 0) {
- return;
- }
- Object introsItems = base.getJSONObject("intro_detail_body").getJSONObject("items");
- JSONArray intros = new JSONArray("[]");
- if (introsItems instanceof JSONObject) {
- intros = ((JSONObject) introsItems).getJSONArray("item");
- }
- if (intros.length() == 0) {
- return;
- }
- JSONObject intro = intros.getJSONObject(0);
- Object commonsItems = base.getJSONObject("common_detail_body").getJSONObject("items");
- JSONArray commons = new JSONArray("[]");
- if (commonsItems instanceof JSONObject) {
- commons = ((JSONObject) commonsItems).getJSONArray("item");
- }
- if (commons.length() == 0) {
- return;
- }
- JSONObject common = commons.getJSONObject(0);
- Long id = Long.valueOf(base.getString("contentid"));
- String name = base.getString("title").replace("'", "''");
- String category = cat3ToString(base.getString("cat3"));
- String description = common.getString("overview").replace("'", "''").replace("\n", "<br/>");
- String zip_code = common.getString("zipcode");
- String address = base.getString("addr1");
- String longitude = base.getString("mapx");
- String latitude = base.getString("mapy");
- String area = address.split(" ")[0];
- String sigungu = address.split(" ")[1];
- sb.append(
- String.format(
- "INSERT INTO Product(id, name, category, description, zip_code, address, longitude, latitude, area, sigungu) VALUES(" +
- "%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n",
- id, name, category, description, zip_code, address, longitude, latitude, area,
- sigungu)
- );
- String image1 = base.getString("firstimage");
- if (image1.isEmpty()) return;
- sb.append(
- String.format("INSERT INTO Product_Image(product_id, url) VALUES(" +
- "%d, '%s');\n",
- id, image1
- )
- );
- String image2 = base.getString("firstimage2");
- if (!image2.isEmpty()) {
- sb.append(
- String.format("INSERT INTO Product_Image(product_id, url) VALUES(" +
- "%d, '%s');\n",
- id, image2
- )
- );
- }
- for (Iterator<Object> it = details.iterator(); it.hasNext(); ) {
- JSONObject roomInfo = (JSONObject) it.next();
- Integer roomCode = Integer.valueOf(roomInfo.getString("roomcode"));
- String roomName = roomInfo.getString("roomtitle").replace("'", "''");
- Integer price = Integer.valueOf(roomInfo.getString("roomoffseasonminfee1"));
- Integer stock = Integer.valueOf(roomInfo.getString("roomcount"));
- String check_in_time = intro.getString("checkintime");
- String check_out_time = intro.getString("checkouttime");
- Integer base_guest_count = Integer.valueOf(roomInfo.getString("roombasecount"));
- Integer max_guest_count = Integer.valueOf(roomInfo.getString("roommaxcount"));
- String room_facilities = roomInfo.toString().replace("'", "''");
- List<String> roomImages = new ArrayList<>();
- for (int i = 1; i <= 5; i++) {
- String image = roomInfo.getString("roomimg" + i);
- if (image.isEmpty()) continue;
- roomImages.add(image);
- }
- if (roomImages.isEmpty()) return;
- sb.append(
- String.format(
- "INSERT INTO Room(id, product_id, name, price, stock, check_in_time, check_out_time, base_guest_count, max_guest_count, room_facilities) VALUES(" +
- "%d, %d, '%s', %d, %d, '%s', '%s', %d, %d, '%s');\n",
- roomCode, id, roomName, price, stock, check_in_time, check_out_time,
- base_guest_count, max_guest_count, room_facilities)
- );
- for (String roomImage : roomImages) {
- sb.append(
- String.format("INSERT INTO Room_Image(room_id, url) VALUES(" +
- "%d, '%s');\n"
- , roomCode, roomImage)
- );
- }
- }
- System.out.println(sb);
- }
- private String cat3ToString(String cat3) {
- switch (cat3) {
- case "B02010100":
- return "관광호텔";
- case "B02010500":
- return "콘도미니엄";
- case "B02010600":
- return "유스호스텔";
- case "B02010700":
- return "펜션";
- case "B02010900":
- return "모텔";
- case "B02011000":
- return "민박";
- case "B02011100":
- return "게스트하우스";
- case "B02011200":
- return "홈스테이";
- case "B02011300":
- return "서비스드레지던스";
- case "B02011600":
- return "한옥";
- case "A02020200":
- return "관광단지";
- case "A03020200":
- return "수련시설";
- case "A03021700":
- return "야영장,오토캠핑장";
- }
- throw new RuntimeException("cat3코드 없음: " + cat3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment