Search in sources :

Example 11 with TextSearchField

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

the class BiBer1992 method parseSearchFields.

/*
     * ----- media types ----- Example Wuerzburg: <td ...><input type="checkbox"
     * name="MT" value="1" ...></td> <td ...><img src="../image/spacer.gif.S"
     * title="Buch"><br>Buch</td>
     *
     * Example Friedrichshafen: <td ...><input type="checkbox" name="MS"
     * value="1" ...></td> <td ...><img src="../image/spacer.gif.S"
     * title="Buch"><br>Buch</td>
     *
     * Example Offenburg: <input type="radio" name="MT" checked
     * value="MTYP0">Alles&nbsp;&nbsp; <input type="radio" name="MT"
     * value="MTYP10">Belletristik&nbsp;&nbsp; Unfortunately Biber miss the end
     * tag </input>, so opt.text() does not work! (at least Offenburg)
     *
     * Example Essen, Aschaffenburg: <input type="radio" name="MT" checked
     * value="MTYP0"><img src="../image/all.gif.S" title="Alles"> <input
     * type="radio" name="MT" value="MTYP7"><img src="../image/cdrom.gif.S"
     * title="CD-ROM">
     *
     * ----- Branches ----- Example Essen,Erkrath: no closing </option> !!!
     * cannot be parsed by Jsoup, so not supported <select name="AORT"> <option
     * value="ZWST1">Altendorf </select>
     *
     * Example Hagen, Würzburg, Friedrichshafen: <select name="ZW" class="sel1">
     * <option selected value="ZWST0">Alle Bibliotheksorte</option> </select>
     */
@Override
public List<SearchField> parseSearchFields() throws IOException {
    List<SearchField> fields = new ArrayList<>();
    HttpGet httpget;
    if (opacDir.contains("opax")) {
        httpget = new HttpGet(opacUrl + "/" + opacDir + "/de/qsel.html.S");
    } else {
        httpget = new HttpGet(opacUrl + "/" + opacDir + "/de/qsel_main.S");
    }
    HttpResponse response = http_client.execute(httpget);
    if (response.getStatusLine().getStatusCode() == 500) {
        throw new NotReachableException(response.getStatusLine().getReasonPhrase());
    }
    String html = convertStreamToString(response.getEntity().getContent());
    HttpUtils.consume(response.getEntity());
    Document doc = Jsoup.parse(html);
    // get text fields
    Elements text_opts = doc.select("form select[name=REG1] option");
    for (Element opt : text_opts) {
        TextSearchField field = new TextSearchField();
        field.setId(opt.attr("value"));
        field.setDisplayName(opt.text());
        field.setHint("");
        fields.add(field);
    }
    // get media types
    Elements mt_opts = doc.select("form input[name~=(MT|MS)]");
    if (mt_opts.size() > 0) {
        DropdownSearchField mtDropdown = new DropdownSearchField();
        mtDropdown.setId(mt_opts.get(0).attr("name"));
        mtDropdown.setDisplayName("Medientyp");
        for (Element opt : mt_opts) {
            if (!opt.val().equals("")) {
                String text = opt.text();
                if (text.length() == 0) {
                    // text is empty, check layouts:
                    // Essen: <input name="MT"><img title="mediatype">
                    // Schaffenb: <input name="MT"><img alt="mediatype">
                    Element img = opt.nextElementSibling();
                    if (img != null && img.tagName().equals("img")) {
                        text = img.attr("title");
                        if (text.equals("")) {
                            text = img.attr("alt");
                        }
                    }
                }
                if (text.length() == 0) {
                    // text is still empty, check table layout, Example
                    // Friedrichshafen
                    // <td><input name="MT"></td> <td><img
                    // title="mediatype"></td>
                    Element td1 = opt.parent();
                    Element td2 = td1.nextElementSibling();
                    if (td2 != null) {
                        Elements td2Children = td2.select("img[title]");
                        if (td2Children.size() > 0) {
                            text = td2Children.get(0).attr("title");
                        }
                    }
                }
                if (text.length() == 0) {
                    // text is still empty, check images in label layout, Example
                    // Wiedenst
                    // <input type="radio" name="MT" id="MTYP1" value="MTYP1">
                    // <label for="MTYP1"><img src="http://www.wiedenest.de/bib/image/books
                    // .png" alt="Bücher" title="Bücher"></label>
                    Element label = opt.nextElementSibling();
                    if (label != null) {
                        Elements td2Children = label.select("img[title]");
                        if (td2Children.size() > 0) {
                            text = td2Children.get(0).attr("title");
                        }
                    }
                }
                if (text.length() == 0) {
                    // text is still empty: missing end tag like Offenburg
                    text = parse_option_regex(opt);
                }
                mtDropdown.addDropdownValue(opt.val(), text);
            }
        }
        fields.add(mtDropdown);
    }
    // get branches
    Elements br_opts = doc.select("form select[name=ZW] option");
    if (br_opts.size() > 0) {
        DropdownSearchField brDropdown = new DropdownSearchField();
        brDropdown.setId(br_opts.get(0).parent().attr("name"));
        brDropdown.setDisplayName(br_opts.get(0).parent().parent().previousElementSibling().text().replace("\u00a0", "").replace("?", "").trim());
        for (Element opt : br_opts) {
            brDropdown.addDropdownValue(opt.val(), opt.text());
        }
        fields.add(brDropdown);
    }
    Elements sort_opts = doc.select("form select[name=SORTX] option");
    if (sort_opts.size() > 0) {
        DropdownSearchField sortDropdown = new DropdownSearchField();
        sortDropdown.setId(sort_opts.get(0).parent().attr("name"));
        sortDropdown.setDisplayName(sort_opts.get(0).parent().parent().previousElementSibling().text().replace("\u00a0", "").replace("?", "").trim());
        for (Element opt : sort_opts) {
            sortDropdown.addDropdownValue(opt.val(), opt.text());
        }
        fields.add(sortDropdown);
    }
    return fields;
}
Also used : BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField)

