de.uni_koblenz_landau.apow.PatientDetailShowFragment.java Source code

Java tutorial

Introduction

Here is the source code for de.uni_koblenz_landau.apow.PatientDetailShowFragment.java

Source

/**
 * Apow - a mobile EHR Management System for low-resource environments
 * in developing countries, exemplified by rural Ghana
 * Copyright (C) 2014 Martin Landua
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package de.uni_koblenz_landau.apow;

import java.util.List;

import org.openmrs.Patient;

import de.uni_koblenz_landau.apow.dialogs.DeleteDialogFragment;
import de.uni_koblenz_landau.apow.dialogs.DeleteDialogListener;
import de.uni_koblenz_landau.apow.helper.Helper;
import de.uni_koblenz_landau.apow.helper.Region;
import de.uni_koblenz_landau.apow.tasks.DataUpdateInterface;
import de.uni_koblenz_landau.apow.tasks.EncounterDeleteTask;
import de.uni_koblenz_landau.apow.tasks.PatientDeleteTask;
import de.uni_koblenz_landau.apow.tasks.PatientDetailInterface;
import de.uni_koblenz_landau.apow.tasks.PatientDetailTask;
import de.uni_koblenz_landau.apow.tasks.TaskActivity;
import de.uni_koblenz_landau.apow.tasks.TaskActivityReference;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout;

public class PatientDetailShowFragment extends Fragment
        implements PatientDetailInterface, DataUpdateInterface, TaskActivity, DeleteDialogListener {

    // Constants
    private static final String ARG_PATIENT = "patient";
    public static final String ARG_PATIENT_ID = "patient_id";
    private static final String DELETE_DIALOG_ID = "deletedialog";

    // Tasks
    private PatientDeleteTask mPatientDeleteTask;
    private PatientDetailTask mPatientDetailTask;

    // UI Variables
    private Patient mPatient;
    private int mPatientID;

    // UI references
    private DeleteDialogFragment mDeleteDialog;
    private TextView mIdentifierView;
    private TextView mNameView;
    private TextView mGenderView;
    private TextView mBirthdateView;
    private TextView mAddress1View;
    private TextView mAddress2View;
    private TextView mCityVillageView;
    private TextView mCountyDistrictView;
    private TextView mStateProvinceView;
    private TextView mCountryView;
    private TextView mLongitudeView;
    private TextView mLatitudeView;
    private TextView mDeathdateView;
    private LinearLayout mDeadLayout;

    /**
     * Empty constructor for Instantiation
     */
    public PatientDetailShowFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);

        // Re-attach Tasks.
        TaskActivityReference.getInstance().attach(PatientDeleteTask.TASK_ID, this);
        TaskActivityReference.getInstance().attach(PatientDetailTask.TASK_ID, this);

        if (getArguments().containsKey(ARG_PATIENT_ID)) {
            mPatientID = getArguments().getInt(ARG_PATIENT_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.patient_detail_show_fragment, container, false);

        // Create UI references.
        mDeleteDialog = (DeleteDialogFragment) getFragmentManager().findFragmentByTag(DELETE_DIALOG_ID);
        if (mDeleteDialog != null) {
            mDeleteDialog.setListener(this);
        }

        mIdentifierView = (TextView) view.findViewById(R.id.patient_detail_show_identifier);
        mNameView = (TextView) view.findViewById(R.id.patient_detail_show_name);
        mGenderView = (TextView) view.findViewById(R.id.patient_detail_show_gender);
        mBirthdateView = (TextView) view.findViewById(R.id.patient_detail_show_birthdate);
        mAddress1View = (TextView) view.findViewById(R.id.patient_detail_show_address_address1);
        mAddress2View = (TextView) view.findViewById(R.id.patient_detail_show_address_address2);
        mCityVillageView = (TextView) view.findViewById(R.id.patient_detail_show_address_cityvillage);
        mCountyDistrictView = (TextView) view.findViewById(R.id.patient_detail_show_address_district);
        mStateProvinceView = (TextView) view.findViewById(R.id.patient_detail_show_address_region);
        mCountryView = (TextView) view.findViewById(R.id.patient_detail_show_address_country);
        mLongitudeView = (TextView) view.findViewById(R.id.patient_detail_show_address_longitude);
        mLatitudeView = (TextView) view.findViewById(R.id.patient_detail_show_address_latitude);
        mDeathdateView = (TextView) view.findViewById(R.id.patient_detail_show_deathdate);
        mDeadLayout = (LinearLayout) view.findViewById(R.id.patient_detail_show_dead_group);

        // Restore UI from saved instance or load data.
        if (savedInstanceState != null) {
            mPatient = (Patient) savedInstanceState.getSerializable(ARG_PATIENT);
            patientToInput();
        } else {
            loadPatient();
        }

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Detach tasks for preventing memory leaks.
        TaskActivityReference.getInstance().detach(PatientDeleteTask.TASK_ID);
        TaskActivityReference.getInstance().detach(PatientDetailTask.TASK_ID);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putSerializable(ARG_PATIENT, mPatient);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.patient_detail_show, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
            NavUtils.navigateUpFromSameTask(getActivity());
            return true;
        }

        if (id == R.id.patient_detail_show_action_discard) {
            mDeleteDialog = DeleteDialogFragment.newInstance(R.string.patient_detail_show_delete_dialog_title);
            mDeleteDialog.setListener(this);
            mDeleteDialog.show(getActivity().getSupportFragmentManager(), DELETE_DIALOG_ID);
            return true;
        }
        if (id == R.id.patient_detail_show_action_edit) {
            Intent intent = new Intent(getActivity(), PatientDetailEditActivity.class);
            intent.putExtra(PatientDetailShowFragment.ARG_PATIENT_ID, mPatientID);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void loadPatient() {
        // Abort if task is already running.
        if (mPatientDetailTask != null) {
            return;
        }

        // Start task.
        PatientDetailTask mPatientDetailTask = new PatientDetailTask();
        TaskActivityReference.getInstance().addTask(PatientDetailTask.TASK_ID, mPatientDetailTask, this);
        mPatientDetailTask.execute(mPatientID);
    }

    private void patientToInput() {
        if (mPatient != null) {
            if (mPatient.getPatientIdentifier() != null) {
                Helper.setViewText(mIdentifierView, mPatient.getPatientIdentifier().getIdentifier());
            }
            if (mPatient.getPersonName() != null) {
                getActivity().setTitle(mPatient.getPersonName().getFullName());
                Helper.setViewText(mNameView, mPatient.getPersonName().getFullName());
            }
            String gender = "";
            String[] genders = getResources().getStringArray(R.array.patient_detail_gender_entries);
            if (mPatient.getGender() != null) {
                switch (mPatient.getGender()) {
                case "U":
                    gender = genders[0];
                    break;
                case "F":
                    gender = genders[1];
                    break;
                case "M":
                    gender = genders[2];
                    break;
                }
            }
            Helper.setViewText(mGenderView, gender);
            String birthdate = Helper.dateToLocaleString(mPatient.getBirthdate(), getActivity());
            if (mPatient.isBirthdateEstimated()) {
                birthdate += getActivity().getString(R.string.patient_detail_show_birthdate_estimated_true);
            }
            Helper.setViewText(mBirthdateView, birthdate);
            if (mPatient.getPersonAddress() != null) {
                Helper.setViewText(mAddress1View, mPatient.getPersonAddress().getAddress1());
                Helper.setViewText(mAddress2View, mPatient.getPersonAddress().getAddress2());
                Helper.setViewText(mCityVillageView, mPatient.getPersonAddress().getCityVillage());
                Helper.setViewText(mCountyDistrictView, mPatient.getPersonAddress().getCountyDistrict());
                Helper.setViewText(mStateProvinceView, mPatient.getPersonAddress().getStateProvince());
                Helper.setViewText(mCountryView, mPatient.getPersonAddress().getCountry());
                Helper.setViewText(mLongitudeView, mPatient.getPersonAddress().getLongitude());
                Helper.setViewText(mLatitudeView, mPatient.getPersonAddress().getLatitude());
            }
            if (mPatient.getDead()) {
                mDeadLayout.setVisibility(View.VISIBLE);
                Helper.setViewText(mDeathdateView,
                        Helper.dateToLocaleString(mPatient.getDeathDate(), getActivity()));
            }
        }
    }

    @Override
    public void onPatientFetched(Patient patient, List<Region> regions) {
        mPatientDetailTask = null;

        // If result is not empty, update UI, else show error message.
        if (patient != null) {
            mPatient = patient;
            patientToInput();
        } else {
            Toast.makeText(getActivity(), R.string.error_database, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Callback for delete dialog.
     */
    @Override
    public void deleteItem(String voidReason) {
        // Abort if task is already running.
        if (mPatientDeleteTask != null) {
            return;
        }

        // Start task.
        mPatientDeleteTask = new PatientDeleteTask(mPatientID, voidReason);
        TaskActivityReference.getInstance().addTask(EncounterDeleteTask.TASK_ID, mPatientDeleteTask, this);
        mPatientDeleteTask.execute();
    }

    /**
     * Called by delete task, when update is finished.
     */
    @Override
    public void onDataUpdateFinished() {
        Intent back = new Intent(getActivity(), PatientListActivity.class);
        back.putExtra(PatientListActivity.ARG_CHANGED, true);
        NavUtils.navigateUpTo(getActivity(), back);
    }
}