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 68235ca..f595cfe 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java @@ -41,26 +41,7 @@ public class SqliteDb extends BaseDb implements StockDb { } @Override - public Property setProperty(long ownerId, long itemId, long existingPropId, Object value) { - try { - Property prop = null; - var rs = select(ALL).from(TABLE_PROPERTIES).where(ID,equal(existingPropId)).exec(db); - if (rs.next()) prop = Property.of(rs); - rs.close(); - if (prop == null) throw databaseException("Failed to add new property to item {0}",itemId); - if ("".equals(value)){ - delete().from(TABLE_ITEM_PROPERTIES).where(OWNER,equal(ownerId)).where(ITEM_ID,equal(itemId)).where(PROPERTY_ID,equal(existingPropId)).execute(db); - } else { - replaceInto(TABLE_ITEM_PROPERTIES,OWNER,ITEM_ID,PROPERTY_ID,VALUE).values(ownerId,itemId,existingPropId,value).execute(db); - } - return prop.value(value); - } catch (SQLException e) { - throw databaseException("Failed to add new property to item {0}",itemId); - } - } - - @Override - public Property addNewProperty(long ownerId, long itemId, String name, Object value, String unit) { + public Property addNewProperty(long itemId, String name, Object value, String unit) { try { db.setAutoCommit(false); var rs = insertInto(TABLE_PROPERTIES,NAME,TYPE,UNIT).values(name,0,unit).execute(db).getGeneratedKeys(); @@ -68,7 +49,7 @@ public class SqliteDb extends BaseDb implements StockDb { if (rs.next()) propertyId = rs.getLong(1); rs.close(); if (propertyId == null || propertyId == 0) throw databaseException("Failed to create new property {0} to DB",name); - insertInto(TABLE_ITEM_PROPERTIES,OWNER,ITEM_ID,PROPERTY_ID,VALUE).values(ownerId,itemId,propertyId,value).execute(db); + insertInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,propertyId,value).execute(db); return new Property(propertyId,name,value,unit); } catch (SQLException e) { throw databaseException("Failed to create new property {0} to DB",name); @@ -323,6 +304,25 @@ public class SqliteDb extends BaseDb implements StockDb { return item; } + @Override + public Property setProperty(long itemId, long existingPropId, Object value) { + try { + Property prop = null; + var rs = select(ALL).from(TABLE_PROPERTIES).where(ID,equal(existingPropId)).exec(db); + if (rs.next()) prop = Property.of(rs); + rs.close(); + if (prop == null) throw databaseException("Failed to add new property to item {0}",itemId); + if ("".equals(value)){ + delete().from(TABLE_ITEM_PROPERTIES).where(ITEM_ID,equal(itemId)).where(PROPERTY_ID,equal(existingPropId)).execute(db); + } else { + replaceInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,existingPropId,value).execute(db); + } + return prop.value(value); + } catch (SQLException e) { + throw databaseException("Failed to add new property to item {0}",itemId); + } + } + private HashMap transformItems(Map oldLocationIdsToNew) throws SQLException { var rs = select(ALL).from(TABLE_ITEMS).exec(db); var insert = insertInto("items_temp",OWNER, OWNER_NUMBER, LOCATION_ID, CODE, NAME); diff --git a/stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java b/stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java index 95081e2..de60e72 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java @@ -6,7 +6,7 @@ import de.srsoftware.umbrella.core.model.*; import java.util.Collection; public interface StockDb { - Property addNewProperty(long ownerId, long itemId, String name, Object value, String unit); + Property addNewProperty(long itemId, String name, Object value, String unit); Collection listChildLocations(long parentId); Collection listCompanyLocations(Company company); Collection listItemsAt(Location location); @@ -15,5 +15,5 @@ public interface StockDb { Item loadItem(long id); DbLocation loadLocation(long locationId); Item save(Item item); - Property setProperty(long ownerId, long itemId, long existingPropId, Object value); + Property setProperty(long itemId, long existingPropId, Object value); } 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 d3bf89b..2384e49 100644 --- a/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java +++ b/stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java @@ -163,22 +163,20 @@ public class StockModule extends BaseHandler implements StockService { private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException { var json = json(ex); if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM); - if (!(itemData.get(OWNER) instanceof JSONObject owner)) throw missingFieldException(OWNER); if (!(itemData.get(ID) instanceof Number itemId)) throw missingFieldException(ID); if (!(json.get("add_prop") instanceof JSONObject propData)) throw missingFieldException("add_prop"); if (!propData.has(VALUE)) throw missingFieldException(VALUE); var value = propData.get(VALUE); if (value == null) throw missingFieldException(VALUE); - var ownerId = toOwnerId(owner); Property property = null; if (propData.get("existing_prop_id") instanceof Number existingPropId && existingPropId.longValue() != 0L){ - property = stockDb.setProperty(ownerId,itemId.longValue(),existingPropId.longValue(),value); + property = stockDb.setProperty(itemId.longValue(),existingPropId.longValue(),value); } else { if (!(propData.get("new_prop") instanceof JSONObject newProp)) throw unprocessable("data must contain either add_prop.existing_prop_id or add_prop.new_prop!"); if (!(newProp.get(NAME) instanceof String name) || name.isBlank()) throw unprocessable("data.add_prop.new_prop does not contain name!"); var unit = newProp.get(UNIT) instanceof String u ? nullIfEmpty(u) : null; - property = stockDb.addNewProperty(ownerId,itemId.longValue(),name,value,unit); + property = stockDb.addNewProperty(itemId.longValue(),name,value,unit); } return sendContent(ex,property); }