Search in sources :

Example 31 with LentItem

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

the class TouchPoint method account.

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    start();
    LoginResponse login = login(acc);
    if (!login.success) {
        return null;
    }
    AccountData adata = new AccountData(acc.getId());
    if (login.warning != null) {
        adata.setWarning(login.warning);
    }
    // Lent media
    httpGet(opac_url + "/userAccount.do?methodToCall=start", ENCODING);
    String html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&accountTyp=loaned", ENCODING);
    List<LentItem> lent = new ArrayList<>();
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    List<LentItem> nextpageLent = parse_medialist(doc);
    if (nextpageLent != null) {
        lent.addAll(nextpageLent);
    }
    if (doc.select(".pagination").size() > 0 && lent != null) {
        Element pagination = doc.select(".pagination").first();
        Elements pages = pagination.select("a");
        for (Element page : pages) {
            if (!page.hasAttr("href")) {
                continue;
            }
            html = httpGet(page.attr("abs:href"), ENCODING);
            doc = Jsoup.parse(html);
            doc.setBaseUri(opac_url);
            nextpageLent = parse_medialist(doc);
            if (nextpageLent != null) {
                lent.addAll(nextpageLent);
            }
        }
    }
    adata.setLent(lent);
    // Requested media ("Vormerkungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&accountTyp=requested", ENCODING);
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    List<ReservedItem> requested = new ArrayList<>();
    List<ReservedItem> nextpageRes = parse_reslist(doc);
    if (nextpageRes != null) {
        requested.addAll(nextpageRes);
    }
    if (doc.select(".pagination").size() > 0 && requested != null) {
        Element pagination = doc.select(".pagination").first();
        Elements pages = pagination.select("a");
        for (Element page : pages) {
            if (!page.hasAttr("href")) {
                continue;
            }
            html = httpGet(page.attr("abs:href"), ENCODING);
            doc = Jsoup.parse(html);
            doc.setBaseUri(opac_url);
            nextpageRes = parse_reslist(doc);
            if (nextpageRes != null) {
                requested.addAll(nextpageRes);
            }
        }
    }
    // Ordered media ("Bestellungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&accountTyp=ordered", ENCODING);
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    List<ReservedItem> nextpageOrd = parse_reslist(doc);
    if (nextpageOrd != null) {
        requested.addAll(nextpageOrd);
    }
    if (doc.select(".pagination").size() > 0 && requested != null) {
        Element pagination = doc.select(".pagination").first();
        Elements pages = pagination.select("a");
        for (Element page : pages) {
            if (!page.hasAttr("href")) {
                continue;
            }
            html = httpGet(page.attr("abs:href"), ENCODING);
            doc = Jsoup.parse(html);
            doc.setBaseUri(opac_url);
            nextpageOrd = parse_reslist(doc);
            if (nextpageOrd != null) {
                requested.addAll(nextpageOrd);
            }
        }
    }
    adata.setReservations(requested);
    // Fees
    if (doc.select("#fees").size() > 0) {
        String text = doc.select("#fees").first().text().trim();
        if (text.matches("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)")) {
            text = text.replaceAll("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)", "$1 $2");
            adata.setPendingFees(text);
        }
    }
    return adata;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 32 with LentItem

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

the class Zones method parseMediaList.

