working on location tree
This commit is contained in:
@@ -133,7 +133,9 @@ public class Constants {
|
||||
|
||||
public static final String OFFSET = "offset";
|
||||
public static final String OPTIONAL = "optional";
|
||||
public static final String OWNER = "owner";
|
||||
|
||||
public static final String PARENT_LOCATION_ID = "parent_location_id";
|
||||
public static final String PARENT_TASK_ID = "parent_task_id";
|
||||
public static final String PASS = "pass";
|
||||
public static final String PASSWORD = "password";
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import de.srsoftware.tools.Mappable;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
|
||||
public class Location {
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Location implements Mappable {
|
||||
private long owner;
|
||||
private boolean ownerIsCompany = false;
|
||||
private long id;
|
||||
@@ -37,8 +40,12 @@ public class Location {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Location of(ResultSet rs){
|
||||
return null;
|
||||
public static Location of(ResultSet rs) throws SQLException {
|
||||
var owner = rs.getLong(OWNER);
|
||||
var id = rs.getLong(ID);
|
||||
var isCompany = owner < 0;
|
||||
if (isCompany) owner = -owner;
|
||||
return new Location(owner,isCompany,id, rs.getLong(PARENT_LOCATION_ID), rs.getString(NAME),rs.getString(DESCRIPTION));
|
||||
}
|
||||
|
||||
|
||||
@@ -50,4 +57,13 @@ public class Location {
|
||||
public Long parent(){
|
||||
return parentLocationId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(
|
||||
ownerIsCompany ? COMPANY : USER,owner,
|
||||
ID,id,
|
||||
NAME,name,
|
||||
DESCRIPTION,description);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,26 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { api } from '../../urls.svelte';
|
||||
import { error, yikes } from '../../warn.svelte';
|
||||
import { t } from '../../translations.svelte';
|
||||
|
||||
import Locations from './Locations.svelte';
|
||||
import ItemList from './ItemList.svelte';
|
||||
import ItemProps from './ItemProps.svelte';
|
||||
|
||||
import { t } from '../../translations.svelte';
|
||||
let companies = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'SRSoftware',
|
||||
locations:{
|
||||
Langenorla:{
|
||||
Hauptgebäude:{
|
||||
Obergeschoss:{
|
||||
},
|
||||
Untergeschoss:{
|
||||
Einliegerwohnung:{
|
||||
Bad:{
|
||||
},
|
||||
Küche:{
|
||||
},
|
||||
Wohnzimmer:{
|
||||
}
|
||||
},
|
||||
Flur:{
|
||||
},
|
||||
Küche:{
|
||||
},
|
||||
Studio:{
|
||||
}
|
||||
},
|
||||
Werkstatt:{
|
||||
}
|
||||
},
|
||||
Nebengebäude:{
|
||||
},
|
||||
Grundstück:{
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{ id: 2, name: 'Alrauna', locations: {} }
|
||||
]
|
||||
let top_level = $state(null);
|
||||
|
||||
async function load(){
|
||||
const url = api('stock/locations/of_user')
|
||||
const res = await fetch(url,{credentials:'include'});
|
||||
if (res.ok){
|
||||
top_level = await res.json();
|
||||
yikes();
|
||||
} else error(res);
|
||||
}
|
||||
|
||||
onMount(load);
|
||||
</script>
|
||||
|
||||
<h2>{t('Stock')}</h2>
|
||||
@@ -49,11 +28,14 @@
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="locations">
|
||||
{#each companies as company}
|
||||
<h3>{company.name}</h3>
|
||||
|
||||
<Locations locations={company.locations} />
|
||||
{#if top_level}
|
||||
{#each top_level as realm,idx}
|
||||
<h3>{realm.name}</h3>
|
||||
{#if realm.locations}
|
||||
<Locations locations={realm.locations} />
|
||||
{/if}
|
||||
{/each}
|
||||
{/if}
|
||||
</td>
|
||||
<td class="items">
|
||||
<ItemList />
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
import { t } from '../../translations.svelte';
|
||||
|
||||
let { locations } = $props();
|
||||
|
||||
console.log(locations);
|
||||
</script>
|
||||
|
||||
<ul>
|
||||
@@ -10,10 +12,12 @@
|
||||
<span class="symbol"></span> {t('add_object',{object:'location'})}
|
||||
</a>
|
||||
</li>
|
||||
{#each Object.entries(locations) as [k,v]}
|
||||
{#each locations as location}
|
||||
<li>
|
||||
{k}
|
||||
<svelte:self locations={v} />
|
||||
{location.name}
|
||||
{#if location.locations}
|
||||
<svelte:self locations={location.locations} />
|
||||
{/if}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
@@ -14,7 +14,7 @@ import static de.srsoftware.umbrella.core.model.Status.PREDEFINED;
|
||||
import static de.srsoftware.umbrella.project.Constants.CONFIG_DATABASE;
|
||||
import static java.lang.Boolean.TRUE;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
import static de.srsoftware.umbrella.core.model.Permission.OWNER;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
public class Constants {
|
||||
public static final String PARENT_LOCATION_ID = "parent_location_id";
|
||||
|
||||
private Constants(){}
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM_ID = "item_id";
|
||||
public static final String OWNER = "owner";
|
||||
public static final String LOCATIONS = "locations";
|
||||
public static final String OF_USER = "of_user";
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.nullIfEmpty;
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
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.*;
|
||||
@@ -11,11 +13,13 @@ import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.Tuple;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
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 java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -61,7 +65,6 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
dropTokenTable();
|
||||
case 2:
|
||||
transformTables();
|
||||
replaceLocationsTable();
|
||||
}
|
||||
return setCurrentVersion(3);
|
||||
}
|
||||
@@ -78,6 +81,12 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
|
||||
private void createIntermediatePropsTable() throws SQLException { // create intermediate table
|
||||
var sql = "CREATE TABLE IF NOT EXISTS item_props_temp ( {0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG NOT NULL, {3} VARCHAR(255) NOT NULL, PRIMARY KEY({0}, {1}, {2}))";
|
||||
sql = format(sql, OWNER, ITEM_ID, PROPERTY_ID, VALUE);
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
|
||||
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))";
|
||||
@@ -136,16 +145,44 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@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 {
|
||||
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));
|
||||
rs.close();
|
||||
return list;
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to load locations for user {0}",user.name());
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -157,15 +194,19 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceLocationsTable() {
|
||||
try {
|
||||
db.setAutoCommit(false);
|
||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_LOCATIONS)).execute();
|
||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","locations_temp",TABLE_LOCATIONS)).execute();
|
||||
db.setAutoCommit(true);
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to replace locations table!");
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
private void replaceItemPropsTable() throws SQLException {
|
||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_ITEM_PROPERTIES)).execute();
|
||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","item_props_temp",TABLE_ITEM_PROPERTIES)).execute();
|
||||
}
|
||||
|
||||
private void replaceLocationsTable() throws SQLException {
|
||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_LOCATIONS)).execute();
|
||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","locations_temp",TABLE_LOCATIONS)).execute();
|
||||
}
|
||||
|
||||
private void transformItems(Map<String, Long> oldLocationIdsToNew) throws SQLException {
|
||||
@@ -224,4 +265,28 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
return oldToNew;
|
||||
}
|
||||
|
||||
private void transformProperties() throws SQLException {
|
||||
var rs = select(ALL).from(TABLE_ITEM_PROPERTIES).exec(db);
|
||||
var insert = insertInto("item_props_temp",OWNER, ITEM_ID, PROPERTY_ID, VALUE);
|
||||
while (rs.next()){
|
||||
var oldItemId = rs.getString(ITEM_ID);
|
||||
var parts = oldItemId.split(":");
|
||||
var owner = 0L;
|
||||
var itemId = 0L;
|
||||
try {
|
||||
owner = Long.parseLong(parts[1]);
|
||||
itemId = Long.parseLong(parts[2]);
|
||||
} catch (NumberFormatException e){
|
||||
throw databaseException("Expected item id to be of format ss:dd:dd, but encountered \"{0}\"",oldItemId);
|
||||
}
|
||||
var ownerIsCompany = switch (parts[0]){
|
||||
case "company" -> true;
|
||||
case "user" -> false;
|
||||
case null, default -> throw databaseException("Expected item id to start with 'company:' or 'user:', encountered \"{0}\"",oldItemId);
|
||||
};
|
||||
insert.values(ownerIsCompany?-owner:owner, itemId, rs.getLong(PROPERTY_ID), rs.getString(VALUE));
|
||||
}
|
||||
rs.close();
|
||||
insert.execute(db).close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,27 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.ID;
|
||||
import static de.srsoftware.umbrella.core.Constants.NAME;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.stock.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.stock.Constants.*;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
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;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class StockApi extends BaseHandler implements StockService {
|
||||
|
||||
@@ -24,6 +35,51 @@ public class StockApi extends BaseHandler implements StockService {
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LOCATIONS -> getLocations(path,user.get(),ex);
|
||||
case null, default -> doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getLocations(Path path, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case OF_USER -> getUserLocations(user,ex);
|
||||
case null, default -> super.doGet(path,ex);
|
||||
};
|
||||
}
|
||||
|
||||
private boolean getUserLocations(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var result = new ArrayList<Object>();
|
||||
var userLocations = stockDb.listUserLocations(user);
|
||||
result.add(Map.of(
|
||||
ID, user.id(),
|
||||
NAME,user.name(),
|
||||
LOCATIONS,userLocations.stream().map(Location::toMap).toList()));
|
||||
|
||||
var companies = companyService().listCompaniesOf(user);
|
||||
companies.values().stream().sorted(Comparator.comparing(a -> a.name().toLowerCase())).forEach(company -> {
|
||||
var locations = stockDb.listCompanyLocations(company);
|
||||
result.add(Map.of(
|
||||
ID, company.id(),
|
||||
NAME,company.name(),
|
||||
LOCATIONS,locations.stream().sorted(Comparator.comparing(a -> a.name().toLowerCase())).map(Location::toMap).toList()));
|
||||
|
||||
});
|
||||
return sendContent(ex, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> redefineMe(long company_id) {
|
||||
return List.of();
|
||||
|
||||
@@ -2,11 +2,18 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
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 java.util.Collection;
|
||||
|
||||
public interface StockDb {
|
||||
Collection<Item> listItems(long companyId) throws UmbrellaException;
|
||||
Collection<Location> listLocations(long companyId);
|
||||
|
||||
Collection<Location> listUserLocations(UmbrellaUser userId);
|
||||
|
||||
Collection<Location> listCompanyLocations(Company company);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package de.srsoftware.umbrella.task;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.is0;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.OWNER;
|
||||
import static de.srsoftware.tools.Optionals.isSet;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
Reference in New Issue
Block a user