Search in sources :

Example 16 with ReservedItem

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

the class AccountDataSource method storeCachedAccountData.

public void storeCachedAccountData(Account account, AccountData adata) {
    if (adata == null) {
        return;
    }
    long time = System.currentTimeMillis();
    ContentValues update = new ContentValues();
    update.put("cached", time);
    update.put("pendingFees", adata.getPendingFees());
    update.put("validUntil", adata.getValidUntil());
    update.put("warning", adata.getWarning());
    database.update(AccountDatabase.TABLENAME_ACCOUNTS, update, "id = ?", new String[] { "" + account.getId() });
    database.delete(AccountDatabase.TABLENAME_LENT, "account = ?", new String[] { "" + account.getId() });
    for (LentItem entry : adata.getLent()) {
        ContentValues insertmapping = lentItemToContentValues(entry, account.getId());
        database.insert(AccountDatabase.TABLENAME_LENT, null, insertmapping);
    }
    database.delete(AccountDatabase.TABLENAME_RESERVATION, "account = ?", new String[] { "" + account.getId() });
    for (ReservedItem entry : adata.getReservations()) {
        ContentValues insertmapping = reservedItemToContentValues(entry, account.getId());
        database.insert(AccountDatabase.TABLENAME_RESERVATION, null, insertmapping);
    }
}
Also used : ContentValues(android.content.ContentValues) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem)

Example 17 with ReservedItem

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

the class PicaOld method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    if (!initialised) {
        start();
    }
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("ACT", "UI_DATA"));
    params.add(new BasicNameValuePair("HOST_NAME", ""));
    params.add(new BasicNameValuePair("HOST_PORT", ""));
    params.add(new BasicNameValuePair("HOST_SCRIPT", ""));
    params.add(new BasicNameValuePair("LOGIN", "KNOWNUSER"));
    params.add(new BasicNameValuePair("STATUS", "HML_OK"));
    params.add(new BasicNameValuePair("BOR_U", account.getName()));
    params.add(new BasicNameValuePair("BOR_PW", account.getPassword()));
    String html = httpPost(https_url + "/loan/DB=" + db + "/LNG=" + getLang() + "/USERINFO", new UrlEncodedFormEntity(params, getDefaultEncoding()), getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    AccountData res = new AccountData(account.getId());
    if (doc.select(".cnt .alert, .cnt .error").size() > 0) {
        String text = doc.select(".cnt .alert, .cnt .error").text();
        if (doc.select("table[summary^=User data]").size() > 0) {
            res.setWarning(text);
        } else {
            throw new OpacErrorException(text);
        }
    }
    // noinspection StatementWithEmptyBody
    if (doc.select("input[name=BOR_PW_ENC]").size() > 0) {
        pwEncoded = URLEncoder.encode(doc.select("input[name=BOR_PW_ENC]").attr("value"), "UTF-8");
    } else {
    // TODO: do something here to help fix bug #229
    }
    html = httpGet(https_url + "/loan/DB=" + db + "/LNG=" + getLang() + "/USERINFO?ACT=UI_LOL&BOR_U=" + account.getName() + "&BOR_PW_ENC=" + pwEncoded, getDefaultEncoding());
    doc = Jsoup.parse(html);
    html = httpGet(https_url + "/loan/DB=" + db + "/LNG=" + getLang() + "/USERINFO?ACT=UI_LOR&BOR_U=" + account.getName() + "&BOR_PW_ENC=" + pwEncoded, getDefaultEncoding());
    Document doc2 = Jsoup.parse(html);
    List<LentItem> media = new ArrayList<>();
    List<ReservedItem> reserved = new ArrayList<>();
    if (doc.select("table[summary^=list]").size() > 0 && !doc.select(".alert").text().contains("Keine Entleihungen") && !doc.select(".alert").text().contains("No outstanding loans") && !doc.select(".alert").text().contains("Geen uitlening") && !doc.select(".alert").text().contains("Aucun emprunt")) {
        List<String> renewalCounts = loadRenewalCounts(doc);
        parseMediaList(media, doc, stringProvider, renewalCounts);
    }
    if (doc2.select("table[summary^=list]").size() > 0 && !doc2.select(".alert").text().contains("Keine Vormerkungen") && !doc2.select(".alert").text().contains("No outstanding reservations") && !doc2.select(".alert").text().contains("Geen reservering") && !doc2.select(".alert").text().contains("Aucune réservation")) {
        updateLorReservations(doc);
        parseResList(reserved, doc2, stringProvider);
    }
    res.setLent(media);
    res.setReservations(reserved);
    return res;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) 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)

Example 18 with ReservedItem

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

the class PicaOld method parseResList.

