Advertisement
Guest User

RegisterPatientActivity.java

a guest
Apr 25th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.29 KB | None | 0 0
  1. package jaguar.com.patientcare;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.Toast;
  11. import android.widget.Button;
  12. import android.widget.Spinner;
  13. import android.widget.EditText;
  14.  
  15. import com.parse.*;
  16.  
  17. import java.util.List;
  18.  
  19. /**
  20.  * Created by graemedrucker on 4/13/15.
  21.  */
  22. public class RegisterPatientActivity extends ActionBarActivity{
  23.    
  24.     //Stores input from the EditText fields
  25.     private String firstName;
  26.     private String lastName;
  27.     private String email;
  28.     private String password;
  29.     private String userType;
  30.    
  31.     //Pointer to UI elements
  32.     private EditText txtFirstName;
  33.     private EditText txtLastName;
  34.     private EditText txtEmail;
  35.     private EditText txtPassword;
  36.     private Button btnRegister;
  37.     private Spinner spnUserType;
  38.    
  39.     //the new ParseUser
  40.     private ParseUser user;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState){
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_register_patient);
  46.        
  47.         //Initialze pointer to UI elements
  48.         txtFirstName = (EditText) findViewById(R.id.regFirstName);
  49.         txtLastName = (EditText) findViewById(R.id.regLastName);
  50.         txtEmail = (EditText) findViewById(R.id.regEmail);
  51.         txtPassword = (EditText) findViewById(R.id.regPassword);
  52.         btnRegister = (Button) findViewById(R.id.regButton);
  53.         spnUserType = (Spinner) findViewById(R.id.regSpinner);
  54.        
  55.         //calls btnRegister() if the 'Register' button is pressed
  56.         btnRegister.setOnClickListener(new View.OnClickListener(){
  57.             @Override
  58.             public void onClick(View v){
  59.                 btnRegister();
  60.             }
  61.         });
  62.  
  63.         //Initializes the correct Parse database
  64.         Parse.initialize(this, "VewvxmlYofAlQkLcRUZjjfJSVf0U9WKeWpWcqfwJ", "K8hbr0l4agNpyJKvRvhmjEBSgkPcvTNrqTVYmxz8");
  65.     }
  66.    
  67.     @Override
  68.     public boolean onCreateOptionsMenu(Menu menu){
  69.         // Inflate the menu; this adds items to the action bar if it is present.
  70.         getMenuInflater().inflate(R.menu.menu_register, menu);
  71.         return true;
  72.     }
  73.    
  74.     @Override
  75.     public boolean onOptionsItemSelected(MenuItem item){
  76.         // Handle action bar item clicks here. The action bar will
  77.         // automatically handle clicks on the Home/Up button, so long
  78.         // as you specify a parent activity in AndroidManifest.xml.
  79.         int id = item.getItemId();
  80.        
  81.         //noinspection SimplifiableIfStatement
  82.         if(id == R.id.logout){
  83.             ParseUser.logOut();
  84.             Intent registerIntent = new Intent(getApplicationContext(),LoginActivity.class);
  85.             startActivity(registerIntent);
  86.             finish();
  87.         }
  88.         return super.onOptionsItemSelected(item);
  89.     }
  90.  
  91.     //gets the text from EditText fields and calls registerPatient() with them
  92.     //as arguments when the 'Register' button is pressed
  93.     public void btnRegister(){
  94.         //get text from EditText fields
  95.         firstName = txtFirstName.getText().toString().trim();
  96.         lastName = txtLastName.getText().toString().trim();
  97.         email = txtEmail.getText().toString().trim();
  98.         password = txtPassword.getText().toString().trim();
  99.         userType = spnUserType.getSelectedItem().toString();
  100.        
  101.         //makes sure all fields are filled
  102.         if(firstName.isEmpty()){
  103.             txtFirstName.setError("Please enter your first name");
  104.             return; //ends method without registration
  105.         }
  106.         if(lastName.isEmpty()){
  107.             txtLastName.setError("Please enter your last name");
  108.             return; //ends method without registration
  109.         }
  110.         if(email.isEmpty()){
  111.             txtEmail.setError("Please enter an email address");
  112.             return; //ends method without registration
  113.         }
  114.         if(password.isEmpty()){
  115.             txtPassword.setError("Please enter a password");
  116.             return; //ends method without registration
  117.         }
  118.  
  119.         registerPatient(firstName, lastName, email, password); //register user if all fields are filled
  120.     }
  121.  
  122.     //creates a new ParseUser with the entered credentials
  123.     public void registerPatient(String fn, String ln, String em, String pass){
  124.        
  125.         //creates the new ParseUser
  126.         user = new ParseUser();
  127.        
  128.         //sets the username, password, email, and userType
  129.         user.put("firstName", fn);
  130.         user.put("lastName", ln);
  131.         user.setUsername(em);
  132.         user.setEmail(em);
  133.         user.setPassword(pass);
  134.         switch (userType){
  135.             case "Patient":
  136.                 user.put("userType", 0);
  137.                 break;
  138.             case "Doctor":
  139.                 user.put("userType", 1);
  140.                 break;
  141.             case "Admin":
  142.                 user.put("userType", 2);
  143.                 break;
  144.             default:
  145.                 user.put("userType", 0);
  146.                 break;
  147.         }
  148.         //returns to a blank registration page if the user was
  149.         //successfully made, throws an exception otherwise
  150.         user.signUpInBackground(new SignUpCallback() {
  151.             public void done(ParseException e) {
  152.                 if (e == null) {
  153.                     Intent registerIntent = new Intent(getApplicationContext(),RegisterPatientActivity.class);
  154.                     startActivity(registerIntent);
  155.                     finish();
  156.                    
  157.                     //display 'Success' toast
  158.                     Toast.makeText(getApplicationContext(), "Registration Successful", Toast.LENGTH_LONG).show();
  159.                 } else {
  160.                     toastExceptionObject(e); //throw exception
  161.                 }
  162.             }
  163.         });
  164.     }
  165.  
  166.     private void toastExceptionObject(Exception e) {
  167.         // Retrieve error message from e : Exception
  168.         String errorMessage = e.getLocalizedMessage();
  169.  
  170.         // Capitalize the first letter
  171.         errorMessage = errorMessage.substring(0, 1).toUpperCase() + errorMessage.substring(1, errorMessage.length());
  172.  
  173.         // Display toast with error message
  174.         Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show();
  175.     }
  176.  
  177.     /*
  178.     Pre-condition: user registration must have been attempted, and user registered must have a unique first name that does not appear already in the user table
  179.      */
  180.     private void unitTest1(){
  181.         ParseQuery<ParseUser> testQuery = ParseUser.getQuery();
  182.         testQuery.whereEqualTo("firstName", firstName);
  183.         testQuery.countInBackground(new CountCallback() {
  184.             public void done(int count, ParseException e) {
  185.                 if (e == null) {
  186.                     if(count > 0){
  187.                         String printCount = Integer.toString(count);
  188.                         Log.d("unitTest1", printCount + " patients found.");
  189.                         Log.d("unitTest1", "Patient registered successfully. Test successful");
  190.                     }
  191.                     else{
  192.                         Log.d("unitTest1", "Patient not registered successfully. Test failed");
  193.                     }
  194.                 } else {
  195.                     Log.d("unitTest1", "Error: " + e.getMessage());
  196.                 }
  197.             }
  198.         });
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement