From 971b7bb1419f643660b0a1ca0fc0b67425cb4e9f Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Thu, 1 Nov 2018 14:23:56 +0100 Subject: [PATCH] Exported FileExporter for Calendar json output file. --- .../business/EventBusiness.java | 24 +--- .../business/FileExporter.java | 119 ++++++++++++++++++ 2 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/FileExporter.java diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java index 86e4141..ea27528 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java @@ -1,10 +1,6 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Collections; @@ -52,26 +48,15 @@ public class EventBusiness { List list = new ArrayList<>(); - File f = new File("google_events.json"); - if (f.exists()) { - try { - Files.delete(f.toPath()); - } catch (IOException e) { - log.error("Error deleting file " + f.getAbsolutePath(), e); - } - } - try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) { - + try (FileExporter ex = FileExporter.builder(log).setFileName("google_events.json").setAppend(false).disable().build()) { + String remoteHost = "localhost"; CalendarAdapter adapter = new CalendarAdapter(); List events = adapter .getAllEvents(remoteHost); for (com.google.api.services.calendar.model.Event ev : events) { - if (out != null) { - out.write(ev.toPrettyString()); - out.newLine(); - } + ex.writeLine(ev.toPrettyString()); if ("cancelled".equals(ev.getStatus())) { log.debug("Cancelled: {}", ev.getSummary()); @@ -79,9 +64,12 @@ public class EventBusiness { list.add(ClubEvent.parse(ev)); } } + } catch (GeneralSecurityException | IOException | InterruptedException e) { log.error("Error loading events from google.", e); + } catch (Exception e1) { + log.warn("Error closing " + FileExporter.class.getSimpleName(), e1); } return Collections.unmodifiableList(list); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/FileExporter.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/FileExporter.java new file mode 100644 index 0000000..a8e7fad --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/FileExporter.java @@ -0,0 +1,119 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Files; + +import org.slf4j.Logger; + +/** + * Writes to a file catching all IOExceptions. + * @author markus + * + */ +public class FileExporter implements AutoCloseable { + + private final Logger log; + private final BufferedWriter out; + + public void writeLine(String prettyString) { + if (out != null) { + try { + out.write(prettyString); + out.newLine(); + } catch (IOException e) { + log.error("Error on write operation.", e); + } + } + } + + public static Builder builder(Logger log) { + Builder b = new Builder(); + b.setLog(log); + return b; + } + + + public static class Builder { + + private Logger log; + private String fileName; + private boolean append = true; + private boolean enabled = true; + + private Builder() { + } + + public Builder setLog(Logger log) { + this.log = log; + return this; + } + + public Builder setFileName(String fileName) { + this.fileName = fileName; + return this; + } + + public Builder setAppend(boolean append) { + this.append = append; + return this; + } + + public Builder setEnabled(boolean enabled) { + this.enabled = enabled; + return this; + } + + public Builder disable() { + this.enabled = false; + return this; + } + + public FileExporter build() { + if (!enabled) { + return new FileExporter(log, null); + } + final File f = new File(fileName); + if (append == false) { + + if (f.exists()) { + try { + Files.delete(f.toPath()); + } catch (IOException e) { + log.error("Error deleting file " + f.getAbsolutePath(), e); + } + } + } + try { + return new FileExporter(this); + } catch (IOException e) { + log.error("Error creating Writer " + f.getAbsolutePath(), e); + log.warn("No Output written."); + return new FileExporter(log, null); + } + } + } + + private FileExporter(Builder builder) throws IOException { + this(builder.log, new FileWriter(builder.fileName)); + } + + FileExporter(Logger log, Writer writer) { + this.log = log; + if (writer == null) { + this.out = null; + } else { + this.out = new BufferedWriter(writer); + } + } + @Override + public void close() throws Exception { + if (out != null) { + out.close(); + } + } + +}