diff --git a/pom.xml b/pom.xml index 3b7825b..036e5df 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,10 @@ com.vaadin vaadin-spring-boot-starter + + com.vaadin + vaadin-push + org.hibernate hibernate-core @@ -98,7 +102,7 @@ ch.qos.logback logback-classic - + org.springframework.boot spring-boot-starter-test @@ -112,7 +116,6 @@ com.h2database h2 - 1.4.197 test diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/EventBusiness.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/EventBusiness.java index 955e87d..1bb0824 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/EventBusiness.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/EventBusiness.java @@ -11,16 +11,21 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import com.vaadin.server.VaadinRequest; import de.kreth.clubhelperbackend.google.calendar.CalendarAdapter; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +@Component public class EventBusiness { private final Logger log = LoggerFactory.getLogger(getClass()); - private final List cache = new ArrayList<>(); + + @Autowired + ClubEventDao dao; public List loadEvents(VaadinRequest request) { return loadEvents(request, false); @@ -28,17 +33,21 @@ public class EventBusiness { public synchronized List loadEvents(VaadinRequest request, boolean forceRefresh) { - if (cache.isEmpty() == false && forceRefresh == false) { - log.trace("Returning cached events: {}", cache); - return Collections.unmodifiableList(cache); + + if (forceRefresh == false) { + List list = dao.list(); + log.trace("Returning events from database: {}"); + return list; } log.debug( - "Loading events from Google Calendar, cache size was: {}, forceRefresh={}", - cache.size(), forceRefresh); - cache.clear(); + "Loading events from Google Calendar, forceRefresh={}", + forceRefresh); BufferedWriter out = null; + + List list = new ArrayList<>(); + try { if (forceRefresh) { File f = new File("google_events.json"); @@ -58,7 +67,7 @@ public class EventBusiness { } if ("cancelled".equals(ev.getStatus()) == false) { - cache.add(ClubEvent.parse(ev)); + list.add(ClubEvent.parse(ev)); } else { log.debug("Cancelled: {}", ev.getSummary()); } @@ -75,6 +84,6 @@ public class EventBusiness { log.error("Error writing File", e); } } - return Collections.unmodifiableList(cache); + return Collections.unmodifiableList(list); } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java index 4bac52f..662b4cf 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java @@ -91,9 +91,9 @@ public class ClubEvent extends BasicItem { @Override public String toString() { - return "ClubEvent [id=" + id + ", iCalUID=" + iCalUID + ", location=" + return "ClubEvent [id=" + id + ", getCaption()=" + getCaption() + ", iCalUID=" + iCalUID + ", location=" + location + ", organizerDisplayName=" + organizerDisplayName - + ", getCaption()=" + getCaption() + ", getDescription()=" + + ", getDescription()=" + getDescription() + ", getEnd()=" + getEnd() + ", getStart()=" + getStart() + ", isAllDay()=" + isAllDay() + "]"; } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java index 55f1c11..d6d93e6 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java @@ -9,8 +9,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.vaadin.addon.calendar.ui.CalendarComponentEvents; +import com.vaadin.annotations.Push; import com.vaadin.annotations.Theme; import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.communication.PushMode; import com.vaadin.spring.annotation.SpringUI; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.UI; @@ -25,6 +27,7 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid; @Theme("vaadin-clubhelpertheme") @SpringUI +@Push(value=PushMode.MANUAL) public class MainUi extends UI { private static final long serialVersionUID = 7581634188909841919L; @@ -35,6 +38,9 @@ public class MainUi extends UI { @Autowired GroupDao groupDao; + + @Autowired + EventBusiness eventBusiness; private PersonGrid personGrid; @@ -67,10 +73,14 @@ public class MainUi extends UI { ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(() -> { - EventBusiness business = new EventBusiness(); - List events = business.loadEvents(request); + final List events = eventBusiness.loadEvents(request); LOGGER.info("Loaded events: {}", events); - calendar.setItems(events); + final UI ui = calendar.getUI(); + ui.access(() -> { + calendar.setItems(events); + ui.push(); + }); + }); exec.shutdown(); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java index 0dcee95..56f3a41 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java @@ -83,6 +83,7 @@ public class CalendarComponent extends CustomComponent { public void setItems(Collection items) { dataProvider.setItems(items); + calendar.markAsDirty(); } class ClubEventProvider extends BasicItemProvider {