|
| 1 | +package com.turning_leaf_technologies.hoopla; |
| 2 | + |
| 3 | +import com.turning_leaf_technologies.logging.BaseIndexingLogEntry; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.logging.log4j.Logger; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.PreparedStatement; |
| 9 | +import java.sql.ResultSet; |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.text.SimpleDateFormat; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Date; |
| 14 | + |
| 15 | +class HooplaExtractLogEntry2 implements BaseIndexingLogEntry { |
| 16 | + private Long logEntryId = null; |
| 17 | + private final Date startTime; |
| 18 | + private Date endTime; |
| 19 | + private int numRegrouped = 0; |
| 20 | + private int numChangedAfterGrouping = 0; |
| 21 | + private final ArrayList<String> notes = new ArrayList<>(); |
| 22 | + private int numProducts = 0; |
| 23 | + private int numErrors = 0; |
| 24 | + private int numAdded = 0; |
| 25 | + private int numDeleted = 0; |
| 26 | + private int numUpdated = 0; |
| 27 | + private int numInvalidRecords = 0; |
| 28 | + private int numEntitlementsUpdated = 0; |
| 29 | + private int numEntitlementsDeleted = 0; |
| 30 | + private int numAvailabilityChanges = 0; |
| 31 | + private final Logger logger; |
| 32 | + |
| 33 | + HooplaExtractLogEntry2(Connection dbConn, Logger logger) { |
| 34 | + this.logger = logger; |
| 35 | + this.startTime = new Date(); |
| 36 | + try { |
| 37 | + insertLogEntry = dbConn.prepareStatement("INSERT into hoopla_export_log (startTime) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS); |
| 38 | + updateLogEntry = dbConn.prepareStatement("UPDATE hoopla_export_log SET lastUpdate = ?, endTime = ?, notes = ?, numProducts = ?, numErrors = ?, numAdded = ?, numUpdated = ?, numDeleted = ?, numRegrouped =?, numChangedAfterGrouping = ?, numInvalidRecords = ?, numAvailabilityChanges = ?, numEntitlementsUpdated = ?, numEntitlementsDeleted = ? WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS); |
| 39 | + } catch (SQLException e) { |
| 40 | + logger.error("Error creating prepared statements to update log", e); |
| 41 | + } |
| 42 | + saveResults(); |
| 43 | + } |
| 44 | + |
| 45 | + private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 46 | + |
| 47 | + //Synchronized to prevent concurrent modification of the notes ArrayList |
| 48 | + public synchronized void addNote(String note) { |
| 49 | + Date date = new Date(); |
| 50 | + this.notes.add(dateFormat.format(date) + " - " + note); |
| 51 | + saveResults(); |
| 52 | + } |
| 53 | + |
| 54 | + private String getNotesHtml() { |
| 55 | + StringBuilder notesText = new StringBuilder("<ol class='cronNotes'>"); |
| 56 | + for (String curNote : notes) { |
| 57 | + String cleanedNote = curNote; |
| 58 | + cleanedNote = StringUtils.replace(cleanedNote,"<pre>", "<code>"); |
| 59 | + cleanedNote = StringUtils.replace(cleanedNote,"</pre>", "</code>"); |
| 60 | + //Replace multiple line breaks |
| 61 | + cleanedNote = cleanedNote.replaceAll("(?:<br?>\\s*)+", "<br/>"); |
| 62 | + cleanedNote = cleanedNote.replaceAll("<meta.*?>", ""); |
| 63 | + cleanedNote = cleanedNote.replaceAll("<title>.*?</title>", ""); |
| 64 | + notesText.append("<li>").append(cleanedNote).append("</li>"); |
| 65 | + } |
| 66 | + notesText.append("</ol>"); |
| 67 | + String returnText = notesText.toString(); |
| 68 | + if (returnText.length() > 25000) { |
| 69 | + returnText = returnText.substring(0, 25000) + " more data was truncated"; |
| 70 | + } |
| 71 | + return returnText; |
| 72 | + } |
| 73 | + |
| 74 | + private static PreparedStatement insertLogEntry; |
| 75 | + private static PreparedStatement updateLogEntry; |
| 76 | + |
| 77 | + public boolean saveResults() { |
| 78 | + try { |
| 79 | + if (logEntryId == null) { |
| 80 | + insertLogEntry.setLong(1, startTime.getTime() / 1000); |
| 81 | + insertLogEntry.executeUpdate(); |
| 82 | + ResultSet generatedKeys = insertLogEntry.getGeneratedKeys(); |
| 83 | + if (generatedKeys.next()) { |
| 84 | + logEntryId = generatedKeys.getLong(1); |
| 85 | + } |
| 86 | + } else { |
| 87 | + int curCol = 0; |
| 88 | + updateLogEntry.setLong(++curCol, new Date().getTime() / 1000); |
| 89 | + if (endTime == null) { |
| 90 | + updateLogEntry.setNull(++curCol, java.sql.Types.INTEGER); |
| 91 | + } else { |
| 92 | + updateLogEntry.setLong(++curCol, endTime.getTime() / 1000); |
| 93 | + } |
| 94 | + updateLogEntry.setString(++curCol, getNotesHtml()); |
| 95 | + updateLogEntry.setInt(++curCol, numProducts); |
| 96 | + updateLogEntry.setInt(++curCol, numErrors); |
| 97 | + updateLogEntry.setInt(++curCol, numAdded); |
| 98 | + updateLogEntry.setInt(++curCol, numUpdated); |
| 99 | + updateLogEntry.setInt(++curCol, numDeleted); |
| 100 | + updateLogEntry.setInt(++curCol, numRegrouped); |
| 101 | + updateLogEntry.setInt(++curCol, numChangedAfterGrouping); |
| 102 | + updateLogEntry.setInt(++curCol, numInvalidRecords); |
| 103 | + updateLogEntry.setInt(++curCol, numAvailabilityChanges); |
| 104 | + updateLogEntry.setInt(++curCol, numEntitlementsUpdated); |
| 105 | + updateLogEntry.setInt(++curCol, numEntitlementsDeleted); |
| 106 | + updateLogEntry.setLong(++curCol, logEntryId); |
| 107 | + updateLogEntry.executeUpdate(); |
| 108 | + } |
| 109 | + return true; |
| 110 | + } catch (SQLException e) { |
| 111 | + logger.error("Error creating updating log", e); |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void setFinished() { |
| 117 | + this.endTime = new Date(); |
| 118 | + this.addNote("Finished Hoopla extraction"); |
| 119 | + this.saveResults(); |
| 120 | + } |
| 121 | + |
| 122 | + public void incErrors(String note) { |
| 123 | + this.addNote("ERROR: " + note); |
| 124 | + numErrors++; |
| 125 | + this.saveResults(); |
| 126 | + logger.error(note); |
| 127 | + } |
| 128 | + |
| 129 | + public void incErrors(String note, Exception e) { |
| 130 | + this.addNote("ERROR: " + note + " " + e.toString()); |
| 131 | + numErrors++; |
| 132 | + this.saveResults(); |
| 133 | + logger.error(note, e); |
| 134 | + } |
| 135 | + |
| 136 | + void incAdded() { |
| 137 | + numAdded++; |
| 138 | + } |
| 139 | + |
| 140 | + void incDeleted() { |
| 141 | + numDeleted++; |
| 142 | + } |
| 143 | + |
| 144 | + void incUpdated() { |
| 145 | + numUpdated++; |
| 146 | + } |
| 147 | + |
| 148 | + void incNumProducts(int size) { |
| 149 | + numProducts += size; |
| 150 | + } |
| 151 | + |
| 152 | + boolean hasErrors() { |
| 153 | + return numErrors > 0; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + int getNumChanges() { |
| 158 | + return numUpdated + numDeleted + numAdded + numAvailabilityChanges + numEntitlementsUpdated + numEntitlementsDeleted; |
| 159 | + } |
| 160 | + |
| 161 | + public void incRecordsRegrouped() { |
| 162 | + numRegrouped++; |
| 163 | + if (numRegrouped % 1000 == 0){ |
| 164 | + this.saveResults(); |
| 165 | + } |
| 166 | + } |
| 167 | + void incAvailabilityChanges(){ |
| 168 | + numAvailabilityChanges++; |
| 169 | + } |
| 170 | + |
| 171 | + public void incChangedAfterGrouping(){ |
| 172 | + numChangedAfterGrouping++; |
| 173 | + } |
| 174 | + |
| 175 | + public int getNumChangedAfterGrouping() { |
| 176 | + return numChangedAfterGrouping; |
| 177 | + } |
| 178 | + |
| 179 | + public void incInvalidRecords(String invalidRecordId){ |
| 180 | + this.numInvalidRecords++; |
| 181 | + this.addNote("Invalid Record found: " + invalidRecordId); |
| 182 | + } |
| 183 | + |
| 184 | + public long getLogEntryId() { |
| 185 | + return logEntryId; |
| 186 | + } |
| 187 | + public void incEntitlementsUpdated() { |
| 188 | + numEntitlementsUpdated++; |
| 189 | + } |
| 190 | + public void incEntitlementsDeleted() { |
| 191 | + numEntitlementsDeleted++; |
| 192 | + } |
| 193 | +} |
0 commit comments