diff --git a/spring/notinote/Dockerfile b/spring/notinote/Dockerfile new file mode 100644 index 0000000..cac2191 --- /dev/null +++ b/spring/notinote/Dockerfile @@ -0,0 +1,7 @@ +FROM adoptopenjdk/openjdk11 + +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} app.jar + +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "/app.jar"] \ No newline at end of file diff --git a/spring/notinote/src/main/java/com/answer/notinote/Event/controller/EventController.java b/spring/notinote/src/main/java/com/answer/notinote/Event/controller/EventController.java index 45c6459..6957a2a 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Event/controller/EventController.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Event/controller/EventController.java @@ -44,12 +44,7 @@ public ResponseEntity getEventList() { } @PutMapping("/event/register") - public ResponseEntity registerEvent(@RequestParam(value = "id") Long id, @RequestBody EventRegisterDto registerDto) throws GeneralSecurityException, IOException { - return ResponseEntity.ok(eventService.registerEvent(id, registerDto)); - } - - @PostMapping("/event/register") - public ResponseEntity registerEventWithoutId(@RequestBody EventRegisterDto registerDto) throws GeneralSecurityException, IOException { - return ResponseEntity.ok(eventService.registerEvent(registerDto)); + public ResponseEntity registerEvent(@RequestParam(value = "eid") Long eid, @RequestBody EventRegisterDto registerDto) throws GeneralSecurityException, IOException { + return ResponseEntity.ok(eventService.registerEvent(eid, registerDto)); } } diff --git a/spring/notinote/src/main/java/com/answer/notinote/Event/service/EventService.java b/spring/notinote/src/main/java/com/answer/notinote/Event/service/EventService.java index 3d620e4..512a65f 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Event/service/EventService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Event/service/EventService.java @@ -28,7 +28,7 @@ public class EventService { public Event create(EventRequestDto requestDto, Notice notice) { Event event = new Event(requestDto); - event.setNotice(notice); + if (notice != null) event.setNotice(notice); eventRepository.save(event); return event; diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java index 71a0d64..ef3941d 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java @@ -1,16 +1,13 @@ package com.answer.notinote.Notice.controller; - import com.answer.notinote.Auth.token.provider.JwtTokenProvider; import com.answer.notinote.Event.dto.EventRequestDto; -import com.answer.notinote.Notice.dto.NoticeOCRDto; -import com.answer.notinote.Notice.dto.NoticeRequestDto; -import com.answer.notinote.Notice.dto.NoticeSentenceDto; -import com.answer.notinote.Notice.dto.NoticeTitleListDto; +import com.answer.notinote.Event.service.EventService; +import com.answer.notinote.Notice.dto.*; import com.answer.notinote.Notice.service.NoticeService; import com.answer.notinote.User.domain.entity.User; import com.answer.notinote.User.service.UserService; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -21,34 +18,30 @@ @RestController +@RequiredArgsConstructor public class NoticeController { - @Autowired - NoticeService noticeService; + private final NoticeService noticeService; - @Autowired - UserService userService; + private final UserService userService; - @Autowired - JwtTokenProvider jwtTokenProvider; + private final JwtTokenProvider jwtTokenProvider; - public NoticeController(NoticeService noticeService) { - this.noticeService = noticeService; - } + private final EventService eventService; @RequestMapping(value = "/notice/ocr", method = RequestMethod.POST) - public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServletRequest userrequest) throws IOException { - String token = jwtTokenProvider.resolveAccessToken(userrequest); + public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServletRequest request) throws IOException { + String token = jwtTokenProvider.resolveAccessToken(request); String email = jwtTokenProvider.getUserEmail(token); User user = userService.findUserByEmail(email); String targetLanguage = user.getUlanguage(); String korean = noticeService.detectText(uploadfile); //원문 추출 String trans_full = noticeService.transText(korean, targetLanguage); //번역문 추출 - List events = noticeService.detectEventOCR(korean, trans_full, targetLanguage); //이벤트 추출 - List fullText = noticeService.extractSentenceFromEventOCR(trans_full, events); - return new NoticeOCRDto(korean, trans_full, fullText); + List eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage); //이벤트 추출 + List fullText = noticeService.extractSentenceFromEventRequestDto(trans_full, eventWords); + return new NoticeOCRDto(korean, trans_full, fullText); } @RequestMapping(value = "/notice/save", method = RequestMethod.POST) diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventDto.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventDto.java new file mode 100644 index 0000000..4d22ab1 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventDto.java @@ -0,0 +1,19 @@ +package com.answer.notinote.Notice.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDate; + +@Getter +@AllArgsConstructor +@Builder +public class NoticeEventDto { + int id; + long eid; + String content; + LocalDate date; + boolean highlight; + boolean registered; +} diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeSentenceDto.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeSentenceDto.java index 755402b..8d64c9b 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeSentenceDto.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeSentenceDto.java @@ -11,9 +11,7 @@ @Builder public class NoticeSentenceDto { int id; - long eid = -1; String content; LocalDate date; boolean highlight; - boolean registered; -} +} \ No newline at end of file diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeTitleListDto.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeTitleListDto.java index e19d1af..045fb1d 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeTitleListDto.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeTitleListDto.java @@ -12,14 +12,16 @@ public class NoticeTitleListDto { private String uploadfile; private String title; private LocalDate date; - private List fullText = new ArrayList<>(); + private List fullText = new ArrayList<>(); private String korean; + private String trans_full; - public NoticeTitleListDto(Notice entity, List sentences){ + public NoticeTitleListDto(Notice entity, List sentences){ this.uploadfile = entity.getNimageurl() + "/" + entity.getNimagename(); this.title = entity.getTitle(); this.date = entity.getNdate(); this.korean = entity.getOrigin_full(); + this.trans_full = entity.getTrans_full(); this.fullText.addAll(sentences); } diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java index 8830c94..78d7d3b 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java @@ -3,16 +3,13 @@ import com.answer.notinote.Auth.token.provider.JwtTokenProvider; import com.answer.notinote.Child.service.ChildService; import com.answer.notinote.Event.domain.Event; -import com.answer.notinote.Notice.dto.NoticeResponseBody; +import com.answer.notinote.Notice.dto.*; import com.answer.notinote.Event.dto.EventRequestDto; import com.answer.notinote.Event.service.EventService; import com.answer.notinote.Exception.CustomException; import com.answer.notinote.Exception.ErrorCode; import com.answer.notinote.Notice.domain.entity.Notice; import com.answer.notinote.Notice.domain.repository.NoticeRepository; -import com.answer.notinote.Notice.dto.NoticeRequestDto; -import com.answer.notinote.Notice.dto.NoticeSentenceDto; -import com.answer.notinote.Notice.dto.NoticeTitleListDto; import com.answer.notinote.User.domain.entity.User; import com.answer.notinote.User.domain.repository.UserRepository; import com.fasterxml.jackson.core.JsonProcessingException; @@ -116,7 +113,7 @@ public String transText(String korean, String targetLanguage) throws IOException return textlist.get(0); } - public List detectEventOCR(String korean, String trans_full, String language) throws JsonProcessingException { + public List detectEvent(String korean, String translation, String language) throws JsonProcessingException { String url = "https://notinote.herokuapp.com/event-dict"; HttpHeaders headers = new HttpHeaders(); @@ -124,115 +121,19 @@ public List detectEventOCR(String korean, String trans_full, St JSONObject body = new JSONObject(); body.put("language", language); - body.put("kr_text",korean); - body.put("translated_text", trans_full); - + body.put("kr_text", korean); + body.put("translated_text", translation); HttpEntity request = new HttpEntity<>(body.toString(), headers); - String response = new RestTemplate().postForObject(url, request, String.class); - - JsonNode root = new ObjectMapper().readTree(response); - if (root.path("message") != null) { - if (root.path("message").asText().equals("no events")) { - return null; - } - } - - EventRequestDto[] eventDtos = new ObjectMapper().treeToValue(root.path("body"), EventRequestDto[].class); - List events = new ArrayList<>(); - for (EventRequestDto dto : eventDtos) { - events.add(dto); - } - return events; - } - - public List extractSentenceFromEventOCR(String text, List events) { - List sentences = new ArrayList<>(); - int lastIndex = 0, id = 1; - - if (events == null) { - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id) - .date(null) - .content(text) - .highlight(false) - .registered(false) - .build(); - sentences.add(dto); - return sentences; - } - - Collections.sort(events); - - for (EventRequestDto event : events) { - if (lastIndex != event.getS_index()) { - // event가 아닌 경우 - String sentence = text.substring(lastIndex, event.getS_index()); - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id++) - .content(sentence) - .date(null) - .highlight(false) - .registered(false) - .build(); - sentences.add(dto); - } - - // event인 경우 - String sentence = text.substring(event.getS_index(), event.getE_index()); - - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id++) - .content(sentence) - .date(LocalDate.parse(event.getDate())) - .highlight(true) - .registered(false) - .build(); - sentences.add(dto); - - lastIndex = event.getE_index(); - } - if (lastIndex != text.length() - 1) { - String sentence = text.substring(lastIndex, text.length() - 1); - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id) - .content(sentence) - .date(null) - .highlight(false) - .registered(false) - .build(); - sentences.add(dto); - } - - return sentences; - } - - public List detectEvent(Notice notice, String language) throws JsonProcessingException { - String url = "https://notinote.herokuapp.com/event-dict"; - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - JSONObject body = new JSONObject(); - body.put("language", language); - body.put("kr_text", notice.getOrigin_full()); - body.put("translated_text", notice.getTrans_full()); - - HttpEntity request = new HttpEntity<>(body.toString(), headers); String response = new RestTemplate().postForObject(url, request, String.class); - JsonNode root = new ObjectMapper().readTree(response); - if (root.path("message") != null) { - if (root.path("message").asText().equals("no events")) { - return null; - } - } + + boolean noEvents = root.path("message") != null && root.path("message").asText().equals("no events"); + if (noEvents) return null; NoticeResponseBody responseBody = new ObjectMapper().treeToValue(root, NoticeResponseBody.class); - List events = new ArrayList<>(); - for (EventRequestDto dto : responseBody.getBody()) { - events.add(eventService.create(dto, notice)); - } - return events; + + return responseBody.getBody(); } public NoticeTitleListDto saveNotice(MultipartFile uploadfile, NoticeRequestDto noticeRequestDto, HttpServletRequest request) throws IOException{ @@ -263,24 +164,21 @@ public NoticeTitleListDto saveNotice(MultipartFile uploadfile, NoticeRequestDto .build(); noticeRepository.save(notice); - List events = detectEvent(notice, user.getUlanguage()); - List sentences = extractSentenceFromEvent(notice.getTrans_full(), events); + List eventWords = detectEvent(notice.getOrigin_full(), notice.getTrans_full(), user.getUlanguage()); + List events = new ArrayList<>(); + for (EventRequestDto dto : eventWords) events.add(eventService.create(dto, notice)); + + List sentences = extractSentenceFromEvent(notice.getTrans_full(), events); return new NoticeTitleListDto(notice, sentences); } - public List extractSentenceFromEvent(String text, List events) { - List sentences = new ArrayList<>(); + public List extractSentenceFromEvent(String text, List events) { + List sentences = new ArrayList<>(); int lastIndex = 0, id = 1; if (events == null) { - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id) - .date(null) - .content(text) - .highlight(false) - .registered(false) - .build(); + NoticeEventDto dto = new NoticeEventDto(id, -1, text, null, false, false); sentences.add(dto); return sentences; } @@ -291,45 +189,65 @@ public List extractSentenceFromEvent(String text, List if (lastIndex != event.getIndex_start()) { // event가 아닌 경우 String sentence = text.substring(lastIndex, event.getIndex_start()); - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id++) - .content(sentence) - .date(null) - .highlight(false) - .registered(false) - .build(); + NoticeEventDto dto = new NoticeEventDto(id++, -1, sentence, null, false, false); sentences.add(dto); } // event인 경우 String sentence = text.substring(event.getIndex_start(), event.getIndex_end()); - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id++) - .eid(event.getEid()) - .content(sentence) - .date(event.getDate()) - .highlight(true) - .registered(event.isRegistered()) - .build(); + NoticeEventDto dto = new NoticeEventDto(id++, event.getEid(), sentence, event.getDate(), true, event.isRegistered()); sentences.add(dto); lastIndex = event.getIndex_end(); } + + if (lastIndex != text.length() - 1) { + String sentence = text.substring(lastIndex, text.length() - 1); + NoticeEventDto dto = new NoticeEventDto(id, -1, sentence, null, false, false); + sentences.add(dto); + } + + return sentences; + } + + public List extractSentenceFromEventRequestDto(String text, List events) { + List sentences = new ArrayList<>(); + int lastIndex = 0, id = 1; + + if (events == null) { + NoticeSentenceDto dto = new NoticeSentenceDto(id, text, null, false); + sentences.add(dto); + return sentences; + } + + Collections.sort(events); + + for (EventRequestDto event : events) { + if (lastIndex != event.getS_index()) { + // event가 아닌 경우 + String sentence = text.substring(lastIndex, event.getS_index()); + NoticeSentenceDto dto = new NoticeSentenceDto(id++, sentence, null, false); + sentences.add(dto); + } + + // event인 경우 + String sentence = text.substring(event.getS_index(), event.getE_index()); + NoticeSentenceDto dto = new NoticeSentenceDto(id++, sentence, LocalDate.parse(event.getDate()), true); + sentences.add(dto); + + lastIndex = event.getE_index(); + } + if (lastIndex != text.length() - 1) { String sentence = text.substring(lastIndex, text.length() - 1); - NoticeSentenceDto dto = NoticeSentenceDto.builder() - .id(id) - .content(sentence) - .date(null) - .highlight(false) - .registered(false) - .build(); + NoticeSentenceDto dto = new NoticeSentenceDto(id, sentence, null, false); sentences.add(dto); } return sentences; } + public Notice findNoticeById(Long id) { return noticeRepository.findById(id).orElseThrow( () -> new CustomException(ErrorCode.NOT_FOUND) diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchResultDetailDto.java b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchResultDetailDto.java index 0978a68..12b2101 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchResultDetailDto.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchResultDetailDto.java @@ -1,7 +1,6 @@ package com.answer.notinote.Search.dto; -import com.answer.notinote.Notice.domain.entity.Notice; -import com.answer.notinote.Notice.dto.NoticeSentenceDto; +import com.answer.notinote.Notice.dto.NoticeEventDto; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -13,11 +12,11 @@ public class SearchResultDetailDto { private Long id; private String imageUri; - private List fullText; + private List fullText; private String korean; @Builder - public SearchResultDetailDto(Long id, String imageUri, List fullText, String korean){ + public SearchResultDetailDto(Long id, String imageUri, List fullText, String korean){ this.id = id; this.imageUri = imageUri; this.fullText = fullText; diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java b/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java index d08f51f..0e0d661 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java @@ -5,7 +5,7 @@ import com.answer.notinote.Event.service.EventService; import com.answer.notinote.Notice.domain.entity.Notice; import com.answer.notinote.Notice.domain.repository.NoticeRepository; -import com.answer.notinote.Notice.dto.NoticeSentenceDto; +import com.answer.notinote.Notice.dto.NoticeEventDto; import com.answer.notinote.Notice.service.NoticeService; import com.answer.notinote.Search.dto.SearchDetailDto; import com.answer.notinote.Search.dto.SearchListDto; @@ -88,7 +88,7 @@ public SearchDetailDto searchDetailList(String date, HttpServletRequest request) Notice notice = notices.get(i); List events = eventService.findAllByNotice(notice); - List fullText = noticeService.extractSentenceFromEvent(notice.getTrans_full(), events); + List fullText = noticeService.extractSentenceFromEvent(notice.getTrans_full(), events); SearchResultDetailDto searchResultDetailDto = SearchResultDetailDto.builder() .imageUri(notice.getNimageurl()+"/"+notice.getNimagename()) diff --git a/spring/notinote/src/main/java/com/answer/notinote/User/dto/UserResponseDto.java b/spring/notinote/src/main/java/com/answer/notinote/User/dto/UserResponseDto.java index 86459c1..12ddfec 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/User/dto/UserResponseDto.java +++ b/spring/notinote/src/main/java/com/answer/notinote/User/dto/UserResponseDto.java @@ -20,7 +20,7 @@ public class UserResponseDto { private String uemail; private String ulanguage; private Long uprofileImg; - private List uchildren = new ArrayList<>(); + private List uchildren; private RoleType uroleType; public UserResponseDto(User user) { @@ -30,6 +30,7 @@ public UserResponseDto(User user) { this.ulanguage = user.getUlanguage(); this.uprofileImg = user.getUprofileImg(); this.uroleType = user.getUroleType(); + this.uchildren = new ArrayList<>(); if (user.getUchildren() != null) { user.getUchildren().forEach( child -> this.uchildren.add(new ChildDto(child))); diff --git a/spring/notinote/tokens/StoredCredential b/spring/notinote/tokens/StoredCredential index 409ed97..c90a219 100644 Binary files a/spring/notinote/tokens/StoredCredential and b/spring/notinote/tokens/StoredCredential differ