refactored item module → stock, added legacy database creation methods
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
public class Constants {
|
||||
private Constants(){}
|
||||
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM_ID = "item_id"; public static final String LOCATION_ID = "location_id";
|
||||
public static final String PROPERTY_ID = "prop_id";
|
||||
public static final String TABLE_ITEMS = "items";
|
||||
public static final String TABLE_ITEM_PROPERTIES = "item_props";
|
||||
public static final String TABLE_LOCATIONS = "locations";
|
||||
public static final String TABLE_PROPERTIES = "properties";
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.stock.Constants.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Item;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class SqliteDb extends BaseDb implements StockDb {
|
||||
public SqliteDb(Connection connection) {
|
||||
super(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int createTables() {
|
||||
int currentVersion = createSettingsTable();
|
||||
switch (currentVersion){
|
||||
case 0:
|
||||
createLocationsTable();
|
||||
createItemsTable();
|
||||
createPropertiesTable();
|
||||
createItemPropsTable();
|
||||
}
|
||||
return setCurrentVersion(1);
|
||||
}
|
||||
|
||||
private void createItemsTable() {
|
||||
try {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL, {3} TEXT, {4} VARCHAR(255))";
|
||||
sql = format(sql, TABLE_ITEMS, ID, CODE, NAME, LOCATION_ID);
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_ITEMS);
|
||||
}
|
||||
}
|
||||
|
||||
private void createItemPropsTable() {
|
||||
try {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ( {1} INT NOT NULL, {2} INT NOT NULL, {3} VARCHAR(255) NOT NULL, PRIMARY KEY({1}, {2}))";
|
||||
sql = format(sql, TABLE_ITEM_PROPERTIES, ITEM_ID, PROPERTY_ID,VALUE);
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_ITEM_PROPERTIES);
|
||||
}
|
||||
}
|
||||
|
||||
private void createLocationsTable() {
|
||||
try {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) DEFAULT NULL, {3} VARCHAR(255) NOT NULL, {4} TEXT)";
|
||||
sql = format(sql, TABLE_LOCATIONS, ID, LOCATION_ID, NAME, DESCRIPTION);
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_LOCATIONS);
|
||||
}
|
||||
}
|
||||
|
||||
private void createPropertiesTable() {
|
||||
try {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ( {1} LONG PRIMARY KEY, {2} VARCHAR(255) NOT NULL, {3} INT NOT NULL, {4} VARCHAR(255))";
|
||||
sql = format(sql, TABLE_PROPERTIES, ID, NAME, TYPE, UNIT);
|
||||
db.prepareStatement(sql).execute();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_PROPERTIES);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Item> listItems(long companyId) throws UmbrellaException {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Location> listLocations(long companyId) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.stock.Constants.CONFIG_DATABASE;
|
||||
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.api.StockService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class StockApi extends BaseHandler implements StockService {
|
||||
|
||||
private final StockDb stockDb;
|
||||
|
||||
public StockApi(Configuration config) throws UmbrellaException {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
stockDb = new SqliteDb(connect(dbFile));
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> redefineMe(long company_id) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Item;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface StockDb {
|
||||
Collection<Item> listItems(long companyId) throws UmbrellaException;
|
||||
Collection<Location> listLocations(long companyId);
|
||||
}
|
||||
Reference in New Issue
Block a user