Example 12 with TextSearchField

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

the class SearchFragment method saveSearchQuery.

public List<SearchQuery> saveSearchQuery() {
    saveHomeBranch();
    List<SearchQuery> query = new ArrayList<>();
    if (fields == null || view == null) {
        return null;
    }
    for (SearchField field : fields) {
        if (!field.isVisible()) {
            continue;
        }
        ViewGroup v = (ViewGroup) view.findViewWithTag(field.getId());
        if (field instanceof TextSearchField) {
            EditText text;
            if (((TextSearchField) field).isFreeSearch()) {
                text = etSimpleSearch;
            } else {
                if (v == null)
                    continue;
                text = (EditText) v.findViewById(R.id.edittext);
            }
            query.add(new SearchQuery(field, text.getEditableText().toString().trim()));
        } else if (field instanceof BarcodeSearchField) {
            if (v == null)
                continue;
            EditText text = (EditText) v.findViewById(R.id.edittext);
            query.add(new SearchQuery(field, text.getEditableText().toString().trim()));
        } else if (field instanceof DropdownSearchField) {
            if (v == null)
                continue;
            Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
            if (spinner.getSelectedItemPosition() != -1) {
                String key = ((DropdownSearchField) field).getDropdownValues().get(spinner.getSelectedItemPosition()).getKey();
                if (!key.equals("")) {
                    query.add(new SearchQuery(field, key));
                }
            }
        } else if (field instanceof CheckboxSearchField) {
            if (v == null)
                continue;
            CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkbox);
            query.add(new SearchQuery(field, String.valueOf(checkbox.isChecked())));
        }
    }
    return query;
}
Also used : SearchQuery(de.geeksfactory.opacclient.searchfields.SearchQuery) EditText(android.widget.EditText) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) CheckBox(android.widget.CheckBox)

Example 13 with TextSearchField

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

the class SearchFragment method buildSearchForm.

