@ -7,8 +7,6 @@ import static de.srsoftware.tools.jdbc.Condition.isNull;
import static de.srsoftware.tools.jdbc.Query.* ;
import static de.srsoftware.tools.jdbc.Query.* ;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL ;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL ;
import static de.srsoftware.umbrella.core.Constants.* ;
import static de.srsoftware.umbrella.core.Constants.* ;
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService ;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService ;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException ;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException ;
import static de.srsoftware.umbrella.stock.Constants.* ;
import static de.srsoftware.umbrella.stock.Constants.* ;
import static java.lang.System.Logger.Level.ERROR ;
import static java.lang.System.Logger.Level.ERROR ;
@ -17,6 +15,7 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable ;
import de.srsoftware.tools.Mappable ;
import de.srsoftware.umbrella.core.BaseDb ;
import de.srsoftware.umbrella.core.BaseDb ;
import de.srsoftware.umbrella.core.model.Location ;
import de.srsoftware.umbrella.core.model.* ;
import de.srsoftware.umbrella.core.model.* ;
import java.sql.Connection ;
import java.sql.Connection ;
import java.sql.ResultSet ;
import java.sql.ResultSet ;
@ -195,11 +194,11 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
@Override
public Collection < Location > listChildLocations ( long parentId ) {
public Collection < Db Location> listChildLocations ( long parentId ) {
try {
try {
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( PARENT_LOCATION_ID , equal ( parentId ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( PARENT_LOCATION_ID , equal ( parentId ) ) . exec ( db ) ;
var list = new ArrayList < Location > ( ) ;
var list = new ArrayList < Db Location> ( ) ;
while ( rs . next ( ) ) list . add ( Location . of ( rs ) ) ;
while ( rs . next ( ) ) list . add ( Db Location. of ( rs ) ) ;
rs . close ( ) ;
rs . close ( ) ;
return list ;
return list ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
@ -208,11 +207,11 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
@Override
public Collection < Location > listCompanyLocations ( Company company ) {
public Collection < Db Location> listCompanyLocations ( Company company ) {
try {
try {
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( OWNER , equal ( - company . id ( ) ) ) . where ( PARENT_LOCATION_ID , isNull ( ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( OWNER , equal ( - company . id ( ) ) ) . where ( PARENT_LOCATION_ID , isNull ( ) ) . exec ( db ) ;
var list = new ArrayList < Location > ( ) ;
var list = new ArrayList < Db Location> ( ) ;
while ( rs . next ( ) ) list . add ( Location . of ( rs ) ) ;
while ( rs . next ( ) ) list . add ( Db Location. of ( rs ) ) ;
rs . close ( ) ;
rs . close ( ) ;
return list ;
return list ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
@ -221,41 +220,32 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
@Override
public Collection < Item > listItemsAt ( long locationId ) {
public Collection < Item > listItemsAt ( Location location ) {
try {
try {
var location = loadLocation ( locationId ) ;
var rs = select ( ALL ) . from ( TABLE_ITEMS ) . where ( LOCATION_ID , equal ( location . id ( ) ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_ITEMS ) . where ( LOCATION_ID , equal ( locationId ) ) . exec ( db ) ;
var list = new ArrayList < Item > ( ) ;
var list = new ArrayList < Item > ( ) ;
var ownerMap = new HashMap < Long , Mappable > ( ) ;
var ownerMap = new HashMap < Long , Mappable > ( ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) list . add ( Item . of ( rs ) ) ;
var ownerId = rs . getLong ( OWNER ) ;
var owner = ownerMap . get ( ownerId ) ;
if ( owner = = null ) {
owner = ownerId < 0 ? companyService ( ) . get ( - ownerId ) : userService ( ) . loadUser ( ownerId ) ;
ownerMap . put ( ownerId , owner ) ;
}
list . add ( Item . of ( rs , owner , location ) ) ;
}
rs . close ( ) ;
rs . close ( ) ;
for ( var item : list ) {
for ( var item : list ) {
var ownerId = item . ownerId ( ) ;
var ownerId = item . owner ( ) ;
rs = select ( ALL ) . from ( TABLE_ITEM_PROPERTIES ) . leftJoin ( PROPERTY_ID , TABLE_PROPERTIES , ID ) . where ( OWNER , equal ( ownerId ) ) . where ( ITEM_ID , equal ( item . id ( ) ) ) . exec ( db ) ;
rs = select ( ALL ) . from ( TABLE_ITEM_PROPERTIES ) . leftJoin ( PROPERTY_ID , TABLE_PROPERTIES , ID ) . where ( OWNER , equal ( ownerId ) ) . where ( ITEM_ID , equal ( item . id ( ) ) ) . exec ( db ) ;
while ( rs . next ( ) ) item . properties ( ) . add ( Property . of ( rs ) ) ;
while ( rs . next ( ) ) item . properties ( ) . add ( Property . of ( rs ) ) ;
rs . close ( ) ;
rs . close ( ) ;
}
}
return list ;
return list ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw databaseException ( "Failed to load items at {0}" , locationId ) ;
throw databaseException ( "Failed to load items at {0}" , location ) ;
}
}
}
}
@Override
@Override
public Item loadItem ( Mappable owner , long itemI d ) {
public Item loadItem ( long id ) {
try {
try {
var rs = select ( ALL ) . from ( TABLE_ITEMS ) . where ( OWNER , equal ( Item . ownerId ( owner ) ) ) . where ( ID , equal ( itemI d ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_ITEMS ) . where ( ID , equal ( id ) ) . exec ( db ) ;
Item result = null ;
Item result = null ;
if ( rs . next ( ) ) result = Item . of ( rs , owner , null ) ;
if ( rs . next ( ) ) result = Item . of ( rs ) ;
rs . close ( ) ;
rs . close ( ) ;
if ( result ! = null ) return result ;
if ( result ! = null ) return result ;
} catch ( SQLException ignored ) {
} catch ( SQLException ignored ) {
@ -263,11 +253,11 @@ public class SqliteDb extends BaseDb implements StockDb {
throw databaseException ( "Failed to load item" ) ;
throw databaseException ( "Failed to load item" ) ;
}
}
private Location loadLocation ( long locationId ) {
public Db Location loadLocation ( long locationId ) {
try {
try {
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( ID , equal ( locationId ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( ID , equal ( locationId ) ) . exec ( db ) ;
Location loc = null ;
Db Location loc = null ;
if ( rs . next ( ) ) loc = Location . of ( rs ) ;
if ( rs . next ( ) ) loc = Db Location. of ( rs ) ;
rs . close ( ) ;
rs . close ( ) ;
if ( loc ! = null ) return loc ;
if ( loc ! = null ) return loc ;
throw databaseException ( "Failed to load location with id = {0}" , locationId ) ;
throw databaseException ( "Failed to load location with id = {0}" , locationId ) ;
@ -290,11 +280,11 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
@Override
public Collection < Location > listUserLocations ( UmbrellaUser user ) {
public Collection < Db Location> listUserLocations ( UmbrellaUser user ) {
try {
try {
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( OWNER , equal ( user . id ( ) ) ) . where ( PARENT_LOCATION_ID , isNull ( ) ) . exec ( db ) ;
var rs = select ( ALL ) . from ( TABLE_LOCATIONS ) . where ( OWNER , equal ( user . id ( ) ) ) . where ( PARENT_LOCATION_ID , isNull ( ) ) . exec ( db ) ;
var list = new ArrayList < Location > ( ) ;
var list = new ArrayList < Db Location> ( ) ;
while ( rs . next ( ) ) list . add ( Location . of ( rs ) ) ;
while ( rs . next ( ) ) list . add ( Db Location. of ( rs ) ) ;
rs . close ( ) ;
rs . close ( ) ;
return list ;
return list ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
@ -324,7 +314,7 @@ public class SqliteDb extends BaseDb implements StockDb {
} else if ( item . isDirty ( ) ) {
} else if ( item . isDirty ( ) ) {
try {
try {
var location = item . location ( ) ;
var location = item . location ( ) ;
var query = update ( TABLE_ITEMS ) . where ( OWNER , equal ( item . ownerId ( ) ) ) . where ( ID , equal ( item . id ( ) ) ) ;
var query = update ( TABLE_ITEMS ) . where ( ID , equal ( item . id ( ) ) ) ;
if ( location = = null ) {
if ( location = = null ) {
query . set ( CODE , NAME ) ;
query . set ( CODE , NAME ) ;
} else {
} else {