Android Open Source - sana User Database






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.util;
import java.util.Date;
/*from  ww w.  j a va  2  s.c om*/
import org.moca.db.PatientInfo;
import org.moca.db.MocaDB.PatientSQLFormat;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class UserDatabase {

    public static final String TAG = UserDatabase.class.toString();
    
    private static final String[] PROJECTION = new String[] {PatientSQLFormat.PATIENT_DOB,
    PatientSQLFormat.PATIENT_FIRSTNAME, PatientSQLFormat.PATIENT_LASTNAME, PatientSQLFormat.PATIENT_GENDER,
    PatientSQLFormat.PATIENT_ID};
    
  public static void addDataToUsers(ContentResolver cr, String users) {
    users = users.trim();
    ContentValues newuser = new ContentValues();

    String[] data = users.split("##");
    
    for (String record : data) {
      record = record.trim();
      if ("".equals(record)) {
        continue;
      }
      
      Log.i(TAG, "Processing:" + record);
      try {
        
        String gender = record.substring(record.length() - 1);
        record = record.substring(0, record.length() - 1);
        String[] findname = record.split("[0-9]+");
        String firstname = findname[0];
        String lastname = findname[1];
        String[] findrest = record.split("[A-Za-z]+");
        int birthdate = 0;
        try {
          birthdate = Integer.parseInt(findrest[1]);
        } catch (Exception e) {

        }
        String id = findrest[2];

        Log.i(TAG, "firstname is " + firstname);
        Log.i(TAG, "lastname is " + lastname);
        Log.i(TAG, "birthdate is " + birthdate);
        Log.i(TAG, "gender is " + gender);
        Log.i(TAG, "id is " + id);

        // add new user to database
        newuser.put(PatientSQLFormat.PATIENT_FIRSTNAME, firstname);
        newuser.put(PatientSQLFormat.PATIENT_LASTNAME, lastname);
        newuser.put(PatientSQLFormat.PATIENT_DOB, birthdate);
        newuser.put(PatientSQLFormat.PATIENT_ID, id);
        newuser.put(PatientSQLFormat.PATIENT_GENDER, gender);
        cr.insert(PatientSQLFormat.CONTENT_URI, newuser);
        newuser.clear();
        Log.i(TAG, "added new patient to database");
      } catch (Exception e) {
        Log.i(TAG, "Exception while processing:" + record + " : "
            + e.toString());
      }
    }
  }
  
  private static Date dateFromString(String dumbBirthdate) {
    try {
      String year = dumbBirthdate.substring(0, 4);
      String month = dumbBirthdate.substring(4, 6);
      String day = dumbBirthdate.substring(6, 8);
      
      int iYear = Integer.parseInt(year);
      int iMonth = Integer.parseInt(month);
      int iDay = Integer.parseInt(day);
      
      return new Date(iYear, iMonth, iDay);
    } catch (NumberFormatException e) {
      Log.e(TAG, "Could not parse birthdate from \"" + dumbBirthdate + "\"");
    }
    return null;
  }
  
  private static String capitalize(String name) {
    if (name.length() == 1) {
      return name.toUpperCase();
    } else if (name.length() > 1) {
      return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
    }
    return name;
  }
  
  public static PatientInfo getPatientFromMDSRecord(String patientIdentifier, String record) {
    PatientInfo pi = new PatientInfo();
    
    record = record.trim();

    // THIS IS DISGUSTING, DIE DIE DIE
    String gender = record.substring(record.length()-1);
    record = record.substring(0, record.length()-1);
    String[] findname = record.split("[0-9]+");
    String firstname = findname[0];
    String lastname = findname[1];
    String[] findrest = record.split("[A-Za-z]+");
    
    int birthdate = 0;
    try {
      birthdate = Integer.parseInt(findrest[1]);
    } catch (Exception e) {
    }
    
    String birthdayStr = String.valueOf(birthdate);
    Date d = dateFromString(birthdayStr);
    pi.setPatientBirthdate(d);

    pi.setPatientIdentifier(patientIdentifier);
    pi.setPatientFirstName(capitalize(firstname));
    pi.setPatientLastName(capitalize(lastname));
    
    pi.setPatientGender("m".equals(gender) ? "Male" : "Female");
    
    pi.setConfirmed(true);
    
    return pi;
  }
  
  public static void populateUsingDatabase(Cursor cursor, String patientId, PatientInfo pi) {
    int firstnameind = cursor.getColumnIndex("patient_firstname");
    int lastnameind = cursor.getColumnIndex("patient_lastname");
    int birthdateind = cursor.getColumnIndex("patient_dob");
    int genderind = cursor.getColumnIndex("patient_gender");

    cursor.moveToFirst();

    pi.setPatientIdentifier(patientId);
    String gender = cursor.getString(genderind).trim().toLowerCase();
    Date d = dateFromString(cursor.getString(birthdateind).trim().toLowerCase());
    pi.setPatientBirthdate(d);
    
    pi.setPatientFirstName(capitalize(cursor.getString(firstnameind).trim().toLowerCase()));
    pi.setPatientLastName(capitalize(cursor.getString(lastnameind).trim().toLowerCase()));
    
    pi.setPatientGender("m".equals(gender) ? "Male" : "Female");
    pi.setConfirmed(true);
  }
  
  //checks cached database on phone for patient information
  public static PatientInfo getPatientFromLocalDatabase(Context c, String id) {
    // TODO SQL escaping
    
    Cursor cursor = null;
    try {
      cursor = c.getContentResolver().query(PatientSQLFormat.CONTENT_URI, PROJECTION, "(patient_id=\""+id+"\")", null, null);
      
      if (cursor.getCount() > 0) {
        PatientInfo pi = new PatientInfo();
        populateUsingDatabase(cursor, id, pi);
        return pi;
      }
      
    } catch (Exception e) {
      
    } finally {
      if (cursor != null)
        cursor.deactivate();
    }
    
    return 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