Exported FileExporter for Calendar json output file.

master
Markus Kreth 7 years ago
parent 107d18bcc2
commit 971b7bb141
  1. 24
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java
  2. 119
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/FileExporter.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<ClubEvent> 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<com.google.api.services.calendar.model.Event> 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);

@ -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();
}
}
}
Loading…
Cancel
Save