Search in sources :

Example 1 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class Open method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, OpacErrorException, JSONException {
    String url = opac_url + "/" + data.getJSONObject("urls").getString("advanced_search") + NO_MOBILE;
    Document doc = Jsoup.parse(httpGet(url, getDefaultEncoding()));
    if (doc.select("[id$=LblErrorMsg]").size() > 0 && doc.select("[id$=ContentPane] input").size() == 0) {
        throw new OpacErrorException(doc.select("[id$=LblErrorMsg]").text());
    }
    Element module = doc.select(".ModOPENExtendedSearchModuleC").first();
    List<SearchField> fields = new ArrayList<>();
    JSONObject selectable = new JSONObject();
    selectable.put("selectable", true);
    JSONObject notSelectable = new JSONObject();
    notSelectable.put("selectable", false);
    // Selectable search criteria
    Elements options = module.select("select[id$=FirstSearchField] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setId(option.val());
        field.setDisplayName(option.text());
        field.setData(selectable);
        fields.add(field);
    }
    // More criteria
    Element moreHeader = null;
    if (module.select("table").size() == 1) {
        moreHeader = module.select("span[id$=LblMoreCriterias]").parents().select("tr").first();
    } else {
        // Newer OPEN, e.g. Erlangen
        moreHeader = module.select("span[id$=LblMoreCriterias]").first();
    }
    if (moreHeader != null) {
        Elements siblings = moreHeader.siblingElements();
        int startIndex = moreHeader.elementSiblingIndex();
        for (int i = startIndex; i < siblings.size(); i++) {
            Element tr = siblings.get(i);
            if (tr.select("input, select").size() == 0)
                continue;
            if (tr.select("input[type=text]").size() == 1) {
                Element input = tr.select("input[type=text]").first();
                TextSearchField field = new TextSearchField();
                field.setId(input.attr("name"));
                field.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field.setData(notSelectable);
                if (tr.text().contains("nur Ziffern"))
                    field.setNumber(true);
                fields.add(field);
            } else if (tr.select("input[type=text]").size() == 2) {
                Element input1 = tr.select("input[type=text]").get(0);
                Element input2 = tr.select("input[type=text]").get(1);
                TextSearchField field1 = new TextSearchField();
                field1.setId(input1.attr("name"));
                field1.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field1.setData(notSelectable);
                if (tr.text().contains("nur Ziffern"))
                    field1.setNumber(true);
                fields.add(field1);
                TextSearchField field2 = new TextSearchField();
                field2.setId(input2.attr("name"));
                field2.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field2.setData(notSelectable);
                field2.setHalfWidth(true);
                if (tr.text().contains("nur Ziffern"))
                    field2.setNumber(true);
                fields.add(field2);
            } else if (tr.select("select").size() == 1) {
                Element select = tr.select("select").first();
                DropdownSearchField dropdown = new DropdownSearchField();
                dropdown.setId(select.attr("name"));
                dropdown.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                List<DropdownSearchField.Option> values = new ArrayList<>();
                for (Element option : select.select("option")) {
                    DropdownSearchField.Option opt = new DropdownSearchField.Option(option.val(), option.text());
                    values.add(opt);
                }
                dropdown.setDropdownValues(values);
                fields.add(dropdown);
            } else if (tr.select("input[type=checkbox]").size() == 1) {
                Element checkbox = tr.select("input[type=checkbox]").first();
                CheckboxSearchField field = new CheckboxSearchField();
                field.setId(checkbox.attr("name"));
                field.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                fields.add(field);
            }
        }
    }
    return fields;
}
Also used : Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) JSONObject(org.json.JSONObject) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField)

