use of de.geeksfactory.opacclient.storage.StarDataSource in project opacclient by opacapp.
the class StarredFragment method getEncodedStarredObjects.
private JSONObject getEncodedStarredObjects() {
JSONObject starred = new JSONObject();
try {
starred.put(JSON_LIBRARY_NAME, app.getLibrary().getIdent());
JSONArray items = new JSONArray();
StarDataSource data = new StarDataSource(getActivity());
List<Starred> libItems = data.getAllItems(app.getLibrary().getIdent());
for (Starred libItem : libItems) {
JSONObject item = new JSONObject();
item.put(JSON_ITEM_MNR, libItem.getMNr());
item.put(JSON_ITEM_TITLE, libItem.getTitle());
item.put(JSON_ITEM_MEDIATYPE, libItem.getMediaType());
items.put(item);
}
starred.put(JSON_STARRED_LIST, items);
} catch (JSONException e) {
showExportError();
}
return starred;
}
use of de.geeksfactory.opacclient.storage.StarDataSource in project opacclient by opacapp.
the class StarredFragment method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EXPORT && resultCode == Activity.RESULT_OK) {
Log.i("StarredFragment", data.toString());
Uri uri = data.getData();
try {
OutputStream os = getActivity().getContentResolver().openOutputStream(uri);
if (os != null) {
JSONObject starred = getEncodedStarredObjects();
PrintWriter pw = new PrintWriter(os, true);
pw.write(starred.toString());
pw.close();
os.close();
} else {
showExportError();
}
} catch (FileNotFoundException e) {
showExportError();
} catch (IOException e) {
showExportError();
}
} else if (requestCode == REQUEST_CODE_IMPORT && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
StarDataSource dataSource = new StarDataSource(getActivity());
InputStream is = getActivity().getContentResolver().openInputStream(uri);
if (is != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String list = builder.toString();
JSONObject savedList = new JSONObject(list);
String bib = savedList.getString(JSON_LIBRARY_NAME);
// disallow import if from different library than current library
if (bib != null && !bib.equals(app.getLibrary().getIdent())) {
Snackbar.make(getView(), R.string.info_different_library, Snackbar.LENGTH_SHORT).show();
return;
}
JSONArray items = savedList.getJSONArray(JSON_STARRED_LIST);
for (int i = 0; i < items.length(); i++) {
JSONObject entry = items.getJSONObject(i);
if (entry.has(JSON_ITEM_MNR) && !dataSource.isStarred(bib, entry.getString(JSON_ITEM_MNR)) || !entry.has(JSON_ITEM_MNR) && !dataSource.isStarredTitle(bib, entry.getString(JSON_ITEM_TITLE))) {
// disallow dupes
dataSource.star(entry.optString(JSON_ITEM_MNR), entry.getString(JSON_ITEM_TITLE), bib, SearchResult.MediaType.valueOf(entry.getString(JSON_ITEM_MEDIATYPE)));
}
}
adapter.notifyDataSetChanged();
Snackbar.make(getView(), R.string.info_starred_updated, Snackbar.LENGTH_SHORT).show();
} else {
showImportError();
}
} catch (JSONException | IOException e) {
showImportError();
}
}
}
Aggregations