Skip to content

Commit cf291af

Browse files
authored
Merge pull request #3417 from LiYanjun19/DIS-1342-Hoopla-New-Endpoints-Switches
DIS-1342 Hoopla New Endpoint Integration
2 parents b8499e8 + 0ad0e87 commit cf291af

File tree

35 files changed

+5112
-1378
lines changed

35 files changed

+5112
-1378
lines changed

code/hoopla_export/src/com/turning_leaf_technologies/hoopla/HooplaExportMain.java

Lines changed: 136 additions & 977 deletions
Large diffs are not rendered by default.

code/hoopla_export/src/com/turning_leaf_technologies/hoopla/HooplaExporter.java

Lines changed: 953 additions & 0 deletions
Large diffs are not rendered by default.

code/hoopla_export/src/com/turning_leaf_technologies/hoopla/HooplaExporter2.java

Lines changed: 1546 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.turning_leaf_technologies.hoopla;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import oracle.net.aso.f;
7+
8+
class HooplaLibrarySettings {
9+
private final long id;
10+
private final long settingId;
11+
private final long libraryId;
12+
private final String displayName;
13+
private final String hooplaLibraryId;
14+
private final boolean circulationEnabled;
15+
private final boolean instantEnabled;
16+
private final boolean flexEnabled;
17+
private final boolean fullUpdateForLibrary;
18+
private final boolean cleanUpInstant;
19+
private final boolean cleanUpFlex;
20+
21+
HooplaLibrarySettings(ResultSet settingsRS) throws SQLException {
22+
id = settingsRS.getLong("id");
23+
settingId = settingsRS.getLong("settingId");
24+
libraryId = settingsRS.getLong("libraryId");
25+
displayName = settingsRS.getString("displayName");
26+
hooplaLibraryId = settingsRS.getString("hooplaLibraryID");
27+
circulationEnabled = settingsRS.getBoolean("circulationEnabled");
28+
instantEnabled = settingsRS.getBoolean("hooplaInstantEnabled");
29+
flexEnabled = settingsRS.getBoolean("hooplaFlexEnabled");
30+
fullUpdateForLibrary = settingsRS.getBoolean("fullUpdateForLibrary");
31+
cleanUpInstant = settingsRS.getBoolean("cleanUpInstant");
32+
cleanUpFlex = settingsRS.getBoolean("cleanUpFlex");
33+
}
34+
35+
long getId() {
36+
return id;
37+
}
38+
39+
long getSettingId() {
40+
return settingId;
41+
}
42+
43+
long getLibraryId() {
44+
return libraryId;
45+
}
46+
47+
String getLibraryDisplayName() {
48+
return displayName;
49+
}
50+
51+
String getHooplaLibraryId() {
52+
return hooplaLibraryId;
53+
}
54+
55+
boolean isInstantEnabled() {
56+
return instantEnabled;
57+
}
58+
59+
boolean isFlexEnabled() {
60+
return flexEnabled;
61+
}
62+
63+
boolean isfullUpdateForLibrary() {
64+
return fullUpdateForLibrary;
65+
}
66+
67+
boolean hasHooplaLibraryId() {
68+
return hooplaLibraryId != null && !hooplaLibraryId.isEmpty();
69+
}
70+
71+
boolean isCirculationEnabled() {
72+
return circulationEnabled;
73+
}
74+
75+
boolean isCleanUpInstant() {
76+
return cleanUpInstant;
77+
}
78+
79+
boolean isCleanUpFlex() {
80+
return cleanUpFlex;
81+
}
82+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.turning_leaf_technologies.hoopla;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
class HooplaSettings2 {
7+
private final long settingsId;
8+
private final String apiUrl;
9+
private final String apiUsername;
10+
private final String apiPassword;
11+
private final int recordExtractionBatchSize;
12+
private final int indexingTime;
13+
private final String countryCode;
14+
15+
// Global metadata settings
16+
private final boolean runFullUpdate;
17+
private final long lastUpdateOfChangedRecords;
18+
private final long lastUpdateOfAllRecords;
19+
private final String lastRecordProcessed;
20+
21+
// Token settings
22+
private final String accessToken;
23+
private final long tokenExpirationTime;
24+
25+
private final boolean regroupAllRecords;
26+
27+
public HooplaSettings2(ResultSet settingsRS) throws SQLException {
28+
settingsId = settingsRS.getLong("id");
29+
apiUrl = settingsRS.getString("apiUrl");
30+
apiUsername = settingsRS.getString("apiUsername");
31+
apiPassword = settingsRS.getString("apiPassword");
32+
countryCode = settingsRS.getString("countryCode");
33+
34+
recordExtractionBatchSize = settingsRS.getInt("recordExtractionBatchSize");
35+
indexingTime = settingsRS.getInt("indexingTime");
36+
runFullUpdate = settingsRS.getBoolean("runFullUpdate");
37+
lastUpdateOfChangedRecords = settingsRS.getLong("lastUpdateOfChangedRecords");
38+
lastUpdateOfAllRecords = settingsRS.getLong("lastUpdateOfAllRecords");
39+
lastRecordProcessed = settingsRS.getString("lastRecordProcessed");
40+
41+
accessToken = settingsRS.getString("accessToken");
42+
tokenExpirationTime = settingsRS.getLong("tokenExpirationTime");
43+
44+
regroupAllRecords = settingsRS.getBoolean("regroupAllRecords");
45+
}
46+
47+
public long getSettingsId() {
48+
return settingsId;
49+
}
50+
51+
public String getApiUrl() {
52+
return apiUrl;
53+
}
54+
55+
public String getCountryCode() {
56+
return countryCode;
57+
}
58+
59+
public String getApiUsername() {
60+
return apiUsername;
61+
}
62+
63+
public String getApiPassword() {
64+
return apiPassword;
65+
}
66+
67+
public boolean isRunFullUpdate() {
68+
return runFullUpdate;
69+
}
70+
71+
public long getLastUpdateOfChangedRecords() {
72+
return lastUpdateOfChangedRecords;
73+
}
74+
75+
public long getLastUpdateOfAllRecords() {
76+
return lastUpdateOfAllRecords;
77+
}
78+
79+
public String getAccessToken() {
80+
return accessToken;
81+
}
82+
83+
public long getTokenExpirationTime() {
84+
return tokenExpirationTime;
85+
}
86+
87+
public boolean isRegroupAllRecords() {
88+
return regroupAllRecords;
89+
}
90+
91+
public int getRecordExtractionBatchSize() {
92+
return recordExtractionBatchSize;
93+
}
94+
95+
public int getIndexingTime() {
96+
return indexingTime;
97+
}
98+
99+
public String getLastRecordProcessed() {
100+
return lastRecordProcessed;
101+
}
102+
103+
}

0 commit comments

Comments
 (0)