finished implementation of migration from legacy item db to stock db
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -7,16 +7,18 @@ import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Field.COMPANY_ID;
|
||||
import static de.srsoftware.umbrella.core.Field.UNIT_PRICE;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.translator;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.stock.Constants.TABLE_ITEMS;
|
||||
|
||||
import de.srsoftware.tools.Tuple;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.model.DbLocation;
|
||||
import de.srsoftware.umbrella.core.model.Item;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import de.srsoftware.umbrella.core.model.Property;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ItemDb {
|
||||
@@ -30,6 +32,7 @@ public class ItemDb {
|
||||
public void migrateTo(StockDb stockDb) {
|
||||
try {
|
||||
var companyLocations = new HashMap<Long,Location>();
|
||||
var companyInfo = new HashMap<Long, Tuple<Company,String>>(); // map from companyId → (company, language)
|
||||
var rs = select(ALL).from(TABLE_ITEMS).exec(db);
|
||||
while (rs.next()){
|
||||
var id = rs.getLong(ID);
|
||||
@@ -40,18 +43,38 @@ public class ItemDb {
|
||||
var unit = rs.getString(UNIT);
|
||||
var unitPrice = rs.getLong(UNIT_PRICE);
|
||||
var tax = rs.getLong(TAX);
|
||||
var company = ModuleRegistry.companyService().get(companyId);
|
||||
var tuple = companyInfo.get(companyId);
|
||||
String lang = null;
|
||||
Company company;
|
||||
if (tuple == null){
|
||||
company = companyService().get(companyId);
|
||||
for (var member : companyService().getMembers(companyId)){
|
||||
lang = member.language();
|
||||
if (lang != null){
|
||||
tuple = Tuple.of(company,lang);
|
||||
companyInfo.put(companyId,tuple);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
company = tuple.a;
|
||||
lang = tuple.b;
|
||||
}
|
||||
var location = companyLocations.get(companyId);
|
||||
if (location == null) { // TODO: ids currently do not get assigned
|
||||
if (location == null) {
|
||||
location = stockDb.save(new DbLocation(0,company,null,"virtual items",null));
|
||||
companyLocations.put(companyId,location);
|
||||
}
|
||||
var stockItem = new Item(0,company,0,location,code,name,description);
|
||||
var props = stockItem.properties(); // TODO: saving props currently does not work
|
||||
props.add(new Property(0,UNIT_PRICE,unitPrice/100d,company.currency()));
|
||||
props.add(new Property(0,UNIT,unit,null));
|
||||
props.add(new Property(0,TAX,tax,"%"));
|
||||
props.add(new Property(0,"legacy_id",id,null));
|
||||
var props = stockItem.properties();
|
||||
var keyUnitPrice = translator().translate(lang,UNIT_PRICE);
|
||||
var keyUnit = translator().translate(lang,UNIT);
|
||||
var keyTax = translator().translate(lang,TAX_RATE);
|
||||
var keyLegacyId = translator().translate(lang,"legacy_id");
|
||||
props.add(new Property(0,keyUnitPrice,unitPrice/100d,company.currency()));
|
||||
props.add(new Property(0,keyUnit,unit,null));
|
||||
props.add(new Property(0,keyTax,tax,"%"));
|
||||
props.add(new Property(0,keyLegacyId,id,null));
|
||||
stockDb.save(stockItem);
|
||||
}
|
||||
rs.close();
|
||||
|
||||
@@ -52,6 +52,7 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
rs.close();
|
||||
if (propertyId == null || propertyId == 0) throw databaseException("Failed to create new property {0} in DB",name);
|
||||
insertInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,propertyId,value).execute(db).close();
|
||||
db.setAutoCommit(true);
|
||||
return new Property(propertyId,name,value,unit);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to create new property {0} in DB",name);
|
||||
@@ -405,11 +406,12 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
@Override
|
||||
public Item save(Item item) {
|
||||
if (item.id() == 0){
|
||||
var number = nextItemNumberFor(item.location().resolve().owner());
|
||||
try {
|
||||
var rs = insertInto(TABLE_ITEMS, OWNER, OWNER_NUMBER, CODE, NAME, LOCATION_ID)
|
||||
.values(item.owner().dbCode(), item.ownerNumber(), item.code(), item.name(), item.location().id())
|
||||
var rs = insertInto(TABLE_ITEMS, OWNER, OWNER_NUMBER, CODE, NAME, DESCRIPTION, LOCATION_ID)
|
||||
.values(item.owner().dbCode(), number, item.code(), item.name(), item.description(), item.location().id())
|
||||
.execute(db).getGeneratedKeys();
|
||||
if (rs.next()) item.id(rs.getLong(1));
|
||||
if (rs.next()) item.id(rs.getLong(1)).ownerNumber(number);
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to save new item to database!");
|
||||
@@ -419,15 +421,15 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
var location = item.location();
|
||||
var query = update(TABLE_ITEMS).where(ID, equal(item.id()));
|
||||
if (location == null) {
|
||||
query.set(CODE,NAME);
|
||||
query.set(CODE,NAME,DESCRIPTION);
|
||||
} else {
|
||||
query.set(CODE,NAME,LOCATION_ID);
|
||||
query.set(CODE,NAME,DESCRIPTION,LOCATION_ID);
|
||||
}
|
||||
var pq = query.prepare(db);
|
||||
if (location == null) {
|
||||
pq.apply(item.code(),item.name());
|
||||
pq.apply(item.code(),item.name(),item.description()).close();
|
||||
} else {
|
||||
pq.apply(item.code(),item.name(),item.location().id());
|
||||
pq.apply(item.code(),item.name(),item.description(),item.location().id()).close();
|
||||
}
|
||||
item.clear();
|
||||
} catch (SQLException e){
|
||||
|
||||
@@ -324,8 +324,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
var location = stockDb.loadLocation(locationData.getLong(ID));
|
||||
var owner = location.owner().resolve();
|
||||
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to {0}!",location);
|
||||
var number = stockDb.nextItemNumberFor(owner);
|
||||
var newItem = new Item(0,owner,number,location,code,name,description);
|
||||
var newItem = new Item(0,owner,0,location,code,name,description);
|
||||
return sendContent(ex,stockDb.save(newItem));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user