refactoring object model

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-16 01:07:50 +02:00
parent c6823b116c
commit 64d7e0cb37
14 changed files with 270 additions and 147 deletions

View File

@@ -89,6 +89,8 @@ public class ModuleRegistry {
return singleton.projectService;
}
public static StockService stockService() { return singleton.stockService; }
public static TagService tagService(){
return singleton.tagService;
}

View File

@@ -1,10 +0,0 @@
/* © 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();
}

View File

@@ -1,6 +1,16 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.api;
public interface Owner {
long signum();
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.tools.Mappable;
public interface Owner extends Mappable {
public static final String SEPARATOR = ":";
default String dbCode(){
return type()+SEPARATOR+id();
}
long id();
String type();
public Owner resolve();
}

View File

@@ -1,10 +0,0 @@
/* © 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);
}

View File

@@ -2,6 +2,7 @@
package de.srsoftware.umbrella.core.api;
import de.srsoftware.umbrella.core.model.DbLocation;
import java.util.Collection;
public interface StockService {
@@ -12,4 +13,6 @@ public interface StockService {
* @return
*/
Collection<Object> redefineMe(long company_id);
DbLocation loadLocation(long locationId);
}

View File

@@ -6,6 +6,7 @@ import static de.srsoftware.umbrella.core.Constants.*;
import static java.util.Map.entry;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -13,7 +14,7 @@ import java.util.*;
import org.json.JSONException;
import org.json.JSONObject;
public class Company implements Mappable {
public class Company implements Mappable, Owner {
private final long id;
private String name, address, court, taxId, phone, decimalSeparator, thousandsSeparator, customerNumberPrefix, currency, email, bankAccount;;
private long lastCustomerNumber;
@@ -80,7 +81,8 @@ public class Company implements Mappable {
return email;
}
public long id(){
@Override
public long id() {
return id;
}
@@ -162,6 +164,11 @@ public class Company implements Mappable {
return phone;
}
@Override
public Owner resolve() {
return this;
}
public String taxId() {
return taxId;
}
@@ -182,7 +189,6 @@ public class Company implements Mappable {
memberMap.put(entry.getKey(),entry.getValue().toMap());
}
return Map.ofEntries(
entry(ID,id),
entry(NAME,name),
entry(ADDRESS,emptyIfNull(address)),
entry(FIELD_COURT,emptyIfNull(court)),
@@ -196,8 +202,14 @@ public class Company implements Mappable {
entry(FIELD_CURRENCY,emptyIfNull(currency)),
entry(EMAIL,emptyIfNull(email)),
entry(FIELD_BANK_ACCOUNT,emptyIfNull(bankAccount)),
entry(MEMBERS,memberMap)
);
entry(MEMBERS,memberMap),
entry(TYPE,type()),
entry(ID,id)
);
}
@Override
public String type() {
return COMPANY;
}
}

View File

@@ -0,0 +1,61 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.umbrella.core.api.Owner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
public class DbLocation extends Location {
private Owner owner;
private Long parentLocationId;
private String name;
private String description;
private String relation; // when added to an item, this field describes the type of the relation
private DbLocation(long id, Owner owner, Long parentLocationId, String name, String description){
super(id);
this.owner = owner;
this.parentLocationId = parentLocationId;
this.name = name;
this.description = description;
}
public String description() {
return description;
}
public String name() {
return name;
}
public static DbLocation of(ResultSet rs) throws SQLException {
return new DbLocation(rs.getLong(ID), OwnerRef.of(rs), rs.getLong(PARENT_LOCATION_ID), rs.getString(NAME),rs.getString(DESCRIPTION));
}
public Long parent(){
return parentLocationId;
}
@Override
public DbLocation resolve() {
return this;
}
@Override
public Map<String, Object> toMap() {
return Map.of(
OWNER,owner.toMap(),
ID,id(),
NAME,name,
DESCRIPTION,description);
}
@Override
public String toString() {
return name;
}
}

View File

@@ -4,8 +4,7 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.api.LocationRef;
import de.srsoftware.umbrella.core.api.OwnerRef;
import de.srsoftware.umbrella.core.api.Owner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
@@ -13,17 +12,17 @@ import org.json.JSONObject;
public class Item implements Mappable {
private long id, number; // id is the database key, number the owner-relative id
private OwnerRef ownerRef;
private Owner owner;
private String code, name;
private LocationRef locationRef;
private Location location;
private Collection<Property> properties;
private Set<String> dirtyFields = new HashSet<>();
private Item(long id, OwnerRef ownerRef, long number, LocationRef locationRef, String code, String name) {
private Item(long id, Owner owner, long number, Location location, String code, String name) {
this.id = id;
this.ownerRef = ownerRef;
this.owner = owner;
this.number = number;
this.locationRef = locationRef;
this.location = location;
this.code = code;
this.name = name;
this.properties = new HashSet<>();
@@ -41,23 +40,26 @@ public class Item implements Mappable {
return id;
}
public LocationRef location(){
return locationRef;
public Location location(){
return location;
}
public String name(){
return name;
}
public static Item of(ResultSet rs, OwnerRef ownerRef, long number, LocationRef locationRef) throws SQLException {
public static Item of(ResultSet rs) throws SQLException {
var id = rs.getLong(ID);
var code = rs.getString(CODE);
var name = rs.getString(NAME);
return new Item(id, ownerRef, number, locationRef, code, name);
var owner = OwnerRef.of(rs);
var number = rs.getLong(NUMBER);
var location = Location.of(rs);
return new Item(id, owner, number, location, code, name);
}
public OwnerRef ownerRef(){
return ownerRef;
public Owner owner(){
return owner;
}
public Collection<Property> properties() {
@@ -67,9 +69,9 @@ public class Item implements Mappable {
@Override
public Map<String, Object> toMap() {
var map = new HashMap<String,Object>();
map.put(OWNER,ownerRef.toMap());
map.put(OWNER,owner.toMap());
map.put(ID,id);
map.put(LOCATION,locationRef.toMap());
map.put(LOCATION,location.toMap());
map.put(CODE,code);
map.put(NAME,name);
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());

View File

@@ -1,72 +1,48 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.ID;
import static de.srsoftware.umbrella.core.Constants.LOCATION_ID;
import static de.srsoftware.umbrella.core.ModuleRegistry.stockService;
import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
public class Location implements Mappable {
private long owner;
private boolean ownerIsCompany = false;
private long id;
private Long parentLocationId;
private String name;
private String description;
private String relation; // when added to an item, this field describes the type of the relation
private Location(long owner, boolean ownerIsCompany, long id, Long parentLocationId, String name, String description){
this.owner = owner;
this.ownerIsCompany = ownerIsCompany;
private final long id;
public Location(long id){
this.id = id;
this.parentLocationId = parentLocationId;
this.name = name;
this.description = description;
}
public final long id(){
return id;
}
public String description() {
return description;
}
public String name() {
return name;
}
public static Location of(ResultSet rs) throws SQLException {
var owner = rs.getLong(OWNER);
var id = rs.getLong(ID);
var isCompany = owner < 0;
if (isCompany) owner = -owner;
return new Location(owner,isCompany,id, rs.getLong(PARENT_LOCATION_ID), rs.getString(NAME),rs.getString(DESCRIPTION));
return new Location(rs.getLong(LOCATION_ID));
}
public long ownerCoded(){
return ownerIsCompany ? -owner : owner;
public static Location of(long locationId) {
return new Location(locationId);
}
public Long parent(){
return parentLocationId;
public DbLocation resolve(){
return stockService().loadLocation(id());
}
public long id(){
return id;
}
@Override
public Map<String, Object> toMap() {
return Map.of(
ownerIsCompany ? COMPANY : USER,owner,
ID,id,
NAME,name,
DESCRIPTION,description);
return Map.of(ID,id);
}
@Override
public String toString() {
return name;
return format("Location({0})",id);
}
}

View File

@@ -0,0 +1,73 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.ID;
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
import de.srsoftware.umbrella.core.api.Owner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class OwnerRef implements Owner {
private final String type;
private final long id;
public OwnerRef(String dbCode){
var parts = dbCode.split(":");
if (parts.length != 2) throw unprocessable("Encountered invalid dbCode: {0}",dbCode);
type = parts[0];
try {
id = Long.parseLong(parts[1]);
} catch (NumberFormatException e) {
throw unprocessable("Encountered invalid dbCode: {0}",dbCode);
}
}
public OwnerRef(String type, long id){
this.type = type;
this.id = id;
}
public static Owner of(ResultSet rs) throws SQLException {
return new OwnerRef(rs.getString(OWNER));
}
public String dbCode(){
return type+SEPARATOR+id;
}
public long id(){
return id;
}
public String type(){
// TODO: is this needed?
return type;
}
/**
* returns the actual owner object
* @return the actual owner object
*/
public Owner resolve(){
return switch (type){
case COMPANY -> companyService().get(id());
case USER -> userService().loadUser(id());
case null, default -> throw unprocessable("Encountered invalid owner type: {0}",type);
};
}
@Override
public Map<String, Object> toMap() {
var map = new HashMap<String,Object>();
map.put(TYPE,type);
map.put(ID, id);
return map;
}
}

View File

@@ -5,12 +5,13 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.api.Owner;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/* © SRSoftware 2025 */
public class UmbrellaUser extends User implements Mappable {
public class UmbrellaUser extends User implements Mappable, Owner {
private final long id;
private final String theme;
@@ -21,7 +22,6 @@ public class UmbrellaUser extends User implements Mappable {
this.theme = theme;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof UmbrellaUser user)) return false;
@@ -40,6 +40,11 @@ public class UmbrellaUser extends User implements Mappable {
return id;
}
@Override
public Owner resolve() {
return this;
}
public String theme(){
return theme;
}
@@ -55,4 +60,9 @@ public class UmbrellaUser extends User implements Mappable {
map.put(LANGUAGE,language());
return map;
}
@Override
public String type() {
return USER;
}
}