use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.
the class BiBer1992 method parseMediaList.
static List<LentItem> parseMediaList(AccountData res, Account account, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent) throws JSONException {
List<LentItem> media = new ArrayList<>();
if (doc == null) {
return media;
}
if (doc.select("form[name=medkl] table").size() == 0) {
return new ArrayList<LentItem>();
}
// parse result list
Map<String, Integer> copymap = new HashMap<>();
Map<String, Integer> colspanmap = new HashMap<>();
Elements headerCells = doc.select("form[name=medkl] table tr:has(th)").last().select("th");
JSONArray headersList = new JSONArray();
JSONArray unknownHeaders = new JSONArray();
int j = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
String colspan_str = headerCell.attr("colspan");
int colspan = 1;
if (!colspan_str.equals("")) {
colspan = Integer.valueOf(colspan_str);
}
headersList.put(header);
if (headers_lent.has(header)) {
if (!headers_lent.isNull(header)) {
copymap.put(headers_lent.getString(header), j);
colspanmap.put(headers_lent.getString(header), colspan);
}
} else {
unknownHeaders.put(header);
}
j += colspan;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(account.getLibrary(), "biber1992", "unknown header - lent", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject accounttable = data.getJSONObject("accounttable");
copymap = jsonToMap(accounttable);
}
Pattern expire = Pattern.compile("Ausweisg.ltigkeit: ([0-9.]+)");
Pattern fees = Pattern.compile("([0-9,.]+) .");
for (Element td : doc.select(".td01x09n")) {
String text = td.text().trim();
if (expire.matcher(text).matches()) {
res.setValidUntil(expire.matcher(text).replaceAll("$1"));
} else if (fees.matcher(text).matches()) {
res.setPendingFees(text);
}
}
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
Elements rowElements = doc.select("form[name=medkl] table tr");
// rows: skip 1st row -> title row
for (int i = 1; i < rowElements.size(); i++) {
Element tr = rowElements.get(i);
if (tr.child(0).tagName().equals("th")) {
continue;
}
LentItem item = new LentItem();
Elements mediatypeImg = tr.select("td img");
if (mediatypeImg.size() > 0) {
item.setMediaType(getMediaTypeFromImageFilename(null, mediatypeImg.get(0).attr("src"), data));
}
Pattern itemIdPat = Pattern.compile("javascript:(?:smAcc|smMedk)\\('[a-z]+','[a-z]+','([A-Za-z0-9]+)'\\)");
// columns: all elements of one media
for (Map.Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
if (tr.child(index).select("a").size() == 1) {
Matcher matcher = itemIdPat.matcher(tr.child(index).select("a").attr("href"));
if (matcher.find())
item.setId(matcher.group(1));
}
String value = tr.child(index).text().trim().replace("\u00A0", "");
if (colspanmap.get(key) > 1) {
for (int k = 1; k < colspanmap.get(key); k++) {
value = value + " " + tr.child(index + k).text().trim().replace("\u00A0", "");
}
value = value.trim();
}
switch(key) {
case "author+title":
item.setTitle(findTitleAndAuthor(value)[0]);
item.setAuthor(findTitleAndAuthor(value)[1]);
continue;
case "returndate":
try {
value = fmt.parseLocalDate(value).toString();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
break;
case "renewals_number":
case "status":
if (value != null && value.length() != 0) {
if (item.getStatus() == null) {
item.setStatus(value);
} else {
item.setStatus(item.getStatus() + ", " + value);
}
}
continue;
}
if (value != null && value.length() != 0)
item.set(key, value);
}
if (tr.select("input[type=checkbox][value=YES]").size() > 0) {
item.setProlongData(tr.select("input[type=checkbox][value=YES]").attr("name"));
}
media.add(item);
}
return media;
}
use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.
the class Bibliotheca method parse_account.
public static AccountData parse_account(Account acc, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent, JSONObject headers_reservations) throws JSONException, NotReachableException {
if (doc.select(".kontozeile_center table").size() == 0) {
throw new NotReachableException();
}
Map<String, Integer> copymap = new HashMap<>();
Elements headerCells = doc.select(".kontozeile_center table").get(0).select("tr.exemplarmenubar").get(0).children();
JSONArray headersList = new JSONArray();
JSONArray unknownHeaders = new JSONArray();
int i = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
headersList.put(header);
if (headers_lent.has(header)) {
if (!headers_lent.isNull(header))
copymap.put(headers_lent.getString(header), i);
} else {
unknownHeaders.put(header);
}
i++;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - lent", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject accounttable = data.getJSONObject("accounttable");
copymap = jsonToMap(accounttable);
}
List<LentItem> media = new ArrayList<>();
Elements exemplartrs = doc.select(".kontozeile_center table").get(0).select("tr.tabKonto");
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d/M/yyyy").withLocale(Locale.GERMAN);
for (Element tr : exemplartrs) {
LentItem item = new LentItem();
for (Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
if (key.equals("prolongurl")) {
if (tr.child(index).children().size() > 0) {
item.setProlongData(tr.child(index).child(0).attr("href"));
item.setRenewable(!tr.child(index).child(0).attr("href").contains("vermsg"));
}
} else if (key.equals("returndate")) {
try {
item.setDeadline(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setDeadline(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
}
}
} else {
item.set(key, tr.child(index).text());
}
}
media.add(item);
}
copymap = new HashMap<>();
headerCells = doc.select(".kontozeile_center table").get(1).select("tr.exemplarmenubar").get(0).children();
headersList = new JSONArray();
unknownHeaders = new JSONArray();
i = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
headersList.put(header);
if (headers_reservations.has(header)) {
if (!headers_reservations.isNull(header)) {
copymap.put(headers_reservations.getString(header), i);
}
} else {
unknownHeaders.put(header);
}
i++;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - reservations", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject reservationtable = data.getJSONObject("reservationtable");
copymap = jsonToMap(reservationtable);
}
List<ReservedItem> reservations = new ArrayList<>();
exemplartrs = doc.select(".kontozeile_center table").get(1).select("tr.tabKonto");
for (Element tr : exemplartrs) {
ReservedItem item = new ReservedItem();
for (Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
if (key.equals("cancelurl")) {
if (tr.child(index).children().size() > 0) {
item.setCancelData(tr.child(index).child(0).attr("href"));
}
} else if (key.equals("availability")) {
try {
item.setReadyDate(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setReadyDate(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
}
}
} else if (key.equals("expirationdate")) {
try {
item.setExpirationDate(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setExpirationDate(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
item.setStatus(tr.child(index).text());
}
}
} else {
item.set(key, tr.child(index).text());
}
}
reservations.add(item);
}
AccountData res = new AccountData(acc.getId());
for (Element row : doc.select(".kontozeile_center, div[align=center]")) {
String text = row.text().trim();
if (text.matches(".*Ausstehende Geb.+hren:[^0-9]+([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*")) {
text = text.replaceAll(".*Ausstehende Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*", "$1 $2");
res.setPendingFees(text);
}
if (text.matches("Ihr Ausweis ist g.ltig bis:.*")) {
text = text.replaceAll("Ihr Ausweis ist g.ltig bis:[^A-Za-z0-9]+", "");
res.setValidUntil(text);
} else if (text.matches("Ausweis g.ltig bis:.*")) {
text = text.replaceAll("Ausweis g.ltig bis:[^A-Za-z0-9]+", "");
res.setValidUntil(text);
}
}
res.setLent(media);
res.setReservations(reservations);
return res;
}
use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.
the class BiBer1992 method parseResList.
static List<ReservedItem> parseResList(Account account, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_reservations) throws JSONException {
List<ReservedItem> reservations = new ArrayList<>();
if (doc == null) {
// error message as html result
return reservations;
}
if (doc.select("form[name=vorml] table").size() == 0) {
return new ArrayList<ReservedItem>();
}
// parse result list
Map<String, Integer> copymap = new HashMap<>();
Elements headerCells = doc.select("form[name=vorml] table tr:has(th)").last().select("th");
JSONArray headersList = new JSONArray();
JSONArray unknownHeaders = new JSONArray();
int j = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
headersList.put(header);
if (headers_reservations.has(header)) {
if (!headers_reservations.isNull(header)) {
copymap.put(headers_reservations.getString(header), j);
}
} else {
unknownHeaders.put(header);
}
String colspan = headerCell.attr("colspan");
j += !colspan.equals("") ? Integer.valueOf(colspan) : 1;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(account.getLibrary(), "biber1992", "unknown header - reservations", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject reservationtable;
if (data.has("reservationtable")) {
reservationtable = data.getJSONObject("reservationtable");
} else {
// reservations not specifically supported, let's just try it
// with default values but fail silently
reservationtable = new JSONObject();
reservationtable.put("author", 3);
reservationtable.put("availability", 6);
reservationtable.put("branch", -1);
reservationtable.put("cancelurl", -1);
reservationtable.put("expirationdate", 5);
reservationtable.put("title", 3);
}
copymap = jsonToMap(reservationtable);
}
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
Elements rowElements = doc.select("form[name=vorml] table tr");
// rows: skip 1st row -> title row
for (int i = 1; i < rowElements.size(); i++) {
Element tr = rowElements.get(i);
if (tr.child(0).tagName().equals("th")) {
continue;
}
ReservedItem item = new ReservedItem();
item.setCancelData(tr.select("input[type=checkbox]").attr("name"));
Elements mediatypeImg = tr.select("td img");
if (mediatypeImg.size() > 0) {
item.setMediaType(getMediaTypeFromImageFilename(null, mediatypeImg.get(0).attr("src"), data));
}
// columns: all elements of one media
for (Map.Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
String value = tr.child(index).text().trim();
switch(key) {
case "author+title":
item.setTitle(findTitleAndAuthor(value)[0]);
item.setAuthor(findTitleAndAuthor(value)[1]);
continue;
case "availability":
try {
value = fmt.parseLocalDate(value).toString();
} catch (IllegalArgumentException e1) {
key = "status";
}
break;
case "expirationdate":
try {
value = fmt.parseLocalDate(value).toString();
} catch (IllegalArgumentException e1) {
key = "status";
}
break;
}
if (value != null && value.length() != 0) {
item.set(key, value);
}
}
reservations.add(item);
}
return reservations;
}
Aggregations