use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class MnemosyneXMLImporter method convert.
@Override
public void convert(String src, String dest) throws Exception {
URL mXMLUrl = new URL("file:///" + src);
cardList = new LinkedList<Card>();
System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
xr.parse(new InputSource(mXMLUrl.openStream()));
if (!new File(dest).exists()) {
amFileUtil.createDbFileWithDefaultSettings(new File(dest));
}
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
try {
CardDao cardDao = helper.getCardDao();
cardDao.createCards(cardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class CSVImporter method convert.
public void convert(String src, String dest) throws Exception {
if (!new File(dest).exists()) {
amFileUtil.createDbFileWithDefaultSettings(new File(dest));
}
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
CSVReader reader;
if (separator == null) {
reader = new CSVReader(new FileReader(src));
} else {
reader = new CSVReader(new FileReader(src), separator);
}
try {
final CardDao cardDao = helper.getCardDao();
String[] nextLine;
final List<Card> cardList = new LinkedList<Card>();
for (int lineNo = 1; (nextLine = reader.readNext()) != null; ++lineNo) {
if (nextLine.length == 1 && nextLine[0].isEmpty()) {
// ignore blank lines
continue;
}
if (nextLine.length < 2) {
Log.w(TAG, "Malformed CSV file at line " + lineNo + ": " + Arrays.toString(nextLine));
throw new Exception("Malformed CSV file at line " + lineNo + ". Please make sure the CSV's first column is question, second one is answer, the optional third is category, and the optional fourth one is note");
}
String note = "";
String category = "";
if (nextLine.length >= 3) {
category = nextLine[2];
}
if (nextLine.length >= 4) {
note = nextLine[3];
}
Card card = new Card();
Category cat = new Category();
LearningData ld = new LearningData();
cat.setName(category);
card.setCategory(cat);
card.setLearningData(ld);
card.setQuestion(nextLine[0]);
card.setAnswer(nextLine[1]);
card.setNote(note);
cardList.add(card);
}
cardDao.createCards(cardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
reader.close();
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class Supermemo2008XMLImporter method convert.
@Override
public void convert(String src, String dest) throws Exception {
URL mXMLUrl = new URL("file:///" + src);
cardList = new LinkedList<Card>();
characterBuf = new StringBuffer();
System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
xr.parse(new InputSource(mXMLUrl.openStream()));
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
try {
CardDao cardDao = helper.getCardDao();
cardDao.createCards(cardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class TabTxtImporterTest method verify.
@Override
protected void verify(String destFilePath) throws Exception {
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
try {
CardDao cardDao = helper.getCardDao();
CategoryDao categoryDao = helper.getCategoryDao();
List<Card> cards = cardDao.queryForAll();
List<Category> categories = categoryDao.queryForAll();
for (Card c : cards) {
categoryDao.refresh(c.getCategory());
}
assertEquals(4, cards.size());
assertEquals(3, categories.size());
assertEquals("Question1", cards.get(0).getQuestion());
assertEquals("Answer1", cards.get(0).getAnswer());
assertEquals("Category1", cards.get(0).getCategory().getName());
assertEquals("Question2", cards.get(1).getQuestion());
assertEquals("Answer2", cards.get(1).getAnswer());
assertEquals("Category1", cards.get(1).getCategory().getName());
assertEquals("Question3", cards.get(2).getQuestion());
assertEquals("Answer3", cards.get(2).getAnswer());
assertEquals("Category2", cards.get(2).getCategory().getName());
assertEquals("Question4", cards.get(3).getQuestion());
assertEquals("Answer4", cards.get(3).getAnswer());
assertEquals("", cards.get(3).getCategory().getName());
} finally {
helper.close();
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class CardProvider method query.
/**
* The query returns null if the db is not valid.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
List<String> uriSegments = uri.getPathSegments();
String dbPath = AMEnv.DEFAULT_ROOT_PATH + uriSegments.get(0);
if (!new File(dbPath).exists()) {
return null;
}
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), dbPath);
try {
CardDao cardDao = helper.getCardDao();
Cursor resultCursor = null;
switch(sUriMatcher.match(uri)) {
case COUNT_URI:
{
long count = cardDao.getTotalCount(null);
resultCursor = buildCursorFromCount(count);
break;
}
case RANDOM_URI:
{
int count = Integer.valueOf(uriSegments.get(2));
List<Card> cards = cardDao.getRandomCards(null, count);
resultCursor = buildCursorFromCards(cards);
break;
}
case ORD_URI:
{
int ord = Integer.valueOf(uriSegments.get(2));
Card card = cardDao.getByOrdinal(ord);
resultCursor = buildCursorFromCard(card);
break;
}
case ID_URI:
{
int id = Integer.valueOf(uriSegments.get(2));
Card card = cardDao.getById(id);
resultCursor = buildCursorFromCard(card);
break;
}
case START_URI:
{
int start = Integer.valueOf(uriSegments.get(2));
int count = Integer.valueOf(uriSegments.get(4));
List<Card> cards = cardDao.getCardsByOrdinalAndSize(start, count);
resultCursor = buildCursorFromCards(cards);
break;
}
case ALL_URI:
{
List<Card> cards = cardDao.getAllCards(null);
resultCursor = buildCursorFromCards(cards);
break;
}
default:
throw new IllegalArgumentException("No matching handler for uri: " + uri);
}
if (resultCursor == null) {
Log.e(TAG, "No case matched for uri: " + uri);
}
return resultCursor;
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
Aggregations