protected void buildSearchForm(Map<String, String> restoreQuery) {
    String skey = "annoyed_" + app.getLibrary().getIdent();
    if (app.getLibrary().getReplacedBy() != null && !"".equals(app.getLibrary().getReplacedBy()) && sp.getInt(skey, 0) < 5 && app.promotePlusApps()) {
        rlReplaced.setVisibility(View.VISIBLE);
        ivReplacedStore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(app.getLibrary().getReplacedBy().replace("https://play.google.com/store/apps/details?id=", "market://details?id=")));
                    startActivity(i);
                } catch (ActivityNotFoundException e) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(app.getLibrary().getReplacedBy()));
                    startActivity(i);
                }
            }
        });
        sp.edit().putInt(skey, sp.getInt(skey, 0) + 1).apply();
    } else {
        rlReplaced.setVisibility(View.GONE);
    }
    llFormFields.removeAllViews();
    llAdvancedFields.removeAllViews();
    llExpand.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setAdvanced(!advanced);
        }
    });
    rlSimpleSearch.setVisibility(View.GONE);
    tvSearchAdvHeader.setVisibility(View.GONE);
    int i = 0;
    if (fields == null) {
        return;
    }
    for (final SearchField field : fields) {
        if (!field.isVisible()) {
            continue;
        }
        ViewGroup v = null;
        if (field instanceof TextSearchField) {
            TextSearchField textSearchField = (TextSearchField) field;
            if (textSearchField.isFreeSearch()) {
                rlSimpleSearch.setVisibility(View.VISIBLE);
                tvSearchAdvHeader.setVisibility(View.VISIBLE);
                etSimpleSearch.setHint(textSearchField.getHint());
            } else {
                v = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.searchfield_text, llFormFields, false);
                TextView title = (TextView) v.findViewById(R.id.title);
                title.setText(textSearchField.getDisplayName());
                EditText edittext = (EditText) v.findViewById(R.id.edittext);
                edittext.setHint(textSearchField.getHint());
                if (((TextSearchField) field).isNumber()) {
                    edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                if (((TextSearchField) field).isHalfWidth() && i >= 1 && !(fields.get(i - 1) instanceof TextSearchField && ((TextSearchField) fields.get(i - 1)).isFreeSearch())) {
                    ViewGroup before = (ViewGroup) view.findViewWithTag(fields.get(i - 1).getId());
                    llFormFields.removeView(before);
                    llAdvancedFields.removeView(before);
                    v.setTag(field.getId());
                    View together = makeHalfWidth(before, v);
                    v = null;
                    if (field.isAdvanced()) {
                        llAdvancedFields.addView(together);
                    } else {
                        llFormFields.addView(together);
                    }
                }
            }
        } else if (field instanceof BarcodeSearchField) {
            BarcodeSearchField bcSearchField = (BarcodeSearchField) field;
            v = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.searchfield_barcode, llFormFields, false);
            TextView title = (TextView) v.findViewById(R.id.title);
            title.setText(bcSearchField.getDisplayName());
            EditText edittext = (EditText) v.findViewById(R.id.edittext);
            edittext.setHint(bcSearchField.getHint());
            ImageView ivBarcode = (ImageView) v.findViewById(R.id.ivBarcode);
            ivBarcode.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    barcodeScanningField = field.getId();
                    callback.scanBarcode();
                }
            });
            if (((BarcodeSearchField) field).isHalfWidth() && i >= 1 && !(fields.get(i - 1) instanceof TextSearchField && ((TextSearchField) fields.get(i - 1)).isFreeSearch())) {
                ViewGroup before = (ViewGroup) view.findViewWithTag(fields.get(i - 1).getId());
                llFormFields.removeView(before);
                llAdvancedFields.removeView(before);
                v = makeHalfWidth(before, v);
            }
        } else if (field instanceof DropdownSearchField) {
            DropdownSearchField ddSearchField = (DropdownSearchField) field;
            if (ddSearchField.getDropdownValues() == null) {
                continue;
            }
            v = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.searchfield_dropdown, llFormFields, false);
            TextView title = (TextView) v.findViewById(R.id.title);
            title.setText(ddSearchField.getDisplayName());
            Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
            spinner.setAdapter(((OpacActivity) getActivity()).new MetaAdapter<DropdownSearchField.Option>(getActivity(), ddSearchField.getDropdownValues(), R.layout.simple_spinner_item));
            // Load saved home branch
            if (field.getMeaning() == Meaning.HOME_BRANCH) {
                String selection;
                if (sp.contains(OpacClient.PREF_HOME_BRANCH_PREFIX + app.getAccount().getId())) {
                    selection = sp.getString(OpacClient.PREF_HOME_BRANCH_PREFIX + app.getAccount().getId(), "");
                } else {
                    try {
                        selection = app.getLibrary().getData().getString("homebranch");
                    } catch (JSONException e) {
                        selection = "";
                    }
                }
                if (!selection.equals("")) {
                    int j = 0;
                    for (DropdownSearchField.Option row : ddSearchField.getDropdownValues()) {
                        if (row.getKey().equals(selection)) {
                            spinner.setSelection(j);
                        }
                        j++;
                    }
                }
            }
        } else if (field instanceof CheckboxSearchField) {
            CheckboxSearchField cbSearchField = (CheckboxSearchField) field;
            v = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.searchfield_checkbox, llFormFields, false);
            CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkbox);
            checkbox.setText(cbSearchField.getDisplayName());
        }
        if (v != null) {
            v.setTag(field.getId());
            if (field.isAdvanced()) {
                llAdvancedFields.addView(v);
            } else {
                llFormFields.addView(v);
            }
        }
        i++;
    }
    llExpand.setVisibility(llAdvancedFields.getChildCount() == 0 ? View.GONE : View.VISIBLE);
    if (restoreQuery != null) {
        loadQuery(restoreQuery);
    }
}
Also used : EditText(android.widget.EditText) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) JSONException(org.json.JSONException) Intent(android.content.Intent) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) ActivityNotFoundException(android.content.ActivityNotFoundException) CheckBox(android.widget.CheckBox) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Example 14 with TextSearchField

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

