From 95a5d8216d52f7d886d1db0773cefbde962d868b Mon Sep 17 00:00:00 2001 From: Awhn Date: Sun, 10 Nov 2024 20:01:37 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=9E=98=EB=AA=BB=EB=90=9C=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ActivityDataUpdateScheduler.java | 9 - .../gamsa/activtydata/ActivityDataUtils.java | 173 ------------------ .../activtydata/DistrictDataService.java | 73 -------- .../gamsa/activtydata/KakaoLocalService.java | 79 -------- 4 files changed, 334 deletions(-) delete mode 100644 src/main/java/com/gamsa/activtydata/ActivityDataUpdateScheduler.java delete mode 100644 src/main/java/com/gamsa/activtydata/ActivityDataUtils.java delete mode 100644 src/main/java/com/gamsa/activtydata/DistrictDataService.java delete mode 100644 src/main/java/com/gamsa/activtydata/KakaoLocalService.java diff --git a/src/main/java/com/gamsa/activtydata/ActivityDataUpdateScheduler.java b/src/main/java/com/gamsa/activtydata/ActivityDataUpdateScheduler.java deleted file mode 100644 index cc23037..0000000 --- a/src/main/java/com/gamsa/activtydata/ActivityDataUpdateScheduler.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.gamsa.activtydata; - -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.stereotype.Component; - -@Component -@EnableScheduling -public class ActivityDataUpdateScheduler { -} diff --git a/src/main/java/com/gamsa/activtydata/ActivityDataUtils.java b/src/main/java/com/gamsa/activtydata/ActivityDataUtils.java deleted file mode 100644 index b49dfaa..0000000 --- a/src/main/java/com/gamsa/activtydata/ActivityDataUtils.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.gamsa.activtydata; - -import com.gamsa.activity.constant.Category; -import com.gamsa.activity.dto.ActivitySaveRequest; -import com.gamsa.activity.dto.InstituteSaveRequest; -import com.gamsa.activity.service.InstituteService; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; -import org.w3c.dom.Node; -import org.xml.sax.InputSource; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import java.io.StringReader; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; - -@Component -public class ActivityDataUtils { - private final InstituteService instituteService; - - @Value("${gamsa.openapi.key}") - private String apiKey; - - @Value("${gamsa.openapi.url}") - private String baseUrl; - - @Value("${gamsa.vol.url}") - private String volUrl; - - private final RestTemplate restTemplate = new RestTemplate(); - - public ActivityDataUtils(InstituteService instituteService) { - this.instituteService = instituteService; - } - - public List getVolunteerParticipationList(String startDate, String endDate, int numOfRows, int pageNo) { - String url = baseUrl + "/getVltrPeriodSrvcList"; - - UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url) - .queryParam("ServiceKey", apiKey) - .queryParam("progrmBgnde", startDate) - .queryParam("progrmEndde", endDate) - .queryParam("numOfRows", numOfRows) - .queryParam("pageNo", pageNo); - - ResponseEntity response = restTemplate.getForEntity(uriBuilder.toUriString(), String.class); - - List volunteerList = new ArrayList<>(); - if (response.getStatusCode().is2xxSuccessful()) { - try { - // XML 파싱 - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - var document = builder.parse(new InputSource(new StringReader(response.getBody()))); - var items = document.getElementsByTagName("item"); - - for (int i = 0; i < items.getLength(); i++) { - Node item = items.item(i); - volunteerList.add(getTextContent(item, "progrmRegistNo").toString()); - } - } catch (Exception e) { - e.printStackTrace(); - // 예외 처리 - } - } else { - System.out.println("Error: " + response.getStatusCode()); - } - return volunteerList; - } - - private ActivitySaveRequest getVolunteerDetail(String programNo, Long instituteId) { - String url = baseUrl + "/getVltrPartcptnItem"; - - UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url) - .queryParam("ServiceKey", apiKey) - .queryParam("progrmRegistNo", programNo); - - ResponseEntity response = restTemplate.getForEntity(uriBuilder.toUriString(), String.class); - - if (response.getStatusCode().is2xxSuccessful()) { - try { - // XML 파싱 - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - var document = builder.parse(new InputSource(new StringReader(response.getBody()))); - var item = document.getElementsByTagName("item").item(0); - - if (item != null) { - InstituteSaveRequest instituteSaveRequest = InstituteSaveRequest.builder() - .name(getTextContent(item, "mnnstNm")) - .location(getTextContent(item, "getTextContent")) - .latitude() - .longitude() - .sidoCode(getTextContent(item, "sidoCd")) - .sidoGunguCode(getTextContent(item, "gugunCd")) - .phone(getTextContent(item, "telno")) - .build(); - - instituteService.save(instituteSaveRequest); - - ActivitySaveRequest activitySaveRequest = ActivitySaveRequest.builder() - .actId(Long.getLong(getTextContent(item, "progrmRegistNo"))) - .actTitle(getTextContent(item, "progrmSj")) - .actLocation(getTextContent(item, "actPlace")) - .description(getTextContent(item, "progrmCn")) - .noticeStartDate(LocalDateTime.parse(getTextContent(item, "noticeBgnde"))) - .noticeEndDate(LocalDateTime.parse(getTextContent(item, "noticeEndde"))) - .actStartDate(LocalDateTime.parse(getTextContent(item, "progrmBgnde"))) - .actEndDate(LocalDateTime.parse(getTextContent(item, "progrmBgnde"))) - .actStartTime(Integer.getInteger(getTextContent(item, "actBeginTm"))) - .actEndTime(Integer.getInteger(getTextContent(item, "actEndTm"))) - .recruitTotalNum(Integer.getInteger(getTextContent(item, "rcritNmpr"))) - .adultPossible(getTextContent(item, "adultPosblAt") == "y" ? true : false) - .teenPossible(getTextContent(item, "yngbgsPosblAt") == "y" ? true : false) - .groupPossible(getTextContent(item, "grpPosblAt") == "y" ? true : false) - .actWeek(Integer.getInteger(getTextContent(item, "actWkdy"))) - .actManager(getTextContent(item, "nanmmbyNmAdmn")) - .actPhone(getTextContent(item, "telno")) - .url(volUrl + getTextContent(item, "progrmRegistNo")) - .category(getCategory(getTextContent(item, "srvcClCode"))) - .instituteId(instituteId) - .sidoGunguCode(Integer.getInteger(getTextContent(item, "gugunCd"))) - .build(); - } - } catch (Exception e) { - e.printStackTrace(); - // 예외 처리 - } - } else { - System.out.println("Error: " + response.getStatusCode()); - } - return null; - } - - private Long getInstituteId(InstituteSaveRequest instituteSaveRequest) { - instituteService.findByName(); - } - - private String getTextContent(Node node, String tagName) { - var element = (Element) node; - var target = element.getElementsByTagName(tagName).item(0); - return (target != null) ? target.getTextContent() : null; - } - - private Category getCategory(String text) { - if ((text.contains("생활편의지원")) - || text.contains("주거환경") - || text.contains("농어촌 봉사")) { - return Category.LIFE_SUPPORT_AND_HOUSING_IMPROVEMENT; - } else if ((text.contains("교육")) - || text.contains("멘토링")) { - return Category.EDUCATION_AND_MENTORING; - } else if (text.contains("행정보조")) { - return Category.ADMINISTRATIVE_AND_OFFICE_SUPPORT; - } else if ((text.contains("문화행사")) - || text.contains("환경보호") - || text.contains("국제협력") - || text.contains("안전.예방")) { - return Category.CULTURE_ENVIRONMENT_AND_INTERNATIONAL_COOPERATION; - } else if ((text.contains("보건의료")) - || text.contains("공익.인권")) { - return Category.HEALTHCARE_AND_PUBLIC_WELFARE; - } else if ((text.contains("상담")) - || text.contains("자원봉사교육")) { - return Category.COUNSELING_AND_VOLUNTEER_TRAINING; - } else { - return Category.OTHER_ACTIVITIES; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/gamsa/activtydata/DistrictDataService.java b/src/main/java/com/gamsa/activtydata/DistrictDataService.java deleted file mode 100644 index 3d42716..0000000 --- a/src/main/java/com/gamsa/activtydata/DistrictDataService.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.gamsa.activtydata; - -import com.gamsa.activity.dto.DistrictSaveRequest; -import com.gamsa.activity.service.DistrictService; -import com.opencsv.CSVReader; -import com.opencsv.exceptions.CsvValidationException; -import jakarta.annotation.PostConstruct; -import jakarta.transaction.Transactional; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.math.BigDecimal; - -@Service -@Component -@RequiredArgsConstructor -public class DistrictDataService { - private final DistrictService districtService; - - @Value("{gamsa.csvPath}") - private String csvPath; - - @PostConstruct - public void DistrictInit() { - if (!isDataChanged()) { - loadDataFromCSV(csvPath); - } - } - - private boolean isDataChanged() { - // 나중에 file 변경에 따른 로직으로 수정 - if ((districtService.findAllGungu().size() == 0) - && (districtService.findAllSido().size() == 0)) { - return true; - } - return false; - } - - @Transactional - public void loadDataFromCSV(String csvPath) { - try { - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource(csvPath).getFile()); - FileReader fileReader = new FileReader(file); - CSVReader csvReader = new CSVReader(fileReader); - - csvReader.readNext(); - String[] nextRecord; - while ((nextRecord = csvReader.readNext()) != null) { - DistrictSaveRequest districtSaveRequest = DistrictSaveRequest.builder() - .sidoGunguCode(Integer.getInteger(nextRecord[0])) - .sidoCode(Integer.getInteger(nextRecord[1])) - .sidoName(nextRecord[2]) - .gunguName(nextRecord[3]) - .latitude(new BigDecimal(nextRecord[4])) - .longitude(new BigDecimal(nextRecord[5])) - .sido(Boolean.parseBoolean(nextRecord[6])) - .build(); - - districtService.save(districtSaveRequest); - } - } catch (IOException e) { - e.printStackTrace(); - } catch (CsvValidationException e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/main/java/com/gamsa/activtydata/KakaoLocalService.java b/src/main/java/com/gamsa/activtydata/KakaoLocalService.java deleted file mode 100644 index 4d7a1a2..0000000 --- a/src/main/java/com/gamsa/activtydata/KakaoLocalService.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.gamsa.activtydata; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; - -import java.math.BigDecimal; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -@Service -public class KakaoLocalService { - - @Value("${kakao.api.key}") - private String kakaoApiKey; - - private final RestTemplate restTemplate = new RestTemplate(); - - public Optional> getCoordinateByKeyword(String address) { - String url = "https://dapi.kakao.com/v2/local/search/keyword.json"; - Optional> coordinates = fetchCoordinates(url, address); - - if (coordinates.isEmpty()) { - // 키워드 검색 결과가 없을 때 주소 검색으로 대체 - coordinates = getCoordinateByAddress(address); - } - - return coordinates; - } - - public Optional> getCoordinateByAddress(String address) { - String url = "https://dapi.kakao.com/v2/local/search/address.json"; - return fetchCoordinates(url, address); - } - - private Optional> fetchCoordinates(String url, String address) { - // 요청 URL 생성 - UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url) - .queryParam("query", address); - - // 헤더 설정 - HttpHeaders headers = new HttpHeaders(); - headers.set("Authorization", "KakaoAK " + kakaoApiKey); - - // HTTP 요청 - HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange( - uriBuilder.toUriString(), HttpMethod.GET, entity, Map.class); - - // 응답 처리 - if (response.getStatusCode().is2xxSuccessful()) { - Map result = response.getBody(); - if (result != null && result.containsKey("documents")) { - // 첫 번째 결과의 x, y 좌표 반환 - var documents = (List>) result.get("documents"); - if (!documents.isEmpty()) { - Map firstDoc = documents.get(0); - BigDecimal x = new BigDecimal(firstDoc.get("x").toString()); - BigDecimal y = new BigDecimal(firstDoc.get("y").toString()); - - Map coordinates = new HashMap<>(); - coordinates.put("longitude", x); - coordinates.put("latitude", y); - return Optional.of(coordinates); - } - } - } - - System.out.println("API 요청 실패: " + response.getStatusCode()); - return Optional.empty(); - } -}