implemented document creation
this required a major refactoring of the object model
This commit is contained in:
@@ -2,5 +2,7 @@ description = "Umbrella : Companies"
|
||||
|
||||
dependencies{
|
||||
implementation(project(":core"))
|
||||
implementation("de.srsoftware:configuration.api:1.0.2")
|
||||
implementation("de.srsoftware:tools.jdbc:1.3.2")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,45 @@
|
||||
/* © SRSoftware 2025 */
|
||||
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.exceptions.UmbrellaException.missingFieldException;
|
||||
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||
import de.srsoftware.umbrella.core.api.CompanyService;
|
||||
import de.srsoftware.umbrella.core.api.UserService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class CompanyModule implements CompanyService {
|
||||
|
||||
private final UserService users;
|
||||
private final CompanyDb companyDb;
|
||||
|
||||
public CompanyModule(UserService userService){
|
||||
public CompanyModule(Configuration config, UserService userService) throws UmbrellaException {
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
companyDb = new SqliteDb(connect(dbFile));
|
||||
users = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmbrellaUser> getMembers(long companyId) {
|
||||
return null;
|
||||
public Company get(long companyId) throws UmbrellaException {
|
||||
return companyDb.load(companyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<UmbrellaUser> getMembers(long companyId) throws UmbrellaException {
|
||||
var members = new HashSet<UmbrellaUser>();
|
||||
for (var userId : companyDb.getMembers(companyId)) members.add(users.loadUser(userId));
|
||||
return members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserService userService() {
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.company;
|
||||
|
||||
public class Constants {
|
||||
private Constants(){}
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.company.database";
|
||||
public static final String TABLE_COMPANIES_USERS = "companies_users";
|
||||
public static final String TABLE_COMPANIES = "companies";
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.company;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES_USERS;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||
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.HashSet;
|
||||
|
||||
public class SqliteDb implements CompanyDb {
|
||||
|
||||
private final Connection db;
|
||||
|
||||
public SqliteDb(Connection connection) {
|
||||
db = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Long> getMembers(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
var rs = select("*").from(TABLE_COMPANIES_USERS).where(COMPANY_ID, equal(companyId)).exec(db);
|
||||
var ids = new HashSet<Long>();
|
||||
while (rs.next()) ids.add(rs.getLong(USER_ID));
|
||||
rs.close();
|
||||
return ids;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to load members of company {0}",companyId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Company load(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
var rs = select("*").from(TABLE_COMPANIES).where(ID, equal(companyId)).exec(db);
|
||||
Company company = null;
|
||||
if (rs.next()) company = Company.of(rs);
|
||||
rs.close();
|
||||
if (company == null) throw new UmbrellaException("Could not load company {0}",companyId);
|
||||
return company;
|
||||
} catch (SQLException e){
|
||||
throw new UmbrellaException("Could not load company {0}",companyId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.company.api;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface CompanyDb {
|
||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
}
|
||||
Reference in New Issue
Block a user