use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.
the class SupermemoXMLImporter 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.entity.Card in project AnyMemo by helloworld1.
the class SupermemoXMLImporter method startElement.
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("SuperMemoElement")) {
card = new Card();
card.setCategory(new Category());
ld = new LearningData();
card.setLearningData(ld);
// Set a default interval, in case of malformed the xml file
interval = 0;
}
characterBuf = new StringBuffer();
}
use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.
the class CellsDBConverter method convertCellsToDb.
/* cardCells contains the question, answer, category and note
* category and note is optionally.
* learningDataCells contains all necessary learning data.
* If learningDataCells, new learning data is used.
* dbPath is the place to store converted database
*/
public void convertCellsToDb(Cells cardCells, Cells learningDataCells, String dbPath) throws IOException {
int numberOfRows = cardCells.getRowCounts();
int numberOfLearningDataRows = 0;
if (learningDataCells != null) {
numberOfLearningDataRows = learningDataCells.getRowCounts();
}
// We ignore the header row
List<Card> cardList = new ArrayList<Card>(numberOfRows + 1);
for (int i = 1; i < numberOfRows; i++) {
List<String> row = cardCells.getRow(i);
Card card = new Card();
Category category = new Category();
if (row.size() == 0) {
Log.w(TAG, "Each row in spreadsheet should have at least 2 column: question and answer. Row number: " + i);
}
if (row.size() >= 1) {
card.setQuestion(row.get(0));
}
if (row.size() >= 2) {
card.setAnswer(row.get(1));
}
if (row.size() >= 3) {
category.setName(row.get(2));
}
if (row.size() >= 4) {
card.setNote(row.get(3));
}
// This can't be null because numberOfLearningDataRows is 0
// if learningDataCells is 0.
LearningData learningData;
if (i < numberOfLearningDataRows) {
learningData = getLearningDataFromRow(learningDataCells.getRow(i));
} else {
learningData = new LearningData();
}
card.setCategory(category);
card.setLearningData(learningData);
cardList.add(card);
}
if (cardList.size() == 0) {
throw new IOException("Wrong spreadsheet format. The spreadsheet should contain at least 1 worksheet with at least 2 columns of questions and answers.");
}
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
try {
CardDao cardDao = helper.getCardDao();
cardDao.createCards(cardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.
the class CSVExporter method convert.
@Override
public void convert(String src, String dest) throws Exception {
new File(dest).delete();
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
final CardDao cardDao = helper.getCardDao();
final CategoryDao categoryDao = helper.getCategoryDao();
CSVWriter writer;
if (separator == null) {
writer = new CSVWriter(new FileWriter(dest));
} else {
writer = new CSVWriter(new FileWriter(dest), separator);
}
try {
final List<Card> cardList = cardDao.queryForAll();
// Populate all category field in a transaction.
categoryDao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
for (Card c : cardList) {
categoryDao.refresh(c.getCategory());
}
return null;
}
});
AnyMemoDBOpenHelperManager.releaseHelper(helper);
if (cardList.size() == 0) {
throw new IOException("Can't retrieve cards for database: " + src);
}
String[] entries = new String[4];
for (Card card : cardList) {
entries[0] = card.getQuestion();
entries[1] = card.getAnswer();
entries[2] = card.getCategory().getName();
entries[3] = card.getNote();
writer.writeNext(entries);
}
} finally {
writer.close();
}
}
use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.
the class CardDaoImpl method searchNextCard.
@SuppressWarnings("unchecked")
public Card searchNextCard(String criteria, int ordinal) {
QueryBuilder<Card, Integer> qb = queryBuilder();
try {
Where<Card, Integer> where = qb.where();
where.and(where.gt("ordinal", ordinal), where.or(where.like("question", criteria), where.like("answer", criteria), where.like("note", criteria)));
qb.setWhere(where);
qb.orderBy("ordinal", true);
PreparedQuery<Card> pq = qb.prepare();
Card nc = queryForFirst(pq);
return nc;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
Aggregations