|
|
|
|
@ -4,14 +4,11 @@ package de.srsoftware.umbrella.core.model;
@@ -4,14 +4,11 @@ package de.srsoftware.umbrella.core.model;
|
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
|
|
|
|
|
|
import de.srsoftware.tools.Mappable; |
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
|
import java.sql.ResultSet; |
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
import java.util.*; |
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
public class Item implements Mappable { |
|
|
|
|
private long id; |
|
|
|
|
@ -30,6 +27,10 @@ public class Item implements Mappable {
@@ -30,6 +27,10 @@ public class Item implements Mappable {
|
|
|
|
|
this.properties = new HashSet<>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String code(){ |
|
|
|
|
return code; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean isDirty(){ |
|
|
|
|
return !dirtyFields.isEmpty(); |
|
|
|
|
} |
|
|
|
|
@ -38,6 +39,14 @@ public class Item implements Mappable {
@@ -38,6 +39,14 @@ public class Item implements Mappable {
|
|
|
|
|
return id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Location location(){ |
|
|
|
|
return location; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String name(){ |
|
|
|
|
return name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException { |
|
|
|
|
var id = rs.getLong(ID); |
|
|
|
|
var code = rs.getString(CODE); |
|
|
|
|
@ -55,15 +64,16 @@ public class Item implements Mappable {
@@ -55,15 +64,16 @@ public class Item implements Mappable {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Map<String, Object> toMap() { |
|
|
|
|
var ownerMap = owner instanceof Company comp ? Map.of(COMPANY,comp.id()) : (owner instanceof UmbrellaUser u ? Map.of(USER,u.id()) : Map.of()); |
|
|
|
|
return Map.of( |
|
|
|
|
OWNER, ownerMap, |
|
|
|
|
ID, id, |
|
|
|
|
LOCATION, location.toMap(), |
|
|
|
|
CODE, code, |
|
|
|
|
NAME, name, |
|
|
|
|
PROPERTIES, properties.stream().map(Property::toMap).toList() |
|
|
|
|
); |
|
|
|
|
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(ID,id); |
|
|
|
|
if (location != null) 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()); |
|
|
|
|
return map; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@ -75,6 +85,9 @@ public class Item implements Mappable {
@@ -75,6 +85,9 @@ public class Item implements Mappable {
|
|
|
|
|
for (var field : json.keySet()){ |
|
|
|
|
var known = true; |
|
|
|
|
switch (field) { |
|
|
|
|
case CODE: |
|
|
|
|
code = json.getString(field); |
|
|
|
|
break; |
|
|
|
|
case NAME: |
|
|
|
|
name = json.getString(field); |
|
|
|
|
break; |
|
|
|
|
@ -85,4 +98,21 @@ public class Item implements Mappable {
@@ -85,4 +98,21 @@ 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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|