From b6ef6def1782176473ee99dc6446f25f0f86747a Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Thu, 16 Oct 2025 12:36:32 +0200 Subject: [PATCH] working on stock refactoring Signed-off-by: Stephan Richter --- .../srsoftware/umbrella/core/Constants.java | 2 +- .../srsoftware/umbrella/core/model/Item.java | 14 +- doc/stock.dia | 334 +++++++----------- .../srsoftware/umbrella/stock/SqliteDb.java | 93 ++--- .../umbrella/stock/StockModule.java | 6 +- 5 files changed, 169 insertions(+), 280 deletions(-) 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 36bc75c..4643c67 100644 --- a/core/src/main/java/de/srsoftware/umbrella/core/Constants.java +++ b/core/src/main/java/de/srsoftware/umbrella/core/Constants.java @@ -138,7 +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 OWNER_NUMBER = "owner_number"; 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/model/Item.java b/core/src/main/java/de/srsoftware/umbrella/core/model/Item.java index d71ea97..7a77d7f 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 @@ -11,17 +11,17 @@ import java.util.*; import org.json.JSONObject; public class Item implements Mappable { - private long id, number; // id is the database key, number the owner-relative id + private long id, ownerNumber; // id is the database key, number the owner-relative id private Owner owner; private String code, name; private Location location; private Collection properties; private Set dirtyFields = new HashSet<>(); - private Item(long id, Owner owner, long number, Location location, String code, String name) { + private Item(long id, Owner owner, long ownerNumber, Location location, String code, String name) { this.id = id; this.owner = owner; - this.number = number; + this.ownerNumber = ownerNumber; this.location = location; this.code = code; this.name = name; @@ -50,12 +50,12 @@ public class Item implements Mappable { public static Item of(ResultSet rs) throws SQLException { var id = rs.getLong(ID); - var code = rs.getString(CODE); - var name = rs.getString(NAME); var owner = OwnerRef.of(rs); - var number = rs.getLong(NUMBER); + var ownerNumber = rs.getLong(OWNER_NUMBER); var location = Location.of(rs); - return new Item(id, owner, number, location, code, name); + var code = rs.getString(CODE); + var name = rs.getString(NAME); + return new Item(id, owner, ownerNumber, location, code, name); } public Owner owner(){ diff --git a/doc/stock.dia b/doc/stock.dia index 24b0f6e..d2baf97 100644 --- a/doc/stock.dia +++ b/doc/stock.dia @@ -91,13 +91,13 @@ - + - + @@ -229,13 +229,13 @@ - + - + - + @@ -261,7 +261,7 @@ - + @@ -277,13 +277,13 @@ - + - + @@ -309,7 +309,7 @@ - + @@ -322,16 +322,16 @@ - + - + - + - + @@ -354,7 +354,7 @@ - + @@ -367,16 +367,16 @@ - + - + - + - + @@ -390,7 +390,7 @@ - #LocationID# + #ParentLocationID# @@ -399,7 +399,7 @@ - + @@ -412,16 +412,16 @@ - + - + - + - + @@ -444,7 +444,7 @@ - + @@ -457,16 +457,16 @@ - + - + - + - + @@ -489,7 +489,7 @@ - + @@ -640,16 +640,16 @@ - + - + - - + + - + @@ -675,16 +675,16 @@ - + - + - - - - + + + + @@ -1017,13 +1017,13 @@ - + - + - + @@ -1049,7 +1049,7 @@ - + @@ -1061,51 +1061,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - #LocationRelation# - - - - - - - - - - - - - - - - - - - - @@ -1153,7 +1108,7 @@ - + @@ -1198,15 +1153,15 @@ - + - + - + - + @@ -1232,7 +1187,7 @@ - + @@ -1243,15 +1198,15 @@ - + - + - + - + @@ -1277,7 +1232,7 @@ - + @@ -1288,18 +1243,18 @@ - + - + - + @@ -1325,7 +1280,7 @@ - + @@ -1336,18 +1291,18 @@ - + - + - + @@ -1370,7 +1325,7 @@ - + @@ -1381,18 +1336,18 @@ - + - + - + @@ -1406,7 +1361,7 @@ - #LocationID# + #ParentLocationID# @@ -1415,7 +1370,7 @@ - + @@ -1426,18 +1381,18 @@ - + - + - + @@ -1460,7 +1415,7 @@ - + @@ -1471,18 +1426,18 @@ - + - + - + @@ -1505,7 +1460,7 @@ - + @@ -1516,15 +1471,15 @@ - + - + - + @@ -1564,7 +1519,7 @@ - + @@ -1609,7 +1564,7 @@ - + @@ -1654,18 +1609,18 @@ - + - + - + - - + + - + @@ -1685,22 +1640,22 @@ - - + + - + - + - + - - - - + + + + @@ -1720,11 +1675,11 @@ - - + + - + @@ -1772,7 +1727,7 @@ - + @@ -1817,7 +1772,7 @@ - + @@ -1862,7 +1817,7 @@ - + @@ -1907,7 +1862,7 @@ - + @@ -1938,11 +1893,11 @@ - - + + - + @@ -1982,11 +1937,11 @@ - - + + - + @@ -2031,15 +1986,15 @@ - + - + - + - + @@ -2056,52 +2011,7 @@ - #Physical# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #LocationRelation# + #Name# @@ -2110,7 +2020,7 @@ - + @@ -2121,7 +2031,7 @@ - + @@ -2166,7 +2076,7 @@ - + @@ -2211,18 +2121,18 @@ - + - + - + @@ -2245,7 +2155,7 @@ - + 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 a2bd2ad..68235ca 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java @@ -29,19 +29,10 @@ public class SqliteDb extends BaseDb implements StockDb { return new LegacyLocation(rs.getString(ID), rs.getString(LOCATION_ID), rs.getString(NAME), rs.getString(DESCRIPTION)); } - public long owner() { + public String owner() { var parts = id.split(":"); if (parts.length != 3) throw databaseException("Expected legacy location id to be of the form ss:dd:ss, encountered {0}!",id); - try { - var owner = Long.parseLong(parts[1]); - switch (parts[0]){ - case "company": return -owner; - case "user": return owner; - case null, default: throw databaseException("Expected legacy location id to start with 'company:' or 'user:', encountered {0}!",id); - } - } catch (NumberFormatException nfe){ - throw databaseException("Expected legacy location id to be of the form ss:dd:ss, encountered {0}!",id); - } + return String.join(":", parts[0], parts[1]); } }; @@ -96,9 +87,8 @@ public class SqliteDb extends BaseDb implements StockDb { {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); + {5} LONG NOT NULL)"""; + sql = format(sql, ID, OWNER, OWNER_NUMBER, CODE, NAME, LOCATION_ID); db.prepareStatement(sql).execute(); } @@ -209,7 +199,7 @@ public class SqliteDb extends BaseDb implements StockDb { @Override public Collection listCompanyLocations(Company company) { try { - var rs = select(ALL).from(TABLE_LOCATIONS).where(OWNER,equal(-company.id())).where(PARENT_LOCATION_ID,isNull()).exec(db); + var rs = select(ALL).from(TABLE_LOCATIONS).where(OWNER,equal(company.dbCode())).where(PARENT_LOCATION_ID,isNull()).exec(db); var list = new ArrayList(); while (rs.next()) list.add(DbLocation.of(rs)); rs.close(); @@ -224,13 +214,11 @@ public class SqliteDb extends BaseDb implements StockDb { try { var rs = select(ALL).from(TABLE_ITEMS).where(LOCATION_ID,equal(location.id())).exec(db); var list = new ArrayList(); - var ownerMap = new HashMap(); while (rs.next()) list.add(Item.of(rs)); rs.close(); for (var item : list){ - var ownerId = item.owner(); - rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID,TABLE_PROPERTIES,ID).where(OWNER,equal(ownerId)).where(ITEM_ID,equal(item.id())).exec(db); + rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID,TABLE_PROPERTIES,ID).where(ITEM_ID,equal(item.id())).exec(db); while (rs.next()) item.properties().add(Property.of(rs)); rs.close(); } @@ -282,7 +270,7 @@ public class SqliteDb extends BaseDb implements StockDb { @Override public Collection listUserLocations(UmbrellaUser user) { try { - var rs = select(ALL).from(TABLE_LOCATIONS).where(OWNER,equal(user.id())).where(PARENT_LOCATION_ID,isNull()).exec(db); + var rs = select(ALL).from(TABLE_LOCATIONS).where(OWNER,equal(user.dbCode())).where(PARENT_LOCATION_ID,isNull()).exec(db); var list = new ArrayList(); while (rs.next()) list.add(DbLocation.of(rs)); rs.close(); @@ -335,32 +323,32 @@ public class SqliteDb extends BaseDb implements StockDb { return item; } - private void transformItems(Map oldLocationIdsToNew) throws SQLException { + private HashMap transformItems(Map oldLocationIdsToNew) throws SQLException { var rs = select(ALL).from(TABLE_ITEMS).exec(db); - var insert = insertInto("items_temp",OWNER, ID, CODE, NAME, LOCATION_ID); + var insert = insertInto("items_temp",OWNER, OWNER_NUMBER, LOCATION_ID, CODE, NAME); + var oldToNew = new HashMap(); // maps from old item ids to new ones while (rs.next()){ var oldId = rs.getString(ID); var parts = oldId.split(":"); - var owner = 0L; - var id = 0L; + if (parts.length != 3) throw databaseException("Expected old item id to be of the form ss:dd:dd, encountered {0}!",oldId); + var owner = String.join(":",parts[0], parts[1]); + long ownerNumber; try { - owner = Long.parseLong(parts[1]); - id = Long.parseLong(parts[2]); - } catch (NumberFormatException e){ - throw databaseException("Expected item id to be of format ss:dd:dd, but encountered \"{0}\"",oldId); + ownerNumber = Long.parseLong(parts[2]); + } catch (NumberFormatException e) { + throw databaseException("Expected old item id to be of the form ss:dd:dd, encountered {0}!",oldId); } - var ownerIsCompany = switch (parts[0]){ - case "company" -> true; - case "user" -> false; - case null, default -> throw databaseException("Expected item id to start with 'company:' or 'user:', encountered \"{0}\"",oldId); - }; + var oldLocationId = rs.getString(LOCATION_ID); var locationId = oldLocationIdsToNew.get(oldLocationId); - if (locationId == null) throw databaseException("Item {0} of {1} {2} refers to location {3}, which is unknown!",id,parts[1],owner,oldLocationId); - insert.values(ownerIsCompany?-owner:owner, id, rs.getString(CODE), rs.getString(NAME), locationId).execute(db).getGeneratedKeys(); + if (locationId == null) throw databaseException("Item {0} of {1} {2} refers to location {3}, which is unknown!",oldId,parts[0],parts[1],oldLocationId); + var rs2 = insert.values(owner, ownerNumber, locationId, rs.getString(CODE), rs.getString(NAME)).execute(db).getGeneratedKeys(); + oldToNew.put(oldId,rs2.getLong(1)); + rs2.close(); } rs.close(); insert.execute(db); + return oldToNew; } private Map transformLocations() throws SQLException { @@ -370,7 +358,7 @@ public class SqliteDb extends BaseDb implements StockDb { while (rs.next()) locations.add(LegacyLocation.of(rs)); rs.close(); - var query = insertInto("locations_temp", PARENT_LOCATION_ID, OWNER, NAME, DESCRIPTION); + var query = insertInto("locations_temp", OWNER, PARENT_LOCATION_ID, NAME, DESCRIPTION); while (!locations.isEmpty()){ var legacyLocation = locations.removeFirst(); var parentRef = nullIfEmpty(legacyLocation.parent()); @@ -378,39 +366,30 @@ public class SqliteDb extends BaseDb implements StockDb { if (parentRef != null) { parentId = oldToNew.get(parentRef); if (parentId == null) { // parent not processed, re-add to end of queue - LOG.log(WARNING,"Postpoining {0}, as {1} is not present…",legacyLocation.id,legacyLocation.parent); + LOG.log(WARNING,"Postponing {0}, as {1} is not present…",legacyLocation.id,legacyLocation.parent); locations.add(legacyLocation); continue; } } - rs = query.values(parentId, legacyLocation.owner(), legacyLocation.name(), legacyLocation.description()).execute(db).getGeneratedKeys(); - var id = rs.getLong(1); - oldToNew.put(legacyLocation.id(),id); + var owner = legacyLocation.owner(); + + rs = query.values(owner, parentId, legacyLocation.name, legacyLocation.description).execute(db).getGeneratedKeys(); + oldToNew.put(legacyLocation.id(),rs.getLong(1)); rs.close(); } return oldToNew; } - private void transformProperties() throws SQLException { + private void transformProperties(HashMap oldItemIdsToNew) throws SQLException { var rs = select(ALL).from(TABLE_ITEM_PROPERTIES).exec(db); - var insert = insertInto("item_props_temp",OWNER, ITEM_ID, PROPERTY_ID, VALUE); + var insert = insertInto("item_props_temp",ITEM_ID, PROPERTY_ID, VALUE); while (rs.next()){ var oldItemId = rs.getString(ITEM_ID); - var parts = oldItemId.split(":"); - var owner = 0L; - var itemId = 0L; - try { - owner = Long.parseLong(parts[1]); - itemId = Long.parseLong(parts[2]); - } catch (NumberFormatException e){ - throw databaseException("Expected item id to be of format ss:dd:dd, but encountered \"{0}\"",oldItemId); + var itemId = oldItemIdsToNew.get(oldItemId); + if (itemId == null) { + throw databaseException("Old item id ({0}) has no new counterpart!",oldItemId); } - var ownerIsCompany = switch (parts[0]){ - case "company" -> true; - case "user" -> false; - case null, default -> throw databaseException("Expected item id to start with 'company:' or 'user:', encountered \"{0}\"",oldItemId); - }; - insert.values(ownerIsCompany?-owner:owner, itemId, rs.getLong(PROPERTY_ID), rs.getString(VALUE)); + insert.values(itemId, rs.getLong(PROPERTY_ID), rs.getString(VALUE)); } rs.close(); insert.execute(db).close(); @@ -423,8 +402,8 @@ public class SqliteDb extends BaseDb implements StockDb { createIntermediateItemsTable(); createIntermediatePropsTable(); var oldLocationIdsToNew = transformLocations(); - transformItems(oldLocationIdsToNew); - transformProperties(); + var oldItemIdsToNew = transformItems(oldLocationIdsToNew); + transformProperties(oldItemIdsToNew); replaceLocationsTable(); replaceItemsTable(); replaceItemPropsTable(); diff --git a/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java b/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java index 26a1413..d3bf89b 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java @@ -49,8 +49,8 @@ public class StockModule extends BaseHandler implements StockService { return switch (head) { case LOCATION -> { try { - var id = Long.parseLong(path.pop()); - yield getLocation(user.get(), Location.of(id),ex); + var location = Location.of(Long.parseLong(path.pop())); + yield getItemsAtLocation(user.get(), location, ex); } catch (Exception e){ yield super.doGet(path,ex); } @@ -100,7 +100,7 @@ public class StockModule extends BaseHandler implements StockService { } - private boolean getLocation(UmbrellaUser user, Location location, HttpExchange ex) throws IOException { + private boolean getItemsAtLocation(UmbrellaUser user, Location location, HttpExchange ex) throws IOException { return sendContent(ex, stockDb.listItemsAt(location).stream().map(Item::toMap).toList()); }