Search in sources :

Example 21 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class Adis method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    start();
    Document doc = htmlGet(opac_url + ";jsessionid=" + s_sid + "?service=" + s_service + getSpParams("SBK"));
    doc = handleLoginForm(doc, account);
    boolean split_title_author = true;
    if (doc.head().html().contains("VOEBB")) {
        split_title_author = false;
    }
    AccountData adata = new AccountData(account.getId());
    for (Element tr : doc.select(".aDISListe tr")) {
        if (tr.child(0).text().matches(".*F.+llige Geb.+hren.*")) {
            adata.setPendingFees(tr.child(1).text().trim());
        }
        if (tr.child(0).text().matches(".*Ausweis g.+ltig bis.*")) {
            adata.setValidUntil(tr.child(1).text().trim());
        }
    }
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    // Ausleihen
    String alink = null;
    int anum = 0;
    List<LentItem> lent = new ArrayList<>();
    for (Element tr : doc.select(".rTable_div tr")) {
        if (tr.select("a").size() == 1) {
            if (tr.select("a").first().absUrl("href").contains("sp=SZA")) {
                alink = tr.select("a").first().absUrl("href");
                anum = Integer.parseInt(tr.child(0).text().trim());
            }
        }
    }
    if (alink != null) {
        Document adoc = htmlGet(alink);
        s_alink = alink;
        List<NameValuePair> form = new ArrayList<>();
        String prolongTest = null;
        for (Element input : adoc.select("input, select")) {
            if (!"image".equals(input.attr("type")) && !"submit".equals(input.attr("type")) && !"".equals(input.attr("name"))) {
                if (input.attr("type").equals("checkbox") && !input.hasAttr("value")) {
                    input.val("on");
                }
                form.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
            } else if (input.val().matches(".+verl.+ngerbar.+")) {
                prolongTest = input.attr("name");
            }
        }
        if (prolongTest != null) {
            form.add(new BasicNameValuePair(prolongTest, "Markierte Titel verlängerbar?"));
            Document adoc_new = htmlPost(opac_url + ";jsessionid=" + s_sid, form);
            if (adoc_new.select(".message h1").size() == 0) {
                adoc = adoc_new;
            }
        }
        parseMediaList(adoc, alink, lent, split_title_author);
        assert (lent.size() == anum);
        form = new ArrayList<>();
        boolean cancelButton = false;
        for (Element input : adoc.select("input, select")) {
            if (!"image".equals(input.attr("type")) && !"submit".equals(input.attr("type")) && !"checkbox".equals(input.attr("type")) && !"".equals(input.attr("name"))) {
                form.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
            }
            if ("submit".equals(input.attr("type")) && "Abbrechen".equals(input.attr("value")) && !cancelButton) {
                // Stuttgart: Cancel button instead of toolbar back button
                form.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
                cancelButton = true;
            }
        }
        if (!cancelButton) {
            form.add(new BasicNameValuePair("$Toolbar_0.x", "1"));
            form.add(new BasicNameValuePair("$Toolbar_0.y", "1"));
        }
        doc = htmlPost(opac_url + ";jsessionid=" + s_sid, form);
    } else {
        assert (anum == 0);
    }
    adata.setLent(lent);
    List<String[]> rlinks = new ArrayList<>();
    int rnum = 0;
    List<ReservedItem> res = new ArrayList<>();
    for (Element tr : doc.select(".rTable_div tr")) {
        if (tr.select("a").size() == 1) {
            if ((tr.text().contains("Reservationen") || tr.text().contains("Vormerkung") || tr.text().contains("Fernleihbestellung") || tr.text().contains("Bereitstellung") || tr.text().contains("Bestellw") || tr.text().contains("Magazin")) && !tr.child(0).text().trim().equals("")) {
                rlinks.add(new String[] { tr.select("a").text(), tr.select("a").first().absUrl("href") });
                rnum += Integer.parseInt(tr.child(0).text().trim());
            }
        }
    }
    for (String[] rlink : rlinks) {
        Document rdoc = htmlGet(rlink[1]);
        boolean error = parseReservationList(rdoc, rlink, split_title_author, res, fmt, stringProvider);
        if (error) {
            // Maybe we should send a bug report here, but using ACRA breaks
            // the unit tests
            adata.setWarning("Beim Abrufen der Reservationen ist ein Problem aufgetreten");
        }
        List<NameValuePair> form = new ArrayList<>();
        for (Element input : rdoc.select("input, select")) {
            if (!"image".equals(input.attr("type")) && !"submit".equals(input.attr("type")) && !"checkbox".equals(input.attr("type")) && !"".equals(input.attr("name"))) {
                form.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
            }
        }
        form.add(new BasicNameValuePair("$Toolbar_0.x", "1"));
        form.add(new BasicNameValuePair("$Toolbar_0.y", "1"));
        htmlPost(opac_url + ";jsessionid=" + s_sid, form);
    }
    assert (res.size() == rnum);
    adata.setReservations(res);
    return adata;
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) AccountData(de.geeksfactory.opacclient.objects.AccountData) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 22 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem 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;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 23 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class BiBer1992 method account.

