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:
@@ -127,6 +127,7 @@ public class Constants {
|
|||||||
public static final String TAG_COLORS = "tag_colors";
|
public static final String TAG_COLORS = "tag_colors";
|
||||||
public static final String TASK_IDS = "task_ids";
|
public static final String TASK_IDS = "task_ids";
|
||||||
public static final String TAX = "tax";
|
public static final String TAX = "tax";
|
||||||
|
public static final String TAX_RATE = "tax_rate";
|
||||||
public static final String TEMPLATE = "template";
|
public static final String TEMPLATE = "template";
|
||||||
public static final String TEXT = "text";
|
public static final String TEXT = "text";
|
||||||
public static final String THOUSANDS_SEPARATOR = "thousands_separator";
|
public static final String THOUSANDS_SEPARATOR = "thousands_separator";
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package de.srsoftware.umbrella.core.model;
|
package de.srsoftware.umbrella.core.model;
|
||||||
|
|
||||||
import static de.srsoftware.umbrella.core.Constants.*;
|
import static de.srsoftware.umbrella.core.Constants.*;
|
||||||
|
import static de.srsoftware.umbrella.core.Util.mapMarkdown;
|
||||||
|
|
||||||
import de.srsoftware.tools.Mappable;
|
import de.srsoftware.tools.Mappable;
|
||||||
import de.srsoftware.umbrella.core.api.Owner;
|
import de.srsoftware.umbrella.core.api.Owner;
|
||||||
@@ -88,6 +89,10 @@ public class Item implements Mappable {
|
|||||||
return ownerNumber;
|
return ownerNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ownerNumber(long newVal) {
|
||||||
|
ownerNumber = newVal;
|
||||||
|
}
|
||||||
|
|
||||||
public Item patch(JSONObject json) {
|
public Item patch(JSONObject json) {
|
||||||
for (var field : json.keySet()){
|
for (var field : json.keySet()){
|
||||||
var known = true;
|
var known = true;
|
||||||
@@ -121,7 +126,7 @@ public class Item implements Mappable {
|
|||||||
map.put(LOCATION,location.toMap());
|
map.put(LOCATION,location.toMap());
|
||||||
map.put(CODE,code);
|
map.put(CODE,code);
|
||||||
map.put(NAME,name);
|
map.put(NAME,name);
|
||||||
map.put(DESCRIPTION,description);
|
map.put(DESCRIPTION,mapMarkdown(description));
|
||||||
map.put(OWNER_NUMBER,ownerNumber);
|
map.put(OWNER_NUMBER,ownerNumber);
|
||||||
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
||||||
return map;
|
return map;
|
||||||
|
|||||||
@@ -69,6 +69,9 @@
|
|||||||
{#if item}
|
{#if item}
|
||||||
<LineEditor type="h3" editable={true} value={item.name} onSet={v => patch('name',v)} />
|
<LineEditor type="h3" editable={true} value={item.name} onSet={v => patch('name',v)} />
|
||||||
Code: <LineEditor type="span" editable={true} value={item.code} onSet={v => patch('code',v)} />
|
Code: <LineEditor type="span" editable={true} value={item.code} onSet={v => patch('code',v)} />
|
||||||
|
<div>
|
||||||
|
{@html item.description.rendered}
|
||||||
|
</div>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
{#each item.properties.toSorted(byName) as prop}
|
{#each item.properties.toSorted(byName) as prop}
|
||||||
|
|||||||
@@ -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.Constants.*;
|
||||||
import static de.srsoftware.umbrella.core.Field.COMPANY_ID;
|
import static de.srsoftware.umbrella.core.Field.COMPANY_ID;
|
||||||
import static de.srsoftware.umbrella.core.Field.UNIT_PRICE;
|
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.core.exceptions.UmbrellaException.databaseException;
|
||||||
import static de.srsoftware.umbrella.stock.Constants.TABLE_ITEMS;
|
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.ModuleRegistry;
|
||||||
import de.srsoftware.umbrella.core.model.DbLocation;
|
import de.srsoftware.umbrella.core.model.*;
|
||||||
import de.srsoftware.umbrella.core.model.Item;
|
|
||||||
import de.srsoftware.umbrella.core.model.Location;
|
|
||||||
import de.srsoftware.umbrella.core.model.Property;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ItemDb {
|
public class ItemDb {
|
||||||
@@ -30,6 +32,7 @@ public class ItemDb {
|
|||||||
public void migrateTo(StockDb stockDb) {
|
public void migrateTo(StockDb stockDb) {
|
||||||
try {
|
try {
|
||||||
var companyLocations = new HashMap<Long,Location>();
|
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);
|
var rs = select(ALL).from(TABLE_ITEMS).exec(db);
|
||||||
while (rs.next()){
|
while (rs.next()){
|
||||||
var id = rs.getLong(ID);
|
var id = rs.getLong(ID);
|
||||||
@@ -40,18 +43,38 @@ public class ItemDb {
|
|||||||
var unit = rs.getString(UNIT);
|
var unit = rs.getString(UNIT);
|
||||||
var unitPrice = rs.getLong(UNIT_PRICE);
|
var unitPrice = rs.getLong(UNIT_PRICE);
|
||||||
var tax = rs.getLong(TAX);
|
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);
|
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));
|
location = stockDb.save(new DbLocation(0,company,null,"virtual items",null));
|
||||||
companyLocations.put(companyId,location);
|
companyLocations.put(companyId,location);
|
||||||
}
|
}
|
||||||
var stockItem = new Item(0,company,0,location,code,name,description);
|
var stockItem = new Item(0,company,0,location,code,name,description);
|
||||||
var props = stockItem.properties(); // TODO: saving props currently does not work
|
var props = stockItem.properties();
|
||||||
props.add(new Property(0,UNIT_PRICE,unitPrice/100d,company.currency()));
|
var keyUnitPrice = translator().translate(lang,UNIT_PRICE);
|
||||||
props.add(new Property(0,UNIT,unit,null));
|
var keyUnit = translator().translate(lang,UNIT);
|
||||||
props.add(new Property(0,TAX,tax,"%"));
|
var keyTax = translator().translate(lang,TAX_RATE);
|
||||||
props.add(new Property(0,"legacy_id",id,null));
|
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);
|
stockDb.save(stockItem);
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ public class SqliteDb extends BaseDb implements StockDb {
|
|||||||
rs.close();
|
rs.close();
|
||||||
if (propertyId == null || propertyId == 0) throw databaseException("Failed to create new property {0} in DB",name);
|
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();
|
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);
|
return new Property(propertyId,name,value,unit);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw databaseException("Failed to create new property {0} in DB",name);
|
throw databaseException("Failed to create new property {0} in DB",name);
|
||||||
@@ -405,11 +406,12 @@ public class SqliteDb extends BaseDb implements StockDb {
|
|||||||
@Override
|
@Override
|
||||||
public Item save(Item item) {
|
public Item save(Item item) {
|
||||||
if (item.id() == 0){
|
if (item.id() == 0){
|
||||||
|
var number = nextItemNumberFor(item.location().resolve().owner());
|
||||||
try {
|
try {
|
||||||
var rs = insertInto(TABLE_ITEMS, OWNER, OWNER_NUMBER, CODE, NAME, LOCATION_ID)
|
var rs = insertInto(TABLE_ITEMS, OWNER, OWNER_NUMBER, CODE, NAME, DESCRIPTION, LOCATION_ID)
|
||||||
.values(item.owner().dbCode(), item.ownerNumber(), item.code(), item.name(), item.location().id())
|
.values(item.owner().dbCode(), number, item.code(), item.name(), item.description(), item.location().id())
|
||||||
.execute(db).getGeneratedKeys();
|
.execute(db).getGeneratedKeys();
|
||||||
if (rs.next()) item.id(rs.getLong(1));
|
if (rs.next()) item.id(rs.getLong(1)).ownerNumber(number);
|
||||||
rs.close();
|
rs.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw databaseException("Failed to save new item to database!");
|
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 location = item.location();
|
||||||
var query = update(TABLE_ITEMS).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,DESCRIPTION);
|
||||||
} else {
|
} else {
|
||||||
query.set(CODE,NAME,LOCATION_ID);
|
query.set(CODE,NAME,DESCRIPTION,LOCATION_ID);
|
||||||
}
|
}
|
||||||
var pq = query.prepare(db);
|
var pq = query.prepare(db);
|
||||||
if (location == null) {
|
if (location == null) {
|
||||||
pq.apply(item.code(),item.name());
|
pq.apply(item.code(),item.name(),item.description()).close();
|
||||||
} else {
|
} else {
|
||||||
pq.apply(item.code(),item.name(),item.location().id());
|
pq.apply(item.code(),item.name(),item.description(),item.location().id()).close();
|
||||||
}
|
}
|
||||||
item.clear();
|
item.clear();
|
||||||
} catch (SQLException e){
|
} catch (SQLException e){
|
||||||
|
|||||||
@@ -324,8 +324,7 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
var location = stockDb.loadLocation(locationData.getLong(ID));
|
var location = stockDb.loadLocation(locationData.getLong(ID));
|
||||||
var owner = location.owner().resolve();
|
var owner = location.owner().resolve();
|
||||||
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to {0}!",location);
|
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,0,location,code,name,description);
|
||||||
var newItem = new Item(0,owner,number,location,code,name,description);
|
|
||||||
return sendContent(ex,stockDb.save(newItem));
|
return sendContent(ex,stockDb.save(newItem));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user