implemented GUI update on cloning items

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-02-10 23:48:10 +01:00
parent f4e85c870c
commit aaf33ffa8f
7 changed files with 102 additions and 8 deletions

View File

@@ -49,11 +49,11 @@ public class MessageApi extends BaseHandler{
if (++counter > 300) counter = sendBeacon(addr,stream);
} else {
var event = eventQueue.removeFirst();
//if (event.isIntendedFor(user.get())) {
if (event.isIntendedFor(user.get())) {
LOG.log(DEBUG, "sending event to {0}", addr);
sendEvent(stream, event);
counter = 0;
//}
}
}
}
LOG.log(INFO,"{0} disconnected from event stream.",addr);

View File

@@ -12,7 +12,7 @@ public class MessageBus {
private MessageBus(){}
public void dispatch(Event event){
public void dispatch(Event<?> event){
new Thread(() -> { // TODO: use thread pool
try {
Thread.sleep(100);

View File

@@ -0,0 +1,49 @@
package de.srsoftware.umbrella.messagebus.events;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.model.*;
import java.util.Collection;
import java.util.List;
import static de.srsoftware.umbrella.core.constants.Field.*;
import static de.srsoftware.umbrella.core.model.Translatable.t;
public class ItemEvent extends Event<Item>{
public ItemEvent(UmbrellaUser initiator, String module, Item item, EventType type) {
super(initiator, module, item, type);
}
@Override
public Collection<UmbrellaUser> audience() {
Owner owner = payload().location().resolve().owner().resolve();
if (owner instanceof UmbrellaUser user) return List.of(user);
if (owner instanceof Company company) return ModuleRegistry.companyService().getMembers(company.id());
return List.of();
}
@Override
public Translatable describe() {
return switch (eventType()){
case CREATE -> describeCreate();
case null, default -> null;
};
}
private Translatable describeCreate() {
var loc = payload().location().resolve().name();
return t("{user} added \"{item}\" to \"{location}\"", USER,initiator().name(), ITEM, payload().name(), LOCATION, loc);
}
@Override
public Translatable subject() {
var loc = payload().location().resolve().name();
return switch (eventType()){
case CREATE -> t("A new item has been added to \"{location}\":",LOCATION,loc);
case null, default -> null;
};
}
}

View File

@@ -1,11 +1,39 @@
<script>
import { api, post } from '../../urls.svelte';
import { onDestroy, onMount } from 'svelte';
import { api, eventStream, post } from '../../urls.svelte';
import { error, yikes } from '../../warn.svelte';
import { t } from '../../translations.svelte';
let { items, selected = $bindable(null), location = null, drag_start = item => console.log({dragging:item}) } = $props();
let eventSource = null;
let newItem = $state({name:null, code: null});
function handleCreateEvent(evt){
let json = JSON.parse(evt.data);
if (json.item.location.id == location.id){
items = [...items,json.item];
selected = json.item;
}
}
function handleEvent(evt,method){
console.log(evt,method);
}
function handleDeleteEvent(evt){
handleEvent(evt,'delete');
}
function handleUpdateEvent(evt){
handleEvent(evt,'update');
}
function load(){
try {
eventSource = eventStream(handleCreateEvent,handleUpdateEvent,handleDeleteEvent);
} catch (ignored) {}
}
async function saveNewItem(){
newItem.location = location;
const url = api('stock/item');
@@ -17,6 +45,12 @@
items = [...items, it];
} else error(res);
}
onDestroy(() => {
if (eventSource) eventSource.close();
});
onMount(load);
</script>
<table>
<thead>

View File

@@ -69,7 +69,7 @@
{#if item}
<LineEditor type="h3" editable={true} value={item.name} onSet={v => update('name',v)} />
<button class="clone symbol" title={t('clone')} onclick={doClone}>C</button>
<button class="clone symbol" title={t('clone')} onclick={doClone}></button>
<div>
{@html item.description.rendered}
</div>

View File

@@ -1,6 +1,7 @@
description = "Umbrella : Stock"
dependencies{
implementation(project(":bus"))
implementation(project(":core"))
implementation("de.srsoftware:configuration.json:1.0.3")
}

View File

@@ -14,6 +14,7 @@ import static de.srsoftware.umbrella.core.constants.Module.USER;
import static de.srsoftware.umbrella.core.constants.Path.*;
import static de.srsoftware.umbrella.core.constants.Path.PROPERTY;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
import static de.srsoftware.umbrella.stock.Constants.*;
import static java.lang.System.Logger.Level.WARNING;
import static java.util.Comparator.comparing;
@@ -26,13 +27,18 @@ import de.srsoftware.umbrella.core.*;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.api.StockService;
import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.constants.Module;
import de.srsoftware.umbrella.core.constants.Path;
import de.srsoftware.umbrella.core.constants.Text;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.*;
import de.srsoftware.umbrella.core.model.Location;
import java.io.IOException;
import java.util.*;
import de.srsoftware.umbrella.messagebus.events.Event;
import de.srsoftware.umbrella.messagebus.events.ItemEvent;
import org.json.JSONObject;
public class StockModule extends BaseHandler implements StockService {
@@ -350,7 +356,9 @@ public class StockModule extends BaseHandler implements StockService {
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to \"{location}\"!", Text.LOCATION,location.name());
var newItem = new Item(0,owner,0,location,item.code(),item.name(),item.description());
for (var property : item.properties()) newItem.properties().add(property);
return sendContent(ex,stockDb.save(newItem));
newItem = stockDb.save(newItem);
messageBus().dispatch(new ItemEvent(user, Module.STOCK, newItem, Event.EventType.CREATE));
return sendContent(ex,newItem);
}
private boolean postItem(UmbrellaUser user, HttpExchange ex) throws IOException {
@@ -362,8 +370,10 @@ public class StockModule extends BaseHandler implements StockService {
var location = stockDb.loadLocation(locationData.getLong(ID));
var owner = location.owner().resolve();
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to {location}!", Field.LOCATION,location);
var newItem = new Item(0,owner,0,location,code,name,description);
return sendContent(ex,stockDb.save(newItem));
var newItem = stockDb.save(new Item(0,owner,0,location,code,name,description));
messageBus().dispatch(new ItemEvent(user, Module.STOCK, newItem, Event.EventType.CREATE));
return sendContent(ex,newItem);
}
private boolean postItemList(UmbrellaUser user, de.srsoftware.tools.Path path, HttpExchange ex) throws IOException {