@ -8,6 +8,8 @@ import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
@@ -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;
@@ -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 {
@@ -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 {
@@ -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 ) ;