|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
/* © SRSoftware 2024 */ |
|
|
|
|
package de.srsoftware.cal.db; |
|
|
|
|
|
|
|
|
|
import static de.srsoftware.cal.Util.extractCoords; |
|
|
|
|
import static de.srsoftware.cal.db.Fields.*; |
|
|
|
|
import static de.srsoftware.cal.db.Fields.ALL; |
|
|
|
|
import static de.srsoftware.tools.Error.error; |
|
|
|
@ -74,38 +75,10 @@ public class MariaDB implements Database {
@@ -74,38 +75,10 @@ public class MariaDB implements Database {
|
|
|
|
|
keys.close(); |
|
|
|
|
if (saved == null) return error("Insert query did not return appointment id!"); |
|
|
|
|
|
|
|
|
|
{ // link to attachments
|
|
|
|
|
var attachments = saved.attachments(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var attachment : attachments) { |
|
|
|
|
var urlId = getOrCreateUrl(attachment.url()); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_ATTACHMENTS, AID, UID, MIME); |
|
|
|
|
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), attachment.mime()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{ // link to links
|
|
|
|
|
var links = saved.links(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var link : links) { |
|
|
|
|
var urlId = getOrCreateUrl(link.url()); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_URLS, AID, UID, DESCRIPTION); |
|
|
|
|
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), link.desciption()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
addAttachments(saved); |
|
|
|
|
addLinks(saved); |
|
|
|
|
addTags(saved); |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
var tags = saved.tags(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var tag : tags) { |
|
|
|
|
var tagId = getOrCreateTag(tag); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_TAGS, AID, TID); |
|
|
|
|
if (tagId.isPresent()) assignQuery.values(saved.id(), tagId.get()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
return Payload.of(saved); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR, "Failed to store appointment", e); |
|
|
|
@ -113,6 +86,39 @@ public class MariaDB implements Database {
@@ -113,6 +86,39 @@ public class MariaDB implements Database {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addAttachments(Appointment saved) throws SQLException { // link to attachments
|
|
|
|
|
var attachments = saved.attachments(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var attachment : attachments) { |
|
|
|
|
var urlId = getOrCreateUrl(attachment.url()); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_ATTACHMENTS, AID, UID, MIME); |
|
|
|
|
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), attachment.mime()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addLinks(Appointment saved) throws SQLException { // link to links
|
|
|
|
|
var links = saved.links(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var link : links) { |
|
|
|
|
var urlId = getOrCreateUrl(link.url()); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_URLS, AID, UID, DESCRIPTION); |
|
|
|
|
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), link.desciption()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addTags(Appointment saved) throws SQLException { |
|
|
|
|
var tags = saved.tags(); |
|
|
|
|
InsertQuery assignQuery = null; |
|
|
|
|
for (var tag : tags) { |
|
|
|
|
var tagId = getOrCreateTag(tag); |
|
|
|
|
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_TAGS, AID, TID); |
|
|
|
|
if (tagId.isPresent()) assignQuery.values(saved.id(), tagId.get()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Optional<Long> getOrCreateUrl(URL url) throws SQLException { |
|
|
|
|
var rs = select(UID).from(URLS).where(URL, equal(url.toString())).exec(connection); |
|
|
|
|
Long uid = null; |
|
|
|
@ -158,8 +164,7 @@ public class MariaDB implements Database {
@@ -158,8 +164,7 @@ public class MariaDB implements Database {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Result<List<Appointment>> list(LocalDateTime from, LocalDateTime till, Integer count, Integer offset) { |
|
|
|
|
var query = Query //
|
|
|
|
|
.select("appointments.*", "GROUP_CONCAT(keyword) AS tags") |
|
|
|
|
var query = select("appointments.*", "GROUP_CONCAT(keyword) AS tags") |
|
|
|
|
.from(APPOINTMENTS) |
|
|
|
|
.leftJoin(AID, "appointment_tags", AID) |
|
|
|
|
.leftJoin("tid", "tags", "tid") |
|
|
|
@ -279,7 +284,7 @@ public class MariaDB implements Database {
@@ -279,7 +284,7 @@ public class MariaDB implements Database {
|
|
|
|
|
var appointment = new BaseAppointment(id, title, description, start, end, location); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Util.extractCoords(results.getString(COORDS)).optional().ifPresent(appointment::coords); |
|
|
|
|
extractCoords(results.getString(COORDS)).optional().ifPresent(appointment::coords); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(WARNING, "Failed to read coordinates from database!"); |
|
|
|
|
} |
|
|
|
@ -301,10 +306,10 @@ public class MariaDB implements Database {
@@ -301,10 +306,10 @@ public class MariaDB implements Database {
|
|
|
|
|
@Override |
|
|
|
|
public Result<Long> removeAppointment(long id) { |
|
|
|
|
try { |
|
|
|
|
Query.delete().from(APPOINTMENTS).where(AID, equal(id)).execute(connection); |
|
|
|
|
Query.delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection); |
|
|
|
|
Query.delete().from(APPOINTMENT_ATTACHMENTS).where(AID, equal(id)).execute(connection); |
|
|
|
|
Query.delete().from(APPOINTMENT_URLS).where(AID, equal(id)).execute(connection); |
|
|
|
|
delete().from(APPOINTMENTS).where(AID, equal(id)).execute(connection); |
|
|
|
|
delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection); |
|
|
|
|
delete().from(APPOINTMENT_ATTACHMENTS).where(AID, equal(id)).execute(connection); |
|
|
|
|
delete().from(APPOINTMENT_URLS).where(AID, equal(id)).execute(connection); |
|
|
|
|
return Payload.of(id); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
return SqlError.of(e, "Failed to delete event %s", id); |
|
|
|
@ -318,15 +323,20 @@ public class MariaDB implements Database {
@@ -318,15 +323,20 @@ public class MariaDB implements Database {
|
|
|
|
|
var coords = event.coords().map(Object::toString).orElse(null); |
|
|
|
|
var location = event.location().orElse(null); |
|
|
|
|
try { |
|
|
|
|
Query |
|
|
|
|
.update(APPOINTMENTS) //
|
|
|
|
|
long id = event.id(); |
|
|
|
|
Query.update(APPOINTMENTS) |
|
|
|
|
.set(TITLE, DESCRIPTION, START, END, LOCATION, COORDS) |
|
|
|
|
.where(AID, equal(event.id())) |
|
|
|
|
.where(AID, equal(id)) |
|
|
|
|
.prepare(connection) |
|
|
|
|
.apply(event.title(), event.description(), start, end, location, coords); |
|
|
|
|
|
|
|
|
|
// TODO: update links, attachments, tags
|
|
|
|
|
LOG.log(WARNING,"updating of tags, links and attachments not implemented!"); |
|
|
|
|
delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection); |
|
|
|
|
addTags(event); |
|
|
|
|
delete().from(APPOINTMENT_ATTACHMENTS).where(AID, equal(id)).execute(connection); |
|
|
|
|
addAttachments(event); |
|
|
|
|
delete().from(APPOINTMENT_URLS).where(AID, equal(id)).execute(connection); |
|
|
|
|
addLinks(event); |
|
|
|
|
|
|
|
|
|
return Payload.of(event); |
|
|
|
|
} catch (SQLException sqle) { |
|
|
|
|
return error(sqle, "Failed to update database entry"); |
|
|
|
|