@ -4,24 +4,26 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.* ;
import static de.srsoftware.umbrella.core.Constants.* ;
import de.srsoftware.tools.Mappable ;
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.ResultSet ;
import java.sql.SQLException ;
import java.sql.SQLException ;
import java.util.* ;
import java.util.* ;
import org.json.JSONObject ;
import org.json.JSONObject ;
public class Item implements Mappable {
public class Item implements Mappable {
private long id ;
private long id , number ; // id is the database key, number the owner-relative id
private Mappable owner ;
private OwnerRef ownerRef ;
private String code , name ;
private String code , name ;
private Location location ;
private LocationRef locationRef ;
private Collection < Property > properties ;
private Collection < Property > properties ;
private Set < String > dirtyFields = new HashSet < > ( ) ;
private Set < String > dirtyFields = new HashSet < > ( ) ;
private Item ( Mappable owner , long id , Location location , String code , String name ) {
private Item ( long id , OwnerRef ownerRef , long number , LocationRef locationRef , String code , String name ) {
this . owner = owner ;
this . id = id ;
this . id = id ;
this . location = location ;
this . ownerRef = ownerRef ;
this . number = number ;
this . locationRef = locationRef ;
this . code = code ;
this . code = code ;
this . name = name ;
this . name = name ;
this . properties = new HashSet < > ( ) ;
this . properties = new HashSet < > ( ) ;
@ -39,23 +41,23 @@ public class Item implements Mappable {
return id ;
return id ;
}
}
public Location location ( ) {
public LocationRef location ( ) {
return location ;
return locationRef ;
}
}
public String name ( ) {
public String name ( ) {
return name ;
return name ;
}
}
public static Item of ( ResultSet rs , Mappable own er, Location location ) throws SQLException {
public static Item of ( ResultSet rs , OwnerRef ownerRef , long numb er, LocationRef locationRef ) throws SQLException {
var id = rs . getLong ( ID ) ;
var id = rs . getLong ( ID ) ;
var code = rs . getString ( CODE ) ;
var code = rs . getString ( CODE ) ;
var name = rs . getString ( NAME ) ;
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 ( ) {
public OwnerRef ownerRef ( ) {
return owner ;
return ownerRef ;
}
}
public Collection < Property > properties ( ) {
public Collection < Property > properties ( ) {
@ -64,12 +66,10 @@ public class Item implements Mappable {
@Override
@Override
public Map < String , Object > toMap ( ) {
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 > ( ) ;
var map = new HashMap < String , Object > ( ) ;
map . put ( OWNER , ownerMap ) ;
map . put ( OWNER , ownerRef . toMap ( ) ) ;
map . put ( ID , id ) ;
map . put ( ID , id ) ;
if ( location ! = null ) map . put ( LOCATION , location . toMap ( ) ) ;
map . put ( LOCATION , locationRef . toMap ( ) ) ;
map . put ( CODE , code ) ;
map . put ( CODE , code ) ;
map . put ( NAME , name ) ;
map . put ( NAME , name ) ;
if ( properties ! = null ) map . put ( PROPERTIES , properties . stream ( ) . map ( Property : : toMap ) . toList ( ) ) ;
if ( properties ! = null ) map . put ( PROPERTIES , properties . stream ( ) . map ( Property : : toMap ) . toList ( ) ) ;
@ -99,18 +99,6 @@ public class Item implements Mappable {
return this ;
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 ( ) {
public Item clear ( ) {
dirtyFields . clear ( ) ;
dirtyFields . clear ( ) ;
return this ;
return this ;