Skip to content

Commit

Permalink
Initial global captions.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Sep 22, 2024
1 parent 57a1b78 commit a8b85f6
Show file tree
Hide file tree
Showing 20 changed files with 806 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package com.io7m.laurel.filemodel;

import com.io7m.jattribute.core.AttributeReadableType;
import com.io7m.laurel.model.LCaptionName;
import com.io7m.laurel.model.LCaption;
import com.io7m.laurel.model.LCaptionID;
import com.io7m.laurel.model.LCaptionName;
import com.io7m.laurel.model.LCategory;
import com.io7m.laurel.model.LCategoryID;
import com.io7m.laurel.model.LCategoryName;
Expand Down Expand Up @@ -51,6 +52,28 @@ public interface LFileModelType

Flow.Publisher<LFileModelEvent> events();

/**
* Add a global caption.
*
* @param text The caption
*
* @return The operation in progress
*/

CompletableFuture<?> globalCaptionAdd(
LCaptionName text);

/**
* Remove a global caption.
*
* @param id The caption
*
* @return The operation in progress
*/

CompletableFuture<?> globalCaptionRemove(
LCaptionID id);

/**
* Set categories as required.
*
Expand Down Expand Up @@ -331,4 +354,10 @@ void close()
*/

CompletableFuture<Optional<InputStream>> imageStream(LImageID id);

/**
* @return The current complete list of global captions
*/

AttributeReadableType<List<LCaption>> globalCaptionList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.laurel.filemodel.internal;

import com.io7m.laurel.model.LCaptionName;
import org.jooq.DSLContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static com.io7m.laurel.filemodel.internal.Tables.GLOBAL_CAPTIONS;

/**
* Add global captions.
*/

public final class LCommandGlobalCaptionsAdd
extends LCommandAbstract<List<LCaptionName>>
{
private final ArrayList<SavedData> savedData;

private record SavedData(
long id,
String text)
{

}

/**
* Add global captions.
*/

public LCommandGlobalCaptionsAdd()
{
this.savedData = new ArrayList<>();
}

/**
* Add global captions.
*
* @return A command factory
*/

public static LCommandFactoryType<List<LCaptionName>> provider()
{
return new LCommandFactory<>(
LCommandGlobalCaptionsAdd.class.getCanonicalName(),
LCommandGlobalCaptionsAdd::fromProperties
);
}

private static LCommandGlobalCaptionsAdd fromProperties(
final Properties p)
{
final var c = new LCommandGlobalCaptionsAdd();

for (int index = 0; index < Integer.MAX_VALUE; ++index) {
final var idKey =
"caption.%d.id".formatted(Integer.valueOf(index));
final var textKey =
"caption.%d.text".formatted(Integer.valueOf(index));

if (!p.containsKey(idKey)) {
break;
}

final var data =
new SavedData(
Long.parseUnsignedLong(p.getProperty(idKey)),
p.getProperty(textKey)
);

c.savedData.add(data);
}

c.setExecuted(true);
return c;
}

@Override
protected LCommandUndoable onExecute(
final LFileModel model,
final LDatabaseTransactionType transaction,
final List<LCaptionName> captions)
{
final var context =
transaction.get(DSLContext.class);

final var max = captions.size();
for (int index = 0; index < max; ++index) {
final var caption = captions.get(index);
model.eventWithProgressCurrentMax(index, max, "Adding caption '%s'", caption);

final var recOpt =
context.insertInto(GLOBAL_CAPTIONS)
.set(GLOBAL_CAPTIONS.GLOBAL_CAPTION_TEXT, caption.text())
.onDuplicateKeyIgnore()
.returning(GLOBAL_CAPTIONS.GLOBAL_CAPTION_ID)
.fetchOptional();

if (recOpt.isEmpty()) {
model.eventWithProgressCurrentMax(
index,
max,
"Caption '%s' already existed.",
caption
);
continue;
}

final var rec = recOpt.get();
this.savedData.add(
new SavedData(
rec.get(GLOBAL_CAPTIONS.GLOBAL_CAPTION_ID).longValue(),
caption.text()
)
);
}

model.eventWithoutProgress("Added %d captions.", this.savedData.size());
model.setGlobalCaptions(LCommandModelUpdates.listGlobalCaptions(context));

if (!this.savedData.isEmpty()) {
return LCommandUndoable.COMMAND_UNDOABLE;
}

return LCommandUndoable.COMMAND_NOT_UNDOABLE;
}

@Override
protected void onUndo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
final var context =
transaction.get(DSLContext.class);

final var max = this.savedData.size();
for (int index = 0; index < max; ++index) {
final var data = this.savedData.get(index);
model.eventWithProgressCurrentMax(
index,
max,
"Removing caption '%s'",
data.text
);
context.deleteFrom(GLOBAL_CAPTIONS)
.where(GLOBAL_CAPTIONS.GLOBAL_CAPTION_ID.eq(data.id))
.execute();
}

model.eventWithoutProgress("Removed %d captions.", Integer.valueOf(max));
model.setGlobalCaptions(LCommandModelUpdates.listGlobalCaptions(context));
}

@Override
protected void onRedo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
final var context =
transaction.get(DSLContext.class);

final var max = this.savedData.size();
for (int index = 0; index < max; ++index) {
final var data = this.savedData.get(index);
model.eventWithProgressCurrentMax(
index,
max,
"Adding caption '%s'",
data.text
);
context.insertInto(GLOBAL_CAPTIONS)
.set(GLOBAL_CAPTIONS.GLOBAL_CAPTION_ID, data.id)
.set(GLOBAL_CAPTIONS.GLOBAL_CAPTION_TEXT, data.text)
.onDuplicateKeyUpdate()
.set(GLOBAL_CAPTIONS.GLOBAL_CAPTION_TEXT, data.text)
.execute();
}

model.eventWithoutProgress("Added %d captions.", Integer.valueOf(max));
model.setGlobalCaptions(LCommandModelUpdates.listGlobalCaptions(context));
}

@Override
public Properties toProperties()
{
final var p = new Properties();

for (int index = 0; index < this.savedData.size(); ++index) {
final var idKey =
"caption.%d.id".formatted(Integer.valueOf(index));
final var textKey =
"caption.%d.text".formatted(Integer.valueOf(index));

final var data = this.savedData.get(index);
p.setProperty(idKey, Long.toUnsignedString(data.id));
p.setProperty(textKey, data.text);
}

return p;
}

@Override
public String describe()
{
return "Add global caption(s)";
}
}
Loading

0 comments on commit a8b85f6

Please sign in to comment.