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