preparing search in stock items
This commit is contained in:
@@ -3,8 +3,8 @@ package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.is0;
|
||||
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.Condition.*;
|
||||
import static de.srsoftware.tools.jdbc.Condition.like;
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
@@ -17,6 +17,7 @@ import static java.text.MessageFormat.format;
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.api.Owner;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import java.sql.Connection;
|
||||
@@ -189,6 +190,28 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Item> find(long userId, Collection<String> keys, boolean fulltext) {
|
||||
try {
|
||||
var items = new HashMap<Long,Item>();
|
||||
var query = select(ALL).from(TABLE_ITEMS).where(USER_ID, equal(userId));
|
||||
if (fulltext) {
|
||||
// TODO: implement full-text search
|
||||
} else {
|
||||
for (var key : keys) query.where(NAME,like("%"+key+"%"));
|
||||
}
|
||||
var rs = query.exec(db);
|
||||
while (rs.next()){
|
||||
var project = Item.of(rs);
|
||||
items.put(project.id(),project);
|
||||
}
|
||||
rs.close();
|
||||
return items;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to load items from database");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<DbLocation> listChildLocations(long parentId) {
|
||||
try {
|
||||
|
||||
@@ -5,11 +5,13 @@ import de.srsoftware.umbrella.core.api.Owner;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface StockDb {
|
||||
Property addNewProperty(long itemId, String name, Object value, String unit);
|
||||
Location delete(DbLocation location);
|
||||
Map<Long, Item> find(long userId, Collection<String> keys, boolean fulltext);
|
||||
Collection<DbLocation> listChildLocations(long parentId);
|
||||
Collection<DbLocation> listCompanyLocations(Company company);
|
||||
Collection<Item> listItemsAt(Location location);
|
||||
@@ -24,5 +26,6 @@ public interface StockDb {
|
||||
Map<String,Object> pathToLocation(Location location);
|
||||
DbLocation save(DbLocation location);
|
||||
Item save(Item item);
|
||||
|
||||
Property setProperty(long itemId, long existingPropId, Object value);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import static de.srsoftware.umbrella.core.Field.ITEM;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
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.stock.Constants.*;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
@@ -174,6 +176,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
case LIST -> postItemList(user.get(), path, ex);
|
||||
case LOCATION -> postLocation(user.get(),ex);
|
||||
case PROPERTY -> postProperty(user.get(),ex);
|
||||
case SEARCH -> postSearch(user.get(),ex);
|
||||
case null, default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
@@ -389,6 +392,15 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
return sendContent(ex,property);
|
||||
}
|
||||
|
||||
private boolean postSearch(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY);
|
||||
var keys = Arrays.asList(key.split(" "));
|
||||
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val;
|
||||
var items = stockDb.find(user.id(),keys,fulltext);
|
||||
return sendContent(ex,mapValues(items));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> redefineMe(long company_id) {
|
||||
// TODO
|
||||
|
||||
Reference in New Issue
Block a user