preparing contact index page

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-06 23:14:39 +02:00
parent f5ea613991
commit bf7de3cb6c
14 changed files with 135 additions and 23 deletions

View File

@@ -4,7 +4,10 @@ package de.srsoftware.umbrella.contact;
public class Constants {
private Constants(){}
public static final String ASSIGNED = "assigned";
public static final String CONFIG_DATABASE = "umbrella.modules.contact.database";
public static final String CONTACT_ID = "contact_id";
public static final String TABLE_CONTACTS_USERS = "contacts_users";
public static final String TABLE_CONTACTS = "contacts";
}

View File

@@ -3,8 +3,8 @@ package de.srsoftware.umbrella.contact;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Contact;
import java.util.Collection;
import java.util.Map;
public interface ContactDb {
Collection<Contact> listContactsOf(long id) throws UmbrellaException;
Map<Long,Contact> listContactsOf(long id) throws UmbrellaException;
}

View File

@@ -3,25 +3,59 @@ package de.srsoftware.umbrella.contact;
import static de.srsoftware.umbrella.contact.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.Paths.LIST;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.configuration.Configuration;
import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.ContactService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Contact;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Collection;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
public class ContactModule implements ContactService {
public class ContactModule extends BaseHandler implements ContactService {
private final ContactDb contactDb;
public ContactModule(Configuration config) throws UmbrellaException {
super();
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
contactDb = new SqliteDb(connect(dbFile));
ModuleRegistry.add(this);
}
@Override
public Collection<Contact> listContactsOf(UmbrellaUser user) throws UmbrellaException {
public boolean doGet(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case LIST -> getContacts(user.get(),ex);
case null, default -> super.doGet(path, ex);
};
} catch (UmbrellaException e) {
return send(ex,e);
}
}
private boolean getContacts(UmbrellaUser user, HttpExchange ex) throws IOException {
return sendContent(ex,mapValues(listContactsOf(user)));
}
@Override
public Map<Long,Contact> listContactsOf(UmbrellaUser user) throws UmbrellaException {
return contactDb.listContactsOf(user.id());
}
}

View File

@@ -4,34 +4,72 @@ package de.srsoftware.umbrella.contact;
import static de.srsoftware.tools.jdbc.Condition.equal;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.tools.jdbc.Query.select;
import static de.srsoftware.umbrella.contact.Constants.TABLE_CONTACTS;
import static de.srsoftware.umbrella.contact.Constants.TABLE_CONTACTS_USERS;
import static de.srsoftware.umbrella.contact.Constants.*;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Contact;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
public class SqliteDb implements ContactDb{
private final Connection conn;
public class SqliteDb extends BaseDb implements ContactDb{
public SqliteDb(Connection connection) {
conn = connection;
super(connection);
}
@Override
public Collection<Contact> listContactsOf(long userId) throws UmbrellaException{
protected int createTables() {
int currentVersion = createSettingsTable();
switch (currentVersion){
case 0:
createContactTable();
createContactsUsersTable();
}
return setCurrentVersion(1);
}
private void createContactTable() {
var sql = "CREATE TABLE IF NOT EXISTS {0} ({1} INTEGER PRIMARY KEY, {2} TEXT)";
sql = format(sql, TABLE_CONTACTS, ID, DATA);
try {
var rs = select(ALL).from(TABLE_CONTACTS).leftJoin(ID,TABLE_CONTACTS_USERS,USER_ID).where(USER_ID,equal(userId)).exec(conn);
var contacts = new HashSet<Contact>();
while (rs.next()) contacts.add(Contact.of(rs));
db.prepareStatement(sql).execute();
} catch (SQLException e) {
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_CONTACTS);
}
}
private void createContactsUsersTable() {
var sql = "CREATE TABLE IF NOT EXISTS {0} ({1} INT NOT NULL, {2} INT NOT NULL, {3} BOOLEAN DEFAULT 0, PRIMARY KEY({1}, {2}))";
sql = format(sql, TABLE_CONTACTS_USERS, CONTACT_ID, USER_ID, ASSIGNED);
try {
db.prepareStatement(sql).execute();
} catch (SQLException e) {
throw databaseException(ERROR_FAILED_CREATE_TABLE,TABLE_CONTACTS_USERS);
}
}
@Override
public Map<Long,Contact> listContactsOf(long userId) throws UmbrellaException{
try {
var rs = select(ALL).from(TABLE_CONTACTS).leftJoin(ID,TABLE_CONTACTS_USERS,CONTACT_ID).where(USER_ID,equal(userId)).exec(db);
var contacts = new HashMap<Long,Contact>();
while (rs.next()) {
var contact = Contact.of(rs);
contacts.put(contact.id(),contact);
}
rs.close();
return contacts;
} catch (SQLException e) {
throw UmbrellaException.databaseException("Failed to load contacts og user {0}",userId);
throw databaseException("Failed to load contacts of user {0}",userId);
}
}
}