Auto update Calendar component asynchronously

master
Markus Kreth 7 years ago
parent 9636234dce
commit fc9661714e
  1. 7
      pom.xml
  2. 27
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/EventBusiness.java
  3. 4
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java
  4. 16
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java
  5. 1
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java

@ -50,6 +50,10 @@
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-push</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
@ -98,7 +102,7 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@ -112,7 +116,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
</dependencies>

@ -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<ClubEvent> cache = new ArrayList<>();
@Autowired
ClubEventDao dao;
public List<ClubEvent> loadEvents(VaadinRequest request) {
return loadEvents(request, false);
@ -28,17 +33,21 @@ public class EventBusiness {
public synchronized List<ClubEvent> 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<ClubEvent> 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<ClubEvent> 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);
}
}

@ -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() + "]";
}

@ -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<ClubEvent> events = business.loadEvents(request);
final List<ClubEvent> 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();
}

@ -83,6 +83,7 @@ public class CalendarComponent extends CustomComponent {
public void setItems(Collection<ClubEvent> items) {
dataProvider.setItems(items);
calendar.markAsDirty();
}
class ClubEventProvider extends BasicItemProvider<ClubEvent> {

Loading…
Cancel
Save