Android Open Source - sana M D S Sync Task






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.moca.task;
//from   w  w w . j av a  2  s. com
import java.util.ArrayList;
import java.util.List;

import org.moca.db.Event;
import org.moca.db.MocaDB.EventSQLFormat;
import org.moca.net.MDSInterface;
import org.moca.util.MocaUtil;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;

public class MDSSyncTask extends AsyncTask<Context, Void, Integer> {
  public static final String TAG = MDSSyncTask.class.toString();
  public static final Integer EMR_SYNC_NO_CONNECTION = 0;
  public static final Integer EMR_SYNC_SUCCESS = 1;
  public static final Integer EMR_SYNC_FAILURE = 2;
  
  private ProgressDialog progressDialog;
  private Context mContext = null; // TODO context leak?
  
  public MDSSyncTask(Context c) {
    mContext = c;
  }
  
  private boolean syncPatients(Context c) {
    return MDSInterface.updatePatientDatabase(c, c.getContentResolver());
  }
  
  private boolean syncEvents(Context c) {
    Cursor cursor = null; 
    Log.i(TAG, "Syncing the event log to the MDS.");
    
    try {
      // Get all un-uploaded events.
      cursor = c.getContentResolver().query(EventSQLFormat.CONTENT_URI, 
          new String[] { EventSQLFormat._ID, EventSQLFormat.CREATED_DATE, 
                  EventSQLFormat.EVENT_TYPE, EventSQLFormat.EVENT_VALUE, 
                  EventSQLFormat.ENCOUNTER_REFERENCE, EventSQLFormat.PATIENT_REFERENCE, 
                  EventSQLFormat.USER_REFERENCE }, 
                EventSQLFormat.UPLOADED+"=?", new String[] { "0" }, null);
      int numEvents = cursor.getCount();
      
      if (numEvents == 0) {
        // Nothing to upload, quit.
        Log.i(TAG, "No unuploaded events. Skipping syncEvents.");
        return true;
      } else {
        Log.i(TAG, "There are " + numEvents + " unuploaded events.");
      }
      
      StringBuilder sb = new StringBuilder("(");
      List<Event> events = new ArrayList<Event>(numEvents);

      cursor.moveToFirst();
      while (!cursor.isAfterLast()) {
        
        Event e = new Event();
        e.event_time = cursor.getLong(cursor.getColumnIndex(EventSQLFormat.CREATED_DATE));
        e.event_type = cursor.getString(cursor.getColumnIndex(EventSQLFormat.EVENT_TYPE));
        e.event_value = cursor.getString(cursor.getColumnIndex(EventSQLFormat.EVENT_VALUE));
        e.encounter_reference = cursor.getString(cursor.getColumnIndex(EventSQLFormat.ENCOUNTER_REFERENCE));
        e.patient_reference = cursor.getString(cursor.getColumnIndex(EventSQLFormat.PATIENT_REFERENCE));
        e.user_reference = cursor.getString(cursor.getColumnIndex(EventSQLFormat.USER_REFERENCE));
        int id = cursor.getInt(cursor.getColumnIndex(EventSQLFormat._ID));
        events.add(e);
        
        sb.append(id);
        if (!cursor.isLast()) {
          sb.append(",");
        }
                
        cursor.moveToNext();
      }
      sb.append(")");

      // Submit the events to the MDS
      boolean result = MDSInterface.submitEvents(c, events);
      
      // Set the uploaded events as uploaded in the database.
      if (result) {
        Log.i(TAG, "Successfully uploaded " + numEvents + " events.");
        ContentValues cv = new ContentValues();
        cv.put(EventSQLFormat.UPLOADED, 1);
        int rowsUpdated = c.getContentResolver().update(EventSQLFormat.CONTENT_URI, cv, EventSQLFormat._ID +" in " + sb.toString(), null);
        if (rowsUpdated != numEvents) {
          Log.w(TAG, "Didn't get as many rows updated as we thought we would.");
        }
      } 
      
      return result;
    } catch (Exception e) {
      Log.e(TAG, "While trying to submit the event log, got exception: " + e.toString());
      e.printStackTrace();
    } finally {
      if (cursor != null)
        cursor.deactivate();
    }
      
    return false;
  }
  
  @Override
  protected Integer doInBackground(Context... params) {
    Log.i(TAG, "Executing EMRSyncTask");
    Context c = params[0];
    
    Integer result = EMR_SYNC_NO_CONNECTION; // TODO detect this case better
    
    if (MocaUtil.checkConnection(c)) {
      boolean patientSyncResult = syncPatients(c);
      boolean eventSyncResult = syncEvents(c);
      
      result = (patientSyncResult && eventSyncResult) ? EMR_SYNC_SUCCESS : EMR_SYNC_FAILURE;  
    }
    
    return result;
  }
  
  @Override
  protected void onPreExecute() {
    Log.i(TAG, "About to execute EMRSyncTask");
    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
      }
    progressDialog = new ProgressDialog(mContext);
      progressDialog.setMessage("Updating patient database cache");
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progressDialog.show();  
    }
  
  @Override
  protected void onPostExecute(Integer result) {
    Log.i(TAG, "Completed EMRSyncTask");
    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
      }
  }
  
}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java