working on stock refactoring
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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<DbLocation> 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<DbLocation>();
|
||||
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<Item>();
|
||||
var ownerMap = new HashMap<Long,Mappable>();
|
||||
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<DbLocation> 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<DbLocation>();
|
||||
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<String, Long> oldLocationIdsToNew) throws SQLException {
|
||||
private HashMap<String, Long> transformItems(Map<String, Long> 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<String,Long>(); // 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<String, Long> 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<String, Long> 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();
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user