implemented storing of appointments
+ attachments next: attach links, tags Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,7 +4,7 @@ dependencies {
|
||||
implementation(project(":de.srsoftware.cal.api"))
|
||||
|
||||
implementation("de.srsoftware:tools.optionals:1.0.0")
|
||||
implementation("de.srsoftware:tools.util:1.2.2")
|
||||
implementation("de.srsoftware:tools.util:1.2.3")
|
||||
implementation("de.srsoftware:tools.web:1.3.8")
|
||||
implementation("org.json:json:20240303")
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ import org.json.JSONObject;
|
||||
*/
|
||||
public class BaseAppointment implements Appointment {
|
||||
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
private final long id;
|
||||
private long id;
|
||||
private final String title, description;
|
||||
private final LocalDateTime end, start;
|
||||
private final String hash;
|
||||
private final String slug;
|
||||
private Coords coords = null;
|
||||
private final Set<Attachment> attachments = new HashSet<>();
|
||||
private final Set<String> tags = new HashSet<>();
|
||||
@@ -39,7 +39,7 @@ public class BaseAppointment implements Appointment {
|
||||
public BaseAppointment(long id, String title, String description, LocalDateTime start, LocalDateTime end, String location, String slug) {
|
||||
this.description = description;
|
||||
this.end = end;
|
||||
this.hash = slug;
|
||||
this.slug = slug;
|
||||
this.id = id;
|
||||
this.location = location;
|
||||
this.start = start;
|
||||
@@ -98,7 +98,12 @@ public class BaseAppointment implements Appointment {
|
||||
|
||||
@Override
|
||||
public Set<Attachment> attachments() {
|
||||
return Set.of();
|
||||
return attachments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Appointment clone(long newId) {
|
||||
return new BaseAppointment(newId, title, description, start, end, location, slug).coords(coords).addLinks(links).add(attachments);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -116,7 +121,6 @@ public class BaseAppointment implements Appointment {
|
||||
return nullable(end);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long id() {
|
||||
return id;
|
||||
@@ -148,7 +152,7 @@ public class BaseAppointment implements Appointment {
|
||||
|
||||
@Override
|
||||
public String slug() {
|
||||
return hash;
|
||||
return slug;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cal;
|
||||
|
||||
import static de.srsoftware.tools.Result.transform;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import de.srsoftware.cal.api.*;
|
||||
@@ -44,8 +45,8 @@ public abstract class BaseImporter implements Importer {
|
||||
.map(tag -> tag.get("src"))
|
||||
.filter(Objects::nonNull)
|
||||
.map(Payload::of)
|
||||
.map(this::url)
|
||||
.map(this::toAttachment)
|
||||
.map(BaseImporter::url)
|
||||
.map(BaseImporter::toAttachment)
|
||||
.map(Result::optional)
|
||||
.flatMap(Optional::stream)
|
||||
.toList();
|
||||
@@ -140,9 +141,9 @@ public abstract class BaseImporter implements Importer {
|
||||
var text = anchor.inner(0).orElse(href);
|
||||
Payload //
|
||||
.of(href)
|
||||
.map(this::url)
|
||||
.map(BaseImporter::url)
|
||||
.map(url -> link(url,text))
|
||||
.optional()
|
||||
.map(url -> new Link(url, text))
|
||||
.ifPresent(links::add);
|
||||
});
|
||||
return links;
|
||||
@@ -195,7 +196,7 @@ public abstract class BaseImporter implements Importer {
|
||||
.map(this::extractEventUrls)
|
||||
.stream();
|
||||
return stream //
|
||||
.map(this::url)
|
||||
.map(BaseImporter::url)
|
||||
.map(this::loadEvent)
|
||||
.peek(e -> {
|
||||
if (e instanceof Error<Appointment> err) System.err.println(err);
|
||||
@@ -216,6 +217,12 @@ public abstract class BaseImporter implements Importer {
|
||||
return Error.format("Invalid parameter: %s", result.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
protected static Result<Link> link(Result<URL> url, String text) {
|
||||
var opt = url.optional();
|
||||
if (opt.isEmpty()) return transform(url);
|
||||
return Payload.of(new Link(opt.get(),text));
|
||||
}
|
||||
|
||||
protected Result<Appointment> loadEvent(Result<URL> urlResult) {
|
||||
var link = urlResult //
|
||||
.optional().map(url -> new Link(url, "Event-Seite")).orElse(null);
|
||||
@@ -271,22 +278,19 @@ public abstract class BaseImporter implements Importer {
|
||||
|
||||
protected abstract String programURL();
|
||||
|
||||
protected Result<Attachment> toAttachment(Result<URL> urlResult) {
|
||||
switch (urlResult) {
|
||||
case Payload<URL> payload:
|
||||
try {
|
||||
var mime = payload.get().openConnection().getContentType();
|
||||
return Payload.of(new Attachment(payload.get(), mime));
|
||||
} catch (Exception e) {
|
||||
return Error.format("Failed to read mime type of %s", payload);
|
||||
}
|
||||
case Error<URL> err:
|
||||
return err.transform();
|
||||
default:
|
||||
return invalidParameter(urlResult);
|
||||
protected static Result<Attachment> toAttachment(Result<URL> urlResult) {
|
||||
var opt = urlResult.optional();
|
||||
if (opt.isEmpty()) return transform(urlResult);
|
||||
try {
|
||||
var mime = opt.get().openConnection().getContentType();
|
||||
return Payload.of(new Attachment(opt.get(), mime));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Error.format("Failed to read mime type of %s", opt.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static Result<Integer> toNumericMonth(String month) {
|
||||
month = month.toLowerCase();
|
||||
if (month.startsWith("ja")) return Payload.of(1);
|
||||
@@ -305,24 +309,13 @@ public abstract class BaseImporter implements Importer {
|
||||
}
|
||||
|
||||
|
||||
protected <T> Result<T> transform(Result<?> result) {
|
||||
if (result instanceof Error<?> err) return err.transform();
|
||||
return invalidParameter(result);
|
||||
}
|
||||
|
||||
protected Result<URL> url(Result<String> urlResult) {
|
||||
switch (urlResult) {
|
||||
case Payload<String> payload:
|
||||
var url = payload.get();
|
||||
try {
|
||||
return Payload.of(new URI(url).toURL());
|
||||
} catch (MalformedURLException | URISyntaxException e) {
|
||||
return de.srsoftware.tools.Error.of("Failed to create URL of %s".formatted(url), e);
|
||||
}
|
||||
case de.srsoftware.tools.Error<String> err:
|
||||
return err.transform();
|
||||
default:
|
||||
return invalidParameter(urlResult);
|
||||
protected static Result<URL> url(Result<String> urlResult) {
|
||||
if (urlResult.optional().isEmpty()) return transform(urlResult);
|
||||
var url = urlResult.optional().get();
|
||||
try {
|
||||
return Payload.of(new URI(url).toURL());
|
||||
} catch (MalformedURLException | URISyntaxException e) {
|
||||
return Error.of("Failed to create URL of %s".formatted(url), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user