저번에는 자바로 구글캘린더와 연동해서 데이터까지 가져와봤다. 이제 그 데이터를 가지고 양방향으로 연동이 되는지 해보자. 우선 가지고 온 데이터를 DB Table에다가 저장해주는 로직을 짰다.
테이블 정보
public void getCalendarList() {
try {
// Google Calendar 서비스 객체 생성
Calendar service = getCalendarService();
// 캘린더 리스트 가져오기
CalendarList calendarList = service.calendarList().list().execute();
// 캘린더 목록 출력
for (CalendarListEntry calendar : calendarList.getItems()) {
SchdulCeoVO scVo = new SchdulCeoVO();
String calendarNm = calendar.getSummary();
Integer empSeq = sDao.getCalendarEmpSeq(calendar.getId());
if(empSeq != null) {
scVo.setEmpSeq(empSeq);
Events event2 = service.events().list(calendar.getId())
.setOrderBy("startTime") // 시작 시간 기준으로 정렬
.setSingleEvents(true) // 반복 이벤트를 개별적으로 표시
.execute();
for (Event event : event2.getItems()) {
scVo.setTitle(event.getSummary());
scVo.setCn(event.getDescription());
scVo.setLocation("googleAPI");
scVo.setEventId(event.getId());
// 이벤트 생성 시간 (UTC -> KST)
DateTime createdTime = event.getCreated();
if (createdTime != null) {
Instant createdInstant = Instant.ofEpochMilli(createdTime.getValue());
ZonedDateTime createdZonedTime = ZonedDateTime.ofInstant(
createdInstant,
ZoneId.of("Asia/Seoul")
);
scVo.setCreateDt(createdZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
// 이벤트 수정 시간 (UTC -> KST)
DateTime updatedTime = event.getUpdated();
if (updatedTime != null) {
Instant updatedInstant = Instant.ofEpochMilli(updatedTime.getValue());
ZonedDateTime updatedZonedTime = ZonedDateTime.ofInstant(
updatedInstant,
ZoneId.of("Asia/Seoul")
);
scVo.setCreateDt(updatedZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
// 이벤트 시작 시간 (start)
EventDateTime start = event.getStart();
if (start.getDateTime() != null) {
Instant startInstant = Instant.ofEpochMilli(start.getDateTime().getValue());
ZonedDateTime startZonedTime = ZonedDateTime.ofInstant(
startInstant,
ZoneId.of("Asia/Seoul")
);
scVo.setStartDt(startZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
} else if (start.getDate() != null) {
Instant startInstant = Instant.ofEpochMilli(start.getDate().getValue());
ZonedDateTime startZonedTime = ZonedDateTime.ofInstant(
startInstant,
ZoneId.of("Asia/Seoul")
);
scVo.setStartDt(startZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
// 이벤트 종료 시간 (end)
EventDateTime end = event.getEnd();
if (end.getDateTime() != null) {
Instant endInstant = Instant.ofEpochMilli(end.getDateTime().getValue());
ZonedDateTime endZonedTime = ZonedDateTime.ofInstant(
endInstant,
ZoneId.of("Asia/Seoul")
);
scVo.setEndDt(endZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
} else if (end.getDate() != null) {
// 종일 이벤트의 경우 종료 날짜를 직접 사용
LocalDate endDate = LocalDate.ofEpochDay(end.getDate().getValue() / (1000 * 60 * 60 * 24));
endDate = endDate.minusDays(1);
// 종료일의 끝 시간을 지정 (23:59:59)
ZonedDateTime endZonedTime = endDate.atTime(23, 59, 59).atZone(ZoneId.of("Asia/Seoul"));
// ZonedDateTime을 포맷팅하여 저장
scVo.setEndDt(endZonedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
dao.saveGoogleApiData(scVo); // 테이블 저장 로직
}
}
}
} catch (IOException | GeneralSecurityException e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();
}
}
먼저 캘린더 리스트를 가져온 후 테이블 컬럼에 맞게 데이터를 vo에 담는 작업을 해준다.
해당 로직을 실행시키면 김형서 캘린더 / 홍길동 캘린더에 있는 일정을 가져와 테이블에 insert작업을 한다. 정상 insert가 됐다면 구글 캘린더일정이 DB에 담겨있는걸 확인할 수 있다.
이제 이 데이터를 event 객체로 jsp로 넘겨주면 fullCalendar에 활성화가 된다. 그럼 google calendar 일정과 똑같은걸 확인할 수 있다.
- fullCalendar
- Google Calendar
Google Calendar -> JAVA 데이터 연동 확인을 했다.
이제 JAVA -> Google Calendar 등록
/수정
/삭제
가 되는지 확인해보자.
기본 게시판 CRUD에 Google Calendar action 로직을 추가해주자.
//일정 등록 로직
@Override
public boolean schdulCeoSave(SchdulCeoVO vo) throws ParseException {
String startDate = vo.getStartDate();
String startTime = vo.getStartTime();
String endDate = vo.getEndDate();
String endTime = vo.getEndTime();
String startDt = startDate + " " + startTime + ":00";
String endDt = endDate + " " + endTime + ":01";
vo.setEmpSeq(SessionEmpInfoHelper.getEmpSeq());
vo.setStartDt(startDt);
vo.setEndDt(endDt);
//java -> google action로직
boolean chk = getToGoogleSave(vo, "insert");
if(chk) {
dao.insertSchdulCeo(vo);
}
return chk;
}
//일정 수정 로직
@Override
public boolean schdulCeoUpdate(SchdulCeoVO vo) throws ParseException {
String startDate = vo.getStartDate();
String startTime = vo.getStartTime();
String endDate = vo.getEndDate();
String endTime = vo.getEndTime();
String startDt = startDate + " " + startTime + ":00";
String endDt = endDate + " " + endTime + ":01";
vo.setStartDt(startDt);
vo.setEndDt(endDt);
boolean chk = getToGoogleSave(vo, "update");
if(chk) {
dao.updateSchdulCeo(vo);
}
return chk;
}
//일정 삭제 로직
@Override
public boolean schdulCeoDelete(SchdulCeoVO vo) throws ParseException {
boolean chk = getToGoogleSave(vo, "delete");
if(chk) {
dao.deleteSchdulCeo(vo);
}
return chk;
}
//java -> google action로직
public boolean getToGoogleSave(SchdulCeoVO vo, String state) {
boolean chk = true;
String calendarId = null;
String chkStr = null;
calendarId = gService.getCalendarId(vo);
String eventId = vo.getEventId();
String title = vo.getTitle();
String description = vo.getCn();
String startDt = vo.getStartDate() + "T" + vo.getStartTime() + ":00";
String endDt = vo.getEndDate() + "T" + vo.getEndTime() + ":01";
if(state.equals("insert")) {
chkStr = gService.createEvent(calendarId, title, description, startDt, endDt);
vo.setEventId(chkStr);
}else if(state.equals("update")){
chk = gService.updateEvent(calendarId, eventId, title, description, startDt, endDt);
}else if(state.equals("delete")) {
chk = gService.deleteEvent(calendarId, eventId);
}
if(chkStr == null && state.equals("insert")) {
chk = false;
}
return chk;
}
//구글캘린더에 일정 추가
@SuppressWarnings("finally")
public String createEvent(String calendarId, String title, String description, String startDate, String endDate) {
String eventId = null;
try {
Calendar service = getCalendarService();
Event event = new Event()
.setSummary(title)
.setDescription(description)
.setStart(new EventDateTime()
.setDateTime(new DateTime(startDate + "+09:00")) // 서울 시간대 적용
.setTimeZone("Asia/Seoul"))
.setEnd(new EventDateTime()
.setDateTime(new DateTime(endDate + "+09:00")) // 서울 시간대 적용
.setTimeZone("Asia/Seoul"));
event = service.events().insert(calendarId, event).execute();
eventId = event.getId();
} catch (Exception e) {
e.printStackTrace();
} finally {
return eventId;
}
}
//구글캘린더에 일정 수정
public boolean updateEvent(String calendarId, String eventId ,String newTitle, String newDescription, String newStartDate, String newEndDate) {
try {
Calendar service = getCalendarService();
Event event = new Event()
.setSummary(newTitle)
.setDescription(newDescription)
.setStart(new EventDateTime()
.setDateTime(new DateTime(newStartDate + "+09:00")) // 서울 시간대 적용
.setTimeZone("Asia/Seoul"))
.setEnd(new EventDateTime()
.setDateTime(new DateTime(newEndDate + "+09:00")) // 서울 시간대 적용
.setTimeZone("Asia/Seoul"));
event = service.events().update(calendarId, eventId, event).execute();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//구글캘린더에 일정 삭제
public boolean deleteEvent(String calendarId, String eventId) {
try {
Calendar service = getCalendarService();
service.events().delete(calendarId, eventId).execute();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
여기서 핵심은 eventId
이다. 구글 캘린더에서 일정 등록이 되면 eventId
가 생성이 되는데 고유값이며 getId()
로 받아온다. 이 eventId
로 수정 및 삭제를 해주면 된다. 나는 구글캘린더 저장 action을 실행 후 chk
값이 true
인 상태에서 일정 등록을 해주었다.
해당 로직까지 작성 완료 후 이제 등록
/ 수정
/ 삭제
를 해보자.
구글캘린더에 자동으로 일정이 등록되면 성공이다. 수정 / 삭제도 해보자.
끝