Guest User

Untitled

a guest
Dec 1st, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.49 KB | None | 0 0
  1. package com.example.openapidatadb.controller;
  2.  
  3. import java.io.IOException;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import lombok.RequiredArgsConstructor;
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16.  
  17. @RestController
  18. @RequiredArgsConstructor
  19. public class MainController {
  20.  
  21.     @PostMapping("/")
  22.     public void start()
  23.         throws IOException, InvocationTargetException, NoSuchMethodException,
  24.         IllegalAccessException {
  25.         List<String> jsons = readTxtFilesFromDirectory("./src/main/resources");
  26.  
  27.         for (int i = jsons.size()-1; i >= 0; i--) {
  28.             makeInsertSql(jsons.get(i));
  29.         }
  30.     }
  31.  
  32.     private List<String> readTxtFilesFromDirectory(String directoryPath) throws IOException {
  33.         List<String> fileContents = new ArrayList<>();
  34.         Files.walk(Paths.get(directoryPath))
  35.             .filter(Files::isRegularFile)
  36.             .filter(path -> path.toString().endsWith(".txt"))
  37.             .forEach(path -> {
  38.                 try {
  39.                     fileContents.addAll(Files.readAllLines(path));
  40.                 } catch (IOException e) {
  41.                     e.printStackTrace();
  42.                 }
  43.             });
  44.         return fileContents;
  45.     }
  46.  
  47.     public void makeInsertSql(String json)
  48.         throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  49.         StringBuilder sb = new StringBuilder();
  50.         JSONObject base = new JSONObject(json);
  51.  
  52.         Object detailsItems = base.getJSONObject("more_detail_body").get("items");
  53.         JSONArray details = new JSONArray("[]");
  54.         if (detailsItems instanceof JSONObject) {
  55.             details = ((JSONObject) detailsItems).getJSONArray("item");
  56.         }
  57.         if (details.length() == 0) {
  58.             return;
  59.         }
  60.  
  61.         Object introsItems = base.getJSONObject("intro_detail_body").getJSONObject("items");
  62.         JSONArray intros = new JSONArray("[]");
  63.         if (introsItems instanceof JSONObject) {
  64.             intros = ((JSONObject) introsItems).getJSONArray("item");
  65.         }
  66.         if (intros.length() == 0) {
  67.             return;
  68.         }
  69.         JSONObject intro = intros.getJSONObject(0);
  70.  
  71.         Object commonsItems = base.getJSONObject("common_detail_body").getJSONObject("items");
  72.         JSONArray commons = new JSONArray("[]");
  73.         if (commonsItems instanceof JSONObject) {
  74.             commons = ((JSONObject) commonsItems).getJSONArray("item");
  75.         }
  76.         if (commons.length() == 0) {
  77.             return;
  78.         }
  79.         JSONObject common = commons.getJSONObject(0);
  80.  
  81.         Long id = Long.valueOf(base.getString("contentid"));
  82.         String name = base.getString("title").replace("'", "''");
  83.         String category = cat3ToString(base.getString("cat3"));
  84.         String description = common.getString("overview").replace("'", "''").replace("\n", "<br/>");
  85.         String zip_code = common.getString("zipcode");
  86.         String address = base.getString("addr1");
  87.         String longitude = base.getString("mapx");
  88.         String latitude = base.getString("mapy");
  89.         String area = address.split(" ")[0];
  90.         String sigungu = address.split(" ")[1];
  91.  
  92.         sb.append(
  93.             String.format(
  94.                 "INSERT INTO Product(id, name, category, description, zip_code, address, longitude, latitude, area, sigungu) VALUES(" +
  95.                     "%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n",
  96.                 id, name, category, description, zip_code, address, longitude, latitude, area,
  97.                 sigungu)
  98.         );
  99.  
  100.         String image1 = base.getString("firstimage");
  101.         if (image1.isEmpty()) return;
  102.  
  103.         sb.append(
  104.             String.format("INSERT INTO Product_Image(product_id, url) VALUES(" +
  105.                     "%d, '%s');\n",
  106.                 id, image1
  107.             )
  108.         );
  109.  
  110.         String image2 = base.getString("firstimage2");
  111.         if (!image2.isEmpty()) {
  112.             sb.append(
  113.                 String.format("INSERT INTO Product_Image(product_id, url) VALUES(" +
  114.                         "%d, '%s');\n",
  115.                     id, image2
  116.                 )
  117.             );
  118.         }
  119.  
  120.         for (Iterator<Object> it = details.iterator(); it.hasNext(); ) {
  121.             JSONObject roomInfo = (JSONObject) it.next();
  122.  
  123.             Integer roomCode = Integer.valueOf(roomInfo.getString("roomcode"));
  124.             String roomName = roomInfo.getString("roomtitle").replace("'", "''");
  125.             Integer price = Integer.valueOf(roomInfo.getString("roomoffseasonminfee1"));
  126.             Integer stock = Integer.valueOf(roomInfo.getString("roomcount"));
  127.             String check_in_time = intro.getString("checkintime");
  128.             String check_out_time = intro.getString("checkouttime");
  129.             Integer base_guest_count = Integer.valueOf(roomInfo.getString("roombasecount"));
  130.             Integer max_guest_count = Integer.valueOf(roomInfo.getString("roommaxcount"));
  131.             String room_facilities = roomInfo.toString().replace("'", "''");
  132.  
  133.             List<String> roomImages = new ArrayList<>();
  134.             for (int i = 1; i <= 5; i++) {
  135.                 String image = roomInfo.getString("roomimg" + i);
  136.                 if (image.isEmpty()) continue;
  137.  
  138.                 roomImages.add(image);
  139.             }
  140.             if (roomImages.isEmpty()) return;
  141.  
  142.             sb.append(
  143.                 String.format(
  144.                     "INSERT INTO Room(id, product_id, name, price, stock, check_in_time, check_out_time, base_guest_count, max_guest_count, room_facilities) VALUES(" +
  145.                         "%d, %d, '%s', %d, %d, '%s', '%s', %d, %d, '%s');\n",
  146.                     roomCode, id, roomName, price, stock, check_in_time, check_out_time,
  147.                     base_guest_count, max_guest_count, room_facilities)
  148.             );
  149.  
  150.             for (String roomImage : roomImages) {
  151.                 sb.append(
  152.                     String.format("INSERT INTO Room_Image(room_id, url) VALUES(" +
  153.                             "%d, '%s');\n"
  154.                         , roomCode, roomImage)
  155.                 );
  156.             }
  157.         }
  158.  
  159.         System.out.println(sb);
  160.     }
  161.  
  162.     private String cat3ToString(String cat3) {
  163.         switch (cat3) {
  164.             case "B02010100":
  165.                 return "관광호텔";
  166.             case "B02010500":
  167.                 return "콘도미니엄";
  168.             case "B02010600":
  169.                 return "유스호스텔";
  170.             case "B02010700":
  171.                 return "펜션";
  172.             case "B02010900":
  173.                 return "모텔";
  174.             case "B02011000":
  175.                 return "민박";
  176.             case "B02011100":
  177.                 return "게스트하우스";
  178.             case "B02011200":
  179.                 return "홈스테이";
  180.             case "B02011300":
  181.                 return "서비스드레지던스";
  182.             case "B02011600":
  183.                 return "한옥";
  184.             case "A02020200":
  185.                 return "관광단지";
  186.             case "A03020200":
  187.                 return "수련시설";
  188.             case "A03021700":
  189.                 return "야영장,오토캠핑장";
  190.         }
  191.         throw new RuntimeException("cat3코드 없음: " + cat3);
  192.     }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment