Search in sources :

Example 1 with Converter

use of org.liberty.android.fantastischmemo.converter.Converter in project AnyMemo by helloworld1.

the class ImportMergingTest method testMergeQATxtIntoDb.

@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);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) QATxtImporter(org.liberty.android.fantastischmemo.converter.QATxtImporter) Converter(org.liberty.android.fantastischmemo.converter.Converter) File(java.io.File) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) BaseTest(org.liberty.android.fantastischmemo.test.BaseTest) SmallTest(android.support.test.filters.SmallTest)

Example 2 with Converter

use of org.liberty.android.fantastischmemo.converter.Converter in project AnyMemo by helloworld1.

the class ConverterFragment method startConversion.

/**
 * Prepare the conversion and start the conversion.
 * If the destiny file exists, it pops up an option to merge the file into existing db.
 */
private void startConversion(File file) {
    final String inputPath = file.getAbsolutePath();
    // Get the converter instance so we can get the dest file path
    Converter converter = converterMap.get(converterClass);
    final String outputPath = FilenameUtils.removeExtension(inputPath) + "." + converter.getDestExtension();
    // the dest will be deleted
    if (new File(outputPath).exists() && converter.getDestExtension().equals("db")) {
        new AlertDialog.Builder(getActivity()).setTitle(R.string.conversion_merge_text).setMessage(String.format(getString(R.string.conversion_merge_message), outputPath, inputPath, outputPath)).setPositiveButton(R.string.yes_text, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                invokeConverterService(inputPath, outputPath);
            }
        }).setNeutralButton(R.string.no_text, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                try {
                    amFileUtil.deleteFileWithBackup(outputPath);
                    invokeConverterService(inputPath, outputPath);
                } catch (IOException e) {
                    Log.e(TAG, "Faield to deleteWithBackup: " + outputPath, e);
                    Toast.makeText(getActivity(), getString(R.string.fail) + ": " + e.toString(), Toast.LENGTH_LONG).show();
                }
            }
        }).setNegativeButton(R.string.cancel_text, null).show();
    } else {
        // If the merging is not possible, do normal conversion
        amFileUtil.deleteDbSafe(outputPath);
        invokeConverterService(inputPath, outputPath);
    }
}
Also used : DialogInterface(android.content.DialogInterface) Converter(org.liberty.android.fantastischmemo.converter.Converter) IOException(java.io.IOException) File(java.io.File)

Example 3 with Converter

use of org.liberty.android.fantastischmemo.converter.Converter in project AnyMemo by helloworld1.

the class ConverterFragment method onCreate.

@Override
@SuppressWarnings("unchecked")
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    fragmentComponents().inject(this);
    Bundle args = getArguments();
    assert args != null : "Null args in ConverterFragment";
    setOnFileClickListener(fileClickListener);
    converterClass = (Class<Converter>) args.getSerializable(EXTRA_CONVERTER_CLASS);
}
Also used : Bundle(android.os.Bundle) Converter(org.liberty.android.fantastischmemo.converter.Converter)

Example 4 with Converter

use of org.liberty.android.fantastischmemo.converter.Converter in project AnyMemo by helloworld1.

the class ImportMergingTest method testMergeCsvIntoDb.

@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);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Converter(org.liberty.android.fantastischmemo.converter.Converter) File(java.io.File) CSVImporter(org.liberty.android.fantastischmemo.converter.CSVImporter) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) BaseTest(org.liberty.android.fantastischmemo.test.BaseTest) SmallTest(android.support.test.filters.SmallTest)

Example 5 with Converter

use of org.liberty.android.fantastischmemo.converter.Converter in project AnyMemo by helloworld1.

the class ConvertIntentService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    if (!Objects.equal(intent.getAction(), ACTION_CONVERT)) {
        throw new IllegalArgumentException("The Action is wrong");
    }
    String inputFilePath = intent.getStringExtra(EXTRA_INPUT_FILE_PATH);
    assert inputFilePath != null : "Input file path should not be null";
    String outputFilePath = intent.getStringExtra(EXTRA_OUTPUT_FILE_PATH);
    assert outputFilePath != null : "Output file path should not be null";
    @SuppressWarnings("unchecked") Class<Converter> converterClass = (Class<Converter>) intent.getSerializableExtra(EXTRA_CONVERTER_CLASS);
    Converter converter = converterMap.get(converterClass);
    // Replace the extension of the file: file.xml -> file.db
    String conversionFileInfo = "" + FilenameUtils.getName(inputFilePath) + " -> " + FilenameUtils.getName(outputFilePath);
    int notificationId = CONVERSION_PROGRESS_NOTIFICATION_ID_BASE + inputFilePath.hashCode();
    showInProgressNotification(notificationId, conversionFileInfo);
    try {
        converter.convert(inputFilePath, outputFilePath);
        showSuccessNotification(notificationId, outputFilePath);
    } catch (Exception e) {
        Log.e(TAG, "Error while converting", e);
        showFailureNotification(notificationId, conversionFileInfo, e);
    }
}
Also used : Converter(org.liberty.android.fantastischmemo.converter.Converter)

Aggregations

Converter (org.liberty.android.fantastischmemo.converter.Converter)5 File (java.io.File)3 SmallTest (android.support.test.filters.SmallTest)2 Test (org.junit.Test)2 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)2 Card (org.liberty.android.fantastischmemo.entity.Card)2 BaseTest (org.liberty.android.fantastischmemo.test.BaseTest)2 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 IOException (java.io.IOException)1 CSVImporter (org.liberty.android.fantastischmemo.converter.CSVImporter)1 QATxtImporter (org.liberty.android.fantastischmemo.converter.QATxtImporter)1