finished location tree
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user