the class SearchFragment method loadQuery.

public void loadQuery(Bundle query) {
    if (query == null) {
        return;
    }
    for (SearchField field : fields) {
        if (!field.isVisible()) {
            continue;
        }
        ViewGroup v = (ViewGroup) view.findViewWithTag(field.getId());
        if (v == null) {
            continue;
        }
        if (field instanceof TextSearchField) {
            EditText text;
            if (((TextSearchField) field).isFreeSearch()) {
                text = etSimpleSearch;
            } else {
                text = (EditText) v.findViewById(R.id.edittext);
            }
            text.setText(query.getString(field.getId()));
        } else if (field instanceof BarcodeSearchField) {
            EditText text = (EditText) v.findViewById(R.id.edittext);
            text.setText(query.getString(field.getId()));
        } else if (field instanceof DropdownSearchField) {
            Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
            int i = 0;
            if (((DropdownSearchField) field).getDropdownValues() == null) {
                continue;
            }
            for (DropdownSearchField.Option map : ((DropdownSearchField) field).getDropdownValues()) {
                if (map.getKey().equals(query.getString(field.getId()))) {
                    spinner.setSelection(i);
                    break;
                }
                i++;
            }
        } else if (field instanceof CheckboxSearchField) {
            CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkbox);
            checkbox.setChecked(Boolean.valueOf(query.getString(field.getId())));
        }
    }
    if (barcodeScanningField != null && scanResult != null) {
        ViewGroup v = (ViewGroup) view.findViewWithTag(barcodeScanningField);
        EditText text = (EditText) v.findViewById(R.id.edittext);
        text.setText(scanResult.getContents());
        barcodeScanningField = null;
        scanResult = null;
    }
}
Also used : EditText(android.widget.EditText) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) CheckBox(android.widget.CheckBox) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField)

Example 15 with TextSearchField

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

the class Littera method addAdvancedSearchFields.

protected void addAdvancedSearchFields(List<SearchField> fields) throws IOException, JSONException {
    final String html = httpGet(getApiUrl() + "&mode=a", getDefaultEncoding());
    final Document doc = Jsoup.parse(html);
    final Elements options = doc.select("select#adv_search_crit_0").first().select("option");
    for (final Element option : options) {
        final SearchField field;
        if (SEARCH_FIELDS_FOR_DROPDOWN.contains(option.val())) {
            field = new DropdownSearchField();
            addDropdownValuesForField(((DropdownSearchField) field), option.val());
        } else {
            field = new TextSearchField();
            ((TextSearchField) field).setHint("");
        }
        field.setDisplayName(option.text());
        field.setId(option.val());
        field.setData(new JSONObject());
        field.getData().put("meaning", field.getId());
        fields.add(field);
    }
}
Also used : TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) JSONObject(org.json.JSONObject) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) 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