use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class Mnemosyne2CardsImporter method convert.
@Override
public void convert(String src, String dest) throws Exception {
// Make the tmp directory tmp/[src file name]/
String srcFilename = FilenameUtils.getName(src);
File tmpDirectory = new File(AMEnv.DEFAULT_TMP_PATH + srcFilename);
FileUtils.deleteDirectory(tmpDirectory);
FileUtils.forceMkdir(tmpDirectory);
AnyMemoDBOpenHelper helper = null;
try {
// First unzip the file since cards is just a zip archive
// Example content of cards
// $ ls
// METADATA cards.xml musicnotes
AMZipUtils.unZipFile(new File(src), tmpDirectory);
// Make sure the XML file exists.
File xmlFile = new File(tmpDirectory + "/cards.xml");
if (!xmlFile.exists()) {
throw new Exception("Could not find the cards.xml after extracting " + src);
}
List<Card> cardList = xmlToCards(xmlFile);
if (!new File(dest).exists()) {
amFileUtil.createDbFileWithDefaultSettings(new File(dest));
}
helper = AnyMemoDBOpenHelperManager.getHelper(dest);
CardDao cardDao = helper.getCardDao();
cardDao.createCards(cardList);
// The last step is to see if there are images to import.
Collection<File> imageFiles = FileUtils.listFiles(tmpDirectory, new SuffixFileFilter(new String[] { "jpg", "png", "bmp" }, IOCase.INSENSITIVE), DirectoryFileFilter.DIRECTORY);
if (!imageFiles.isEmpty()) {
String destDbName = FilenameUtils.getName(dest);
File imageDir = new File(AMEnv.DEFAULT_IMAGE_PATH + destDbName);
FileUtils.forceMkdir(imageDir);
for (File imageFile : imageFiles) {
FileUtils.copyFileToDirectory(imageFile, imageDir);
}
}
} finally {
if (helper != null) {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
FileUtils.deleteDirectory(tmpDirectory);
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class MnemosyneXMLExporter method convert.
public void convert(String src, String dest) throws Exception {
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
String dbName = FilenameUtils.getName(dest);
PrintWriter outxml = null;
try {
final CardDao cardDao = helper.getCardDao();
final LearningDataDao learningDataDao = helper.getLearningDataDao();
final CategoryDao categoryDao = helper.getCategoryDao();
// Populate all category field in a transaction.
List<Card> cardList = cardDao.callBatchTasks(new Callable<List<Card>>() {
public List<Card> call() throws Exception {
List<Card> cards = cardDao.queryForAll();
for (Card c : cards) {
categoryDao.refresh(c.getCategory());
learningDataDao.refresh(c.getLearningData());
}
return cards;
}
});
if (cardList == null || cardList.size() == 0) {
throw new IOException("Read empty or corrupted file");
}
outxml = new PrintWriter(new BufferedWriter(new FileWriter(dest)));
if (outxml.checkError()) {
throw new IOException("Can't open: " + dest);
}
int count = 0;
long timeOfStart = 0L;
String id, u, gr, e, ac_rp, rt_rp, lps, ac_rp_l, rt_rp_l, l_rp, n_rp, question, answer, category;
// Now write the xml to the file
for (Card card : cardList) {
// At the first item, we write all metadata
if (count == 0) {
// timeOfStart = item.getDatelearnUnix();
// 2000-01-01 12:00
timeOfStart = 946728000L;
outxml.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
outxml.print("<mnemosyne core_version=\"1\" time_of_start=\"" + timeOfStart + "\" >\n");
outxml.print("<category active=\"1\">\n");
outxml.print("<name>" + dbName + "</name>\n");
outxml.print("</category>\n");
}
// Convert the learning data
LearningData ld = card.getLearningData();
id = "" + card.getOrdinal();
gr = "" + ld.getGrade();
e = "" + ld.getEasiness();
ac_rp = "" + ld.getAcqReps();
rt_rp = "" + ld.getRetReps();
lps = "" + ld.getLapses();
ac_rp_l = "" + ld.getAcqRepsSinceLapse();
rt_rp_l = "" + ld.getRetRepsSinceLapse();
;
if (ac_rp.equals("0")) {
u = "1";
} else {
u = "0";
}
// Add 1 here to avoid rounding problem
l_rp = Long.toString((ld.getLastLearnDate().getTime() / 1000 - timeOfStart) / 86400);
n_rp = Long.toString((ld.getNextLearnDate().getTime() / 1000 - timeOfStart) / 86400 + 1);
// Replace the illegal symbols from the question and answer
question = card.getQuestion();
answer = card.getAnswer();
category = card.getCategory().getName();
if (question == null) {
question = "";
}
if (answer == null) {
answer = "";
}
if (category == null) {
category = "";
}
question = question.replaceAll("&", "&");
question = question.replaceAll("<", "<");
question = question.replaceAll(">", ">");
question = question.replaceAll("'", "'");
question = question.replaceAll("\"", """);
answer = answer.replaceAll("&", "&");
answer = answer.replaceAll("<", "<");
answer = answer.replaceAll(">", ">");
answer = answer.replaceAll("'", "'");
answer = answer.replaceAll("\"", """);
category = category.replaceAll("&", "&");
category = category.replaceAll("<", "<");
category = category.replaceAll(">", ">");
category = category.replaceAll("'", "'");
category = category.replaceAll("\"", """);
outxml.print("<item id=\"" + id + "\" u=\"" + u + "\" gr=\"" + gr + "\" e=\"" + e + "\" ac_rp=\"" + ac_rp + "\" rt_rp=\"" + rt_rp + "\" lps=\"" + lps + "\" ac_rp_l=\"" + ac_rp_l + "\" rt_rp_l=\"" + rt_rp_l + "\" l_rp=\"" + l_rp + "\" n_rp=\"" + n_rp + "\">\n");
if (category.equals("")) {
outxml.print("<cat>" + dbName + "</cat>\n");
} else {
outxml.print("<cat>" + category + "</cat>\n");
}
outxml.print("<Q>" + question + "</Q>\n");
outxml.print("<A>" + answer + "</A>\n");
outxml.print("</item>\n");
if (outxml.checkError()) {
throw new IOException("Error writing xml on id: " + id);
}
count += 1;
}
outxml.print("</mnemosyne>");
outxml.close();
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
if (outxml != null) {
outxml.close();
}
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class QATxtExporter method convert.
public void convert(String src, String dest) throws Exception {
new File(dest).delete();
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
try {
final CardDao cardDao = helper.getCardDao();
PrintWriter outtxt = new PrintWriter(new BufferedWriter(new FileWriter(dest)));
if (outtxt.checkError()) {
throw new IOException("Can't open: " + dest + " for writting");
}
List<Card> cardList = cardDao.queryForAll();
if (cardList == null || cardList.size() == 0) {
throw new IOException("Can't retrieve items for database: " + src);
}
for (Card card : cardList) {
outtxt.print("Q: " + card.getQuestion() + "\n");
outtxt.print("A: " + card.getAnswer() + "\n\n");
}
outtxt.close();
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class QATxtImporter 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);
try {
final CardDao cardDao = helper.getCardDao();
BufferedReader txtfile = new BufferedReader(new FileReader(src));
String line;
int count = 0;
List<Card> cardList = new LinkedList<Card>();
boolean isQ = false;
StringBuffer qBuf = null;
StringBuffer aBuf = null;
while ((line = txtfile.readLine()) != null) {
/* remove BOM */
line = line.replace("\uFEFF", "");
String head = "";
if (line.length() >= 2) {
head = line.substring(0, 2);
}
if (line.equals("")) {
continue;
} else if (head.equals("Q:")) {
if (isQ == true) {
/* next line */
qBuf.append("<br />" + line.replaceAll("Q:\\s*", ""));
} else {
isQ = true;
/* Save item when the Q is after A
* because it is a new item */
if (count != 0) {
Card card = new Card();
card.setQuestion(qBuf.toString());
card.setAnswer(aBuf.toString());
card.setCategory(new Category());
card.setLearningData(new LearningData());
cardList.add(card);
}
count += 1;
qBuf = new StringBuffer();
qBuf.append(line.replaceAll("Q:\\s*", ""));
}
} else if (head.equals("A:")) {
if (isQ == true) {
isQ = false;
aBuf = new StringBuffer();
aBuf.append(line.replaceAll("A:\\s*", ""));
} else {
aBuf.append("<br />" + line.replaceAll("A:\\s*", ""));
}
} else {
if (isQ) {
qBuf.append("<br />" + line);
} else {
aBuf.append("<br />" + line);
}
}
}
/* Last item need to be added manually */
count += 1;
Card card = new Card();
card.setQuestion(qBuf.toString());
card.setAnswer(aBuf.toString());
card.setCategory(new Category());
card.setLearningData(new LearningData());
cardList.add(card);
txtfile.close();
cardDao.createCards(cardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class Mnemosyne2CardsImporterTest 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();
LearningDataDao learningDataDao = helper.getLearningDataDao();
List<Card> cards = cardDao.queryForAll();
List<Category> categories = categoryDao.queryForAll();
for (Card c : cards) {
categoryDao.refresh(c.getCategory());
learningDataDao.refresh(c.getLearningData());
}
assertEquals(4, cards.size());
assertEquals(3, categories.size());
// LOL, the test cards has a typo
assertEquals("Qustion1", 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("Category2", cards.get(3).getCategory().getName());
} finally {
helper.close();
}
}
Aggregations