Projekt

Obecné

Profil

Stáhnout (32.7 KB) Statistiky
| Větev: | Revize:
1 a4c65df6 Ondrej Drtina
import java.io.File;
2
import java.sql.*;
3
import java.util.ArrayList;
4
import java.util.Scanner;
5
6
public class Starter {
7
    public static void main(String[] args){
8
        //establish connection to DBs
9
        Connection dalimil1Con = establishDalimilX("dalimil1");
10
        Connection dalimil2Con = establishDalimilX("dalimil2");
11
        //retrieve content of "dd_wordform"s
12
        ResultSet dalimil1Wordform = retrieveWordformDalimilX(dalimil1Con, "dalimil1");
13
        //start process of finding whats missing in dalimil2 (AND is present in dalimil1)
14
        checkMissingWordformDalimil2(dalimil1Con, dalimil2Con, dalimil1Wordform);
15
        //close connection to DBs
16
        closeDalimilX(dalimil1Con, "dalimil1");
17
        closeDalimilX(dalimil2Con, "dalimil2");
18
    }
19
20
    /**
21
     * Go through entries present in dalimil1.dd_wordform and check which items are missing in dalimil2. Then perform respective steps (add wordform and possibly entries to other tables - dd_lemma etc).
22
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
23
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
24
     * @param dalimil1Wordform ResultSet which represents entries in dalimil1.wordform
25
     */
26
    private static void checkMissingWordformDalimil2(Connection dalimil1Con, Connection dalimil2Con, ResultSet dalimil1Wordform){
27
        ArrayList<Integer>[] doNotDeleteID = parseConflictsFile("shody - okomentované_rem_VYHODIT.txt"); //load ids, which should NOT be deleted (according to db admins)
28
29
        ArrayList<String> dalimil1Var = new ArrayList<>();
30
        ArrayList<String> dalimil2Var = new ArrayList<>();
31
32
        try{
33
            while(dalimil1Wordform.next()){ //go through dalimil1/dd_wordform
34
                //get all fields dalimil1/dd_wordform - START
35
                String id = dalimil1Wordform.getString("id");
36
                String context = dalimil1Wordform.getString("context");
37
                String date = dalimil1Wordform.getString("date");
38
                String description = dalimil1Wordform.getString("description");
39
                String description2 = dalimil1Wordform.getString("description2");
40
                String description3 = dalimil1Wordform.getString("description3");
41
                String ending = dalimil1Wordform.getString("ending");
42
                String finished = dalimil1Wordform.getString("finished");
43
                String namedentity = dalimil1Wordform.getString("namedentity");
44
                String position1 = dalimil1Wordform.getString("position1");
45
                String position2 = dalimil1Wordform.getString("position2");
46
                String positiondetail = dalimil1Wordform.getString("positiondetail");
47
                String prefix = dalimil1Wordform.getString("prefix");
48
                String suffix = dalimil1Wordform.getString("suffix");
49
                String word = dalimil1Wordform.getString("word");
50
                String lemma_id = dalimil1Wordform.getString("lemma_id");
51
                String tag_id = dalimil1Wordform.getString("tag_id");
52
                //get all fields dalimil1/dd_wordform - END
53
54
                //look where points lemma_id and tag_id
55
                String[] lemma_id_dd_lemma = retrieveLemmaContForId(dalimil1Con, "dalimil1", lemma_id);
56
                String tag_id_dd_tag = retrieveTagContForId(dalimil1Con, "dalimil1", tag_id);
57
                ArrayList<String> manuscript_cont = retrieveManuscriptNumsForId(dalimil1Con, "dalimil1", id);
58
59
                String dal1Print = "";
60
                dal1Print += "Dal1: id: " + id + " context: " + context + " date: " + date + " description: " + description + " description2: " + description2 + " description3: " + description3 + " ending: " + ending + " finished: " + finished + " namedentity: " + namedentity + " position1: " + position1 + " position2: " + position2 + " positiondetail: " + positiondetail + " prefix: " + prefix + " suffix: " + suffix + " word: " + word + " lemma_id: " + lemma_id + " tag_id: " + tag_id + " dd_lemma lemma: " + lemma_id_dd_lemma[0] + " dd_lemma pos: " + lemma_id_dd_lemma[1] + " dd_tag tag: " + tag_id_dd_tag;
61
                dal1Print += " in manuscripts: ";
62
                for(String manuscriptId : manuscript_cont){
63
                    dal1Print += manuscriptId + ", ";
64
                }
65
66
                //check if item already in dalimil2/dd_wordform
67
                Statement statement = dalimil2Con.createStatement();
68
                String sqlQuery = "SELECT * FROM public.dd_wordform WHERE position1='" + position1 + "' AND position2='" + position2 + "' AND positiondetail='" + positiondetail + "' AND word='" + word + "'" + ";";
69
                ResultSet results = statement.executeQuery(sqlQuery);
70
71
                int matchCountSec = 0; //number of matches found in second DB
72
                while(results.next()){
73
                    matchCountSec++;
74
                    String idSec = results.getString("id");
75
                    String contextSec = results.getString("context");
76
                    String dateSec = results.getString("date");
77
                    String descriptionSec = results.getString("description");
78
                    String description2Sec = results.getString("description2");
79
                    String description3Sec = results.getString("description3");
80
                    String endingSec = results.getString("ending");
81
                    String finishedSec = results.getString("finished");
82
                    String namedentitySec = results.getString("namedentity");
83
                    String position1Sec = results.getString("position1");
84
                    String position2Sec = results.getString("position2");
85
                    String positiondetailSec = results.getString("positiondetail");
86
                    String prefixSec = results.getString("prefix");
87
                    String suffixSec = results.getString("suffix");
88
                    String wordSec = results.getString("word");
89
                    String lemma_idSec = results.getString("lemma_id");
90
                    String tag_idSec = results.getString("tag_id");
91
92
                    ArrayList<String> manuscript_cont2 = retrieveManuscriptNumsForId(dalimil2Con, "dalimil2", idSec);
93
                    String[] lemma_id_dd_lemma2 = retrieveLemmaContForId(dalimil2Con, "dalimil2", lemma_idSec);
94
                    String tag_id_dd_tag2 = retrieveTagContForId(dalimil2Con, "dalimil2", tag_idSec);
95
96
                    String dal2Print = "";
97
                    dal2Print += "Dal2: id: " + idSec + " context: " + contextSec + " date: " + dateSec + " description: " + descriptionSec + " description2: " + description2Sec + " description3: " + description3Sec + " ending: " + endingSec + " finished: " + finishedSec + " namedentity: " + namedentitySec + " position1: " + position1Sec + " position2: " + position2Sec + " positiondetail: " + positiondetailSec + " prefix: " + prefixSec + " suffix: " + suffixSec + " word: " + wordSec + " lemma_id: " + lemma_idSec + " tag_id: " + tag_idSec + " dd_lemma lemma: " + lemma_id_dd_lemma2[0] + " dd_lemma pos: " + lemma_id_dd_lemma2[1] + " dd_tag tag: " + tag_id_dd_tag2;
98
                    dal2Print += " in manuscripts: ";
99
                    for(String manuscriptId : manuscript_cont2){
100
                        dal2Print += manuscriptId + ", ";
101
                    }
102
103
                    if(finished.equals("f") && finishedSec.equals("t")){
104
                        System.out.println("OK, found finished equivalent to unfinished item.. From first drop item: " + dal1Print);
105
                        //if unfinished item from first db has finished equivalent in second db, then drop item from first db and keep entry in second
106
                        if(!doNotDeleteID[0].contains(Integer.valueOf(id))){ //drop from first only if supervisor doesnt want to keep
107
                            deleteConflict(dalimil1Con, "dalimil1", id, dal1Print); //drop from first db
108
                        }else{
109
                            System.out.println("Conflicting ID: " + id + " kept..");
110
                        }
111
                    }else{
112
                        dalimil1Var.add(dal1Print);
113
                        dalimil2Var.add(dal2Print);
114
                        //drop items which are same across two given DBs
115
                        if(!doNotDeleteID[0].contains(Integer.valueOf(id))){ //drop from first only if supervisor doesnt want to keep
116
                            deleteConflict(dalimil1Con, "dalimil1", id, dal1Print); //delete conflicting item from first db
117
                        }else{
118
                            System.out.println("Conflicting ID: " + id + " kept..");
119
                        }
120
                        if(!doNotDeleteID[1].contains(Integer.valueOf(idSec))){ //drop from second only if supervisor doesnt want to keep
121
                            deleteConflict(dalimil2Con, "dalimil2", idSec, dal2Print); //delete conflicting item from second db
122
                        }else{
123
                            System.out.println("Conflicting ID: " + idSec + " kept..");
124
                        }
125
                    }
126
                }
127
                if(matchCountSec > 1){
128
                    //System.out.println("Found more equivalents, might be OK " + matchCountSec);
129
                }
130
            }
131
            addRemainingFromFirstToSecDB(dalimil1Con, dalimil2Con); //add data remaining in first DB to second DB
132
            //printConflicts(dalimil1Var, dalimil2Var);
133
        } catch (SQLException throwables) {
134
            System.out.println("err, working with dbs");
135
            throwables.printStackTrace();
136
        }
137
    }
138
139
    /**
140
     * Parses content of the file which contains only valid entries and returns ArrayLists which contains ids of entries which should stay in DB. Each entry begins with "Dal1:" or "Dal2:" and ends with ", OK"
141
     * @param pathToFile path to the file which contains valid entries (checked by contracting authority)
142
     * @return ArrayList<Integer>[] - on first index is list of ids, which should stay in first DB (Dal1) and on index 1 is list of ids, which should stay in the second DB
143
     */
144
    private static ArrayList<Integer>[] parseConflictsFile(String pathToFile){
145
        ArrayList<String> fileCont = new ArrayList<>(); //content of the file
146
147
        ArrayList<Integer>[] toStayInDB = new ArrayList[2]; //init outer array (0 = first DB, 1 = second DB)
148
        toStayInDB[0] = new ArrayList<>(); //represents indexes which should stay in first DB (Dal1)
149
        toStayInDB[1] = new ArrayList<>(); //represents indexes which should stay in second DB (Dal2)
150
151
        try{
152
            File conflictsFile = new File(pathToFile);
153
            Scanner conflictsSc = new Scanner(conflictsFile);
154
            while(conflictsSc.hasNextLine()){
155
                String line = conflictsSc.nextLine();
156
                fileCont.add(line);
157
            }
158
            conflictsSc.close();
159
        }catch(Exception e){
160
            System.out.println("Err, reading conflicts file");
161
            e.printStackTrace();
162
        }
163
164
        //content should be loaded by now, parse it
165
        for(String line : fileCont){
166
            if(line.startsWith("Dal1: id: ")){ //line contains id of item in first DB, Dal1
167
                String[] splitted = line.split("Dal1: id: ");
168
                splitted = splitted[1].split(" ");
169
                toStayInDB[0].add(Integer.valueOf(splitted[0]));
170
            }else if(line.startsWith("Dal2: id: ")){ //line contains id of item in second DB, Dal2
171
                String[] splitted = line.split("Dal2: id: ");
172
                splitted = splitted[1].split(" ");
173
                toStayInDB[1].add(Integer.valueOf(splitted[0]));
174
            }else{
175
                System.out.println("Err, file is in invalid format...");
176
            }
177
        }
178
179
        System.out.println("size is: " + toStayInDB[0].size() + " and " + toStayInDB[1].size());
180
181
        return toStayInDB;
182
    }
183
184
    /**
185
     * Used for deleting conflicting entries from database.
186
     * @param dalimilXCon Connection obj which represents connection to DB
187
     * @param whichDB name of DB (dalimil1 or dalimil2)
188
     * @param id id of entry which should be deleted (in dd_wordform)
189
     * @param textRepres text representation of deleted item, used for debug print
190
     */
191
    private static void deleteConflict(Connection dalimilXCon, String whichDB, String id, String textRepres){
192
        try {
193
            Statement statement = dalimilXCon.createStatement();
194
            int affectedManuscript = statement.executeUpdate("DELETE FROM public.dd_manuscript WHERE wordform_id=" + id); //delete entry which is associated with the word from dd_manuscript
195
            int affectedWordform = statement.executeUpdate("DELETE FROM public.dd_wordform WHERE id=" + id); //delete entry from dd_wordform
196
            //System.out.println("Removing affected wordform num:  " + affectedWordform + ", affected manuscript num: " + affectedManuscript + " info line: " + whichDB + " " + textRepres);
197
        } catch (SQLException throwables) {
198
            System.out.println("ERR, " + whichDB + " - deleting conflicts from database.");
199
            throwables.printStackTrace();
200
        }
201
    }
202
203
    /**
204
     * Adds remaining data presesent in Dalimil1 to Dalimil2
205
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
206
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
207
     */
208
    private static void addRemainingFromFirstToSecDB(Connection dalimil1Con, Connection dalimil2Con){
209
        ResultSet dalimil1Wordform = retrieveWordformDalimilX(dalimil1Con, "dalimil1"); //retrieve all data from dd_wordform, first DB (Dalimil 1)
210
211
        try {
212
            while(dalimil1Wordform.next()){ //go through remaining items present in first DB and put them to second DB
213
                //get all fields dalimil1/dd_wordform - START
214
                String id = dalimil1Wordform.getString("id");
215
                String lemma_id = dalimil1Wordform.getString("lemma_id");
216
                String tag_id = dalimil1Wordform.getString("tag_id");
217
                //System.out.println("Got remaining: " + id + " " + lemma_id + " " + tag_id);
218
                //get all fields dalimil1/dd_wordform - END
219
220
                //ok, got lemma + tag + manuscript data - now add them to respective tables before adding dd_wordform entry
221
                int lemma_idSecondDB = addLemmaToSecDB(dalimil1Con, dalimil2Con, lemma_id);
222
                int tag_idSecondDB = addTagToSecDB(dalimil1Con, dalimil2Con, tag_id);
223
                int wordform_idSecondDB = addWordformToSecDB(dalimil1Con, dalimil2Con, id, lemma_idSecondDB, tag_idSecondDB);
224
                //System.out.println("added as: " + wordform_idSecondDB + " " + lemma_idSecondDB + " " + tag_idSecondDB);
225
                addManuscriptToSecDB(dalimil1Con, dalimil2Con, String.valueOf(id), String.valueOf(wordform_idSecondDB));
226
            }
227
        } catch (SQLException throwables) {
228
            System.out.println("Error while adding remaining from first to sec DB.");
229
            throwables.printStackTrace();
230
        }
231
    }
232
233
    /**
234
     * Used for adding wordform to table dd_wordform (from first DB to second DB ; Dalimil1 -> Dalimil2).
235
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
236
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
237
     * @param idWordformFirstDB id of the entry in first DB, table dd_wordform, which should be inserted to second DB, same table
238
     * @param lemma_idSecondDB lemma_id attribute which should be used in SECOND DB for given entry
239
     * @param tag_idSecondDB tag_id attribute which should be used in SECOND DB for given entry
240
     * @return id of the newly inserted wordform
241
     */
242
    private static int addWordformToSecDB(Connection dalimil1Con, Connection dalimil2Con, String idWordformFirstDB, int lemma_idSecondDB, int tag_idSecondDB){
243
        ResultSet wordformDataFirstDB = retrieveWordformIdDalimilX(dalimil1Con, "dalimil1", idWordformFirstDB);
244
245
        int newWordformId = -1;
246
        try{
247
            Statement secondDBWordformMaxState = dalimil2Con.createStatement();
248
            ResultSet secondDBWordformMaxRes = secondDBWordformMaxState.executeQuery("SELECT MAX(id) FROM public.dd_wordform;"); //get max id, currently in dd_wordform (sec DB, Dalimil2) - newly added item has to be value+1
249
            while(secondDBWordformMaxRes.next()){
250
                int maxId = secondDBWordformMaxRes.getInt("max"); //get max id currently in wordform (second DB, Dalimil2) - newly added item has to have retrieved value+1
251
                newWordformId = maxId + 1; //newly inserted item : current_id + 1 for new id
252
            }
253
254
            //now retrieve data from first DB
255
            while(wordformDataFirstDB.next()){
256
                //get relevant fields dalimil1/dd_wordform - START
257
                String context = wordformDataFirstDB.getString("context");
258
                String date = wordformDataFirstDB.getString("date");
259
                String description = wordformDataFirstDB.getString("description");
260
                String description2 = wordformDataFirstDB.getString("description2");
261
                String description3 = wordformDataFirstDB.getString("description3");
262
                String ending = wordformDataFirstDB.getString("ending");
263
                String finished = wordformDataFirstDB.getString("finished");
264
                String namedentity = wordformDataFirstDB.getString("namedentity");
265
                String position1 = wordformDataFirstDB.getString("position1");
266
                String position2 = wordformDataFirstDB.getString("position2");
267
                String positiondetail = wordformDataFirstDB.getString("positiondetail");
268
                String prefix = wordformDataFirstDB.getString("prefix");
269
                String suffix = wordformDataFirstDB.getString("suffix");
270
                String word = wordformDataFirstDB.getString("word");
271
                //get relevant fields dalimil1/dd_wordform - END
272
273
                if(newWordformId == -1){
274
                    System.out.println("err, cannot retrieve wordform id!");
275
                    return newWordformId;
276
                }
277
278
                //now try to insert data to second DB
279
                Statement secondDBWordformInsert = dalimil2Con.createStatement();
280
281
                String insertIntoSql = "INSERT INTO public.dd_wordform (id, context, date, description, description2, description3, ending, finished, namedentity, position1, position2, positiondetail, prefix, suffix, word, lemma_id, tag_id) VALUES (";
282
                insertIntoSql += newWordformId + ",'" + context + "',TO_DATE('"+date+"', 'YYYY-MM-DD HH24:MI:ss.fff' ),'"+description+"','"+description2+"','"+description3+"','"+ending+"','"+finished+"','"+namedentity+"','"+position1+"','"+position2+"','"+positiondetail+"','"+prefix+"','"+suffix+"','"+word+"',"+lemma_idSecondDB+","+tag_idSecondDB+");";
283
                int secondDBWordformInsertRes = secondDBWordformInsert.executeUpdate(insertIntoSql);
284
                //System.out.println("Inserted wordform with id: " + newWordformId + " RES: " + secondDBWordformInsertRes);
285
            }
286
            return newWordformId;
287
        }catch (SQLException throwables){
288
            System.out.println("err, inserting wordform");
289
            throwables.printStackTrace();
290
            return newWordformId;
291
        }
292
    }
293
294
    /**
295
     * Used for adding manuscripts associated with the word to table dd_manuscript (from first to second DB).
296
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
297
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
298
     * @param wordformIdFirstDB id of the word (in first DB, table dd_wordform), used for retrieving data
299
     * @param wordformIdSecondDB id of the wordform in second DB
300
     */
301
    private static void addManuscriptToSecDB(Connection dalimil1Con, Connection dalimil2Con, String wordformIdFirstDB, String wordformIdSecondDB){
302
        ArrayList<String> manuscriptNumsFirstDB = retrieveManuscriptNumsForId(dalimil1Con, "dalimil1", wordformIdFirstDB);
303
        //got numbers of manuscripts in first DB, now edit them to match with the second one -> wordform_id = newly assigned id in second DB, manuscript = stays the same
304
305
        try{
306
            for(int i = 0; i < manuscriptNumsFirstDB.size(); i++){ //go through all manuscripts for each item and add them to second database
307
                String oneManuscriptNum = manuscriptNumsFirstDB.get(i); //get one manuscript num from retrieved manuscript nums (first DB)
308
                //now try to insert the data to second DB
309
                Statement secondDBManuscriptInsert = dalimil2Con.createStatement();
310
                int secondDBManuscriptInsertRes = secondDBManuscriptInsert.executeUpdate("INSERT INTO public.dd_manuscript (wordform_id, manuscript) VALUES (" + wordformIdSecondDB + "," + oneManuscriptNum + ");");
311
                //System.out.println("Inserted manuscript with wordform_id: " + wordformIdSecondDB + ", manuscript: " + oneManuscriptNum + " RES: " + secondDBManuscriptInsertRes);
312
            }
313
314
        }catch (SQLException throwables) {
315
            System.out.println("err, inserting manuscript");
316
            throwables.printStackTrace();
317
        }
318
    }
319
320
    /**
321
     * Used for adding lemma associated with the word to table dd_lemma (from first to second DB).
322
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
323
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
324
     * @param lemma_idFirstDB lemma_id of respective item (from first DB, dd_wordform! ie. Dalimil1)
325
     * @return id of the newly inserted lemma
326
     */
327
    private static int addLemmaToSecDB(Connection dalimil1Con, Connection dalimil2Con, String lemma_idFirstDB){
328
        String[] lemmaFirstDB = retrieveLemmaContForId(dalimil1Con, "dalimil1", lemma_idFirstDB); //retrieve lemma data from first DB (from table dd_lemma)
329
        String lemma = lemmaFirstDB[0];
330
        String pos = lemmaFirstDB[1];
331
332
        int newLemmaId = -1;
333
        try {
334
            Statement secondDBLemmaMaxState = dalimil2Con.createStatement();
335
            ResultSet secondDBLemmaMaxRes = secondDBLemmaMaxState.executeQuery("SELECT MAX(id) FROM public.dd_lemma;"); //get max id currently in lemma (second DB, Dalimil2) - newly added item has to have retrieved value+1
336
            while(secondDBLemmaMaxRes.next()){
337
                int maxId = secondDBLemmaMaxRes.getInt("max"); //get max id currently in lemma (second DB, Dalimil2) - newly added item has to have retrieved value+1
338
                newLemmaId = maxId + 1; //newly inserted item : current_id + 1 for new id
339
            }
340
341
            if(newLemmaId == -1){
342
                System.out.println("err, cannot retrieve lemma id!");
343
                return newLemmaId;
344
            }
345
            //now try to insert data to second DB
346
            Statement secondDBLemmaInsert = dalimil2Con.createStatement();
347
            int secondDBLemmaInsertRes = secondDBLemmaInsert.executeUpdate("INSERT INTO public.dd_lemma (id, lemma, pos) VALUES (" + newLemmaId + ",'" + lemma + "'," + pos + ");"); //insert lemma data retrieved from first DB to second DB (table dd_lemma)
348
            //System.out.print("Inserted lemma with id: " + newLemmaId + ", lemma: " + lemma + ", pos: " + pos + " RES: " + secondDBLemmaInsertRes);
349
            return newLemmaId;
350
        } catch (SQLException throwables) {
351
            System.out.println("err, inserting lemma");
352
            throwables.printStackTrace();
353
            return newLemmaId;
354
        }
355
    }
356
357
    /**
358
     * Used for adding tag associated with the word to table dd_tag (from first to second DB).
359
     * @param dalimil1Con Connection obj, represents connection to dalimil1 DB
360
     * @param dalimil2Con Connection obj, represents connection to dalimil2 DB
361
     * @param tag_idFirstDB tag_id of respective item (from first DB, dd_wordform! ie. Dalimil1)
362
     * @return id of the newly inserted tag
363
     */
364
    private static int addTagToSecDB(Connection dalimil1Con, Connection dalimil2Con, String tag_idFirstDB){
365
        String tagFirstDB = retrieveTagContForId(dalimil1Con, "dalimil1", tag_idFirstDB); //retrieve tag data from first DB (from table dd_tag)
366
367
        //tags in first DB are shorter than the ones in second DB (sec is 12char length) -> make it match
368
        while(tagFirstDB.length() < 12){
369
            tagFirstDB += "-";
370
        }
371
372
        //try to insert tag data to dd_tag table located in second DB
373
        int newTagId = -1;
374
        try{
375
            Statement secondDBTagMaxState = dalimil2Con.createStatement();
376
            ResultSet secondDBTagMaxRes = secondDBTagMaxState.executeQuery("SELECT MAX(id) FROM public.dd_tag;"); //get max id currently in tag (second DB, Dalimil2) - newly added item has to have retrieved value+1
377
            while(secondDBTagMaxRes.next()){
378
                int maxId = secondDBTagMaxRes.getInt("max"); //get max id currently in tag (second DB, Dalimil2) - newly added item has to have retrieved value+1
379
                newTagId = maxId + 1; //newly inserted item : current_id + 1 for new id
380
            }
381
382
            if(newTagId == -1){
383
                System.out.println("err, cannot retrieve tag id!");
384
                return newTagId;
385
            }
386
            //now try to insert tag data retrieved from first DB to second DB
387
            Statement secondDBTagInsert = dalimil2Con.createStatement();
388
            int secondDBTagInsertRes = secondDBTagInsert.executeUpdate("INSERT INTO public.dd_tag (id, tag) VALUES (" + newTagId + ",'" + tagFirstDB + "');");
389
            //System.out.println("Inserted tag with id: " + newTagId + ", tag: " + tagFirstDB + " RES: " + secondDBTagInsertRes);
390
            return newTagId;
391
        }catch (SQLException throwables){
392
            System.out.println("err, inserting tag");
393
            throwables.printStackTrace();
394
            return newTagId;
395
        }
396
    }
397
398
    /**
399
     * Prints conflicting rows.
400
     * @param dal1 list with dalimil1 content
401
     * @param dal2 list with dalimil2 content
402
     */
403
    private static void printConflicts(ArrayList<String> dal1, ArrayList<String> dal2){
404
        if(dal1.size() != dal2.size()){
405
            System.out.println("Err, size must be same...");
406
            return;
407
        }else{
408
            for(int i = 0; i < dal1.size(); i++){
409
                System.out.println(dal1.get(i));
410
                System.out.println(dal2.get(i));
411
            }
412
        }
413
    }
414
415
    /**
416
     * Retrieve manuscript numbers from table dd_manuscript for specific wordform_id (typically id from table dd_wordform).
417
     * @param dalimilXCon Connection obj which represents connection to DB
418
     * @param whichDB name of DB (dalimil1 or dalimil2)
419
     * @param wordform_id id from table dd_wordform
420
     * @return string arr which contains data from dd_manuscript (each index = manuscript assigned to wordform_id)
421
     */
422
    private static ArrayList<String> retrieveManuscriptNumsForId(Connection dalimilXCon, String whichDB, String wordform_id){
423
        ArrayList<String> manuscriptNumsList = new ArrayList<>();
424
425
        try {
426
            Statement statement = dalimilXCon.createStatement();
427
            ResultSet results = statement.executeQuery("SELECT * FROM public.dd_manuscript WHERE wordform_id=" + wordform_id + ";");
428
            while(results.next()){
429
                String manuscript = results.getString("manuscript");
430
                manuscriptNumsList.add(manuscript);
431
            }
432
433
            //System.out.println("OK, " + whichDB + " - retrieving data from dd_manuscript.");
434
        } catch (SQLException throwables) {
435
            System.out.println("ERR, " + whichDB + " - retrieving data from dd_manuscript.");
436
            throwables.printStackTrace();
437
        }
438
439
        return manuscriptNumsList;
440
    }
441
442
    /**
443
     * Retrieve content from table dd_lemma for specific id (typically lemma_id from table dd_wordform).
444
     * @param dalimilXCon Connection obj which represents connection to DB
445
     * @param whichDB name of DB (dalimil1 or dalimil2)
446
     * @param id lemma_id from table dd_wordform
447
     * @return String arr which contains data from dd_lemma (index 0 = lemma, index 1 = pos)
448
     */
449
    private static String[] retrieveLemmaContForId(Connection dalimilXCon, String whichDB, String id){
450
        String[] lemmaCont = new String[2];
451
452
        try {
453
            Statement statement = dalimilXCon.createStatement();
454
            ResultSet results = statement.executeQuery("SELECT * FROM public.dd_lemma WHERE id=" + id + ";");
455
            while(results.next()){
456
                String lemma = results.getString("lemma");
457
                String pos = results.getString("pos");
458
459
                lemmaCont[0] = lemma;
460
                lemmaCont[1] = pos;
461
            }
462
        } catch (SQLException throwables) {
463
            System.out.println("ERR, " + whichDB + " - retrieving data from dd_lemma.");
464
            throwables.printStackTrace();
465
        }
466
467
        return lemmaCont;
468
    }
469
470
    /**
471
     * Retrieve tag from table dd_lemma for specific id (typically tag_id from table dd_wordform).
472
     * @param dalimilXCon Connection obj which represents connection to DB
473
     * @param whichDB name of DB (dalimil1 or dalimil2)
474
     * @param id tag_id from table dd_wordform
475
     * @return
476
     */
477
    private static String retrieveTagContForId(Connection dalimilXCon, String whichDB, String id){
478
        String tag = null;
479
480
        try {
481
            Statement statement = dalimilXCon.createStatement();
482
            ResultSet results = statement.executeQuery("SELECT * FROM public.dd_tag WHERE id=" + id + ";");
483
            while(results.next()){
484
                tag = results.getString("tag");
485
            }
486
        } catch (SQLException throwables) {
487
            System.out.println("ERR, " + whichDB + " - retrieving data from dd_tag.");
488
            throwables.printStackTrace();
489
        }
490
491
        return tag;
492
    }
493
494
    /**
495
     * Get data from table dd_wordform (from dalimilX DB)
496
     * @param dalimilXCon Connection obj which represents connection to DB
497
     * @param whichDB name of DB (dalimil1 or dalimil2)
498
     * @return ResultSet, which represents data retrieved from dd_wordform table
499
     */
500
    private static ResultSet retrieveWordformDalimilX(Connection dalimilXCon, String whichDB){
501
        ResultSet results = null;
502
503
        try {
504
            Statement statement = dalimilXCon.createStatement();
505
            results = statement.executeQuery("SELECT * FROM public.dd_wordform;");
506
            //System.out.println("OK, " + whichDB + " - retrieving data from dd_wordform.");
507
        } catch (SQLException throwables) {
508
            System.out.println("ERR, " + whichDB + " - retrieving data from dd_wordform.");
509
            throwables.printStackTrace();
510
        }
511
512
        return results;
513
    }
514
515
    /**
516
     * Used for retrieving wordform with given id from DB (table dd_wordform).
517
     * @param dalimilXCon Connection obj which represents connection to DB
518
     * @param whichDB name of DB (dalimil1 or dalimil2)
519
     * @param id id of the entry in table dd_wordform which should be retrieved
520
     * @return ResultSet contains data about object with the specified id
521
     */
522
    private static ResultSet retrieveWordformIdDalimilX(Connection dalimilXCon, String whichDB, String id){
523
        ResultSet res = null;
524
525
        try{
526
            Statement statement = dalimilXCon.createStatement();
527
            res = statement.executeQuery("SELECT * FROM public.dd_wordform WHERE id=" + id + ";");
528
        } catch (SQLException throwables) {
529
            System.out.println("ERR, " + whichDB + " - retrieving data from dd_wordform.");
530
            throwables.printStackTrace();
531
        }
532
533
        return res;
534
    }
535
536
537
538
    /**
539
     * Establish connection to dalimilX DB.
540
     * @param whichDB name of DB (dalimil1 or dalimil2)
541
     * @return Connection obj, represents connection to dalimilX DB
542
     */
543
    private static Connection establishDalimilX(String whichDB){
544
        String jdbcDal1 = "jdbc:postgresql://localhost:5432/" + whichDB;
545
        String username = "postgres";
546
        String password = "PASS_TO_DB"; //obviously - replace this with your admin password
547
548
        Connection connection = null;
549
550
        try {
551
            connection = DriverManager.getConnection(jdbcDal1, username, password);
552
            //System.out.println("OK, " + whichDB + " - connect to PostgreSQL server.");
553
        } catch (SQLException throwables) {
554
            System.out.println("ERR, " + whichDB + " - connect to PostgreSQL server.");
555
            throwables.printStackTrace();
556
        }
557
558
        return connection;
559
    }
560
561
    /**
562
     * Close connection to dalimilX DB.
563
     * @param dalimilXCon Connection obj which represents connection to DB
564
     * @param whichDB name of DB (dalimil1 or dalimil2)
565
     * @return true if closed successfully, else false
566
     */
567
    private static boolean closeDalimilX(Connection dalimilXCon, String whichDB){
568
        try {
569
            dalimilXCon.close();
570
            //System.out.println("OK, " + whichDB + " - disconnect from PostgreSQL server.");
571
            return true;
572
        } catch (SQLException throwables) {
573
            throwables.printStackTrace();
574
            System.out.println("ERR, " + whichDB + " - disconnect from PostgreSQL server.");
575
            return false;
576
        }
577
    }
578
}