static void parseResList(List<ReservedItem> media, Document doc, StringProvider stringProvider) throws OpacErrorException {
    Elements copytrs = doc.select("table[summary^=list] > tbody >  tr[valign=top]");
    int trs = copytrs.size();
    if (trs < 1) {
        throw new OpacErrorException(stringProvider.getString(StringProvider.COULD_NOT_LOAD_ACCOUNT));
    }
    assert (trs > 0);
    for (Element tr : copytrs) {
        ReservedItem item = new ReservedItem();
        if (tr.select("table[summary=title data]").size() > 0) {
            // Check if there is a checkbox to cancel this item
            if (tr.select("input").size() > 0) {
                item.setCancelData(tr.select("input").attr("value"));
            }
            Elements datatrs = tr.select("table[summary=title data] tr");
            item.setTitle(datatrs.get(0).text());
            List<TextNode> textNodes = datatrs.get(1).select("td").first().textNodes();
            List<TextNode> nodes = new ArrayList<>();
            Elements titles = datatrs.get(1).select("span.label-small");
            for (TextNode node : textNodes) {
                if (!node.text().equals(" ")) {
                    nodes.add(node);
                }
            }
            assert (nodes.size() == titles.size());
            for (int j = 0; j < nodes.size(); j++) {
                String title = titles.get(j).text();
                String value = nodes.get(j).text().trim().replace(";", "");
                // noinspection StatementWithEmptyBody
                if (title.contains("Signatur") || title.contains("shelf mark") || title.contains("signatuur")) {
                // not supported
                } else // noinspection StatementWithEmptyBody
                if (title.contains("Vormerkdatum")) {
                // not supported
                }
            }
        } else {
            // like in Kiel
            item.setTitle(tr.child(5).text().trim());
            item.setStatus(tr.child(17).text().trim());
            item.setCancelData(tr.child(1).select("input").attr("value"));
        }
        media.add(item);
    }
    assert (media.size() == trs);
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) TextNode(org.jsoup.nodes.TextNode) Elements(org.jsoup.select.Elements)

Example 19 with ReservedItem

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

the class TestApi method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    AccountData data = new AccountData(account.getId());
    List<LentItem> lent = new ArrayList<>();
    List<ReservedItem> reservations = new ArrayList<>();
    try {
        JSONObject d = new JSONObject(httpGet(library.getData().getString("url"), "UTF-8"));
        for (int i = 0; i < d.getJSONArray("lent").length(); i++) {
            JSONObject l = d.getJSONArray("lent").getJSONObject(i);
            LentItem lentItem = new LentItem();
            for (Iterator iter = l.keys(); iter.hasNext(); ) {
                String key = (String) iter.next();
                lentItem.set(key, l.getString(key));
            }
            lent.add(lentItem);
        }
        for (int i = 0; i < d.getJSONArray("reservations").length(); i++) {
            JSONObject l = d.getJSONArray("reservations").getJSONObject(i);
            ReservedItem resItem = new ReservedItem();
            for (Iterator iter = l.keys(); iter.hasNext(); ) {
                String key = (String) iter.next();
                resItem.set(key, l.getString(key));
            }
            reservations.add(resItem);
        }
    } catch (NotReachableException e) {
        for (int i = 0; i < 6; i++) {
            LentItem lentItem = new LentItem();
            lentItem.setAuthor("Max Mustermann");
            lentItem.setTitle("Lorem Ipsum");
            lentItem.setStatus("hier ist der Status");
            lentItem.setDeadline(new LocalDate(1442564454547L));
            lentItem.setRenewable(true);
            lentItem.setProlongData("foo");
            lentItem.setHomeBranch("Meine Zweigstelle");
            lentItem.setLendingBranch("Ausleihzweigstelle");
            lentItem.setBarcode("Barcode");
            lent.add(lentItem);
            ReservedItem reservedItem = new ReservedItem();
            reservedItem.setAuthor("Max Mustermann");
            reservedItem.setTitle("Lorem Ipsum");
            reservedItem.setReadyDate(LocalDate.now());
            reservations.add(reservedItem);
        }
    }
    data.setLent(lent);
    data.setReservations(reservations);
    return data;
}
Also used : JSONObject(org.json.JSONObject) AccountData(de.geeksfactory.opacclient.objects.AccountData) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) LocalDate(org.joda.time.LocalDate)

Example 20 with ReservedItem

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

the class TouchPoint method parse_reslist.

static List<ReservedItem> parse_reslist(Document doc) {
    List<ReservedItem> reservations = new ArrayList<>();
    Elements copytrs = doc.select(".data tr, #account-data .table tr");
    int trs = copytrs.size();
    if (trs <= 1) {
        return null;
    }
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        ReservedItem item = new ReservedItem();
        if (tr.text().contains("keine Daten") || tr.children().size() == 1) {
            return null;
        }
        item.setTitle(tr.child(2).select("b, strong").text().trim());
        try {
            String[] rowsplit2 = tr.child(2).html().split("<br[ /]*>");
            String[] rowsplit3 = tr.child(3).html().split("<br[ /]*>");
            if (rowsplit2.length > 1)
                item.setAuthor(rowsplit2[1].replace("</a>", "").trim());
            if (rowsplit3.length > 2)
                item.setBranch(rowsplit3[2].replace("</a>", "").trim());
            if (rowsplit3.length > 2) {
                item.setStatus(rowsplit3[0].trim() + " (" + rowsplit3[1].trim() + ")");
            }
            if (tr.select("a").size() > 0) {
                for (Element link : tr.select("a")) {
                    String href = link.attr("abs:href");
                    Map<String, String> hrefq = getQueryParamsFirst(href);
                    if (hrefq.containsKey("q")) {
                        item.setId(extractIdFromQ(hrefq.get("q")));
                    } else if ("cancel".equals(hrefq.get("methodToCall"))) {
                        item.setCancelData(href.split("\\?")[1]);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        reservations.add(item);
    }
    return reservations;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) Elements(org.jsoup.select.Elements) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Aggregations

ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)34 ArrayList (java.util.ArrayList)20 Element (org.jsoup.nodes.Element)17 LentItem (de.geeksfactory.opacclient.objects.LentItem)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)13 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)8 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)7 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)6 DummyStringProvider (de.geeksfactory.opacclient.i18n.DummyStringProvider)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Pattern (java.util.regex.Pattern)3 JSONException (org.json.JSONException)3 Intent (android.content.Intent)2 Account (de.geeksfactory.opacclient.objects.Account)2 Report (de.geeksfactory.opacclient.reporting.Report)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2