|
|
|
|
@@ -7,6 +7,7 @@ import static de.srsoftware.document.mustang.Constants.KEY_PDF;
|
|
|
|
|
import static de.srsoftware.document.mustang.Constants.KEY_PRODUCER;
|
|
|
|
|
import static de.srsoftware.document.mustang.Constants.KEY_TEMPLATE;
|
|
|
|
|
import static de.srsoftware.tools.MimeType.MIME_FORM_URL;
|
|
|
|
|
import static de.srsoftware.tools.MimeType.MIME_PDF;
|
|
|
|
|
import static de.srsoftware.tools.Optionals.isSet;
|
|
|
|
|
import static de.srsoftware.tools.Strings.escapeHtmlEntities;
|
|
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
|
|
|
|
@@ -28,6 +29,7 @@ import de.srsoftware.configuration.Configuration;
|
|
|
|
|
import de.srsoftware.document.api.Content;
|
|
|
|
|
import de.srsoftware.document.api.DocumentRegistry;
|
|
|
|
|
import de.srsoftware.document.api.RenderError;
|
|
|
|
|
import de.srsoftware.document.api.RenderResult;
|
|
|
|
|
import de.srsoftware.document.files.DocumentDirectory;
|
|
|
|
|
import de.srsoftware.document.processor.latex.LatexFactory;
|
|
|
|
|
import de.srsoftware.document.processor.weasyprint.WeasyFactory;
|
|
|
|
|
@@ -68,6 +70,7 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
private final DocumentDb db;
|
|
|
|
|
private final UserService users;
|
|
|
|
|
private final Translator translator;
|
|
|
|
|
private final PostBox messages;
|
|
|
|
|
|
|
|
|
|
public DocumentApi(CompanyService companyService, Translator translator, Configuration config) throws UmbrellaException {
|
|
|
|
|
this.config = config;
|
|
|
|
|
@@ -76,6 +79,7 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
db = new SqliteDb(connect(dbFile));
|
|
|
|
|
companies = companyService;
|
|
|
|
|
users = companyService.userService();
|
|
|
|
|
messages = users.postBox();
|
|
|
|
|
|
|
|
|
|
Optional<String> templates = config.get(CONFIG_TEMPLATES);
|
|
|
|
|
if (templates.isEmpty()) throw missingFieldException(CONFIG_TEMPLATES);
|
|
|
|
|
@@ -145,6 +149,7 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
yield switch (head){
|
|
|
|
|
case null -> getDocument(ex,docId,user.get());
|
|
|
|
|
case PATH_PDF -> getRenderedDocument(ex,docId,user.get());
|
|
|
|
|
case SETTINGS -> getDocumentSettings(ex,docId,user.get());
|
|
|
|
|
default -> super.doGet(path,ex);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -214,7 +219,16 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean sendDocument(HttpExchange ex, Path path, UmbrellaUser umbrellaUser, long docId) throws IOException {
|
|
|
|
|
private boolean sendDocument(HttpExchange ex, Path path, UmbrellaUser user, long docId) throws IOException, UmbrellaException {
|
|
|
|
|
var doc = getDocument(docId,user).a;
|
|
|
|
|
var rendered = renderDocument(doc,user);
|
|
|
|
|
var json = json(ex);
|
|
|
|
|
if (!(json.has(EMAIL) && json.get(EMAIL) instanceof String email)) throw missingFieldException(EMAIL);
|
|
|
|
|
if (!(json.has(SUBJECT) && json.get(SUBJECT) instanceof String subject)) throw missingFieldException(SUBJECT);
|
|
|
|
|
if (!(json.has(CONTENT) && json.get(CONTENT) instanceof String content)) throw missingFieldException(CONTENT);
|
|
|
|
|
|
|
|
|
|
postBox.send(…)
|
|
|
|
|
|
|
|
|
|
return sendEmptyResponse(HTTP_NOT_IMPLEMENTED,ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -249,13 +263,13 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
|
|
|
|
|
private Document getDocumentWithCompanyData(long docId, UmbrellaUser user) throws UmbrellaException {
|
|
|
|
|
var tuple = getDocument(docId,user);
|
|
|
|
|
|
|
|
|
|
var company = tuple.b;
|
|
|
|
|
var sep = company.decimalSeparator();
|
|
|
|
|
|
|
|
|
|
var doc = tuple.a;
|
|
|
|
|
if (sep != null) doc.setDecimalSeparator(sep);
|
|
|
|
|
doc.setCompanyName(company.name());
|
|
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -264,6 +278,14 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
return sendContent(ex,doc.renderToMap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean getDocumentSettings(HttpExchange ex, long docId, UmbrellaUser user) throws IOException, UmbrellaException {
|
|
|
|
|
var tuple = getDocument(docId,user);
|
|
|
|
|
var doc = tuple.a;
|
|
|
|
|
var company = tuple.b;
|
|
|
|
|
var settings = db.getCustomerSettings(company.id(),doc.type(),doc.customer().id());
|
|
|
|
|
return sendContent(ex,settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PriceFormat priceFormat(String currency, String language) {
|
|
|
|
|
var pattern = switch (currency){
|
|
|
|
|
case "$" -> "$ {0,number,#,###.00}";
|
|
|
|
|
@@ -343,9 +365,7 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
return new DocumentData(document.number(),currency,typeCode,document.date(),author,customer,notes,deliveryDate,null,terms,lineItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean getRenderedDocument(HttpExchange ex, long docId, UmbrellaUser user) throws IOException, UmbrellaException {
|
|
|
|
|
var document = getDocumentWithCompanyData(docId,user);
|
|
|
|
|
private Content renderDocument(Document document, UmbrellaUser user) throws UmbrellaException {
|
|
|
|
|
var template = document.template().name();
|
|
|
|
|
var templateName = template+".html.pdf";
|
|
|
|
|
var type = document.type().name();
|
|
|
|
|
@@ -377,18 +397,21 @@ public class DocumentApi extends BaseHandler {
|
|
|
|
|
} catch (Converter.ConversionError e){
|
|
|
|
|
throw new UmbrellaException(500,"Failed to convert data: {0}",e.getMessage()).causedBy(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rendered = optDoc.get().render(data);
|
|
|
|
|
var source = optDoc.get();
|
|
|
|
|
var source = optDoc.get();
|
|
|
|
|
var rendered = source.render(data);
|
|
|
|
|
if (rendered instanceof RenderError err) throw new UmbrellaException(500,"Failed to render {0}: {1}",source.name(),err.toString());
|
|
|
|
|
if (rendered instanceof Content content) {
|
|
|
|
|
var headers = ex.getResponseHeaders();
|
|
|
|
|
headers.add(CONTENT_TYPE, source.mimeType());
|
|
|
|
|
headers.add(CONTENT_DISPOSITION,"attachment; filename=\""+document.number()+".pdf\"");
|
|
|
|
|
return sendContent(ex,content.bytes());
|
|
|
|
|
}
|
|
|
|
|
throw new UmbrellaException(500,"Unknown result type ({0}) returned from render process!",rendered.getClass().getSimpleName());
|
|
|
|
|
if (!(rendered instanceof Content content)) throw new UmbrellaException(500,"Unknown result type ({0}) returned from render process!",rendered.getClass().getSimpleName());
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private boolean getRenderedDocument(HttpExchange ex, long docId, UmbrellaUser user) throws IOException, UmbrellaException {
|
|
|
|
|
var document = getDocumentWithCompanyData(docId,user);
|
|
|
|
|
var content = renderDocument(document,user);
|
|
|
|
|
var headers = ex.getResponseHeaders();
|
|
|
|
|
headers.add(CONTENT_TYPE, MIME_PDF);
|
|
|
|
|
headers.add(CONTENT_DISPOSITION,"attachment; filename=\""+document.number()+".pdf\"");
|
|
|
|
|
return sendContent(ex,content.bytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONArray getLegacyContacts(HttpExchange ex, UmbrellaUser umbrellaUser, Token token) throws IOException, UmbrellaException {
|
|
|
|
|
|