implemented stock display from location tree to property list

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-13 23:41:12 +02:00
parent 6e9a2b6aca
commit a52df2b434
10 changed files with 185 additions and 138 deletions

View File

@@ -7,6 +7,8 @@ import static de.srsoftware.tools.jdbc.Condition.isNull;
import static de.srsoftware.tools.jdbc.Query.*;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static de.srsoftware.umbrella.stock.Constants.*;
import static java.lang.System.Logger.Level.ERROR;
@@ -15,12 +17,7 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Company;
import de.srsoftware.umbrella.core.model.Item;
import de.srsoftware.umbrella.core.model.Location;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import de.srsoftware.umbrella.core.model.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -53,23 +50,6 @@ public class SqliteDb extends BaseDb implements StockDb {
super(connection);
}
@Override
protected int createTables() {
int currentVersion = createSettingsTable();
switch (currentVersion){
case 0:
createLocationsTable();
createItemsTable();
createPropertiesTable();
createItemPropsTable();
case 1:
dropTokenTable();
case 2:
transformTables();
}
return setCurrentVersion(3);
}
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);
@@ -128,6 +108,23 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
protected int createTables() {
int currentVersion = createSettingsTable();
switch (currentVersion){
case 0:
createLocationsTable();
createItemsTable();
createPropertiesTable();
createItemPropsTable();
case 1:
dropTokenTable();
case 2:
transformTables();
}
return setCurrentVersion(3);
}
private void dropTokenTable() {
try {
db.prepareStatement("DROP TABLE IF EXISTS tokens").execute();
@@ -136,48 +133,6 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public Collection<Item> listItems(long companyId) throws UmbrellaException {
return List.of();
}
@Override
public Collection<Item> listItemsAt(long locationId) {
try {
var rs = select(ALL).from(TABLE_ITEMS).where(LOCATION_ID,equal(locationId)).exec(db);
var list = new ArrayList<Item>();
while (rs.next()) {
var ownerId = rs.getLong(OWNER);
Mappable owner = ownerId < 0 ? ModuleRegistry.companyService().get(-ownerId) : ModuleRegistry.userService().loadUser(ownerId);
var location = loadLocation(rs.getLong(LOCATION_ID));
list.add(Item.of(rs, owner, location));
}
rs.close();
return list;
} catch (SQLException e){
throw databaseException("Failed to load items at {0}",locationId);
}
}
private Location loadLocation(long locationId) {
try {
var rs = select(ALL).from(TABLE_LOCATIONS).where(ID,equal(locationId)).exec(db);
Location loc = null;
if (rs.next()) loc = Location.of(rs);
rs.close();
if (loc != null) return loc;
throw databaseException("Failed to load location with id = {0}",locationId);
} catch (SQLException e){
throw databaseException("Failed to load location with id = {0}",locationId);
}
}
@Override
public Collection<Location> listLocations(long companyId) {
return List.of();
}
@Override
public Collection<Location> listChildLocations(long parentId) {
try {
@@ -204,6 +159,51 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public Collection<Item> listItemsAt(long locationId) {
try {
var location = loadLocation(locationId);
var rs = select(ALL).from(TABLE_ITEMS).where(LOCATION_ID,equal(locationId)).exec(db);
var list = new ArrayList<Item>();
var ownerMap = new HashMap<Long,Mappable>();
while (rs.next()) {
var ownerId = rs.getLong(OWNER);
var owner = ownerMap.get(ownerId);
if (owner == null) {
owner = ownerId < 0 ? companyService().get(-ownerId) : userService().loadUser(ownerId);
ownerMap.put(ownerId,owner);
}
list.add(Item.of(rs, owner, location));
}
rs.close();
for (var item : list){
var owner = item.owner();
var ownerId = owner instanceof Company comp ? -comp.id() : (owner instanceof UmbrellaUser u ? u.id() : 0);
if (ownerId == 0) throw databaseException("Encountered unknown item owner of type {0}",owner.getClass().getSimpleName());
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);
while (rs.next()) item.properties().add(Property.of(rs));
rs.close();
}
return list;
} catch (SQLException e){
throw databaseException("Failed to load items at {0}",locationId);
}
}
private Location loadLocation(long locationId) {
try {
var rs = select(ALL).from(TABLE_LOCATIONS).where(ID,equal(locationId)).exec(db);
Location loc = null;
if (rs.next()) loc = Location.of(rs);
rs.close();
if (loc != null) return loc;
throw databaseException("Failed to load location with id = {0}",locationId);
} catch (SQLException e){
throw databaseException("Failed to load location with id = {0}",locationId);
}
}
@Override
public Collection<Location> listUserLocations(UmbrellaUser user) {
try {
@@ -217,29 +217,6 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
private void transformTables(){
try {
db.setAutoCommit(false);
createIntermediateLocationTable();
createIntermediateItemsTable();
createIntermediatePropsTable();
var oldLocationIdsToNew = transformLocations();
transformItems(oldLocationIdsToNew);
transformProperties();
replaceLocationsTable();
replaceItemsTable();
replaceItemPropsTable();
db.setAutoCommit(true);
} catch (Exception e) {
try {
db.rollback();
} catch (SQLException ignored) {
}
LOG.log(ERROR,"Failed to transform {0} table!",TABLE_LOCATIONS,e);
throw databaseException("Failed to transform {0} table!",TABLE_LOCATIONS);
}
}
private void replaceItemsTable() throws SQLException {
db.prepareStatement(format("DROP TABLE {0}",TABLE_ITEMS)).execute();
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","items_temp",TABLE_ITEMS)).execute();
@@ -335,4 +312,27 @@ public class SqliteDb extends BaseDb implements StockDb {
rs.close();
insert.execute(db).close();
}
private void transformTables(){
try {
db.setAutoCommit(false);
createIntermediateLocationTable();
createIntermediateItemsTable();
createIntermediatePropsTable();
var oldLocationIdsToNew = transformLocations();
transformItems(oldLocationIdsToNew);
transformProperties();
replaceLocationsTable();
replaceItemsTable();
replaceItemPropsTable();
db.setAutoCommit(true);
} catch (Exception e) {
try {
db.rollback();
} catch (SQLException ignored) {
}
LOG.log(ERROR,"Failed to transform {0} table!",TABLE_LOCATIONS,e);
throw databaseException("Failed to transform {0} table!",TABLE_LOCATIONS);
}
}
}

View File

@@ -1,7 +1,6 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.stock;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Company;
import de.srsoftware.umbrella.core.model.Item;
import de.srsoftware.umbrella.core.model.Location;
@@ -11,12 +10,6 @@ import java.util.Collection;
public interface StockDb {
Collection<Location> listChildLocations(long parentId);
Collection<Location> listCompanyLocations(Company company);
Collection<Item> listItems(long companyId) throws UmbrellaException;
Collection<Item> listItemsAt(long locationId);
Collection<Location> listLocations(long companyId);
Collection<Item> listItemsAt(long locationId);
Collection<Location> listUserLocations(UmbrellaUser userId);
}