Search in sources :

Example 1 with Copy

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

the class SearchResultDetailFragment method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final String bib = app.getLibrary().getIdent();
    if (item.getItemId() == R.id.action_reservation) {
        reservationStart();
        return true;
    } else if (item.getItemId() == R.id.action_lendebook) {
        bookingStart();
        return true;
    } else if (item.getItemId() == R.id.action_tocollection) {
        if (getActivity().getIntent().getBooleanExtra("from_collection", false)) {
            getActivity().finish();
        } else {
            Intent intent = new Intent(getActivity(), SearchResultDetailActivity.class);
            intent.putExtra(SearchResultDetailFragment.ARG_ITEM_ID, getItem().getCollectionId());
            startActivity(intent);
            getActivity().finish();
        }
        return true;
    } else if (item.getItemId() == R.id.action_share) {
        if (getItem() == null) {
            Toast toast = Toast.makeText(getActivity(), getString(R.string.share_wait), Toast.LENGTH_SHORT);
            toast.show();
        } else {
            final String title = getItem().getTitle();
            final String id = getItem().getId();
            final CharSequence[] items = { getString(R.string.share_link), getString(R.string.share_details) };
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(R.string.share_dialog_select);
            builder.setItems(items, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int di) {
                    OpacApi api = null;
                    try {
                        api = app.getApi();
                    } catch (OpacClient.LibraryRemovedException e) {
                        return;
                    }
                    if (di == 0) {
                        // Share link
                        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.addFlags(CompatibilityUtils.getNewDocumentIntentFlag());
                        // Add data to the intent, the receiving app will
                        // decide
                        // what to do with it.
                        intent.putExtra(Intent.EXTRA_SUBJECT, title);
                        String t = title;
                        try {
                            t = java.net.URLEncoder.encode(t, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                        }
                        String shareUrl = api.getShareUrl(id, t);
                        if (shareUrl != null) {
                            intent.putExtra(Intent.EXTRA_TEXT, shareUrl);
                            startActivity(Intent.createChooser(intent, getResources().getString(R.string.share)));
                        } else {
                            Toast toast = Toast.makeText(getActivity(), getString(R.string.share_notsupported), Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    } else {
                        // Share details
                        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.addFlags(CompatibilityUtils.getNewDocumentIntentFlag());
                        // Add data to the intent, the receiving app will
                        // decide
                        // what to do with it.
                        intent.putExtra(Intent.EXTRA_SUBJECT, title);
                        String t = title;
                        try {
                            t = t != null ? java.net.URLEncoder.encode(t, "UTF-8") : "";
                        } catch (UnsupportedEncodingException e) {
                        }
                        String text = title + "\n\n";
                        for (Detail detail : getItem().getDetails()) {
                            String colon = "";
                            if (!detail.getDesc().endsWith(":")) {
                                colon = ":";
                            }
                            text += detail.getDesc() + colon + "\n" + detail.getContent() + "\n\n";
                        }
                        List<Copy> copies = getItem().getCopies();
                        if (copies.size() > 0) {
                            text += getString(R.string.copies_head) + ":\n\n";
                        }
                        for (Copy copy : copies) {
                            String labelSeparator = ": ";
                            String infoTypeSeparator = "\n";
                            String branch = copy.getBranch();
                            String branchTxt = "";
                            if (branch != null && !branch.isEmpty()) {
                                branchTxt += getString(R.string.branch) + labelSeparator + branch + infoTypeSeparator;
                            }
                            String dept = copy.getDepartment();
                            String deptTxt = "";
                            if (dept != null && !dept.isEmpty()) {
                                deptTxt += getString(R.string.department) + labelSeparator + dept + infoTypeSeparator;
                            }
                            String loc = copy.getLocation();
                            String locTxt = "";
                            if (loc != null && !loc.isEmpty()) {
                                locTxt += getString(R.string.location) + labelSeparator + loc + infoTypeSeparator;
                            }
                            String shelfMark = copy.getShelfmark();
                            String shelfMarkTxt = "";
                            if (shelfMark != null && !shelfMark.isEmpty()) {
                                shelfMarkTxt += getString(R.string.shelfmark) + labelSeparator + shelfMark + infoTypeSeparator;
                            }
                            String status = copy.getStatus();
                            String statusTxt = "";
                            if (status != null && !status.isEmpty()) {
                                statusTxt += getString(R.string.status) + labelSeparator + status + infoTypeSeparator;
                            }
                            String res = copy.getReservations();
                            String resTxt = "";
                            if (res != null && !res.isEmpty()) {
                                resTxt += getString(R.string.reservations) + labelSeparator + res + infoTypeSeparator;
                            }
                            String url = copy.getUrl();
                            String urlTxt = "";
                            if (url != null && !url.isEmpty()) {
                                urlTxt += getString(R.string.url) + labelSeparator + url + infoTypeSeparator;
                            }
                            LocalDate retDate = copy.getReturnDate();
                            String retDateTxt = "";
                            if (retDate != null) {
                                retDateTxt += getString(R.string.return_date) + labelSeparator + DateTimeFormat.shortDate().print(copy.getReturnDate()) + infoTypeSeparator;
                            }
                            text += branchTxt + deptTxt + locTxt + shelfMarkTxt + statusTxt + resTxt + urlTxt + retDateTxt + "\n";
                        }
                        String shareUrl = api.getShareUrl(id, t);
                        if (shareUrl != null) {
                            text += shareUrl;
                        }
                        intent.putExtra(Intent.EXTRA_TEXT, text);
                        startActivity(Intent.createChooser(intent, getResources().getString(R.string.share)));
                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
        return true;
    } else if (item.getItemId() == R.id.action_star) {
        StarDataSource star = new StarDataSource(getActivity());
        if (getItem() == null) {
            Toast toast = Toast.makeText(getActivity(), getString(R.string.star_wait), Toast.LENGTH_SHORT);
            toast.show();
        } else if (getItem().getId() == null || getItem().getId().equals("")) {
            final String title = getItem().getTitle();
            if (title == null || title.equals("")) {
                Toast toast = Toast.makeText(getActivity(), getString(R.string.star_unsupported), Toast.LENGTH_LONG);
                toast.show();
            } else {
                if (star.isStarredTitle(bib, title)) {
                    star.remove(star.getItemByTitle(bib, title));
                    item.setIcon(R.drawable.ic_star_0_white_24dp);
                } else {
                    star.star(null, title, bib, getItem().getMediaType());
                    Toast toast = Toast.makeText(getActivity(), getString(R.string.starred), Toast.LENGTH_SHORT);
                    toast.show();
                    item.setIcon(R.drawable.ic_star_1_white_24dp);
                }
            }
        } else {
            final String title = getItem().getTitle();
            final String id = getItem().getId();
            if (star.isStarred(bib, id)) {
                star.remove(star.getItem(bib, id));
                item.setIcon(R.drawable.ic_star_0_white_24dp);
            } else {
                star.star(id, title, bib, getItem().getMediaType());
                Toast toast = Toast.makeText(getActivity(), getString(R.string.starred), Toast.LENGTH_SHORT);
                toast.show();
                item.setIcon(R.drawable.ic_star_1_white_24dp);
            }
        }
        return true;
    } else if (item.getItemId() == R.id.action_print) {
        if (getItem() == null) {
            Toast toast = Toast.makeText(getActivity(), getString(R.string.print_wait), Toast.LENGTH_SHORT);
            toast.show();
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                print();
            }
        }
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) OpacClient(de.geeksfactory.opacclient.OpacClient) DialogInterface(android.content.DialogInterface) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) LocalDate(org.joda.time.LocalDate) StarDataSource(de.geeksfactory.opacclient.storage.StarDataSource) Toast(android.widget.Toast) Copy(de.geeksfactory.opacclient.objects.Copy) OpacApi(de.geeksfactory.opacclient.apis.OpacApi) OnClickListener(android.view.View.OnClickListener) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 2 with Copy

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

the class CopiesAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Copy copy = copies.get(position);
    setTextOrHide(copy.getBranch(), holder.tvBranch);
    setTextOrHide(copy.getDepartment(), holder.tvDepartment);
    setTextOrHide(copy.getLocation(), holder.tvLocation);
    setTextOrHide(copy.getShelfmark(), holder.tvShelfmark);
    setTextOrHide(copy.getStatus(), holder.tvStatus);
    setTextOrHide(copy.getReservations(), holder.tvReservations);
    setTextOrHide(copy.getUrl(), holder.tvUrl);
    if (copy.getReturnDate() != null) {
        holder.tvReturndate.setText(DateTimeFormat.shortDate().print(copy.getReturnDate()));
        holder.tvReturndate.setVisibility(View.VISIBLE);
    } else {
        holder.tvReturndate.setVisibility(View.GONE);
    }
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy)

Example 3 with Copy

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

the class VuFindSearchTest method testParseDetail.

@Test
public void testParseDetail() throws OpacApi.OpacErrorException, JSONException, NotReachableException {
    String html = readResource("/vufind/result_detail/" + file);
    // we may not have all files for all libraries
    if (html == null)
        return;
    DetailedItem result = VuFind.parseDetail("0", Jsoup.parse(html), getData(file));
    for (Copy copy : result.getCopies()) {
        assertContainsData(copy.getStatus());
        assertNullOrNotEmpty(copy.getBarcode());
        assertNullOrNotEmpty(copy.getBranch());
        assertNullOrNotEmpty(copy.getDepartment());
        assertNullOrNotEmpty(copy.getLocation());
        assertNullOrNotEmpty(copy.getReservations());
        assertNullOrNotEmpty(copy.getShelfmark());
        assertNullOrNotEmpty(copy.getUrl());
        if (copy.getStatus().equals("Entliehen"))
            assertNotNull(copy.getReturnDate());
    }
    for (Volume volume : result.getVolumes()) {
        assertContainsData(volume.getId());
        assertContainsData(volume.getTitle());
    }
    assertEquals(result.getTitle(), getDetailTitle(file));
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) Volume(de.geeksfactory.opacclient.objects.Volume) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Test(org.junit.Test)

Example 4 with Copy

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

the class BibliothecaSearchTest method testParseResult.

@Test
public void testParseResult() throws OpacApi.OpacErrorException, JSONException, NotReachableException {
    String html = readResource("/bibliotheca/result_detail/" + file);
    // we may not have all files for all libraries
    if (html == null)
        return;
    DetailedItem result = Bibliotheca.parseResult(html, getData(file));
    for (Copy copy : result.getCopies()) {
        assertContainsData(copy.getStatus());
        assertNullOrNotEmpty(copy.getBarcode());
        assertNullOrNotEmpty(copy.getBranch());
        assertNullOrNotEmpty(copy.getDepartment());
        assertNullOrNotEmpty(copy.getLocation());
        assertNullOrNotEmpty(copy.getReservations());
        assertNullOrNotEmpty(copy.getShelfmark());
        assertNullOrNotEmpty(copy.getUrl());
        if (copy.getStatus().equals("Entliehen"))
            assertNotNull(copy.getReturnDate());
    }
    assertContainsData(result.getReservation_info());
    assertEquals(result.getTitle(), getDetailTitle(file));
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Test(org.junit.Test)

Example 5 with Copy

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

the class Bibliotheca method parseResult.

static DetailedItem parseResult(String html, JSONObject data) {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(data.optString("baseurl"));
    DetailedItem result = new DetailedItem();
    if (doc.select(".detail_cover img").size() == 1) {
        result.setCover(doc.select(".detail_cover img").get(0).attr("src"));
    }
    result.setTitle(doc.select(".detail_titel").text());
    Elements detailtrs = doc.select(".detailzeile table tr");
    for (int i = 0; i < detailtrs.size(); i++) {
        Element tr = detailtrs.get(i);
        if (tr.child(0).hasClass("detail_feld")) {
            String title = tr.child(0).text();
            String content = tr.child(1).text();
            if (title.equals("Gesamtwerk:") || title.equals("Erschienen in:")) {
                try {
                    if (tr.child(1).select("a").size() > 0) {
                        Element link = tr.child(1).select("a").first();
                        List<NameValuePair> query = URLEncodedUtils.parse(new URI(link.absUrl("href")), "UTF-8");
                        for (NameValuePair q : query) {
                            if (q.getName().equals("MedienNr")) {
                                result.setCollectionId(q.getValue());
                            }
                        }
                    }
                } catch (URISyntaxException e) {
                }
            } else {
                if (content.contains("hier klicken") && tr.child(1).select("a").size() > 0) {
                    content += " " + tr.child(1).select("a").first().attr("href");
                }
                result.addDetail(new Detail(title, content));
            }
        }
    }
    Elements detailcenterlinks = doc.select(".detailzeile_center a.detail_link");
    for (int i = 0; i < detailcenterlinks.size(); i++) {
        Element a = detailcenterlinks.get(i);
        result.addDetail(new Detail(a.text().trim(), a.absUrl("href")));
    }
    try {
        JSONObject copymap = new JSONObject();
        if (data.has("copiestable")) {
            copymap = data.getJSONObject("copiestable");
        } else {
            Elements ths = doc.select(".exemplartab .exemplarmenubar th");
            for (int i = 0; i < ths.size(); i++) {
                Element th = ths.get(i);
                String head = th.text().trim();
                if (head.equals("Zweigstelle")) {
                    copymap.put("branch", i);
                } else if (head.equals("Abteilung")) {
                    copymap.put("department", i);
                } else if (head.equals("Bereich") || head.equals("Standort")) {
                    copymap.put("location", i);
                } else if (head.equals("Signatur")) {
                    copymap.put("signature", i);
                } else if (head.equals("Barcode") || head.equals("Medien-Nummer")) {
                    copymap.put("barcode", i);
                } else if (head.equals("Status")) {
                    copymap.put("status", i);
                } else if (head.equals("Frist") || head.matches("Verf.+gbar")) {
                    copymap.put("returndate", i);
                } else if (head.equals("Vorbestellungen") || head.equals("Reservierungen")) {
                    copymap.put("reservations", i);
                }
            }
        }
        Elements exemplartrs = doc.select(".exemplartab .tabExemplar, .exemplartab .tabExemplar_");
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        for (int i = 0; i < exemplartrs.size(); i++) {
            Element tr = exemplartrs.get(i);
            Copy copy = new Copy();
            Iterator<?> keys = copymap.keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                int index;
                try {
                    index = copymap.has(key) ? copymap.getInt(key) : -1;
                } catch (JSONException e1) {
                    index = -1;
                }
                if (index >= 0) {
                    try {
                        copy.set(key, tr.child(index).text(), fmt);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                }
            }
            result.addCopy(copy);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        Elements bandtrs = doc.select("table .tabBand a");
        for (int i = 0; i < bandtrs.size(); i++) {
            Element tr = bandtrs.get(i);
            Volume volume = new Volume();
            volume.setId(tr.attr("href").split("=")[1]);
            volume.setTitle(tr.text());
            result.addVolume(volume);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (doc.select(".detail_vorbest a").size() == 1) {
        result.setReservable(true);
        result.setReservation_info(doc.select(".detail_vorbest a").attr("href"));
    }
    return result;
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Element(org.jsoup.nodes.Element) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) Volume(de.geeksfactory.opacclient.objects.Volume) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Aggregations

Copy (de.geeksfactory.opacclient.objects.Copy)22 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)17 Detail (de.geeksfactory.opacclient.objects.Detail)16 Element (org.jsoup.nodes.Element)15 Document (org.jsoup.nodes.Document)13 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)10 Elements (org.jsoup.select.Elements)10 HashMap (java.util.HashMap)8 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)8 IOException (java.io.IOException)6 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 Node (org.jsoup.nodes.Node)5 TextNode (org.jsoup.nodes.TextNode)5 Volume (de.geeksfactory.opacclient.objects.Volume)4 URI (java.net.URI)3