Changeset 32745
- Timestamp:
- 2019-02-05T23:03:16+13:00 (5 years ago)
- Location:
- other-projects/the-macronizer/trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/the-macronizer/trunk/src/java/util/MacroniserLogFileData.java
r32742 r32745 35 35 sb.append(time.format(DateTimeFormatter.ofPattern("HH:mm:ss"))); 36 36 sb.append("] DirectInput.doPost() \n Input:"); 37 sb.append(inputText);37 if(inputText != null) sb.append(inputText); 38 38 sb.append("Output:"); 39 sb.append(outputText);39 if(outputText != null) sb.append(outputText); 40 40 41 41 return sb.toString(); -
other-projects/the-macronizer/trunk/src/java/util/MacroniserLogFileProcessor.java
r32742 r32745 15 15 16 16 public class MacroniserLogFileProcessor { 17 static boolean debug = false; 17 18 /** ARGUMENTS: 18 19 * /home/wjkw1/RESEARCH_2018-19/bash_test/loggingtest.log … … 26 27 27 28 public static void main(String[] args) { 28 //TODO: re enable this method and add text to command line interface 29 //checkArgs(args.length); 29 checkArgs(args.length); 30 30 String filename = args[0]; 31 31 //extracts using yesterdays date 32 32 extractFromLogFile(filename); 33 33 34 //TODO: REMOVE 35 int count = 0; 36 for (MacroniserLogFileData entry: extractedEntries 37 ) { 38 printMessage((++count)+": "+entry.toString()); 39 } 34 35 if(debug) { 36 int count = 0; 37 for (MacroniserLogFileData entry : extractedEntries) { 38 printMessage((++count) + ": " + entry.toString()); 39 } 40 } 41 40 42 if(exportToDB()) { 41 // TODO: move the log file to processed folder42 43 System.out.println("Success. Move the log file " + filename + " to processed folder"); 44 System.exit(0); 43 45 } else { 44 // TODO: move the log file to reprocess folder 46 System.err.println("FAILED. Move the log file " + filename + " to reprocess folder"); 47 System.exit(-1); 45 48 } 46 49 } … … 57 60 58 61 //loop through all entries 59 for (MacroniserLogFileData entry : extractedEntries 60 ) { 62 for (MacroniserLogFileData entry : extractedEntries) { 61 63 //get the marked words from first entry 62 64 ArrayList<String> markedWords = getMarkedWordsFromOutput(entry.getOutputText()); 63 if (markedWords != null) { 65 // all these markedWords share the same date and time 66 LocalDate date = entry.getDate(); 67 LocalTime time = entry.getTime(); 68 69 if (markedWords == null) { // no words, enter NULL into db 70 if(debug) { 71 printMessage("word=NULL"); 72 printMessage(date.toString()); 73 printMessage(time.format(DateTimeFormatter.ofPattern("HH:mm:ss"))); 74 printMessage(""); // newline 75 } 76 77 MySQLAccess.Tuple tuple = new MySQLAccess.Tuple(null,date,time); 78 // b. add to DB 79 sqlAccess.addNewEntry(tuple); 80 } else { // process all the words 64 81 for (String word : markedWords) { 65 // create the tuple 66 MySQLAccess.Tuple tuple = new MySQLAccess.Tuple(word,entry.getDate(),entry.getTime()); 67 //TODO: insert all into database 68 //insert all into database 69 printMessage(word); 70 printMessage(entry.getDate().toString()); 71 printMessage(entry.getTime().format(DateTimeFormatter.ofPattern("HH:mm:ss"))); 72 printMessage(""); 73 82 if(debug) { 83 printMessage(word); 84 printMessage(date.toString()); 85 printMessage(time.format(DateTimeFormatter.ofPattern("HH:mm:ss"))); 86 printMessage(""); // newline 87 } 88 89 //insert all into database: 90 // a. create the tuple: always entering into db as lowercase so we don't consider tÅ different from TÅ 91 // whether when inserting into DB or searching for the term 92 MySQLAccess.Tuple tuple = new MySQLAccess.Tuple(word.toLowerCase(),date,time); 93 // b. add to DB 74 94 sqlAccess.addNewEntry(tuple); 75 95 } 96 76 97 } 77 98 } 78 99 success = true; 79 100 80 } catch (Exception e) {81 e.printStackTrace(); 101 } catch (Exception e) { 102 e.printStackTrace(); // goes to std.err, see https://stackoverflow.com/questions/12095378/difference-between-e-printstacktrace-and-system-out-printlne 82 103 success = false; 83 104 … … 91 112 //returns an array list of all marked words, null if there are none 92 113 private static ArrayList<String> getMarkedWordsFromOutput(String outputText) { 114 if(outputText == null) { return null; } 115 93 116 final Pattern TAG_REGEXP = Pattern.compile("<mark>(.+?)</mark>", Pattern.DOTALL); 94 117 final Matcher matcher = TAG_REGEXP.matcher(outputText); … … 104 127 //reads the log file and creates a list of data 105 128 private static void extractFromLogFile(String filename) { 106 //TODO: change the date that is used107 // LocalDate yesterday = LocalDate.now().minusDays(1L);108 LocalDate specifiedDate = LocalDate.of(2018,11,06);109 129 110 130 BufferedReader br = null; … … 133 153 //perform operations if not null, else error 134 154 if(entry != null) { 135 ////check if entry date is after specified date 136 if(entry.getDate().isAfter(specifiedDate)){ 137 //stop processing 138 break; 139 } else if (entry.getDate().isEqual(specifiedDate)){ 140 extractedEntries.add(entry); 141 } 155 extractedEntries.add(entry); 142 156 //remove old content and keep new tag 143 157 extractedSB.setLength(0); 144 158 extractedSB.append(line); 145 159 } else{ 146 printErrorMsg("Parsing of entry in log file found an error, continuing on next lines...");160 //printErrorMsg("Parsing of entry in log file found an error, continuing on next lines..."); 147 161 extractedSB.setLength(0); 148 162 extractedSB.append(line); … … 179 193 //Get the input output portion of the string 180 194 String input_output = ""; 181 if (extractedString.charAt(0)=='I'){195 if (extractedString.charAt(0) == 'I') { 182 196 183 197 //Get the date and time of entry … … 191 205 input_output = extractedString.replaceAll(directInputRegexp, "").trim(); 192 206 193 int INDEX_STARTOF_INPUT = 6, INDEX_ENDOF_INPUT = getEndofInputIndex(input_output), 194 INDEX_STARTOF_OUTPUT = INDEX_ENDOF_INPUT + 8, INDEX_ENDOF_OUTPUT = input_output.length(); 195 entryInput = input_output.substring(INDEX_STARTOF_INPUT, INDEX_ENDOF_INPUT); 196 entryOutput = input_output.substring(INDEX_STARTOF_OUTPUT, INDEX_ENDOF_OUTPUT); 197 207 int INDEX_STARTOF_INPUT = 6; 208 int INDEX_ENDOF_INPUT = getEndofInputIndex(input_output); 209 int INDEX_STARTOF_OUTPUT = INDEX_ENDOF_INPUT + 8; 210 int INDEX_ENDOF_OUTPUT = input_output.length(); 211 if (INDEX_ENDOF_INPUT == -1) { 212 entryInput = null; 213 entryOutput = null; 214 215 } else { 216 entryInput = input_output.substring(INDEX_STARTOF_INPUT, INDEX_ENDOF_INPUT); 217 entryOutput = input_output.substring(INDEX_STARTOF_OUTPUT, INDEX_ENDOF_OUTPUT); 218 } 198 219 entry = new MacroniserLogFileData(entryDate, entryTime, entryInput, entryOutput); 220 199 221 return entry; 200 222 201 } else if (extractedString.charAt(0) =='E') {223 } else if (extractedString.charAt(0) == 'E') { 202 224 input_output = extractedString.replaceAll(fileUploadRegexp, ""); 203 225 return null; … … 225 247 return indexes.get(middle_index); 226 248 } else { 227 print ErrorMsg("No output tag could be found, error in log file.");249 printMessage("Warning: No output tag could be found. Probably NULL input."); 228 250 return -1; 229 251 } -
other-projects/the-macronizer/trunk/src/java/util/MySQLAccess.java
r32743 r32745 56 56 57 57 private final String DB_NAME = "Macroniser"; 58 // TODO: from Properties file58 // obtained from Properties file: 59 59 private String USERNAME; //= "root"; by default 60 60 private String PASSWORD; //= "pinky"; … … 91 91 92 92 } 93 System.err.println("*** Found user: |" + USERNAME + "|");94 System.err.println("*** Found pwd: " + PASSWORD);93 //System.out.println("*** Found user: |" + USERNAME + "|"); 94 //System.out.println("*** Found pwd: " + PASSWORD); 95 95 96 96 // This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver"); … … 99 99 statement = connect.createStatement(); 100 100 101 // TODO: can we use preparedStatement here instead of statement? 102 int result = statement.executeUpdate("set names utf8mb4"); 103 System.err.println("Was set utf8 a success? " + result); // should return 0 for SQL stmts that return nothing 101 int result = statement.executeUpdate("set names utf8mb4"); // should return 0 for SQL stmts that return nothing 102 104 103 success = true; 105 104 } catch (SQLException e) { … … 192 191 insertOccurrence(word_id, time_id, date_id); 193 192 194 System.err.println("The ID's are:(word, date, time) (" + word_id + "," + date_id + "," + time_id + ")"); 193 if(MacroniserLogFileProcessor.debug) { 194 System.out.println("The IDs are:(word, date, time) (" + word_id + "," + date_id + "," + time_id + ")"); 195 } 195 196 196 197 connect.commit(); … … 234 235 //gets the specified marked word's id 235 236 private int getMarkedWordID(String marked_word) throws SQLException { 237 // in cases where the user didn't enter any input str, marked_word will be null. Still of interest, store to db as string "NULL" 238 if(marked_word == null) { 239 marked_word = "NULL"; 240 } 241 236 242 // Warning: "select * from table WHERE str_field LIKE ?" does not work when we want exact matches (or matches featuring macrons) 237 243 // Use "WHERE BINARY str_filed = ?" instead … … 253 259 private int insertMarkedWord(String word) throws SQLException { 254 260 261 if(word == null) { 262 word = "NULL"; 263 } 264 255 265 String query = "INSERT INTO MarkedWords (marked_word) VALUES (?)"; 256 266 preparedStatement = connect.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); 257 267 preparedStatement.setString(1, word); 268 258 269 259 270 int word_id = -1; … … 293 304 while (resultSet.next()) { 294 305 date_id = resultSet.getInt("date_id"); 295 System.err.println(date_id + " - " + date.toString());306 if(MacroniserLogFileProcessor.debug) System.out.println(date_id + " - " + date.toString()); 296 307 } 297 308 return date_id; … … 382 393 383 394 public static void main(String[] args) { 384 System.err.println("Hello pinky!");385 395 MySQLAccess mysql = new MySQLAccess(); 386 396 -
other-projects/the-macronizer/trunk/src/sql-scripts/MySQL_Code.sql
r32742 r32745 6 6 7 7 8 /* 9 Unique constraint on marked_word field needs to be specified specially to deal with utf8 10 e.g. to distinguish between Äno and anÅ, rather than treating them both the same breaking uniqueness. 11 https://stackoverflow.com/questions/25318479/unique-constraint-violation-with-utf-8-values 12 */ 8 13 CREATE TABLE IF NOT EXISTS MarkedWords ( 9 14 word_id INT AUTO_INCREMENT, 10 marked_word varchar (255)UNIQUE,15 marked_word varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin UNIQUE, 11 16 PRIMARY KEY (word_id) 12 17 );
Note:
See TracChangeset
for help on using the changeset viewer.