@ -7,6 +7,8 @@ import static de.srsoftware.tools.jdbc.Condition.isNull;
@@ -7,6 +7,8 @@ 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 ;
@ -15,12 +17,7 @@ import static java.text.MessageFormat.format;
@@ -15,12 +17,7 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable ;
import de.srsoftware.umbrella.core.BaseDb ;
import de.srsoftware.umbrella.core.ModuleRegistry ;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException ;
import de.srsoftware.umbrella.core.model.Company ;
import de.srsoftware.umbrella.core.model.Item ;
import de.srsoftware.umbrella.core.model.Location ;
import de.srsoftware.umbrella.core.model.UmbrellaUser ;
import de.srsoftware.umbrella.core.model.* ;
import java.sql.Connection ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
@ -53,23 +50,6 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -53,23 +50,6 @@ public class SqliteDb extends BaseDb implements StockDb {
super ( connection ) ;
}
@Override
protected int createTables ( ) {
int currentVersion = createSettingsTable ( ) ;
switch ( currentVersion ) {
case 0 :
createLocationsTable ( ) ;
createItemsTable ( ) ;
createPropertiesTable ( ) ;
createItemPropsTable ( ) ;
case 1 :
dropTokenTable ( ) ;
case 2 :
transformTables ( ) ;
}
return setCurrentVersion ( 3 ) ;
}
private void createIntermediateItemsTable ( ) throws SQLException { // create intermediate table
var sql = "CREATE TABLE IF NOT EXISTS items_temp ({0} LONG NOT NULL, {1} LONG NOT NULL, {2} VARCHAR(255), {3} VARCHAR(255) NOT NULL, {4} LONG NOT NULL, PRIMARY KEY({0}, {1}))" ;
sql = format ( sql , OWNER , ID , CODE , NAME , LOCATION_ID ) ;
@ -128,6 +108,23 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -128,6 +108,23 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
protected int createTables ( ) {
int currentVersion = createSettingsTable ( ) ;
switch ( currentVersion ) {
case 0 :
createLocationsTable ( ) ;
createItemsTable ( ) ;
createPropertiesTable ( ) ;
createItemPropsTable ( ) ;
case 1 :
dropTokenTable ( ) ;
case 2 :
transformTables ( ) ;
}
return setCurrentVersion ( 3 ) ;
}
private void dropTokenTable ( ) {
try {
db . prepareStatement ( "DROP TABLE IF EXISTS tokens" ) . execute ( ) ;
@ -137,23 +134,57 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -137,23 +134,57 @@ public class SqliteDb extends BaseDb implements StockDb {
}
@Override
public Collection < Item > listItems ( long companyId ) throws UmbrellaException {
return List . of ( ) ;
public Collection < 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 ) ) ;
rs . close ( ) ;
return list ;
} catch ( SQLException e ) {
throw databaseException ( "Failed to load child locations for {0}" , parentId ) ;
}
}
@Override
public Collection < 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 ) ) ;
rs . close ( ) ;
return list ;
} catch ( SQLException e ) {
throw databaseException ( "Failed to load locations for user {0}" , company . name ( ) ) ;
}
}
@Override
public Collection < Item > listItemsAt ( long locationId ) {
try {
var location = loadLocation ( locationId ) ;
var rs = select ( ALL ) . from ( TABLE_ITEMS ) . where ( LOCATION_ID , equal ( locationId ) ) . exec ( db ) ;
var list = new ArrayList < Item > ( ) ;
var ownerMap = new HashMap < Long , Mappable > ( ) ;
while ( rs . next ( ) ) {
var ownerId = rs . getLong ( OWNER ) ;
Mappable owner = ownerId < 0 ? ModuleRegistry . companyService ( ) . get ( - ownerId ) : ModuleRegistry . userService ( ) . loadUser ( ownerId ) ;
var location = loadLocation ( rs . getLong ( LOCATION_ID ) ) ;
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 ( ) ;
for ( var item : list ) {
var owner = item . owner ( ) ;
var ownerId = owner instanceof Company comp ? - comp . id ( ) : ( owner instanceof UmbrellaUser u ? u . id ( ) : 0 ) ;
if ( ownerId = = 0 ) throw databaseException ( "Encountered unknown item owner of type {0}" , owner . getClass ( ) . getSimpleName ( ) ) ;
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 ) ;
@ -173,37 +204,6 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -173,37 +204,6 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public Collection < Location > listLocations ( long companyId ) {
return List . of ( ) ;
}
@Override
public Collection < 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 ) ) ;
rs . close ( ) ;
return list ;
} catch ( SQLException e ) {
throw databaseException ( "Failed to load child locations for {0}" , parentId ) ;
}
}
@Override
public Collection < 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 ) ) ;
rs . close ( ) ;
return list ;
} catch ( SQLException e ) {
throw databaseException ( "Failed to load locations for user {0}" , company . name ( ) ) ;
}
}
@Override
public Collection < Location > listUserLocations ( UmbrellaUser user ) {
try {
@ -217,29 +217,6 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -217,29 +217,6 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
private void transformTables ( ) {
try {
db . setAutoCommit ( false ) ;
createIntermediateLocationTable ( ) ;
createIntermediateItemsTable ( ) ;
createIntermediatePropsTable ( ) ;
var oldLocationIdsToNew = transformLocations ( ) ;
transformItems ( oldLocationIdsToNew ) ;
transformProperties ( ) ;
replaceLocationsTable ( ) ;
replaceItemsTable ( ) ;
replaceItemPropsTable ( ) ;
db . setAutoCommit ( true ) ;
} catch ( Exception e ) {
try {
db . rollback ( ) ;
} catch ( SQLException ignored ) {
}
LOG . log ( ERROR , "Failed to transform {0} table!" , TABLE_LOCATIONS , e ) ;
throw databaseException ( "Failed to transform {0} table!" , TABLE_LOCATIONS ) ;
}
}
private void replaceItemsTable ( ) throws SQLException {
db . prepareStatement ( format ( "DROP TABLE {0}" , TABLE_ITEMS ) ) . execute ( ) ;
db . prepareStatement ( format ( "ALTER TABLE {0} RENAME TO {1}" , "items_temp" , TABLE_ITEMS ) ) . execute ( ) ;
@ -335,4 +312,27 @@ public class SqliteDb extends BaseDb implements StockDb {
@@ -335,4 +312,27 @@ public class SqliteDb extends BaseDb implements StockDb {
rs . close ( ) ;
insert . execute ( db ) . close ( ) ;
}
private void transformTables ( ) {
try {
db . setAutoCommit ( false ) ;
createIntermediateLocationTable ( ) ;
createIntermediateItemsTable ( ) ;
createIntermediatePropsTable ( ) ;
var oldLocationIdsToNew = transformLocations ( ) ;
transformItems ( oldLocationIdsToNew ) ;
transformProperties ( ) ;
replaceLocationsTable ( ) ;
replaceItemsTable ( ) ;
replaceItemPropsTable ( ) ;
db . setAutoCommit ( true ) ;
} catch ( Exception e ) {
try {
db . rollback ( ) ;
} catch ( SQLException ignored ) {
}
LOG . log ( ERROR , "Failed to transform {0} table!" , TABLE_LOCATIONS , e ) ;
throw databaseException ( "Failed to transform {0} table!" , TABLE_LOCATIONS ) ;
}
}
}