started refactoring
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -138,6 +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 PARENT_LOCATION_ID = "parent_location_id";
|
||||
public static final String PARENT_TASK_ID = "parent_task_id";
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
|
||||
public interface LocationRef extends Mappable {
|
||||
Location resolve(StockService stockService);
|
||||
long id();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
public interface Owner {
|
||||
long signum();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
|
||||
public interface OwnerRef extends Mappable {
|
||||
long id();
|
||||
Owner resolve(ModuleRegistry registry);
|
||||
}
|
||||
@@ -4,24 +4,26 @@ package de.srsoftware.umbrella.core.model;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.api.LocationRef;
|
||||
import de.srsoftware.umbrella.core.api.OwnerRef;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Item implements Mappable {
|
||||
private long id;
|
||||
private Mappable owner;
|
||||
private long id, number; // id is the database key, number the owner-relative id
|
||||
private OwnerRef ownerRef;
|
||||
private String code, name;
|
||||
private Location location;
|
||||
private LocationRef locationRef;
|
||||
private Collection<Property> properties;
|
||||
private Set<String> dirtyFields = new HashSet<>();
|
||||
|
||||
private Item(Mappable owner, long id, Location location, String code, String name) {
|
||||
this.owner = owner;
|
||||
private Item(long id, OwnerRef ownerRef, long number, LocationRef locationRef, String code, String name) {
|
||||
this.id = id;
|
||||
this.location = location;
|
||||
this.ownerRef = ownerRef;
|
||||
this.number = number;
|
||||
this.locationRef = locationRef;
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.properties = new HashSet<>();
|
||||
@@ -39,23 +41,23 @@ public class Item implements Mappable {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Location location(){
|
||||
return location;
|
||||
public LocationRef location(){
|
||||
return locationRef;
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException {
|
||||
public static Item of(ResultSet rs, OwnerRef ownerRef, long number, LocationRef locationRef) throws SQLException {
|
||||
var id = rs.getLong(ID);
|
||||
var code = rs.getString(CODE);
|
||||
var name = rs.getString(NAME);
|
||||
return new Item(owner, id, location, code, name);
|
||||
return new Item(id, ownerRef, number, locationRef, code, name);
|
||||
}
|
||||
|
||||
public Mappable owner(){
|
||||
return owner;
|
||||
public OwnerRef ownerRef(){
|
||||
return ownerRef;
|
||||
}
|
||||
|
||||
public Collection<Property> properties() {
|
||||
@@ -64,12 +66,10 @@ public class Item implements Mappable {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
var ownerId = ownerId();
|
||||
var ownerMap = ownerId < 0 ? Map.of(COMPANY,-ownerId) : Map.of(USER,ownerId);
|
||||
var map = new HashMap<String,Object>();
|
||||
map.put(OWNER,ownerMap);
|
||||
map.put(OWNER,ownerRef.toMap());
|
||||
map.put(ID,id);
|
||||
if (location != null) map.put(LOCATION,location.toMap());
|
||||
map.put(LOCATION,locationRef.toMap());
|
||||
map.put(CODE,code);
|
||||
map.put(NAME,name);
|
||||
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
||||
@@ -99,18 +99,6 @@ public class Item implements Mappable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Long ownerId(Mappable owner){
|
||||
return switch (owner) {
|
||||
case Company comp -> -comp.id();
|
||||
case UmbrellaUser user -> user.id();
|
||||
case null, default -> throw UmbrellaException.unprocessable("Item has owner of unknown class ({0})", owner.getClass().getSimpleName());
|
||||
};
|
||||
}
|
||||
|
||||
public Long ownerId() {
|
||||
return ownerId(owner);
|
||||
}
|
||||
|
||||
public Item clear() {
|
||||
dirtyFields.clear();
|
||||
return this;
|
||||
|
||||
1362
doc/stock.dia
1362
doc/stock.dia
File diff suppressed because it is too large
Load Diff
@@ -85,21 +85,47 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* id, owner, owner_id, code, name, location_id
|
||||
* @throws SQLException
|
||||
*/
|
||||
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);
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS items_temp (
|
||||
{0} INTEGER PRIMARY KEY,
|
||||
{1} VARCHAR(50) NOT NULL,
|
||||
{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);
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* id, owner, parent_location_id, name, description
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void createIntermediateLocationTable() throws SQLException { // create intermediate table
|
||||
var sql = "CREATE TABLE IF NOT EXISTS locations_temp ({0} INTEGER PRIMARY KEY, {1} INT DEFAULT NULL, {2} LONG NOT NULL, {3} VARCHAR(255) NOT NULL, {4} TEXT)";
|
||||
sql = format(sql, ID, PARENT_LOCATION_ID, OWNER, NAME, DESCRIPTION);
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS locations_temp (
|
||||
{0} INTEGER PRIMARY KEY,
|
||||
{1} VARCHAR(50) NOT NULL,
|
||||
{2} LONG DEFAULT NULL,
|
||||
{3} VARCHAR(255) NOT NULL,
|
||||
{4} TEXT)""";
|
||||
sql = format(sql, ID, OWNER, PARENT_LOCATION_ID, NAME, DESCRIPTION);
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* item_id, prop_id , value
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void createIntermediatePropsTable() throws SQLException { // create intermediate table
|
||||
var sql = "CREATE TABLE IF NOT EXISTS item_props_temp ( {0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG NOT NULL, {3} VARCHAR(255) NOT NULL, PRIMARY KEY({0}, {1}, {2}))";
|
||||
sql = format(sql, OWNER, ITEM_ID, PROPERTY_ID, VALUE);
|
||||
var sql = "CREATE TABLE IF NOT EXISTS item_props_temp ( {0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG NOT NULL, PRIMARY KEY({0}, {1}))";
|
||||
sql = format(sql, ITEM_ID, PROPERTY_ID, VALUE);
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user