working on customer settings creation
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -169,6 +169,13 @@ public final class Document implements Mappable {
|
||||
return !dirtyFields.isEmpty() || sender.isDirty() || customer.isDirty();
|
||||
}
|
||||
|
||||
public boolean isDirty(String ... fields){
|
||||
for (var field : fields){
|
||||
if (dirtyFields.contains(field)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isNew(){
|
||||
return dirtyFields.contains(ID);
|
||||
}
|
||||
|
||||
@@ -411,7 +411,13 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
||||
|
||||
private boolean patchDocument(long docId, UmbrellaUser user, HttpExchange ex) throws UmbrellaException, IOException {
|
||||
var doc = getDocument(docId,user).a;
|
||||
doc.patch(json(ex));
|
||||
var data = json(ex);
|
||||
doc.patch(data);
|
||||
if (doc.isDirty(FIELD_FOOTER,FIELD_HEAD)) {
|
||||
var settings = db.getCustomerSettings(doc.companyId(),doc.type(),doc.customer().id());
|
||||
if (settings == null) settings = CustomerSettings.empty();
|
||||
db.save(doc.companyId(),doc.type(),doc.customer().id(), settings.patch(data));
|
||||
}
|
||||
db.save(doc);
|
||||
return ok(ex);
|
||||
}
|
||||
|
||||
@@ -556,6 +556,7 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
|
||||
@Override
|
||||
public CustomerSettings save(long companyId, Type docType, String customerId, CustomerSettings settings) throws UmbrellaException {
|
||||
if (!settings.isDirty()) return settings;
|
||||
try {
|
||||
replaceInto(TABLE_CUSTOMER_SETTINGS,FIELD_COMPANY_ID,FIELD_DOC_TYPE_ID,FIELD_CUSTOMER_NUMBER,FIELD_DEFAULT_HEADER,FIELD_DEFAULT_FOOTER,FIELD_DEFAULT_MAIL)
|
||||
.values(companyId,docType.id(),customerId,settings.header(),settings.footer(),settings.mailText())
|
||||
|
||||
@@ -3,14 +3,50 @@ package de.srsoftware.umbrella.documents.model;
|
||||
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.documents.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public record CustomerSettings(String header, String footer, String mailText) implements Mappable {
|
||||
public class CustomerSettings implements Mappable {
|
||||
private static final System.Logger LOG = System.getLogger(CustomerSettings.class.getSimpleName());
|
||||
private String footer, header, mailText;
|
||||
private HashSet<String> dirtyFields = new HashSet<>();
|
||||
|
||||
public CustomerSettings(String header, String footer, String mailText){
|
||||
this.header = header;
|
||||
this.footer = footer;
|
||||
this.mailText = mailText;
|
||||
}
|
||||
|
||||
public CustomerSettings clear(){
|
||||
dirtyFields.clear();
|
||||
return this;
|
||||
}
|
||||
public static CustomerSettings empty() {
|
||||
return new CustomerSettings("","","");
|
||||
}
|
||||
|
||||
public String footer(){
|
||||
return footer;
|
||||
}
|
||||
|
||||
public String header() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public boolean isDirty() {
|
||||
return !dirtyFields.isEmpty();
|
||||
}
|
||||
|
||||
public String mailText(){
|
||||
return mailText;
|
||||
}
|
||||
|
||||
public static CustomerSettings of(ResultSet rs) throws SQLException {
|
||||
var header = rs.getString(FIELD_DEFAULT_HEADER);
|
||||
var footer = rs.getString(FIELD_DEFAULT_FOOTER);
|
||||
@@ -18,12 +54,24 @@ public record CustomerSettings(String header, String footer, String mailText) im
|
||||
return new CustomerSettings(header,footer,mailText);
|
||||
}
|
||||
|
||||
public static CustomerSettings empty() {
|
||||
return new CustomerSettings("","","");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(FIELD_FOOTER,footer,FIELD_HEAD,header,CONTENT,mailText);
|
||||
}
|
||||
|
||||
public CustomerSettings patch(JSONObject json) {
|
||||
for (var key : json.keySet()){
|
||||
var valid = true;
|
||||
switch (key){
|
||||
case FIELD_FOOTER:
|
||||
footer = json.getString(key); break;
|
||||
case FIELD_HEAD:
|
||||
header = json.getString(key); break;
|
||||
default:
|
||||
valid = false;
|
||||
}
|
||||
if (valid) dirtyFields.add(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user