major refactoring: working towards more interface-implementation splitting
This commit is contained in:
@@ -10,6 +10,8 @@ public class Constants {
|
||||
private Constants(){}
|
||||
|
||||
public static final Pattern POST_CODE = compile("(.*\\w+.*)\n(.*\\d+.*)\n(\\d{5}) (\\w+)",DOTALL);
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.document.database";
|
||||
public static final String CONTACTS = "contacts";
|
||||
|
||||
public static final String CUSTOMER_NUMBER_PREFIX = "customer_number_prefix";
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
package de.srsoftware.umbrella.documents;
|
||||
|
||||
import static de.srsoftware.tools.MimeType.MIME_FORM_URL;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.ERROR_MISSING_FIELD;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.Util.request;
|
||||
import static de.srsoftware.umbrella.documents.Constants.*;
|
||||
import static java.lang.System.Logger.Level.DEBUG;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
@@ -14,29 +16,29 @@ import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.Token;
|
||||
import de.srsoftware.umbrella.core.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.api.UserHelper;
|
||||
import de.srsoftware.umbrella.core.api.UserService;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import de.srsoftware.umbrella.documents.model.Type;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.srsoftware.umbrella.documents.model.Type;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class DocumentApi extends BaseHandler {
|
||||
|
||||
private final UserHelper users;
|
||||
private final UserService users;
|
||||
private final Configuration config;
|
||||
private final DocumentDb db;
|
||||
|
||||
public DocumentApi(DocumentDb documentDb, UserHelper userHelper, Configuration moduleConfig){
|
||||
config = moduleConfig;
|
||||
db = documentDb;
|
||||
public DocumentApi(UserService userHelper, Configuration config) throws UmbrellaException {
|
||||
this.config = config;
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> new UmbrellaException(ERROR_MISSING_FIELD,CONFIG_DATABASE));
|
||||
db = new SqliteDb(connect(dbFile));
|
||||
users = userHelper;
|
||||
}
|
||||
|
||||
@@ -69,7 +71,8 @@ public class DocumentApi extends BaseHandler {
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case LIST -> listDocuments(ex,user.get(),token.orElse(null));
|
||||
case null, default -> super.doPost(path,ex);
|
||||
case null -> postDocument(ex,user.get());
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
@@ -89,17 +92,17 @@ public class DocumentApi extends BaseHandler {
|
||||
var map = types.values().stream().collect(Collectors.toMap(Type::id, Type::name));
|
||||
return sendContent(ex,map);
|
||||
}
|
||||
|
||||
private HashMap<Long, Map<String, Object>> getLegacyCompanies(HttpExchange ex, UmbrellaUser umbrellaUser, Token token) throws IOException, UmbrellaException {
|
||||
var location = config.get("company.baseUrl").map(s -> s+"/json").orElseThrow(() -> new UmbrellaException(500,"umbrella.modules.company.baseUrl not configured!"));
|
||||
var location = config.get("umbrella.modules.company.baseUrl").map(s -> s+"/json").orElseThrow(() -> new UmbrellaException(500,"umbrella.modules.company.baseUrl not configured!"));
|
||||
var resp = request(location, token.asMap(),MIME_FORM_URL,null);
|
||||
if (!(resp instanceof JSONObject json)) throw new UmbrellaException(500,"{0} did not return JSON!",location);
|
||||
var result = new HashMap<Long, Map<String,Object>>();
|
||||
for (var key : json.keySet()) result.put(Long.parseLong(key),json.getJSONObject(key).toMap());
|
||||
return result;
|
||||
}
|
||||
|
||||
private JSONArray getLegacyContacts(HttpExchange ex, UmbrellaUser umbrellaUser, Token token) throws IOException, UmbrellaException {
|
||||
var location = config.get("contact.baseUrl").map(s -> s+"/json").orElseThrow(() -> new UmbrellaException(500,"umbrella.modules.company.baseUrl not configured!"));
|
||||
var location = config.get("umbrella.modules.contact.baseUrl").map(s -> s+"/json").orElseThrow(() -> new UmbrellaException(500,"umbrella.modules.contact.baseUrl not configured!"));
|
||||
var resp = request(location, token.asMap(),MIME_FORM_URL,null);
|
||||
if (!(resp instanceof String s && s.startsWith("["))) throw new UmbrellaException(500,"{0} did not return JSON Array!",location);
|
||||
return new JSONArray(s);
|
||||
@@ -122,4 +125,18 @@ public class DocumentApi extends BaseHandler {
|
||||
throw new UmbrellaException( 500,"Failed to parse JSON data from request").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean postDocument(HttpExchange ex, UmbrellaUser umbrellaUser) throws IOException, UmbrellaException {
|
||||
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);
|
||||
long companyId;
|
||||
try {
|
||||
companyId = Long.parseLong(cid);
|
||||
} catch (NumberFormatException nfe){
|
||||
throw new UmbrellaException(HTTP_BAD_REQUEST,nfe.getMessage());
|
||||
}
|
||||
|
||||
LOG.log(DEBUG,json.toString(2));
|
||||
return notImplemented(ex,"{0}.postDocument(…)",this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user