implemented document creation

this required a major refactoring of the object model
This commit is contained in:
2025-07-10 12:49:32 +02:00
parent 21812d2b42
commit 163bcebf4f
43 changed files with 345 additions and 153 deletions

View File

@@ -10,6 +10,7 @@ import static java.text.MessageFormat.format;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.tools.Path;
import de.srsoftware.tools.PathHandler;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
@@ -35,7 +36,7 @@ public abstract class BaseHandler extends PathHandler {
return sendEmptyResponse(HTTP_FORBIDDEN,ex);
}
public record Document(String mime, byte[] bytes){}
public record Page(String mime, byte[] bytes){}
public boolean load(Path path, HttpExchange ex) throws IOException {
try {
@@ -47,7 +48,7 @@ public abstract class BaseHandler extends PathHandler {
}
}
public Document load(String resourcePath) throws IOException, UmbrellaException {
public Page load(String resourcePath) throws IOException, UmbrellaException {
var url = getClass().getClassLoader().getResource(resourcePath);
if (url == null) throw new UmbrellaException(HTTP_NOT_FOUND,"{0} not found!",resourcePath);
LOG.log(DEBUG,"Trying to load {0}",url);
@@ -56,7 +57,7 @@ public abstract class BaseHandler extends PathHandler {
var mime = conn.getContentType();
try (var stream = conn.getInputStream()){
stream.transferTo(bos);
return new Document(mime,bos.toByteArray());
return new Page(mime,bos.toByteArray());
} catch (Exception e) {
LOG.log(WARNING,"Failed to load {0}",url);
throw new UmbrellaException(HTTP_NOT_FOUND,"Failed to load {0}",url);

View File

@@ -64,4 +64,16 @@ public class Constants {
public static final String USER_LIST = "user_list";
public static final String UTF8 = UTF_8.displayName();
public static final String VALUE = "value";
public static final String COMPANY_ID = "company_id";
public static final String FIELD_COURT = "court";
public static final String FIELD_TAX_NUMBER = "tax_number";
public static final String FIELD_PHONE = "phone";
public static final String DECIMAL_SEPARATOR = "decimal_separator";
public static final String THOUSANDS_SEPARATOR = "thousands_separator";
public static final String LAST_CUSTOMER_NUMBER = "last_customer_number";
public static final String DECIMALS = "decimals";
public static final String CUSTOMER_NUMBER_PREFIX = "customer_number_prefix";
public static final String FIELD_CURRENCY = "currency";
public static final String FIELD_BANK_ACCOUNT = "bank_account";
}

View File

@@ -1,28 +0,0 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static java.text.MessageFormat.format;
public class UmbrellaException extends Exception{
private final int statusCode;
public UmbrellaException(String message, Object ... fills){
this(HTTP_SERVER_ERROR,message,fills);
}
public UmbrellaException(int statusCode, String message, Object ... fills){
super(format(message,fills));
this.statusCode = statusCode;
}
public UmbrellaException causedBy(Exception e) {
initCause(e);
return this;
}
public int statusCode(){
return statusCode;
}
}

View File

@@ -9,6 +9,7 @@ import static java.lang.System.Logger.Level.WARNING;
import static java.nio.charset.StandardCharsets.UTF_8;
import de.srsoftware.tools.Query;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;

View File

@@ -1,9 +1,14 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.api;
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;
public interface CompanyService {
public List<UmbrellaUser> getMembers(long companyId);
public Collection<UmbrellaUser> getMembers(long companyId) throws UmbrellaException;
public UserService userService();
Company get(long companyId) throws UmbrellaException;
}

View File

@@ -2,7 +2,7 @@
package de.srsoftware.umbrella.core.api;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.umbrella.core.UmbrellaException;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Optional;

View File

@@ -0,0 +1,46 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.exceptions;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
import static java.lang.System.Logger.Level.ERROR;
import static java.text.MessageFormat.format;
public class UmbrellaException extends Exception{
private final int statusCode;
public UmbrellaException(String message, Object ... fills){
this(HTTP_SERVER_ERROR,message,fills);
}
public UmbrellaException(int statusCode, String message, Object ... fills){
super(format(message,fills));
this.statusCode = statusCode;
}
public UmbrellaException causedBy(Exception e) {
initCause(e);
return this;
}
public static UmbrellaException invalidFieldException(String field,String expected){
return new UmbrellaException(HTTP_UNPROCESSABLE, ERROR_INVALID_FIELD, field, expected);
}
public static UmbrellaException missingConfigException(String field){
System.getLogger("Configuration").log(ERROR,ERROR_MISSING_CONFIG, field);
return new UmbrellaException(HTTP_SERVER_ERROR, ERROR_MISSING_CONFIG, field);
}
public static UmbrellaException missingFieldException(String field){
return new UmbrellaException(HTTP_UNPROCESSABLE, ERROR_MISSING_FIELD, field);
}
public int statusCode(){
return statusCode;
}
}

View File

@@ -0,0 +1,57 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONException;
import org.json.JSONObject;
public record Company(long id, String name, String address, String court, String taxId, String phone, String decimalSeparator, String thousandsSeparator, long lastCustomerNumber, int decimals, String customerNumberPrefix, String currency, String email, String bankAccount) {
public static Company of(JSONObject json) throws UmbrellaException {
try {
var id = json.getLong(ID);
var name = json.getString(NAME);
var address = json.getString(ADDRESS);
var court = json.getString(FIELD_COURT);
var taxId = json.getString(FIELD_TAX_NUMBER);
var phone = json.getString(FIELD_PHONE);
var decimalSep = json.getString(DECIMAL_SEPARATOR);
var thousandsSep = json.getString(THOUSANDS_SEPARATOR);
var lastCustomerNumber = json.getLong(LAST_CUSTOMER_NUMBER);
var decimals = json.getInt(DECIMALS);
var customerNumberPrefix = json.getString(CUSTOMER_NUMBER_PREFIX);
var currency = json.getString(FIELD_CURRENCY);
var email = json.getString(EMAIL);
var bankAccount = json.getString(FIELD_BANK_ACCOUNT);
return new Company(id,name,address,court,taxId,phone,decimalSep,thousandsSep,lastCustomerNumber,decimals,customerNumberPrefix,currency,email,bankAccount);
} catch (JSONException e){
throw new UmbrellaException(500,"Failed to convert JSON to Company!").causedBy(e);
}
}
public static Company of(ResultSet json) throws UmbrellaException {
try {
var id = json.getLong(ID);
var name = json.getString(NAME);
var address = json.getString(ADDRESS);
var email = json.getString(EMAIL);
var phone = json.getString(FIELD_PHONE);
var bankAccount = json.getString(FIELD_BANK_ACCOUNT);
var court = json.getString(FIELD_COURT);
var currency = json.getString(FIELD_CURRENCY);
var taxId = json.getString(FIELD_TAX_NUMBER);
var decimals = json.getInt(DECIMALS);
var decimalSep = json.getString(DECIMAL_SEPARATOR);
var thousandsSep = json.getString(THOUSANDS_SEPARATOR);
var lastCustomerNumber = json.getLong(LAST_CUSTOMER_NUMBER);
var customerNumberPrefix = json.getString(CUSTOMER_NUMBER_PREFIX);
return new Company(id,name,address,court,taxId,phone,decimalSep,thousandsSep,lastCustomerNumber,decimals,customerNumberPrefix,currency,email,bankAccount);
} catch (SQLException e){
throw new UmbrellaException(500,"Failed to convert ResultSet to Company!").causedBy(e);
}
}
}

View File

@@ -4,7 +4,8 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.tools.Optionals.allSet;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import de.srsoftware.umbrella.core.UmbrellaException;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.Objects;
public class EmailAddress {
private final String email;
@@ -15,6 +16,11 @@ public class EmailAddress {
email = addr;
}
@Override
public boolean equals(Object obj) {
return obj instanceof EmailAddress other && Objects.equals(email,other.email);
}
@Override
public String toString() {
return email;

View File

@@ -3,10 +3,10 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.UmbrellaException;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@@ -60,7 +60,7 @@ public class UmbrellaUser implements Mappable {
public static UmbrellaUser of(JSONObject json) throws UmbrellaException {
if (json.has(USER)) json = json.getJSONObject(USER);
if (!json.has(ID)) throw new UmbrellaException(HTTP_BAD_REQUEST,ERROR_MISSING_FIELD,ID);
if (!json.has(ID)) throw missingFieldException(ID);
var id = json.getLong(ID);
var name = json.has(NAME) ? json.getString(NAME) : null;
var email = json.has(EMAIL) ? new EmailAddress(json.getString(EMAIL)) : null;