@Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); if (action != null) { Importer importer = null; String path = ""; if (action.equals(ACTION_IMPORT_ADDRESSES)) { importer = new AddressImporter(this); path = ADDRESSES_PATH; } else if (action.equals(ACTION_IMPORT_HOSPITAL_ADMITTANCES)) { importer = new AdmittanceImporter(this); path = ADMITTANCES_PATH; } else if (action.equals(ACTION_IMPORT_DOCTOR_STANDBY)) { importer = new StandbyImporter(this); path = STANDBY_PATH; } if (importer != null) { importer.setOnPostProcessProgressListener( new Importer.OnPostProcessProgressListener() { @Override public void onProgress(double progressPercent) { reportProgress(progressPercent); } @Override public void onResult(String action, int processed, int modified) { reportResultPostProcess(action, processed, modified); } @Override public void onResult(String action, boolean result) { reportResultPostProcess(action, result); } }); startImport(path, importer); } } reportCompleted(intent.getAction()); DataImportReceiver.completeWakefulIntent(intent); }
private void processFile(File doc, Importer importer) { InputPositionReader posRdr; CSVReader rdr; try { posRdr = new InputPositionReader(new FileReader(doc)); rdr = new CSVReader(posRdr, CSV_SEPARATOR); } catch (FileNotFoundException e) { // Shouldn't occur, because the caller checks for existence e.printStackTrace(); return; } reportProgress(0); EGODbHelper helper = new EGODbHelper(getBaseContext(), false); db = helper.getWritableDatabase(); if (db == null) { reportError(getResources().getString(R.string.error_db_object_null)); return; } db.beginTransaction(); importer.setDatabase(db); importer.preProcess(); try { long fileLength = doc.length(); double lastPercent = 0; int numEntries = 0; int failedEntries = 0; String[] fields = rdr.readNext(); fields[0] = fields[0].replace( "\uFEFF", ""); // Replace UTF-8 BOM in first field of the file if it exists while (fields != null) { if (fields.length > 0 && !fields[0].startsWith(CSV_COMMENT)) { int result = importer.process(fields); if (result != Importer.PROCESS_IGNORED) { numEntries++; if (result == Importer.PROCESS_ERROR) { failedEntries++; } } } // Because BufferedReader reads chunks from the file, line lengths don't correlate with file // position double newPercent = posRdr.getPosition() / (double) fileLength; if (newPercent > lastPercent) { lastPercent = newPercent; reportProgress(lastPercent); } fields = rdr.readNext(); } rdr.close(); db.setTransactionSuccessful(); db.endTransaction(); reportResultImport(numEntries - failedEntries, failedEntries); importer.postProcess(); } catch (IOException e) { reportError(getResources().getString(R.string.service_dataimport_error_readfail)); } finally { try { rdr.close(); } catch (Exception e) { } if (db.inTransaction()) { db.endTransaction(); } db.close(); } }