static List<LentItem> parseMediaList(Document doc) {
    List<LentItem> lent = new ArrayList<>();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy").withLocale(Locale.GERMAN);
    Pattern id_pat = Pattern.compile("javascript:renewItem\\('[0-9]+','(.*)'\\)");
    Pattern cannotrenew_pat = Pattern.compile("javascript:CannotRenewLoan\\('[0-9]+','(.*)','[0-9]+'\\)");
    for (Element table : doc.select(".LoansBrowseItemDetailsCellStripe table, " + ".LoansBrowseItemDetailsCell " + "table")) {
        LentItem item = new LentItem();
        for (Element tr : table.select("tr")) {
            String desc = tr.select(".LoanBrowseFieldNameCell").text().trim();
            String value = tr.select(".LoanBrowseFieldDataCell").text().trim();
            if (desc.equals("Titel")) {
                item.setTitle(value);
                if (tr.select(".LoanBrowseFieldDataCell a[href]").size() > 0) {
                    String href = tr.select(".LoanBrowseFieldDataCell a[href]").attr("href");
                    Map<String, String> params = getQueryParamsFirst(href);
                    if (params.containsKey("BACNO")) {
                        item.setId(params.get("BACNO"));
                    }
                }
            }
            if (desc.equals("Verfasser"))
                item.setAuthor(value);
            if (desc.equals("Mediennummer"))
                item.setBarcode(value);
            if (desc.equals("ausgeliehen in"))
                item.setHomeBranch(value);
            if (desc.matches("F.+lligkeits.*datum")) {
                value = value.split(" ")[0];
                try {
                    item.setDeadline(fmt.parseLocalDate(value));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
        if (table.select(".button[Title~=Zum]").size() == 1) {
            Matcher matcher1 = id_pat.matcher(table.select(".button[Title~=Zum]").attr("href"));
            if (matcher1.matches())
                item.setProlongData(matcher1.group(1));
        } else if (table.select(".CannotRenewLink").size() == 1) {
            Matcher matcher = cannotrenew_pat.matcher(table.select(".CannotRenewLink").attr("href").trim());
            if (matcher.matches()) {
                item.setProlongData("cannotrenew|" + matcher.group(1));
            }
            item.setRenewable(false);
        }
        lent.add(item);
    }
    return lent;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 33 with LentItem

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

the class AccountFragment method display.

public void display(final AccountData result, boolean fromcache) {
    accountData = result;
    if (getActivity() == null) {
        return;
    }
    swipeRefreshLayout.setVisibility(View.VISIBLE);
    llLoading.setVisibility(View.GONE);
    unsupportedErrorView.setVisibility(View.GONE);
    answerErrorView.setVisibility(View.GONE);
    errorView.removeAllViews();
    this.fromcache = fromcache;
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(app.getApplicationContext());
    displayHeader();
    /*
            Lent items
         */
    final boolean notification_on = sp.getBoolean(SyncAccountJobCreator.PREF_SYNC_SERVICE, false);
    boolean notification_problems = false;
    displayWarning();
    displayLentHeader();
    displayLentItems();
    if (lentEmpty != null) {
        // phone
        lentEmpty.setVisible(result.getLent().size() == 0);
    } else {
        // tablet
        tvLentEmpty.setVisibility(result.getLent().size() == 0 ? View.VISIBLE : View.GONE);
    }
    for (final LentItem item : result.getLent()) {
        try {
            if (notification_on && item.getDeadline() == null && !item.isEbook()) {
                notification_problems = true;
            }
        } catch (Exception e) {
            notification_problems = true;
        }
    }
    if (notification_problems) {
        if (tvError != null) {
            tvError.setVisibility(View.VISIBLE);
            tvError.setText(R.string.notification_problems);
        }
    }
    /*
            Reservations
         */
    displayResHeader();
    displayReservedItems();
    if (reservationsEmpty != null) {
        // phone
        reservationsEmpty.setVisible(result.getReservations().size() == 0);
    } else {
        // tablet
        tvReservationsEmpty.setVisibility(result.getReservations().size() == 0 ? View.VISIBLE : View.GONE);
    }
    displayAge();
    boolean hideCovers = true;
    for (LentItem item : result.getLent()) {
        if (item.getMediaType() != null || item.getCover() != null)
            hideCovers = false;
    }
    for (ReservedItem item : result.getReservations()) {
        if (item.getMediaType() != null || item.getCover() != null)
            hideCovers = false;
    }
    lentAdapter.setCoversHidden(hideCovers);
    resAdapter.setCoversHidden(hideCovers);
}
Also used : SharedPreferences(android.content.SharedPreferences) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) NoHttpResponseException(org.apache.http.NoHttpResponseException) JSONException(org.json.JSONException) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) ActivityNotFoundException(android.content.ActivityNotFoundException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) OpacErrorException(de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException) IOException(java.io.IOException)

Example 34 with LentItem

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

the class AccountFragment method export.

private void export() {
    if (refreshing) {
        Toast.makeText(getActivity(), R.string.account_no_concurrent, Toast.LENGTH_LONG).show();
        if (!refreshing) {
            refresh();
        }
        return;
    }
    Context ctx = getActivity() != null ? getActivity() : OpacClient.getEmergencyContext();
    AccountDataSource adatasource = new AccountDataSource(ctx);
    AccountData data = adatasource.getCachedAccountData(account);
    LocalDateTime dt = new LocalDateTime(adatasource.getCachedAccountDataTime(account));
    if (data == null)
        return;
    StringBuilder string = new StringBuilder();
    DateTimeFormatter fmt1 = DateTimeFormat.shortDateTime().withLocale(getResources().getConfiguration().locale);
    DateTimeFormatter fmt2 = DateTimeFormat.shortDate().withLocale(getResources().getConfiguration().locale);
    String dateStr = fmt1.print(dt);
    string.append(getResources().getString(R.string.accountdata_export_header, account.getLabel(), dateStr));
    string.append("\n\n");
    string.append(getResources().getString(R.string.lent_head));
    string.append("\n\n");
    for (LentItem item : data.getLent()) {
        appendIfNotEmpty(string, item.getTitle(), R.string.accountdata_title);
        appendIfNotEmpty(string, item.getAuthor(), R.string.accountdata_author);
        appendIfNotEmpty(string, item.getFormat(), R.string.accountdata_format);
        appendIfNotEmpty(string, item.getStatus(), R.string.accountdata_status);
        appendIfNotEmpty(string, item.getBarcode(), R.string.accountdata_lent_barcode);
        if (item.getDeadline() != null) {
            appendIfNotEmpty(string, fmt2.print(item.getDeadline()), R.string.accountdata_lent_deadline);
        }
        appendIfNotEmpty(string, item.getHomeBranch(), R.string.accountdata_lent_home_branch);
        appendIfNotEmpty(string, item.getLendingBranch(), R.string.accountdata_lent_lending_branch);
        string.append("\n");
    }
    if (data.getLent().size() == 0) {
        string.append(getResources().getString(R.string.lent_none));
    }
    string.append(getResources().getString(R.string.reservations_head));
    string.append("\n\n");
    for (ReservedItem item : data.getReservations()) {
        appendIfNotEmpty(string, item.getTitle(), R.string.accountdata_title);
        appendIfNotEmpty(string, item.getAuthor(), R.string.accountdata_author);
        appendIfNotEmpty(string, item.getFormat(), R.string.accountdata_format);
        appendIfNotEmpty(string, item.getStatus(), R.string.accountdata_status);
        if (item.getReadyDate() != null) {
            appendIfNotEmpty(string, fmt2.print(item.getReadyDate()), R.string.accountdata_reserved_ready_date);
        }
        if (item.getExpirationDate() != null) {
            appendIfNotEmpty(string, fmt2.print(item.getExpirationDate()), R.string.accountdata_reserved_expiration_date);
        }
        appendIfNotEmpty(string, item.getBranch(), R.string.accountdata_reserved_branch);
        string.append("\n");
    }
    if (data.getReservations().size() == 0) {
        string.append(getResources().getString(R.string.reservations_none));
    }
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, string.toString());
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share_dialog_select)));
}
Also used : Context(android.content.Context) LocalDateTime(org.joda.time.LocalDateTime) AccountDataSource(de.geeksfactory.opacclient.storage.AccountDataSource) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) Intent(android.content.Intent) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 35 with LentItem

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

