working on company editing
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -9,29 +9,102 @@ import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Currency;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
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) implements Mappable {
|
||||
public class Company implements Mappable {
|
||||
private final long id;
|
||||
private String name, address, court, taxId, phone, decimalSeparator, thousandsSeparator, customerNumberPrefix, currency, email, bankAccount;;
|
||||
private long lastCustomerNumber;
|
||||
private int decimals;
|
||||
private final Set<String> dirtyFields = new HashSet<>();
|
||||
|
||||
private Company(long id){
|
||||
this(id,null,null,null,null,null,null,null,0,2,null,null,null,null);
|
||||
}
|
||||
|
||||
public Company(long id, String name, String address, String court, String taxId, String phone, String decimalSep, String thousandsSep, long lastCustomerNumber, int decimals, String customerNumberPrefix, String currency, String email, String bankAccount){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.court = court;
|
||||
this.taxId = taxId;
|
||||
this.phone = phone;
|
||||
this.decimalSeparator = decimalSep;
|
||||
this.thousandsSeparator = thousandsSep;
|
||||
this.lastCustomerNumber = lastCustomerNumber;
|
||||
this.decimals = decimals;
|
||||
this.customerNumberPrefix = customerNumberPrefix;
|
||||
this.currency = currency;
|
||||
this.email = email;
|
||||
this.bankAccount = bankAccount;
|
||||
}
|
||||
|
||||
public String address(){
|
||||
return address;
|
||||
}
|
||||
|
||||
public String bankAccount() {
|
||||
return bankAccount;
|
||||
}
|
||||
|
||||
public Company clean(){
|
||||
dirtyFields.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public String court() {
|
||||
return court;
|
||||
}
|
||||
|
||||
public String currency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public String customerNumberPrefix() {
|
||||
return customerNumberPrefix;
|
||||
}
|
||||
|
||||
public int decimals() {
|
||||
return decimals;
|
||||
}
|
||||
|
||||
public String decimalSeparator() {
|
||||
return decimalSeparator;
|
||||
}
|
||||
|
||||
public String email() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public long id(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isDirty() {
|
||||
return !dirtyFields.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isDirty(String field){
|
||||
return dirtyFields.contains(field);
|
||||
}
|
||||
|
||||
public long lastCustomerNumber() {
|
||||
return lastCustomerNumber;
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
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);
|
||||
return new Company(json.getLong(ID)).patch(json).clean();
|
||||
} catch (JSONException e){
|
||||
throw new UmbrellaException(500,"Failed to convert JSON to Company!").causedBy(e);
|
||||
}
|
||||
@@ -59,6 +132,42 @@ public record Company(long id, String name, String address, String court, String
|
||||
}
|
||||
}
|
||||
|
||||
public Company patch(JSONObject json) {
|
||||
for (var key : json.keySet()){
|
||||
switch (key){
|
||||
case NAME: name = json.getString(NAME); break;
|
||||
case ADDRESS: address = json.getString(ADDRESS); break;
|
||||
case FIELD_COURT: court = json.getString(FIELD_COURT);
|
||||
case FIELD_TAX_NUMBER: taxId = json.getString(FIELD_TAX_NUMBER); break;
|
||||
case FIELD_PHONE: phone = json.getString(FIELD_PHONE); break;
|
||||
case DECIMAL_SEPARATOR: decimalSeparator = json.getString(DECIMAL_SEPARATOR); break;
|
||||
case THOUSANDS_SEPARATOR: thousandsSeparator = json.getString(THOUSANDS_SEPARATOR); break;
|
||||
case LAST_CUSTOMER_NUMBER: lastCustomerNumber = json.getLong(LAST_CUSTOMER_NUMBER); break;
|
||||
case DECIMALS: decimals = json.getInt(DECIMALS); break;
|
||||
case CUSTOMER_NUMBER_PREFIX: customerNumberPrefix = json.getString(CUSTOMER_NUMBER_PREFIX); break;
|
||||
case FIELD_CURRENCY: currency = json.getString(FIELD_CURRENCY); break;
|
||||
case EMAIL: email = json.getString(EMAIL); break;
|
||||
case FIELD_BANK_ACCOUNT: bankAccount = json.getString(FIELD_BANK_ACCOUNT); break;
|
||||
default: key = null;
|
||||
}
|
||||
if (key != null) dirtyFields.add(key);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public String phone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public String taxId() {
|
||||
return taxId;
|
||||
}
|
||||
|
||||
public String thousandsSeparator() {
|
||||
return thousandsSeparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
@@ -83,4 +192,5 @@ public record Company(long id, String name, String address, String court, String
|
||||
entry(FIELD_BANK_ACCOUNT,emptyIfNull(bankAccount))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user