Exemplo n.º 1
0
  @SmallTest
  @Test
  public void testMergeCsvIntoDb() throws Exception {
    srcFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.csv";
    destFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.db";
    new File(srcFilePath).delete();
    new File(destFilePath).delete();

    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
      // Create an new db with some contents
      helper.getCardDao().createCards(newDbCardList);
    } finally {
      AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }

    // Now convert the csv-test.csv into csv-test.db which will be merged
    // into existing csv-test.db
    amFileUtil.copyFileFromAsset("csv-test.csv", new File(srcFilePath));
    Converter converter = new CSVImporter(amFileUtil);
    converter.convert(srcFilePath, destFilePath);

    // verify the content of csv-test has merged cards
    helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
      List<Card> cards = helper.getCardDao().getAllCards(null);
      assertEquals(6, cards.size());

      assertEquals(1, (int) cards.get(0).getId());
      assertEquals(1, (int) cards.get(0).getOrdinal());
      assertEquals("old question 1", cards.get(0).getQuestion());
      assertEquals("old answer 1", cards.get(0).getAnswer());

      assertEquals(2, (int) cards.get(1).getId());
      assertEquals(2, (int) cards.get(1).getOrdinal());
      assertEquals("old question 2", cards.get(1).getQuestion());
      assertEquals("old answer 2", cards.get(1).getAnswer());

      assertEquals(3, (int) cards.get(2).getId());
      assertEquals(3, (int) cards.get(2).getOrdinal());
      assertEquals("Question1", cards.get(2).getQuestion());
      assertEquals("Answer1", cards.get(2).getAnswer());
      assertEquals("Category1", cards.get(2).getCategory().getName());
      assertEquals("Note1", cards.get(2).getNote());

      assertEquals(4, (int) cards.get(3).getId());
      assertEquals(4, (int) cards.get(3).getOrdinal());
      assertEquals("Question2", cards.get(3).getQuestion());
      assertEquals("Answer2", cards.get(3).getAnswer());
      assertEquals("Category1", cards.get(3).getCategory().getName());
      assertEquals("Note2", cards.get(3).getNote());
    } finally {
      AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
  }
Exemplo n.º 2
0
  @SmallTest
  @Test
  public void testMergeQATxtIntoDb() throws Exception {
    srcFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "qa-text-test.txt";
    destFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "qa-text-test.db";
    new File(srcFilePath).delete();
    new File(destFilePath).delete();

    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
      // Create an new db with some contents
      helper.getCardDao().createCards(newDbCardList);
    } finally {
      AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }

    amFileUtil.copyFileFromAsset("qa-text-test.txt", new File(srcFilePath));
    Converter converter = new QATxtImporter(amFileUtil);
    converter.convert(srcFilePath, destFilePath);

    helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
      List<Card> cards = helper.getCardDao().getAllCards(null);
      assertEquals(4, cards.size());

      assertEquals(1, (int) cards.get(0).getId());
      assertEquals(1, (int) cards.get(0).getOrdinal());
      assertEquals("old question 1", cards.get(0).getQuestion());
      assertEquals("old answer 1", cards.get(0).getAnswer());

      assertEquals(2, (int) cards.get(1).getId());
      assertEquals(2, (int) cards.get(1).getOrdinal());
      assertEquals("old question 2", cards.get(1).getQuestion());
      assertEquals("old answer 2", cards.get(1).getAnswer());

      assertEquals(3, (int) cards.get(2).getId());
      assertEquals(3, (int) cards.get(2).getOrdinal());
      assertEquals("This is question1", cards.get(2).getQuestion());
      assertEquals("Answer1", cards.get(2).getAnswer());

      assertEquals(4, (int) cards.get(3).getId());
      assertEquals(4, (int) cards.get(3).getOrdinal());
      assertEquals("Question2", cards.get(3).getQuestion());
      assertEquals("Answer2", cards.get(3).getAnswer());
    } finally {
      AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
  }
Exemplo n.º 3
0
  private void uploadDB(String dbPath) throws Exception {
    final String dbName = AMUtil.getFilenameFromPath(dbPath);
    int cardId = addCardSet(dbName, "Import from AnyMemo");
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(this, dbPath);
    CardDao cardDao = helper.getCardDao();
    List<Card> cards = cardDao.queryForAll();

    for (Card card : cards) {
      String question = card.getQuestion();
      String answer = card.getAnswer();
      addCard(cardId, question, answer);
    }
  }
Exemplo n.º 4
0
 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);
   }
 }
Exemplo n.º 5
0
  public void convert(String src, String dest) throws Exception {
    new File(dest).delete();
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, 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.setOrdinal(count);
              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.setOrdinal(count);
      card.setCategory(new Category());
      card.setLearningData(new LearningData());
      cardList.add(card);

      txtfile.close();
      cardDao.createCards(cardList);
    } finally {
      AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
  }