overhauling constants, working on translations
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,12 +4,15 @@ package de.srsoftware.umbrella.company;
|
||||
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.company.Constants.NEXT_CUSTOMER_NUMBER;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.*;
|
||||
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.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.LIST;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.SEARCH;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.COMPANY_WITH_ID;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.LONG;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
@@ -30,17 +33,17 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
|
||||
public CompanyModule(Configuration config) throws UmbrellaException {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
|
||||
companyDb = new SqliteDb(connect(dbFile));
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
private boolean deleteCompany(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var company = get(companyId);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
if (!documentService().list(companyId).isEmpty()) throw forbidden("There are documents owned by {0}",company.name());
|
||||
if (!itemService().redefineMe(companyId).isEmpty()) throw forbidden("There are items owned by {0}",company.name());
|
||||
if (!projectService().listCompanyProjects(companyId,true).isEmpty()) throw forbidden("There are projects owned by {0}",company.name());
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {company}", COMPANY,company.name());
|
||||
if (!documentService().list(companyId).isEmpty()) throw forbidden("There are documents owned by {company}", COMPANY,company.name());
|
||||
if (!itemService().redefineMe(companyId).isEmpty()) throw forbidden("There are items owned by {company}", COMPANY,company.name());
|
||||
if (!projectService().listCompanyProjects(companyId,true).isEmpty()) throw forbidden("There are projects owned by {company}", COMPANY,company.name());
|
||||
return sendContent(ex, companyDb.drop(companyId));
|
||||
}
|
||||
|
||||
@@ -57,7 +60,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
default -> deleteCompany(Long.parseLong(head), user.get(), ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
return send(ex,invalidField(ID,t(LONG)));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
@@ -99,7 +102,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
default -> patchCompany(Long.parseLong(head), user.get(), ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
return send(ex,invalidField(ID,t(LONG)));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
@@ -119,7 +122,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
return send(ex,invalidField(ID,t(LONG)));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
@@ -153,7 +156,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
|
||||
private boolean getNextCustomerNumber(UmbrellaUser user, long companyId, HttpExchange ex) throws IOException {
|
||||
var company = companyDb.load(companyId);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
if (!membership(companyId,user.id())) throw notAmember(t(COMPANY_WITH_ID,ID,company.name()));
|
||||
var nextCustomerNumber = companyDb.getNextCustomerNumber(companyId);
|
||||
return sendContent(ex,nextCustomerNumber);
|
||||
}
|
||||
@@ -186,7 +189,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
|
||||
private boolean patchCompany(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var company = get(companyId);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
if (!membership(companyId,user.id())) throw notAmember(t(COMPANY_WITH_ID,ID,company.name()));
|
||||
|
||||
var json = json(ex);
|
||||
company = companyDb.save(company.patch(json));
|
||||
@@ -221,7 +224,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
|
||||
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);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingField(KEY);
|
||||
var keys = Arrays.asList(key.split(" "));
|
||||
var companies = companyDb.find(user,keys);
|
||||
return sendContent(ex,mapValues(companies));
|
||||
|
||||
@@ -7,17 +7,19 @@ 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.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.COMPANY;
|
||||
import static de.srsoftware.umbrella.core.Errors.*;
|
||||
import static de.srsoftware.umbrella.core.Field.*;
|
||||
import static de.srsoftware.umbrella.core.Field.COMPANY_ID;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.COMPANY;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.NUMBER;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.TYPE;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.constants.Text;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
@@ -67,7 +69,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_COMPANIES).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_COMPANIES).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +80,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_COMPANIES).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_COMPANIES_USERS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +93,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
.execute(db)
|
||||
.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_ASSIGN_USER_TO_COMPANY).causedBy(e);
|
||||
throw databaseException(FAILED_TO_ASSIGN_A_TO_B,"a", t(USER_WITH_ID, ID,user_id),"b", t(COMPANY_WITH_ID, ID,company_id)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +104,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
delete().from(TABLE_COMPANIES).where(ID,equal(companyId)).execute(db);
|
||||
return companyId;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,"company",companyId).causedBy(e);
|
||||
throw failedToDropObject("company "+ companyId).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +113,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
try {
|
||||
delete().from(TABLE_COMPANIES_USERS).where(COMPANY_ID,equal(companyId)).where(USER_ID,equal(userId)).execute(db);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY_OF_ENTITY,"user",userId,"company",companyId).causedBy(e);
|
||||
throw failedToDropObjectFromObject("user",userId,"company",companyId).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,14 +126,14 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
rs.close();
|
||||
return ids;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_MEMBERS,COMPANY,companyId).causedBy(e);
|
||||
throw failedToLoadMembers(t("company {company}", COMPANY,companyId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNextCustomerNumber(long companyId) {
|
||||
try {
|
||||
var rs = select(LAST_CUSTOMER_NUMBER,CUSTOMER_NUMBER_PREFIX).from(TABLE_COMPANIES).where(ID,equal(companyId)).exec(db);
|
||||
var rs = select(LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX).from(TABLE_COMPANIES).where(ID,equal(companyId)).exec(db);
|
||||
var last = 0L;
|
||||
String prefix = null;
|
||||
if (rs.next()){
|
||||
@@ -142,7 +144,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
var next = last+1;
|
||||
return prefix+next; // TODO: currently not taking growing number lengths into account, this should be resolved!
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_CUSTOMER_NUM_SETTINGS,companyId).causedBy(e);
|
||||
throw failedToLoadObject(t("customer number settings for {company_id}",COMPANY_ID,companyId)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +154,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
var query = select(DISTINCT).from(TABLE_COMPANIES).leftJoin(ID,TABLE_COMPANIES_USERS,COMPANY_ID)
|
||||
.where(USER_ID,equal(user.id()));
|
||||
for (var key : keys){
|
||||
query.where(format("CONCAT({0},\" \",{1},\" \",{2},\" \",{3},\" \",{4})",NAME,ADDRESS,EMAIL,PHONE,BANK_ACCOUNT),like("%"+key+"%"));
|
||||
query.where(format("CONCAT({0},\" \",{1},\" \",{2},\" \",{3},\" \",{4})", NAME, ADDRESS, EMAIL,PHONE,BANK_ACCOUNT),like("%"+key+"%"));
|
||||
}
|
||||
var rs = query.exec(db);
|
||||
var companies = new HashMap<Long,Company>();
|
||||
@@ -163,7 +165,8 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
rs.close();
|
||||
return companies;
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_SEARCH_DB, ModuleRegistry.translator().translate(user.language(),COMPANY)).causedBy(e);
|
||||
throw failedToSearchDb(t(Text.COMPANY)).causedBy(e);
|
||||
//throw databaseException(FAILED_TO_SEARCH_DB, ModuleRegistry.translator().translate(user.language(),COMPANY)).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +182,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
rs.close();
|
||||
return companies;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,"companies","user "+userId).causedBy(e);
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITIES_OF_OWNER,TYPE,COMPANIES, OWNER, t(USER_WITH_ID, ID,userId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,10 +193,10 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
Company company = null;
|
||||
if (rs.next()) company = Company.of(rs);
|
||||
rs.close();
|
||||
if (company == null) throw notFound(FAILED_TO_LOAD_ENTITY_BY_ID,"company",companyId);
|
||||
if (company == null) throw notFound(FAILED_TO_LOAD_OBJECT_BY_ID, OBJECT, t(Text.COMPANY), ID,companyId);
|
||||
return company;
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY_BY_ID,"company",companyId).causedBy(e);
|
||||
throw failedToLoadObject(t(Text.COMPANY),companyId).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +205,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
try {
|
||||
if (company.id() == 0){ // new
|
||||
long id = 0;
|
||||
var rs = insertInto(TABLE_COMPANIES,NAME, ADDRESS, EMAIL, PHONE, BANK_ACCOUNT, COURT, CURRENCY, TAX_NUMBER, DECIMALS, DECIMAL_SEPARATOR, THOUSANDS_SEPARATOR, LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX)
|
||||
var rs = insertInto(TABLE_COMPANIES, NAME, ADDRESS, EMAIL, PHONE, BANK_ACCOUNT, COURT, CURRENCY, TAX_NUMBER, DECIMALS, DECIMAL_SEPARATOR, THOUSANDS_SEPARATOR, LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX)
|
||||
.values(company.name(),company.address(),company.email(),company.phone(),company.bankAccount(),company.court(),company.currency(),company.taxId(),company.decimals(),company.decimalSeparator(),company.thousandsSeparator(),0,company.customerNumberPrefix())
|
||||
.execute(db)
|
||||
.getGeneratedKeys();
|
||||
@@ -225,7 +228,7 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
return company;
|
||||
}
|
||||
} catch (SQLException e){
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,company.name()).causedBy(e);
|
||||
throw failedToStoreObject(company.name()).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,13 +236,13 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
||||
public void saveNewCustomer(long companyId, String id) {
|
||||
var p = Pattern.compile("(?s)(.*?)(\\d+)$");
|
||||
var m = p.matcher(id);
|
||||
if (!m.matches()) throw unprocessable("{0} is not a valid customer id: it does not end with a number!");
|
||||
if (!m.matches()) throw unprocessable("{number} is not a valid customer id: it does not end with a number!", NUMBER,id);
|
||||
String prefix = m.group(1); // Prefix before last number
|
||||
long number = Long.parseLong(m.group(2)); // The last numeric part
|
||||
try {
|
||||
update(TABLE_COMPANIES).set(LAST_CUSTOMER_NUMBER,CUSTOMER_NUMBER_PREFIX).where(ID,equal(companyId)).prepare(db).apply(number,prefix).close();
|
||||
update(TABLE_COMPANIES).set(LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX).where(ID,equal(companyId)).prepare(db).apply(number,prefix).close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_UPDATE_ENTITY, LAST_CUSTOMER_NUMBER).causedBy(e);
|
||||
throw failedToStoreObject(LAST_CUSTOMER_NUMBER).causedBy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user