Skip to content

Commit c81c8a5

Browse files
committed
Moved AnswerImporter (from teach) and DatabaseInterface (from assist) to core
1 parent ee52b1d commit c81c8a5

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

src/main/java/net/b07z/sepia/server/core/data/Answer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
66

77
import net.b07z.sepia.server.core.server.ConfigDefaults;
8+
import net.b07z.sepia.server.core.tools.JSON;
89

910
import java.io.IOException;
1011
import java.text.SimpleDateFormat;
@@ -16,6 +17,7 @@
1617
public class Answer {
1718

1819
//database types to organize commands
20+
public static final String ANSWERS_INDEX = "answers";
1921
public static final String ANSWERS_TYPE = "all";
2022

2123
// NOTE: keep in sync with answers-mapping.json! Only this way we can use Jackson to automatically
@@ -241,6 +243,9 @@ public static Answer importAnswerJSON(JSONObject json) throws IOException{
241243
return importAnswerJSON(json.toJSONString());
242244
}
243245

246+
/**
247+
* Return object as JSON formatted string (or throw error).
248+
*/
244249
public String toJsonString() {
245250
try {
246251
ObjectMapper mapper = new ObjectMapper();
@@ -250,6 +255,13 @@ public String toJsonString() {
250255
throw new RuntimeException("Error serializing " + this + " to JSON", e);
251256
}
252257
}
258+
/**
259+
* Convert object first to JSON string and then to JSONObject. Kind of stupid I know, but ...<br>
260+
* Throws error on parsing exception.
261+
*/
262+
public JSONObject toJson(){
263+
return JSON.parseStringOrFail(toJsonString());
264+
}
253265

254266
@Override
255267
public String toString() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.b07z.sepia.server.core.database;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.List;
7+
8+
import net.b07z.sepia.server.core.data.Answer;
9+
import net.b07z.sepia.server.core.data.Language;
10+
import net.b07z.sepia.server.core.database.DatabaseInterface;
11+
12+
/**
13+
* Import answers stored in a file to Elasticsearch.
14+
*/
15+
public class AnswerImporter {
16+
17+
public static void run(File file, Language language, DatabaseInterface es, boolean isMachineTranslated) throws IOException {
18+
List<String> lines = Files.readAllLines(file.toPath());
19+
for (String line : lines) {
20+
Answer answer = Answer.importAnswerString(line, language, isMachineTranslated);
21+
System.out.println(answer);
22+
es.setAnyItemData(Answer.ANSWERS_INDEX, Answer.ANSWERS_TYPE, answer.toJson());
23+
//System.out.println(answer.toJsonString());
24+
}
25+
}
26+
27+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package net.b07z.sepia.server.core.database;
2+
3+
import org.json.simple.JSONObject;
4+
5+
/**
6+
* Interface for classes that supply database access. First and foremost this is meant to be an interface for Elasticsearch (and not DynamoDB, but feel free ...).
7+
*
8+
* @author Florian Quirin
9+
*
10+
*/
11+
public interface DatabaseInterface {
12+
13+
/**
14+
* Set the data/properties/values of an item of "type" at "index".
15+
* @param index - index or table name like e.g. "account" or "knowledge"
16+
* @param type - subclass name, e.g. "user", "lists", "banking" (for account) or "geodata" and "dictionary" (for knowledge)
17+
* @param item_id - unique item/id name, e.g. user email address, dictionary word or geodata location name
18+
* @param data - JSON string with data objects that should be stored for index/type/item, e.g. {"name":"john"}
19+
* @return error code indicating success (0) or different errors (1 - database not reached) etc. ...
20+
*/
21+
public int setItemData(String index, String type, String item_id, JSONObject data);
22+
23+
/**
24+
* Set the data/properties/values of an arbitrary item of "type" at "index". Use this if you don't care about the unique id like
25+
* when you store blog posts. The item id will be generated automatically.
26+
* @param index - index or table name like e.g. "homepage"
27+
* @param type - subclass name, e.g. "blogpost"
28+
* @param data - JSON string with data objects that should be stored for index/type/any_id, e.g. {"title":"Hot News", "body":"bla bla...", "author":"john james"}
29+
* @return JSON with "_id" of created doc and error "code" indicating success (0) or different errors (1 - database not reached) etc. ...
30+
*/
31+
public JSONObject setAnyItemData(String index, String type, JSONObject data);
32+
33+
/**
34+
* Get item at path "index/type/item_id"
35+
* @param index - index or table name like e.g. "account" or "knowledge"
36+
* @param type - subclass name, e.g. "user", "lists", "banking" (for account) or "geodata" and "dictionary" (for knowledge)
37+
* @param item_id - unique item/id name, e.g. user email address, dictionary word or geodata location name
38+
* @return JSONObject with result or error description
39+
*/
40+
public JSONObject getItem(String index, String type, String item_id);
41+
42+
/**
43+
* Get filtered entries of the item at path "index/type/item_id". Use this if you want for example only a user name or something.
44+
* @param index - index or table name like e.g. "account" or "knowledge"
45+
* @param type - subclass name, e.g. "user", "lists", "banking" (for account) or "geodata" and "dictionary" (for knowledge)
46+
* @param item_id - unique item/id name, e.g. user email address, dictionary word or geodata location name
47+
* @param filters - String array with filters like ["name", "address", "language", "age"]
48+
* @return JSONObject with result or error description
49+
*/
50+
public JSONObject getItemFiltered(String index, String type, String item_id, String[] filters);
51+
52+
/**
53+
* Update or create the data/properties/values of an item of "type" at "index".
54+
* @param index - index or table name like e.g. "account" or "knowledge"
55+
* @param type - subclass name, e.g. "user", "lists", "banking" (for account) or "geodata" and "dictionary" (for knowledge)
56+
* @param item_id - unique item/id name, e.g. user email address, dictionary word or geodata location name
57+
* @param data - JSON string with data objects that should be stored for index/type/item, e.g. {"name":"john"}
58+
* @return error code indicating success (0) or different errors (1 - database not reached) etc. ...
59+
*/
60+
public int updateItemData(String index, String type, String item_id, JSONObject data);
61+
62+
/**
63+
* Search at "path" for a keyword.
64+
* @param path - can be index, type or item, e.g. "index/type/item_id" or only "index/"
65+
* @param search_term - something to search like "name:John" or simply "John" or "*" for all.
66+
* @return JSONObject with search result or error description
67+
*/
68+
public JSONObject searchSimple(String path, String search_term);
69+
70+
/**
71+
* Search at "path" for something via a query in JSON format
72+
* @param path - can be index, type or item, e.g. "index/type/item_id" or only "index/"
73+
* @param jsonQuery - query as JSON string
74+
* @return JSONObject with search result or error description
75+
*/
76+
public JSONObject searchByJson(String path, String jsonQuery);
77+
78+
/**
79+
* Delete item of "type" at "index".
80+
* @param index - index or table name like e.g. "account"
81+
* @param type - subclass name, e.g. "user"
82+
* @param item_id - item to delete, e.g. a user_id (email)
83+
* @return error code indicating success (0) or different errors (1 - database not reached) etc. ...
84+
*/
85+
public int deleteItem(String index, String type, String item_id);
86+
87+
/**
88+
* Delete any object like "index" (with all entries), "type" or "item"
89+
* @param path - path of what you want to delete like "account/" or "account/banking/"
90+
* @return error code indicating success (0) or different errors (1 - database not reached) etc. ...
91+
*/
92+
public int deleteAnything(String path);
93+
94+
/**
95+
* Search at "path" for something via a query in JSON format and if it exists delete it.
96+
* @param path - can be index, type or item, e.g. "index/type/item_id" or only "index/"
97+
* @param jsonQuery - query as JSON string
98+
* @return JSONObject with delete result or error description
99+
*/
100+
public JSONObject deleteByJson(String path, String jsonQuery);
101+
102+
}

src/main/java/net/b07z/sepia/server/core/tools/Connectors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static HttpClientResult httpGetSelfSignedSSL(String url) throws IOExcepti
191191
/**
192192
* HTTP GET method for JSON string. Check with {@code httpSuccess(...)} for status.
193193
* @param url - URL address to call including all parameters
194-
* @return JSONObject response of URL call
194+
* @return JSONObject response of URL call - Note: if response is not JSON it will be placed e.g. as "STRING" field in the result.
195195
*/
196196
public static JSONObject httpGET(String url) {
197197
return httpGET(url, new String[0]);
@@ -200,7 +200,7 @@ public static JSONObject httpGET(String url) {
200200
* HTTP GET method for JSON string. Check with {@code httpSuccess(...)} for status.
201201
* @param url - URL address to call including none or only some parameters
202202
* @param params - additional parameters added to URL (use e.g. "?q=search_term" or "&type=json" etc.)
203-
* @return
203+
* @return JSONObject response of URL call - Note: if response is not JSON it will be placed e.g. as "STRING" field in the result.
204204
*/
205205
public static JSONObject httpGET(String url, String[] params) {
206206
int responseCode = -1;

0 commit comments

Comments
 (0)