implemented search in companies

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-09-02 09:06:35 +02:00
parent 9887f78949
commit 9f4f095023
5 changed files with 69 additions and 9 deletions

View File

@@ -3,9 +3,11 @@ package de.srsoftware.umbrella.company;
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.ID;
import static de.srsoftware.umbrella.core.Constants.MEMBERS;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.FULLTEXT;
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 com.sun.net.httpserver.HttpExchange;
@@ -105,6 +107,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case SEARCH -> postSearch(user.get(),ex);
case null -> postNewCompany(user.get(),ex);
default -> super.doPost(path,ex);
};
@@ -193,4 +196,13 @@ public class CompanyModule extends BaseHandler implements CompanyService {
companyDb.addUser(company.id(),user.id());
return sendContent(ex,company);
}
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 companies = companyDb.find(user.id(),keys);
return sendContent(ex,mapValues(companies));
}
}

View File

@@ -5,6 +5,7 @@ public class Constants {
private Constants(){}
public static final String CONFIG_DATABASE = "umbrella.modules.company.database";
public static final String DISTINCT = "DISTINCT *";
public static final String TABLE_COMPANIES_USERS = "companies_users";
public static final String TABLE_COMPANIES = "companies";
}

View File

@@ -2,14 +2,15 @@
package de.srsoftware.umbrella.company;
import static de.srsoftware.tools.jdbc.Condition.equal;
import static de.srsoftware.tools.jdbc.Condition.like;
import static de.srsoftware.tools.jdbc.Query.*;
import static de.srsoftware.tools.jdbc.Query.Dialect.SQLITE;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES;
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES_USERS;
import static de.srsoftware.umbrella.company.Constants.*;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static java.lang.System.Logger.Level.ERROR;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.company.api.CompanyDb;
import de.srsoftware.umbrella.core.BaseDb;
@@ -17,10 +18,8 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Company;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.*;
import org.json.JSONObject;
public class SqliteDb extends BaseDb implements CompanyDb {
@@ -126,6 +125,26 @@ CREATE TABLE IF NOT EXISTS "companies" (
}
}
@Override
public HashMap<Long, Company> find(long userId, Collection<String> keys) {
try {
var query = select(DISTINCT).from(TABLE_COMPANIES).leftJoin(ID,TABLE_COMPANIES_USERS,COMPANY_ID)
.where(USER_ID,equal(userId));
for (var key : keys){
query.where(format("CONCAT({0},\" \",{1},\" \",{2},\" \",{3},\" \",{4})",NAME,ADDRESS,EMAIL,FIELD_PHONE,FIELD_BANK_ACCOUNT),like("%"+key+"%"));
}
var rs = query.exec(db);
var companies = new HashMap<Long,Company>();
while (rs.next()) {
var company = Company.of(rs);
companies.put(company.id(),company);
}
rs.close();
return companies;
} catch (SQLException ex){
throw databaseException("Failed to search in company database!");
}
}
@Override
public Map<Long,Company> listCompaniesOf(long userId) throws UmbrellaException {

View File

@@ -4,6 +4,7 @@ package de.srsoftware.umbrella.company.api;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Company;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public interface CompanyDb {
@@ -13,6 +14,8 @@ public interface CompanyDb {
void dropUser(long company_id, long user_id);
HashMap<Long, Company> find(long userId, Collection<String> keys);
Collection<Long> getMembers(long companyId) throws UmbrellaException;
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;