Example 2 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class Primo method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, OpacErrorException, JSONException {
    start();
    String html = httpGet(opac_url + "/action/search.do?mode=Advanced&ct=AdvancedSearch&vid=" + vid, getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();
    if (doc.select("select#exlidInput_scope_1").size() < 1) {
        throw new NotReachableException();
    }
    Elements options = doc.select("select#exlidInput_scope_1").first().select("option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.val());
        field.setHint("");
        field.setData(new JSONObject());
        field.getData().put("meaning", option.val());
        fields.add(field);
    }
    if (fields.size() == 0) {
        // Weird JavaScript, e.g. view-source:http://vopac.nlg.gr/Search/Advanced
        Pattern pattern_key = Pattern.compile("searchFields\\[\"([^\"]+)\"\\] = \"([^\"]+)\";");
        for (Element script : doc.select("script")) {
            if (!script.html().contains("searchFields"))
                continue;
            for (String line : script.html().split("\n")) {
                Matcher matcher = pattern_key.matcher(line);
                if (matcher.find()) {
                    TextSearchField field = new TextSearchField();
                    field.setDisplayName(matcher.group(2));
                    field.setId(matcher.group(1));
                    field.setHint("");
                    field.setData(new JSONObject());
                    field.getData().put("meaning", field.getId());
                    fields.add(field);
                }
            }
        }
    }
    Elements selects = doc.select("#exlidInput_mediaType_, #exlidInput_publicationDate_, " + "#exlidInput_language_, #exlidSearchIn");
    for (Element select : selects) {
        DropdownSearchField field = new DropdownSearchField();
        if (select.parent().select("label").size() > 0) {
            field.setDisplayName(select.parent().select("label").first().text());
        } else {
            continue;
        }
        field.setId("#" + select.attr("id"));
        for (Element option : select.select("option")) {
            if (option.val().equals("all_items")) {
                field.addDropdownValue(0, option.val(), option.text());
            } else {
                field.addDropdownValue(option.val(), option.text());
            }
        }
        field.setData(new JSONObject());
        field.getData().put("meaning", field.getId());
        fields.add(field);
    }
    return fields;
}
Also used : Pattern(java.util.regex.Pattern) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) JSONObject(org.json.JSONObject)

Example 3 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class Primo method parse_search.

