re-implemented updating of item properties
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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<String, Long> transformItems(Map<String, Long> oldLocationIdsToNew) throws SQLException {
|
||||
var rs = select(ALL).from(TABLE_ITEMS).exec(db);
|
||||
var insert = insertInto("items_temp",OWNER, OWNER_NUMBER, LOCATION_ID, CODE, NAME);
|
||||
|
||||
@@ -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<DbLocation> listChildLocations(long parentId);
|
||||
Collection<DbLocation> listCompanyLocations(Company company);
|
||||
Collection<Item> 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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user