/*
     * (non-Javadoc)
     *
     * @see
     * OpacApi#account(de.geeksfactory.opacclient
     * .objects.Account)
     *
     * POST-format: BENUTZER xxxxxxxxx FUNC medk LANG de PASSWORD ddmmyyyy
     */
@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    AccountData res = new AccountData(account.getId());
    // get media
    List<LentItem> media = accountGetMedia(account, res);
    res.setLent(media);
    // get reservations
    List<ReservedItem> reservations = accountGetReservations(account);
    res.setReservations(reservations);
    return res;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem)

Example 24 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class ReminderHelperTest method stubLentItem.

private LentItem stubLentItem(LocalDate date, long dbId) {
    ArrayList<LentItem> lentItems = new ArrayList<>();
    LentItem item = new LentItem();
    item.setDeadline(date);
    item.setDbId(dbId);
    lentItems.add(item);
    when(data.getAllLentItems()).thenReturn(lentItems);
    return item;
}
Also used : ArrayList(java.util.ArrayList) LentItem(de.geeksfactory.opacclient.objects.LentItem)

Example 25 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class AccountDataSource method getCachedAccountData.

public AccountData getCachedAccountData(Account account) {
    AccountData adata = new AccountData(account.getId());
    List<LentItem> lent = new ArrayList<>();
    String[] selectionArgs = { "" + account.getId() };
    Cursor cursor = database.query(AccountDatabase.TABLENAME_LENT, AccountDatabase.COLUMNS_LENT, "account = ?", selectionArgs, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        LentItem entry = cursorToLentItem(cursor);
        lent.add(entry);
        cursor.moveToNext();
    }
    cursor.close();
    adata.setLent(lent);
    List<ReservedItem> res = new ArrayList<>();
    cursor = database.query(AccountDatabase.TABLENAME_RESERVATION, AccountDatabase.COLUMNS_RESERVATIONS, "account = ?", selectionArgs, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        ReservedItem entry = cursorToReservedItem(cursor);
        res.add(entry);
        cursor.moveToNext();
    }
    cursor.close();
    adata.setReservations(res);
    String[] selA = { "" + account.getId() };
    cursor = database.query("accounts", new String[] { "pendingFees", "validUntil", "warning" }, "id = ?", selA, null, null, null);
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {
        adata.setPendingFees(cursor.getString(0));
        adata.setValidUntil(cursor.getString(1));
        adata.setWarning(cursor.getString(2));
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return adata;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Cursor(android.database.Cursor)

Aggregations

LentItem (de.geeksfactory.opacclient.objects.LentItem)43 ArrayList (java.util.ArrayList)26 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)16 Element (org.jsoup.nodes.Element)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)14 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)12 Test (org.junit.Test)10 JSONObject (org.json.JSONObject)9 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)7 Pattern (java.util.regex.Pattern)5 Intent (android.content.Intent)4 Cursor (android.database.Cursor)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Matcher (java.util.regex.Matcher)4 LocalDate (org.joda.time.LocalDate)4 JSONException (org.json.JSONException)4 Account (de.geeksfactory.opacclient.objects.Account)3