|
|
|
|
@@ -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;
|
|
|
|
|
|