Android Open Source - sana Patient Provider






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.db;
//from   w  w w.  j a  v a 2s .c o m
import java.util.HashMap;

import org.moca.db.MocaDB.DatabaseHelper;
import org.moca.db.MocaDB.PatientSQLFormat;
import org.moca.db.MocaDB.ProcedureSQLFormat;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/*
 * Used to store data for patient authentication, birthdates are stored as integers 
 * with the year then month then day appended together, i.e. 19880922 
 * corresponds to September 22, 1988
 */
public class PatientProvider extends ContentProvider {

  private static final String TAG = "PatientProvider";

  private static final String PATIENT_TABLE_NAME = "patients";

  private static final int PATIENTS = 1;
  private static final int PATIENTS_ID = 2;

  private DatabaseHelper mOpenHelper;
  private static final UriMatcher sUriMatcher;
  private static HashMap<String,String> sPatientProjectionMap;

  @Override
  public boolean onCreate() {
    Log.i(TAG, "onCreate()");
    mOpenHelper = new DatabaseHelper(getContext());
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
    Log.i(TAG, "query() uri="+uri.toString() + " projection=" + TextUtils.join(",",projection));
    
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(PATIENT_TABLE_NAME);

    switch(sUriMatcher.match(uri)) {
    case PATIENTS:    
      break;
    case PATIENTS_ID:
      qb.appendWhere(PatientSQLFormat._ID + "=" + uri.getPathSegments().get(1));
      break;
    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    String orderBy;
    if(TextUtils.isEmpty(sortOrder)) {
      orderBy = PatientSQLFormat.DEFAULT_SORT_ORDER;
    } else {
      orderBy = sortOrder;
    }
    
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count = 0; 

    switch(sUriMatcher.match(uri)) {
    case PATIENTS:
      count = db.update(PATIENT_TABLE_NAME, values, selection, selectionArgs);
      break;
    case PATIENTS_ID:
      String patientId = uri.getPathSegments().get(1);
      count = db.update(PATIENT_TABLE_NAME, values, PatientSQLFormat._ID + "=" + patientId + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : ""), selectionArgs);
      break;
    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;
    switch (sUriMatcher.match(uri)) {
    case PATIENTS:
      count = db.delete(PATIENT_TABLE_NAME, selection, selectionArgs);
      break;
    case PATIENTS_ID:
      String patientId = uri.getPathSegments().get(1); 
      count = db.delete(PATIENT_TABLE_NAME, PatientSQLFormat._ID + "=" + patientId + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : ""), selectionArgs);
      break;
    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }

  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    Log.i(TAG,"starting insert method");
    if (sUriMatcher.match(uri) != PATIENTS) {
      Log.i(TAG, "Throwing IllegalArgumentException");
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    ContentValues values;
    if(initialValues != null) {
      Log.i(TAG,"do we get to this point?");
      values = new ContentValues(initialValues);
    } else {
      values = new ContentValues();
    }

    Long now = Long.valueOf(System.currentTimeMillis());

    if(values.containsKey(PatientSQLFormat.PATIENT_FIRSTNAME) == false) {
      values.put(PatientSQLFormat.PATIENT_FIRSTNAME, "");
    }
    
    if(values.containsKey(PatientSQLFormat.PATIENT_LASTNAME) == false) {
      values.put(PatientSQLFormat.PATIENT_LASTNAME, "");
    }

    if(values.containsKey(PatientSQLFormat.PATIENT_DOB) == false) {
      values.put(PatientSQLFormat.PATIENT_DOB, "");
    }

    if(values.containsKey(PatientSQLFormat.PATIENT_ID) == false) {
      values.put(PatientSQLFormat.PATIENT_ID, now);
    }
    
    if(values.containsKey(PatientSQLFormat.PATIENT_GENDER) == false) {
      values.put(PatientSQLFormat.PATIENT_GENDER, "");
    }
 
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
 
    try {
      long rowId = db.insertOrThrow(PATIENT_TABLE_NAME, PatientSQLFormat.PATIENT_FIRSTNAME, values);
      if(rowId > 0) {
        Uri patientUri = ContentUris.withAppendedId(PatientSQLFormat.CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(patientUri, null);
        return patientUri;
      }
    } catch (Exception e) {
      Log.i(TAG,e.getClass().toString());
      Log.i(TAG, e.getMessage());
    }
 
    throw new SQLException("Failed to insert row into " + uri);
  }

  @Override
  public String getType(Uri uri) {
    Log.i(TAG, "getType(uri="+uri.toString()+")");
    switch(sUriMatcher.match(uri)) {
    case PATIENTS:
      return PatientSQLFormat.CONTENT_TYPE;
    case PATIENTS_ID:
      return PatientSQLFormat.CONTENT_ITEM_TYPE;
    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }
  }

  public static void onCreateDatabase(SQLiteDatabase db) {
    Log.i(TAG, "Creating Patient Data Table");
    db.execSQL("CREATE TABLE " + PATIENT_TABLE_NAME + " ("
        + PatientSQLFormat._ID + " INTEGER PRIMARY KEY,"
        + PatientSQLFormat.PATIENT_ID + " TEXT,"
        + PatientSQLFormat.PATIENT_FIRSTNAME + " TEXT,"
        + PatientSQLFormat.PATIENT_LASTNAME + " TEXT,"
        + PatientSQLFormat.PATIENT_GENDER + " TEXT,"
        + PatientSQLFormat.PATIENT_DOB + " INTEGER"
        + ");");
    Log.i(TAG, "Finished Creating Patient Data TAble");
    
  }
    
  public static void onUpgradeDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
        + newVersion);
        if (oldVersion == 1 && newVersion == 2) {
          // Do nothing
        }
  }

  static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(MocaDB.PATIENT_AUTHORITY, "patients", PATIENTS);
    sUriMatcher.addURI(MocaDB.PATIENT_AUTHORITY, "patients/#", PATIENTS_ID);

    sPatientProjectionMap = new HashMap<String, String>();
    sPatientProjectionMap.put(PatientSQLFormat._ID, PatientSQLFormat._ID);
    sPatientProjectionMap.put(PatientSQLFormat.PATIENT_ID, PatientSQLFormat.PATIENT_ID);
    sPatientProjectionMap.put(PatientSQLFormat.PATIENT_FIRSTNAME, PatientSQLFormat.PATIENT_FIRSTNAME);
    sPatientProjectionMap.put(PatientSQLFormat.PATIENT_LASTNAME, PatientSQLFormat.PATIENT_LASTNAME);
    sPatientProjectionMap.put(PatientSQLFormat.PATIENT_DOB, PatientSQLFormat.PATIENT_DOB);
    sPatientProjectionMap.put(PatientSQLFormat.PATIENT_GENDER, PatientSQLFormat.PATIENT_GENDER);
  }
}




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