protected SearchRequestResult parse_search(Document doc, int page) throws OpacErrorException, IOException {
    doc.setBaseUri(opac_url + "/action/search.do");
    List<SearchResult> reslist = new ArrayList<>();
    int resnum = -1;
    if (doc.select(".EXLResultsNumbers").size() > 0) {
        try {
            resnum = Integer.valueOf(doc.select(".EXLResultsNumbers em, .PaginationLabel strong").first().text().trim().replace(".", "").replace(",", "").replace(" ", "").replace("Ergebnisse", ""));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    for (Element resrow : doc.select(".EXLResult")) {
        SearchResult res = new SearchResult();
        StringBuilder description = new StringBuilder();
        description.append("<b>").append(resrow.select(".EXLResultTitle").text()).append("</b>");
        if (resrow.select(".EXLResultAuthor").size() > 0) {
            description.append("<br />").append(resrow.select(".EXLResultAuthor").text());
        }
        if (resrow.select(".EXLResultDetails").size() > 0) {
            description.append("<br />").append(resrow.select(".EXLResultDetails").text());
        }
        String availSelect = ".EXLResultAvailability span, .EXLResultAvailability em";
        if (resrow.select(availSelect).size() > 0) {
            description.append("<br />").append(resrow.select(availSelect).first().ownText());
        }
        res.setInnerhtml(description.toString());
        if (resrow.select(".EXLResultStatusAvailable").size() > 0) {
            res.setStatus(SearchResult.Status.GREEN);
        } else if (resrow.select(".EXLResultStatusNotAvailable").size() > 0) {
            res.setStatus(SearchResult.Status.RED);
        } else if (resrow.select(".EXLResultStatusMaybeAvailable").size() > 0) {
            res.setStatus(SearchResult.Status.YELLOW);
        }
        res.setPage(page);
        for (Element a : resrow.select(".EXLResultTitle a, a.EXLThumbnailLinkMarker, .EXLDetailsTab a")) {
            Map<String, String> q = getQueryParamsFirst(a.absUrl("href"));
            if (q.containsKey("doc")) {
                res.setId(q.get("doc"));
                break;
            }
        }
        if (res.getId() == null) {
            continue;
        }
        if (resrow.select("img.EXLBriefResultsCover").size() > 0) {
            String src = resrow.select("img.EXLBriefResultsCover").first().absUrl("src");
            if (!src.contains("pixel.png")) {
                res.setCover(src);
            }
        }
        for (Map.Entry<String, SearchResult.MediaType> cls : mediaTypeClasses.entrySet()) {
            if (resrow.hasClass(cls.getKey()) || resrow.select("." + cls.getKey()).size() > 0) {
                res.setType(cls.getValue());
                break;
            }
        }
        if (resrow.select("a.EXLBriefResultsDisplayMultipleLink").size() > 0) {
            String url = resrow.select("a.EXLBriefResultsDisplayMultipleLink").first().absUrl("href");
            List<SearchQuery> query = new ArrayList<>();
            TextSearchField field = new TextSearchField("url", "url", false, false, "url", false, false);
            field.setVisible(false);
            query.add(new SearchQuery(field, url));
            res.setChildQuery(query);
        }
        reslist.add(res);
    }
    return new SearchRequestResult(reslist, resnum, page);
}
Also used : SearchQuery(de.geeksfactory.opacclient.searchfields.SearchQuery) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) SearchResult(de.geeksfactory.opacclient.objects.SearchResult) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class Bibliotheca method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();
    }
    List<SearchField> fields = new ArrayList<>();
    // Read branches and media types
    FormBody.Builder formData = new FormBody.Builder(Charset.forName(getDefaultEncoding()));
    formData.add("link_profis.x", "0");
    formData.add("link_profis.y", "1");
    String html = httpPost(opac_url + "/index.asp", formData.build(), getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    Elements fieldElems = doc.select(".suchfeldinhalt");
    for (Element fieldElem : fieldElems) {
        String name = fieldElem.select(".suchfeld_inhalt_titel label").text();
        String hint = "";
        if (fieldElem.select(".suchfeld_inhalt_input").size() > 0) {
            List<TextNode> textNodes = fieldElem.select(".suchfeld_inhalt_input").first().textNodes();
            if (textNodes.size() > 0) {
                for (TextNode node : textNodes) {
                    String text = node.getWholeText().replace("\n", "");
                    if (!text.equals("")) {
                        hint = node.getWholeText().replace("\n", "");
                        break;
                    }
                }
            }
        }
        Elements inputs = fieldElem.select(".suchfeld_inhalt_input input[type=text], " + ".suchfeld_inhalt_input select");
        if (inputs.size() == 1) {
            SearchField field = createSearchField(name, hint, inputs.get(0));
            Elements radios = fieldElem.select("input[type=radio]");
            if (field instanceof TextSearchField && radios.size() > 0) {
                TextSearchField tf = (TextSearchField) field;
                if (radios.get(0).attr("value").equals("stich")) {
                    tf.setFreeSearch(true);
                    if (fieldElem.select("label[for=stichtit_sich]").size() > 0) {
                        tf.setHint(fieldElem.select("label[for=stichtit_sich]").text().trim());
                    }
                    JSONObject addData = new JSONObject();
                    JSONObject params = new JSONObject();
                    params.put("stichtit", "stich");
                    addData.put("additional_params", params);
                    tf.setData(addData);
                }
                if (radios.size() == 2 && radios.get(1).attr("value").equals("titel")) {
                    TextSearchField tf2 = new TextSearchField();
                    tf2.setId(tf.getId());
                    if (fieldElem.select("label[for=stichtit_titel]").size() > 0) {
                        tf2.setDisplayName(fieldElem.select("label[for=stichtit_titel]").text().trim());
                    }
                    JSONObject addData = new JSONObject();
                    JSONObject params = new JSONObject();
                    params.put("stichtit", "titel");
                    addData.put("additional_params", params);
                    tf2.setData(addData);
                    fields.add(tf2);
                }
            }
            fields.add(field);
        } else if (inputs.size() == 2 && inputs.select("input[type=text]").size() == 2) {
            // Two text fields, e.g. year from/to or two keywords
            fields.add(createSearchField(name, hint, inputs.get(0)));
            TextSearchField secondField = (TextSearchField) createSearchField(name, hint, inputs.get(1));
            secondField.setHalfWidth(true);
            fields.add(secondField);
        } else if (inputs.size() == 2 && inputs.get(0).tagName().equals("select") && inputs.get(1).tagName().equals("input") && inputs.get(0).attr("name").equals("feld1")) {
            // Break it down into single text fields.
            for (Element option : inputs.get(0).select("option")) {
                TextSearchField field = new TextSearchField();
                field.setHint(hint);
                field.setDisplayName(option.text());
                field.setId(inputs.get(1).attr("name") + "$" + option.attr("value"));
                JSONObject data = new JSONObject();
                JSONObject params = new JSONObject();
                params.put(inputs.get(0).attr("name"), option.attr("value"));
                data.put("additional_params", params);
                field.setData(data);
                fields.add(field);
            }
        }
    }
    if (fields.size() > 0) {
        DropdownSearchField orderField = new DropdownSearchField("orderselect", stringProvider.getString(StringProvider.ORDER), false, null);
        orderField.addDropdownValue("1", stringProvider.getString(StringProvider.ORDER_DEFAULT));
        orderField.addDropdownValue("2:desc", stringProvider.getString(StringProvider.ORDER_YEAR_DESC));
        orderField.addDropdownValue("2:asc", stringProvider.getString(StringProvider.ORDER_YEAR_ASC));
        orderField.addDropdownValue("3:desc", stringProvider.getString(StringProvider.ORDER_CATEGORY_DESC));
        orderField.addDropdownValue("3:asc", stringProvider.getString(StringProvider.ORDER_CATEGORY_ASC));
        orderField.setMeaning(Meaning.ORDER);
        fields.add(orderField);
    }
    return fields;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) FormBody(okhttp3.FormBody) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) JSONObject(org.json.JSONObject)

