Commit 55223b52 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Merge branch 'calendar-controller' into 'master'

Calendar controller

See merge request s7_spruge_k/calendar!14
parents 0e291ec3 01837289
......@@ -5,13 +5,13 @@ import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.Event;
......@@ -38,8 +38,67 @@ public class CalendarController {
models.addAttribute("currentTime", LocalTime.now());
models.addAttribute("currentDate", LocalDate.now());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
//1. Get currently logged in user
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "";
if (principal instanceof UserDetails) {
username = ((UserDetails)principal).getUsername();
} else {
username = principal.toString();
}
User currUser = userRepo.findByUsername(username);
//2. Get Calendar By User
Calendar c1 = calendarRepo.findByUser(currUser);
//3. Get Events By Calendar
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
int totalDaysInCurrentMonth = LocalDate.now().lengthOfMonth();
int currentDayByIndex = LocalDate.now().getDayOfMonth();
int numberOfPreviousDaysNeedToGet = currentDayByIndex - 1;
int numberOfDaysAheadNeedToGet = totalDaysInCurrentMonth - currentDayByIndex;
//Sakumu un beigu datums pashreizeejam menesim.
LocalDate monthStartingDate = LocalDate.now().minusDays(numberOfPreviousDaysNeedToGet);
LocalDate monthEndingDate = LocalDate.now().plusDays(numberOfDaysAheadNeedToGet);
ArrayList<Event> monthEvents = new ArrayList<>();
//Get events in current week.
if(allEvents.isEmpty())
System.out.println("No events");
else
{
for (Event e : allEvents) {
LocalDate temp = e.getStartDate();
if(temp == null)
continue;
if(temp.isBefore(monthEndingDate.plusDays(1)) && temp.isAfter(monthStartingDate.minusDays(1)))
monthEvents.add(e);
}
}
Collections.sort(monthEvents, Event.eventDateComp);
//TODO If the dates are the same, sort By Time!!!!!!!!!!!!!!!!!
models.addAttribute("monthStartingDate", monthStartingDate);
models.addAttribute("monthEndingDate", monthEndingDate);
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("monthEvents", monthEvents);
//TODO how to pass User?
return "monthView";
}
......@@ -49,8 +108,63 @@ public class CalendarController {
models.addAttribute("currentTime", LocalTime.now());
models.addAttribute("currentDate", LocalDate.now());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
//1. Get currently logged in user
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "";
if (principal instanceof UserDetails) {
username = ((UserDetails)principal).getUsername();
} else {
username = principal.toString();
}
User currUser = userRepo.findByUsername(username);
//2. Get Calendar By User
Calendar c1 = calendarRepo.findByUser(currUser);
//3. Get Events By Calendar
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
int currentDayByIndex = LocalDate.now().getDayOfWeek().getValue();
int numberOfPreviousDaysNeedToGet = currentDayByIndex - 1;
int numberOfDaysAheadNeedToGet = 7 - currentDayByIndex;
//Sakumu un beigu datums pashreizeejai nedeelai.
LocalDate weekStartingDate = LocalDate.now().minusDays(numberOfPreviousDaysNeedToGet);
LocalDate weekEndingDate = LocalDate.now().plusDays(numberOfDaysAheadNeedToGet);
ArrayList<Event> weekEvents = new ArrayList<>();
//Get events in current week.
if(allEvents.isEmpty())
System.out.println("No events");
else
{
for (Event e : allEvents) {
LocalDate temp = e.getStartDate();
if(temp == null)
continue;
if(temp.isBefore(weekEndingDate.plusDays(1)) && temp.isAfter(weekStartingDate.minusDays(1)))
weekEvents.add(e);
}
}
Collections.sort(weekEvents, Event.eventDateComp);
//TODO If the dates are the same, sort By Time!!!!!!!!!!!!!!!!!
models.addAttribute("weekStartingDate", weekStartingDate);
models.addAttribute("weekEndingDate", weekEndingDate);
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("weekEvents", weekEvents);
//TODO how to pass User?
return "weekView";
}
......@@ -81,18 +195,15 @@ public class CalendarController {
//3. Get Events By Calendar
ArrayList<Event> todayEvents = eventRepo.findAllByCalendar(c1);
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
ArrayList<Event> todayEventsConfirmed = new ArrayList<>();
if(todayEvents.isEmpty())
System.out.println("No events current date");
if(allEvents.isEmpty())
System.out.println("No events");
else
{
//4. Get Events By currentDay.
for (Event e : todayEvents) {
if(e.getStartDate() == null || e.getStartTime() == null)
{
for (Event e : allEvents) {
if(e.getStartDate() == null)
continue;
}
if(e.getStartDate().equals(LocalDate.now()))
todayEventsConfirmed.add(e);
......@@ -102,7 +213,7 @@ public class CalendarController {
System.out.println(todayEventsConfirmed);
//Vajag sakartot hronologiski pec laikiem, lai vieglak izmantotu skataa.
//event klasee ir compareTo, kas salidzina pec laikiem
Collections.sort(todayEvents);
Collections.sort(todayEventsConfirmed, Event.eventTimeComp);
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("todayEvents", todayEventsConfirmed);
......
......@@ -2,6 +2,7 @@ package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Comparator;
import javax.persistence.Column;
import javax.persistence.Entity;
......@@ -16,7 +17,7 @@ import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name = "EventTable")
public class Event implements Comparable<Event>{
public class Event{
@Column(name = "Name")
private String name;
......@@ -156,13 +157,33 @@ public class Event implements Comparable<Event>{
super();
}
@Override
public int compareTo(Event o) {
if (getStartTime() == null || o.getStartTime() == null) {
return 0;
}
return getStartTime().compareTo(o.getStartTime());
}
/*Comparator for sorting the list by Student Name*/
public static Comparator<Event> eventDateComp = new Comparator<Event>() {
public int compare(Event e1, Event e2) {
//ascending order
return e1.getStartDate().compareTo(e2.getStartDate());
}};
/*Comparator for sorting the list by Student Name*/
public static Comparator<Event> eventTimeComp = new Comparator<Event>() {
public int compare(Event e1, Event e2) {
//ascending order
return e1.getStartTime().compareTo(e2.getStartTime());
}};
// @Override
// public int compareTo(Event o) {
//
// if (getStartTime() == null || o.getStartTime() == null) {
// return 0;
// }
// return getStartTime().compareTo(o.getStartTime());
// }
//
}
......@@ -14,6 +14,7 @@
<tr th:each="temp: ${todayEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></td>
<td th:text = "'Time: ' + ${temp.getStartTime()}"></td>
</tr>
</table>
</body>
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This Month</title>
</head>
<h2 th:text = "'Hello, ' + ${currentUser}"></h2>
<body>
<table>
<tr>
<td th:text = "'Date: ' + ${currentDate}"></td>
<td th:text = "'Month start: ' + ${monthStartingDate}"></td>
<td th:text = "'Month end: ' + ${monthEndingDate}"></td>
</tr>
<tr th:each="temp: ${monthEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></td>
<td th:text = "'Time: ' + ${temp.getStartTime()}"></td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This Week</title>
</head>
<h2 th:text = "'Hello, ' + ${currentUser}"></h2>
<body>
<table>
<tr>
<td th:text = "'Date: ' + ${currentDate}"></td>
<td th:text = "'Week start: ' + ${weekStartingDate}"></td>
<td th:text = "'Week end: ' + ${weekEndingDate}"></td>
</tr>
<tr th:each="temp: ${weekEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></td>
<td th:text = "'Time: ' + ${temp.getStartTime()}"></td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment