finished location tree
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Location implements Mappable {
|
||||
|
||||
@@ -1,9 +1,25 @@
|
||||
<script>
|
||||
import { api } from '../../urls.svelte';
|
||||
import { error, yikes } from '../../warn.svelte';
|
||||
import { t } from '../../translations.svelte';
|
||||
|
||||
let { locations } = $props();
|
||||
|
||||
console.log(locations);
|
||||
async function toggleChildren(ev, location){
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
if (location.locations) {
|
||||
delete location.locations;
|
||||
} else {
|
||||
const url = api(`stock/locations/below/${location.id}`);
|
||||
const res = await fetch(url,{credentials:'include'});
|
||||
if (res.ok){
|
||||
yikes();
|
||||
location.locations = await res.json();
|
||||
} else error(res);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul>
|
||||
@@ -13,7 +29,7 @@
|
||||
</a>
|
||||
</li>
|
||||
{#each locations as location}
|
||||
<li>
|
||||
<li onclick={e => toggleChildren(e, location)} class={location.locations?'expanded':'collapsed'}>
|
||||
{location.name}
|
||||
{#if location.locations}
|
||||
<svelte:self locations={location.locations} />
|
||||
|
||||
@@ -9,12 +9,13 @@ import static de.srsoftware.umbrella.core.Paths.SEARCH;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.OWNER;
|
||||
import static de.srsoftware.umbrella.core.model.Status.OPEN;
|
||||
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;
|
||||
|
||||
@@ -5,6 +5,7 @@ public class Constants {
|
||||
|
||||
private Constants(){}
|
||||
|
||||
public static final String BELOW = "below";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM_ID = "item_id";
|
||||
public static final String LOCATIONS = "locations";
|
||||
|
||||
@@ -19,7 +19,6 @@ 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;
|
||||
@@ -145,6 +144,19 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
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 {
|
||||
|
||||
@@ -8,6 +8,8 @@ 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.*;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
@@ -20,7 +22,6 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
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.*;
|
||||
|
||||
@@ -55,11 +56,26 @@ public class StockApi extends BaseHandler implements StockService {
|
||||
private boolean getLocations(Path path, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case BELOW -> {
|
||||
try {
|
||||
var id = Long.parseLong(path.pop());
|
||||
yield getChildLocations(user, id, ex);
|
||||
} catch (Exception e){
|
||||
yield super.doGet(path,ex);
|
||||
}
|
||||
|
||||
}
|
||||
case OF_USER -> getUserLocations(user,ex);
|
||||
case null, default -> super.doGet(path,ex);
|
||||
};
|
||||
}
|
||||
|
||||
private boolean getChildLocations(UmbrellaUser user, long parentId, HttpExchange ex) throws IOException {
|
||||
LOG.log(WARNING,"No security check implemented for {0}.getChildLocations(user, parentId, ex)!",getClass().getSimpleName()); // TODO check, that user is allowed to request that location
|
||||
return sendContent(ex, stockDb.listChildLocations(parentId).stream().sorted(comparing(l -> l.name().toLowerCase())).map(Location::toMap));
|
||||
|
||||
}
|
||||
|
||||
private boolean getUserLocations(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var result = new ArrayList<Object>();
|
||||
var userLocations = stockDb.listUserLocations(user);
|
||||
@@ -69,12 +85,12 @@ public class StockApi extends BaseHandler implements StockService {
|
||||
LOCATIONS,userLocations.stream().map(Location::toMap).toList()));
|
||||
|
||||
var companies = companyService().listCompaniesOf(user);
|
||||
companies.values().stream().sorted(Comparator.comparing(a -> a.name().toLowerCase())).forEach(company -> {
|
||||
companies.values().stream().sorted(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()));
|
||||
LOCATIONS,locations.stream().sorted(comparing(a -> a.name().toLowerCase())).map(Location::toMap).toList()));
|
||||
|
||||
});
|
||||
return sendContent(ex, result);
|
||||
|
||||
@@ -6,14 +6,15 @@ 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<Location> listChildLocations(long parentId);
|
||||
Collection<Location> listCompanyLocations(Company company);
|
||||
|
||||
Collection<Item> listItems(long companyId) throws UmbrellaException;
|
||||
|
||||
Collection<Location> listLocations(long companyId);
|
||||
|
||||
Collection<Location> listUserLocations(UmbrellaUser userId);
|
||||
|
||||
Collection<Location> listCompanyLocations(Company company);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
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.*;
|
||||
@@ -12,6 +11,7 @@ import static de.srsoftware.umbrella.core.ResponseCode.HTTP_NOT_IMPLEMENTED;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.OWNER;
|
||||
import static de.srsoftware.umbrella.project.Constants.PERMISSIONS;
|
||||
import static de.srsoftware.umbrella.task.Constants.*;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
|
||||
Reference in New Issue
Block a user