Example 5 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class Bibliotheca method createSearchField.

private SearchField createSearchField(String name, String hint, Element input) {
    if (input.tagName().equals("input") && input.attr("type").equals("text")) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(name);
        field.setHint(hint);
        field.setId(input.attr("name"));
        return field;
    } else if (input.tagName().equals("select")) {
        DropdownSearchField field = new DropdownSearchField();
        field.setDisplayName(name);
        field.setId(input.attr("name"));
        for (Element option : input.select("option")) {
            field.addDropdownValue(option.attr("value"), option.text());
        }
        return field;
    } else {
        return null;
    }
}
Also used : Element(org.jsoup.nodes.Element) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField)

Aggregations

TextSearchField (de.geeksfactory.opacclient.searchfields.TextSearchField)32 DropdownSearchField (de.geeksfactory.opacclient.searchfields.DropdownSearchField)27 SearchField (de.geeksfactory.opacclient.searchfields.SearchField)23 Element (org.jsoup.nodes.Element)21 ArrayList (java.util.ArrayList)19 Document (org.jsoup.nodes.Document)18 Elements (org.jsoup.select.Elements)15 BarcodeSearchField (de.geeksfactory.opacclient.searchfields.BarcodeSearchField)13 CheckboxSearchField (de.geeksfactory.opacclient.searchfields.CheckboxSearchField)10 JSONObject (org.json.JSONObject)9 SearchQuery (de.geeksfactory.opacclient.searchfields.SearchQuery)6 ViewGroup (android.view.ViewGroup)5 CheckBox (android.widget.CheckBox)5 EditText (android.widget.EditText)5 Spinner (android.widget.Spinner)5 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)3 HashMap (java.util.HashMap)3