use of de.geeksfactory.opacclient.searchfields.SearchField in project opacclient by opacapp.
the class MainActivity method onNewIntent.
@SuppressLint("NewApi")
@Override
public void onNewIntent(Intent intent) {
if (nfc_capable && sp.getBoolean("nfc_search", false)) {
android.nfc.Tag tag = intent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG);
String scanResult = readPageToString(tag);
if (scanResult != null) {
if (scanResult.length() > 5) {
SearchFieldDataSource source = new JsonSearchFieldDataSource(this);
if (source.hasSearchFields(app.getLibrary().getIdent())) {
List<SearchField> fields = source.getSearchFields(app.getLibrary().getIdent());
for (SearchField field : fields) {
if (field.getMeaning() == SearchField.Meaning.BARCODE) {
List<SearchQuery> queries = new ArrayList<>();
queries.add(new SearchQuery(field, scanResult));
app.startSearch(this, queries);
return;
}
}
}
Intent detailIntent = new Intent(this, SearchResultDetailActivity.class);
detailIntent.putExtra(SearchResultDetailFragment.ARG_ITEM_ID, scanResult);
startActivity(detailIntent);
}
}
}
}
use of de.geeksfactory.opacclient.searchfields.SearchField in project opacclient by opacapp.
the class Main method main.
public static void main(String[] args) throws IOException, JSONException {
Security.addProvider(new BouncyCastleProvider());
Collection<String[]> libraries = libraries();
Set<String> ignored = new MeaningDetectorImpl(null).getIgnoredFields();
Scanner in = new Scanner(System.in);
final ExecutorService service = Executors.newFixedThreadPool(25);
List<TaskInfo> tasks = new ArrayList<>();
for (String[] libraryNameArray : libraries) {
String libraryName = libraryNameArray[0];
Library library;
try {
library = Library.fromJSON(libraryName, new JSONObject(readFile(BIBS_DIR + libraryName + ".json")));
Future<Map<String, List<SearchField>>> future = service.submit(new GetSearchFieldsCallable(library));
tasks.add(new TaskInfo(library, future));
} catch (JSONException | IOException e) {
// e.printStackTrace();
}
}
for (TaskInfo entry : tasks) {
Library library = entry.lib;
try {
Map<String, List<SearchField>> fields = entry.future.get();
if (fields == null)
continue;
for (String lang : fields.keySet()) {
System.out.println("Bibliothek: " + library.getIdent() + ", Sprache: " + lang);
MeaningDetectorImpl detector = new MeaningDetectorImpl(library);
for (int i = 0; i < fields.get(lang).size(); i++) {
fields.get(lang).set(i, detector.detectMeaning(fields.get(lang).get(i)));
}
for (SearchField field : fields.get(lang)) {
if (field.getMeaning() != null || ignored.contains(field.getDisplayName()) || field.getData() != null && field.getData().has("meaning") && ignored.contains(field.getData().getString("meaning")))
continue;
String name;
if (field.getData() != null && field.getData().has("meaning")) {
name = field.getData().getString("meaning");
System.out.print("Unbekanntes Feld: '" + name + "' (Anzeigename: " + field.getDisplayName() + ") ");
} else {
name = field.getDisplayName();
System.out.print("Unbekanntes Feld: '" + name + "' ");
}
Meaning meaning = null;
boolean ignoredField = false;
while (meaning == null && !ignoredField) {
String str = in.nextLine();
if (str.equals("") || str.toLowerCase().equals("ignore")) {
ignoredField = true;
addIgnoredField(name);
ignored.add(name);
} else {
try {
meaning = Meaning.valueOf(str.toUpperCase());
} catch (IllegalArgumentException e) {
meaning = null;
}
}
}
if (meaning != null) {
detector.addMeaning(name, meaning);
saveMeaning(name, meaning);
}
}
}
} catch (JSONException | IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
in.close();
service.shutdown();
}
use of de.geeksfactory.opacclient.searchfields.SearchField in project opacclient by opacapp.
the class JsonSearchFieldDataSource method getSearchFields.
@Override
public List<SearchField> getSearchFields(String libraryId) {
try {
JSONObject json = readJsonFile(libraryId);
List<SearchField> list = new ArrayList<>();
JSONArray fields = json.getJSONArray(KEY_FIELDS);
for (int i = 0; i < fields.length(); i++) {
list.add(SearchField.fromJSON(fields.getJSONObject(i)));
}
return list;
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
use of de.geeksfactory.opacclient.searchfields.SearchField in project opacclient by opacapp.
the class JsonSearchFieldDataSource method saveSearchFields.
@Override
public void saveSearchFields(String libraryId, List<SearchField> fields) {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
try {
for (SearchField field : fields) {
if (field.getId().length() > 0)
array.put(field.toJSON());
}
object.put(KEY_FIELDS, array);
object.put(KEY_TIME, System.currentTimeMillis());
object.put(KEY_LANGUAGE, context.getResources().getConfiguration().locale.getLanguage());
try {
object.put(KEY_VERSION, context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
} catch (NameNotFoundException e) {
// should never happen
e.printStackTrace();
}
writeToJsonFile(libraryId, object);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
use of de.geeksfactory.opacclient.searchfields.SearchField 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;
}
Aggregations