From c6823b116c54795ac60de20e58b5ac31cdb14012 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 15 Oct 2025 23:53:53 +0200 Subject: [PATCH] started refactoring Signed-off-by: Stephan Richter --- .../srsoftware/umbrella/core/Constants.java | 1 + .../umbrella/core/api/LocationRef.java | 10 + .../srsoftware/umbrella/core/api/Owner.java | 6 + .../umbrella/core/api/OwnerRef.java | 10 + .../srsoftware/umbrella/core/model/Item.java | 46 +- doc/stock.dia | 1362 +++++++++++++++-- .../srsoftware/umbrella/stock/SqliteDb.java | 38 +- 7 files changed, 1328 insertions(+), 145 deletions(-) create mode 100644 core/src/main/java/de/srsoftware/umbrella/core/api/LocationRef.java create mode 100644 core/src/main/java/de/srsoftware/umbrella/core/api/Owner.java create mode 100644 core/src/main/java/de/srsoftware/umbrella/core/api/OwnerRef.java diff --git a/core/src/main/java/de/srsoftware/umbrella/core/Constants.java b/core/src/main/java/de/srsoftware/umbrella/core/Constants.java index 03c9553..36bc75c 100644 --- a/core/src/main/java/de/srsoftware/umbrella/core/Constants.java +++ b/core/src/main/java/de/srsoftware/umbrella/core/Constants.java @@ -138,6 +138,7 @@ public class Constants { public static final String OFFSET = "offset"; public static final String OPTIONAL = "optional"; public static final String OWNER = "owner"; + public static final String OWNER_ID = "owner_id"; public static final String PARENT_LOCATION_ID = "parent_location_id"; public static final String PARENT_TASK_ID = "parent_task_id"; diff --git a/core/src/main/java/de/srsoftware/umbrella/core/api/LocationRef.java b/core/src/main/java/de/srsoftware/umbrella/core/api/LocationRef.java new file mode 100644 index 0000000..2611fdc --- /dev/null +++ b/core/src/main/java/de/srsoftware/umbrella/core/api/LocationRef.java @@ -0,0 +1,10 @@ +/* © SRSoftware 2025 */ +package de.srsoftware.umbrella.core.api; + +import de.srsoftware.tools.Mappable; +import de.srsoftware.umbrella.core.model.Location; + +public interface LocationRef extends Mappable { + Location resolve(StockService stockService); + long id(); +} diff --git a/core/src/main/java/de/srsoftware/umbrella/core/api/Owner.java b/core/src/main/java/de/srsoftware/umbrella/core/api/Owner.java new file mode 100644 index 0000000..e1929e5 --- /dev/null +++ b/core/src/main/java/de/srsoftware/umbrella/core/api/Owner.java @@ -0,0 +1,6 @@ +/* © SRSoftware 2025 */ +package de.srsoftware.umbrella.core.api; + +public interface Owner { + long signum(); +} diff --git a/core/src/main/java/de/srsoftware/umbrella/core/api/OwnerRef.java b/core/src/main/java/de/srsoftware/umbrella/core/api/OwnerRef.java new file mode 100644 index 0000000..c16fc06 --- /dev/null +++ b/core/src/main/java/de/srsoftware/umbrella/core/api/OwnerRef.java @@ -0,0 +1,10 @@ +/* © SRSoftware 2025 */ +package de.srsoftware.umbrella.core.api; + +import de.srsoftware.tools.Mappable; +import de.srsoftware.umbrella.core.ModuleRegistry; + +public interface OwnerRef extends Mappable { + long id(); + Owner resolve(ModuleRegistry registry); +} diff --git a/core/src/main/java/de/srsoftware/umbrella/core/model/Item.java b/core/src/main/java/de/srsoftware/umbrella/core/model/Item.java index 0aac787..10a090a 100644 --- a/core/src/main/java/de/srsoftware/umbrella/core/model/Item.java +++ b/core/src/main/java/de/srsoftware/umbrella/core/model/Item.java @@ -4,24 +4,26 @@ package de.srsoftware.umbrella.core.model; import static de.srsoftware.umbrella.core.Constants.*; import de.srsoftware.tools.Mappable; -import de.srsoftware.umbrella.core.exceptions.UmbrellaException; +import de.srsoftware.umbrella.core.api.LocationRef; +import de.srsoftware.umbrella.core.api.OwnerRef; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; import org.json.JSONObject; public class Item implements Mappable { - private long id; - private Mappable owner; + private long id, number; // id is the database key, number the owner-relative id + private OwnerRef ownerRef; private String code, name; - private Location location; + private LocationRef locationRef; private Collection properties; private Set dirtyFields = new HashSet<>(); - private Item(Mappable owner, long id, Location location, String code, String name) { - this.owner = owner; + private Item(long id, OwnerRef ownerRef, long number, LocationRef locationRef, String code, String name) { this.id = id; - this.location = location; + this.ownerRef = ownerRef; + this.number = number; + this.locationRef = locationRef; this.code = code; this.name = name; this.properties = new HashSet<>(); @@ -39,23 +41,23 @@ public class Item implements Mappable { return id; } - public Location location(){ - return location; + public LocationRef location(){ + return locationRef; } public String name(){ return name; } - public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException { + public static Item of(ResultSet rs, OwnerRef ownerRef, long number, LocationRef locationRef) throws SQLException { var id = rs.getLong(ID); var code = rs.getString(CODE); var name = rs.getString(NAME); - return new Item(owner, id, location, code, name); + return new Item(id, ownerRef, number, locationRef, code, name); } - public Mappable owner(){ - return owner; + public OwnerRef ownerRef(){ + return ownerRef; } public Collection properties() { @@ -64,12 +66,10 @@ public class Item implements Mappable { @Override public Map toMap() { - var ownerId = ownerId(); - var ownerMap = ownerId < 0 ? Map.of(COMPANY,-ownerId) : Map.of(USER,ownerId); var map = new HashMap(); - map.put(OWNER,ownerMap); + map.put(OWNER,ownerRef.toMap()); map.put(ID,id); - if (location != null) map.put(LOCATION,location.toMap()); + map.put(LOCATION,locationRef.toMap()); map.put(CODE,code); map.put(NAME,name); if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList()); @@ -99,18 +99,6 @@ public class Item implements Mappable { return this; } - public static Long ownerId(Mappable owner){ - return switch (owner) { - case Company comp -> -comp.id(); - case UmbrellaUser user -> user.id(); - case null, default -> throw UmbrellaException.unprocessable("Item has owner of unknown class ({0})", owner.getClass().getSimpleName()); - }; - } - - public Long ownerId() { - return ownerId(owner); - } - public Item clear() { dirtyFields.clear(); return this; diff --git a/doc/stock.dia b/doc/stock.dia index 8e2942c..24b0f6e 100644 --- a/doc/stock.dia +++ b/doc/stock.dia @@ -58,16 +58,7 @@ - - - - - - - - - - + @@ -100,13 +91,13 @@ - + - + - + @@ -135,7 +126,7 @@ - + @@ -148,13 +139,13 @@ - + - + - + @@ -180,7 +171,7 @@ - + @@ -193,13 +184,13 @@ - + - + - + @@ -225,7 +216,7 @@ - + @@ -238,13 +229,13 @@ - + - + - + @@ -270,7 +261,7 @@ - + @@ -283,13 +274,13 @@ - + - + - + @@ -318,7 +309,7 @@ - + @@ -331,13 +322,13 @@ - + - + - + @@ -363,7 +354,7 @@ - + @@ -376,13 +367,13 @@ - + - + - + @@ -408,7 +399,7 @@ - + @@ -421,13 +412,13 @@ - + - + - + @@ -453,7 +444,7 @@ - + @@ -466,13 +457,13 @@ - + - + - + @@ -498,7 +489,7 @@ - + @@ -511,13 +502,13 @@ - + - + - + @@ -546,7 +537,7 @@ - + @@ -559,13 +550,13 @@ - + - + - + @@ -591,7 +582,7 @@ - + @@ -604,13 +595,13 @@ - + - + - + @@ -636,7 +627,7 @@ - + @@ -649,16 +640,16 @@ - + - + - - - - + + + + @@ -684,16 +675,16 @@ - + - + - - - - + + + + @@ -719,13 +710,13 @@ - + - + - + @@ -754,7 +745,7 @@ - + @@ -767,13 +758,13 @@ - + - + - + @@ -799,7 +790,7 @@ - + @@ -812,13 +803,13 @@ - + - + - + @@ -844,7 +835,7 @@ - + @@ -857,13 +848,13 @@ - + - + - + @@ -889,7 +880,7 @@ - + @@ -902,16 +893,16 @@ - + - + - - - - + + + + @@ -937,16 +928,16 @@ - + - + - - - - + + + + @@ -981,13 +972,13 @@ - + - + - + @@ -1013,7 +1004,7 @@ - + @@ -1026,13 +1017,13 @@ - + - + - + @@ -1058,7 +1049,7 @@ - + @@ -1071,13 +1062,13 @@ - + - + - + @@ -1103,7 +1094,1158 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Items# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Id# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Code# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #LocationID# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Locations# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Id# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #LocationID# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Name# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Description# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Properties# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Id# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Name# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #ItemProps# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #ItemId# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Property# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Value# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Type# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Physical# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #LocationRelation# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Owner# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #OwnerNumber# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Owner# + + + + + + + + + diff --git a/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java b/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java index e81fad9..0ab04b3 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java @@ -85,21 +85,47 @@ public class SqliteDb extends BaseDb implements StockDb { } } + /** + * id, owner, owner_id, code, name, location_id + * @throws SQLException + */ private void createIntermediateItemsTable() throws SQLException { // create intermediate table - var sql = "CREATE TABLE IF NOT EXISTS items_temp ({0} LONG NOT NULL, {1} LONG NOT NULL, {2} VARCHAR(255), {3} VARCHAR(255) NOT NULL, {4} LONG NOT NULL, PRIMARY KEY({0}, {1}))"; - sql = format(sql, OWNER, ID, CODE, NAME, LOCATION_ID); + var sql = """ + CREATE TABLE IF NOT EXISTS items_temp ( + {0} INTEGER PRIMARY KEY, + {1} VARCHAR(50) NOT NULL, + {2} LONG NOT NULL, + {3} VARCHAR(255), + {4} VARCHAR(255) NOT NULL, + {5} LONG NOT NULL, + PRIMARY KEY({0}, {1}))"""; + sql = format(sql, ID, OWNER, OWNER_ID, CODE, NAME, LOCATION_ID); db.prepareStatement(sql).execute(); } + /** + * id, owner, parent_location_id, name, description + * @throws SQLException + */ private void createIntermediateLocationTable() throws SQLException { // create intermediate table - var sql = "CREATE TABLE IF NOT EXISTS locations_temp ({0} INTEGER PRIMARY KEY, {1} INT DEFAULT NULL, {2} LONG NOT NULL, {3} VARCHAR(255) NOT NULL, {4} TEXT)"; - sql = format(sql, ID, PARENT_LOCATION_ID, OWNER, NAME, DESCRIPTION); + var sql = """ + CREATE TABLE IF NOT EXISTS locations_temp ( + {0} INTEGER PRIMARY KEY, + {1} VARCHAR(50) NOT NULL, + {2} LONG DEFAULT NULL, + {3} VARCHAR(255) NOT NULL, + {4} TEXT)"""; + sql = format(sql, ID, OWNER, PARENT_LOCATION_ID, NAME, DESCRIPTION); db.prepareStatement(sql).execute(); } + /** + * item_id, prop_id , value + * @throws SQLException + */ private void createIntermediatePropsTable() throws SQLException { // create intermediate table - var sql = "CREATE TABLE IF NOT EXISTS item_props_temp ( {0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG NOT NULL, {3} VARCHAR(255) NOT NULL, PRIMARY KEY({0}, {1}, {2}))"; - sql = format(sql, OWNER, ITEM_ID, PROPERTY_ID, VALUE); + var sql = "CREATE TABLE IF NOT EXISTS item_props_temp ( {0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG NOT NULL, PRIMARY KEY({0}, {1}))"; + sql = format(sql, ITEM_ID, PROPERTY_ID, VALUE); db.prepareStatement(sql).execute(); }