|
|
|
@ -3,25 +3,29 @@ package de.srsoftware.umbrella.documents; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.tools.MimeType.MIME_FORM_URL; |
|
|
|
import static de.srsoftware.tools.MimeType.MIME_FORM_URL; |
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.ERROR_MISSING_FIELD; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.SENDER; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.Util.request; |
|
|
|
import static de.srsoftware.umbrella.core.Util.request; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException; |
|
|
|
import static de.srsoftware.umbrella.documents.Constants.*; |
|
|
|
import static de.srsoftware.umbrella.documents.Constants.*; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.documents.model.Document.State.NEW; |
|
|
|
import static java.lang.System.Logger.Level.DEBUG; |
|
|
|
import static java.lang.System.Logger.Level.DEBUG; |
|
|
|
import static java.lang.System.Logger.Level.WARNING; |
|
|
|
import static java.lang.System.Logger.Level.WARNING; |
|
|
|
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; |
|
|
|
import static java.net.HttpURLConnection.*; |
|
|
|
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
import de.srsoftware.configuration.Configuration; |
|
|
|
import de.srsoftware.configuration.Configuration; |
|
|
|
import de.srsoftware.tools.Path; |
|
|
|
import de.srsoftware.tools.Path; |
|
|
|
import de.srsoftware.tools.SessionToken; |
|
|
|
import de.srsoftware.tools.SessionToken; |
|
|
|
import de.srsoftware.umbrella.core.BaseHandler; |
|
|
|
import de.srsoftware.umbrella.core.BaseHandler; |
|
|
|
import de.srsoftware.umbrella.core.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.api.CompanyService; |
|
|
|
import de.srsoftware.umbrella.core.api.UserService; |
|
|
|
import de.srsoftware.umbrella.core.api.UserService; |
|
|
|
|
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.model.Token; |
|
|
|
import de.srsoftware.umbrella.core.model.Token; |
|
|
|
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
|
|
|
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
|
|
|
import de.srsoftware.umbrella.documents.model.Type; |
|
|
|
import de.srsoftware.umbrella.documents.model.*; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
@ -31,15 +35,17 @@ import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
|
|
public class DocumentApi extends BaseHandler { |
|
|
|
public class DocumentApi extends BaseHandler { |
|
|
|
|
|
|
|
|
|
|
|
private final UserService users; |
|
|
|
private final CompanyService companies; |
|
|
|
private final Configuration config; |
|
|
|
private final Configuration config; |
|
|
|
private final DocumentDb db; |
|
|
|
private final DocumentDb db; |
|
|
|
|
|
|
|
private final UserService users; |
|
|
|
|
|
|
|
|
|
|
|
public DocumentApi(UserService userHelper, Configuration config) throws UmbrellaException { |
|
|
|
public DocumentApi(CompanyService companyService, Configuration config) throws UmbrellaException { |
|
|
|
this.config = config; |
|
|
|
this.config = config; |
|
|
|
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> new UmbrellaException(ERROR_MISSING_FIELD,CONFIG_DATABASE)); |
|
|
|
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
|
|
|
db = new SqliteDb(connect(dbFile)); |
|
|
|
db = new SqliteDb(connect(dbFile)); |
|
|
|
users = userHelper; |
|
|
|
companies = companyService; |
|
|
|
|
|
|
|
users = companyService.userService(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -111,7 +117,7 @@ public class DocumentApi extends BaseHandler { |
|
|
|
private boolean listDocuments(HttpExchange ex, UmbrellaUser user, Token token) throws UmbrellaException { |
|
|
|
private boolean listDocuments(HttpExchange ex, UmbrellaUser user, Token token) throws UmbrellaException { |
|
|
|
try { |
|
|
|
try { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!json.has(COMPANY)) throw new UmbrellaException(HTTP_BAD_REQUEST,ERROR_MISSING_FIELD, COMPANY); |
|
|
|
if (!json.has(COMPANY)) throw missingFieldException(COMPANY); |
|
|
|
long companyId = json.getLong(COMPANY); |
|
|
|
long companyId = json.getLong(COMPANY); |
|
|
|
var companies = getLegacyCompanies(ex,user, token); |
|
|
|
var companies = getLegacyCompanies(ex,user, token); |
|
|
|
var company = companies.get(companyId); |
|
|
|
var company = companies.get(companyId); |
|
|
|
@ -126,17 +132,34 @@ public class DocumentApi extends BaseHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean postDocument(HttpExchange ex, UmbrellaUser umbrellaUser) throws IOException, UmbrellaException { |
|
|
|
private boolean postDocument(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!(json.has(COMPANY) && json.get(COMPANY) instanceof String cid) || cid.isBlank()) throw new UmbrellaException(HTTP_BAD_REQUEST,ERROR_MISSING_FIELD,COMPANY); |
|
|
|
if (!(json.has(SENDER) && json.get(SENDER) instanceof JSONObject senderData)) throw missingFieldException(SENDER); |
|
|
|
long companyId; |
|
|
|
if (!senderData.has(FIELD_COMPANY) || !(senderData.get(FIELD_COMPANY) instanceof Number companyId)) throw missingFieldException(FIELD_COMPANY); |
|
|
|
try { |
|
|
|
|
|
|
|
companyId = Long.parseLong(cid); |
|
|
|
var company = companies.get(companyId.longValue()); |
|
|
|
} catch (NumberFormatException nfe){ |
|
|
|
var members = companies.getMembers(companyId.longValue()); |
|
|
|
throw new UmbrellaException(HTTP_BAD_REQUEST,nfe.getMessage()); |
|
|
|
var match = false; |
|
|
|
} |
|
|
|
for (var member : members) match |= user.equals(member); |
|
|
|
|
|
|
|
if (!match) return sendContent(ex,HTTP_FORBIDDEN,"You are mot a member of company "+companyId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!json.has(FIELD_CUSTOMER) || !(json.get(FIELD_CUSTOMER) instanceof JSONObject customerData)) throw missingFieldException(FIELD_CUSTOMER); |
|
|
|
|
|
|
|
if (!json.has(FIELD_TYPE) || !(json.get(FIELD_TYPE) instanceof Number docTypeId)) throw missingFieldException(FIELD_TYPE); |
|
|
|
|
|
|
|
var type = db.getType(docTypeId.intValue()); |
|
|
|
|
|
|
|
var customer = Customer.of(customerData); |
|
|
|
|
|
|
|
Template template = new Template(6,companyId.longValue(),"unknwon",null); |
|
|
|
|
|
|
|
String currency = company.currency(); |
|
|
|
|
|
|
|
String sep = company.decimalSeparator(); |
|
|
|
|
|
|
|
var settings = db.getCustomerSettings(companyId.longValue(),type,customer.id()); |
|
|
|
|
|
|
|
var companySettings = db.getCompanySettings(companyId.longValue(),type); |
|
|
|
|
|
|
|
var nextNumber = companySettings.nextDocId(); |
|
|
|
|
|
|
|
String lastHead = settings.header(); |
|
|
|
|
|
|
|
String lastFooter = settings.footer(); |
|
|
|
|
|
|
|
var sender = Sender.of(senderData); |
|
|
|
LOG.log(DEBUG,json.toString(2)); |
|
|
|
LOG.log(DEBUG,json.toString(2)); |
|
|
|
return notImplemented(ex,"{0}.postDocument(…)",this); |
|
|
|
var doc = new Document(0,companyId.longValue(),nextNumber,type, LocalDate.now(), NEW,template,null,lastHead,lastFooter,currency,sep,sender,customer,new PositionList()); |
|
|
|
|
|
|
|
var saved = db.save(doc); |
|
|
|
|
|
|
|
db.step(companySettings); |
|
|
|
|
|
|
|
return sendContent(ex,saved.toMap()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|