the class AccountItemDetailActivity method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_account_item_detail, menu);
    MenuItem prolong = menu.findItem(R.id.action_prolong);
    MenuItem download = menu.findItem(R.id.action_download);
    MenuItem cancel = menu.findItem(R.id.action_cancel);
    MenuItem booking = menu.findItem(R.id.action_booking);
    OpacClient app = (OpacClient) getApplication();
    OpacApi api = null;
    try {
        api = app.getApi();
    } catch (OpacClient.LibraryRemovedException e) {
        e.printStackTrace();
    }
    if (item instanceof LentItem) {
        final LentItem i = (LentItem) item;
        cancel.setVisible(false);
        booking.setVisible(false);
        if (i.getProlongData() != null) {
            prolong.setVisible(true);
            // ViewCompat.setAlpha(prolong, item.isRenewable() ? 1f : 0.4f);
            download.setVisible(false);
        } else if (i.getDownloadData() != null && api != null && api instanceof EbookServiceApi) {
            prolong.setVisible(false);
            download.setVisible(true);
        } else {
            prolong.setVisible(false);
            download.setVisible(false);
        }
    } else if (item instanceof ReservedItem) {
        final ReservedItem i = (ReservedItem) item;
        prolong.setVisible(false);
        download.setVisible(false);
        if (i.getBookingData() != null) {
            booking.setVisible(true);
            cancel.setVisible(false);
        } else if (i.getCancelData() != null) {
            cancel.setVisible(true);
            booking.setVisible(false);
        } else {
            cancel.setVisible(false);
            booking.setVisible(false);
        }
    }
    return true;
}
Also used : OpacClient(de.geeksfactory.opacclient.OpacClient) OpacApi(de.geeksfactory.opacclient.apis.OpacApi) EbookServiceApi(de.geeksfactory.opacclient.apis.EbookServiceApi) MenuItem(android.view.MenuItem) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem)

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