openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/PatientRepository.java
/**
     * Sync patient simple promise.
     *
     * @param patient  the patient
     * @param callback the callback
     * @return the simple promise
     */
Sync patient simple promise.
@param patient  the patient
@param callback the callback
@return the simple promise
Sync patient simple promise . @param patient the patient @param callback the callback @return the simple promise
public SimplePromise<Patient> syncPatient(final Patient patient, @Nullable final PatientDeferredResponseCallback callback) {
        final SimpleDeferredObject<Patient> deferred = new SimpleDeferredObject<>();
        if (NetworkUtils.isOnline()) {
            AndroidDeferredManager dm = new AndroidDeferredManager();
            dm.when(locationRepository.getLocationUuid(), getIdGenPatientIdentifier(), getPatientIdentifierTypeUuid())
                    .done(results -> {
                        final List<PatientIdentifier> identifiers = new ArrayList<>();
                        final PatientIdentifier identifier = new PatientIdentifier();
                        identifier.setLocation((LocationEntity) results.get(0).getResult());
                        identifier.setIdentifier((String) results.get(1).getResult());
                        identifier.setIdentifierType((IdentifierType) results.get(2).getResult());
                        identifiers.add(identifier);
                        patient.setIdentifiers(identifiers);
                        patient.setUuid(null);
                        PatientDto patientDto = patient.getPatientDto();
                        Call<PatientDto> call = restApi.createPatient(patientDto);
                        call.enqueue(new Callback<PatientDto>() {
                            @Override
                            public void onResponse(@NonNull Call<PatientDto> call, @NonNull Response<PatientDto> response) {
                                if (response.isSuccessful()) {
                                    PatientDto newPatient = response.body();
                                    patient.setUuid(newPatient.getUuid());
                                    if (patient.getPhoto() != null) {
                                        uploadPatientPhoto(patient);
                                    }
                                    patientDAO.updatePatient(patient.getId(), patient);
                                    if (!patient.getEncounters().equals("")) {
                                        addEncounters(patient);
                                    }
                                    deferred.resolve(patient);
                                    if (callback != null) {
                                        callback.onResponse();
                                    }
                                    ToastUtil.success(context.getString(R.string.patent_data_synced_successfully));
                                } else {
                                    if (callback != null) {
                                        callback.onErrorResponse(context.getString(R.string.patient_cannot_be_synced_due_to_server_error_message, patient.getId(), response.message()));
                                    }
                                    ToastUtil.error("Patient[" + patient.getId() + "] cannot be synced due to server error" + response.message());
                                    deferred.reject(new RuntimeException("Patient cannot be synced due to server error: " + response.errorBody().toString()));
                                }
                            }
                            @Override
                            public void onFailure(@NonNull Call<PatientDto> call, @NonNull Throwable t) {
                                if (callback != null) {
                                    callback.onErrorResponse(context.getString(R.string.patient_cannot_be_synced_due_to_server_error_message, patient.getId(), t.getMessage()));
                                }
                                ToastUtil.notify("Patient[ " + patient.getId() + "] cannot be synced due to request error " + t.toString());
                                deferred.reject(t);
                            }
                        });
                    });
        } else {
            if (callback != null) {
                callback.onNotifyResponse(context.getString(R.string.offline_mode_patient_data_saved_locally_notification_message));
            }
        }
        return deferred.promise();
    }
public SimplePromise < Patient > syncPatient ( final Patient patient , @ Nullable final PatientDeferredResponseCallback callback ) { final SimpleDeferredObject < Patient > deferred = new SimpleDeferredObject < > ( ) ; if ( NetworkUtils . isOnline ( ) ) { AndroidDeferredManager dm = new AndroidDeferredManager ( ) ; dm . when ( locationRepository . getLocationUuid ( ) , getIdGenPatientIdentifier ( ) , getPatientIdentifierTypeUuid ( ) ) . done ( results -> { final List < PatientIdentifier > identifiers = new ArrayList < > ( ) ; final PatientIdentifier identifier = new PatientIdentifier ( ) ; identifier . setLocation ( ( LocationEntity ) results . get ( 0 ) . getResult ( ) ) ; identifier . setIdentifier ( ( String ) results . get ( 1 ) . getResult ( ) ) ; identifier . setIdentifierType ( ( IdentifierType ) results . get ( 2 ) . getResult ( ) ) ; identifiers . add ( identifier ) ; patient . setIdentifiers ( identifiers ) ; patient . setUuid ( null ) ; PatientDto patientDto = patient . getPatientDto ( ) ; Call < PatientDto > call = restApi . createPatient ( patientDto ) ; call . enqueue ( new Callback < PatientDto > ( ) { @ Override public void onResponse ( @ NonNull Call < PatientDto > call , @ NonNull Response < PatientDto > response ) { if ( response . isSuccessful ( ) ) { PatientDto newPatient = response . body ( ) ; patient . setUuid ( newPatient . getUuid ( ) ) ; if ( patient . getPhoto ( ) != null ) { uploadPatientPhoto ( patient ) ; } patientDAO . updatePatient ( patient . getId ( ) , patient ) ; if ( ! patient . getEncounters ( ) . equals ( "" ) ) { addEncounters ( patient ) ; } deferred . resolve ( patient ) ; if ( callback != null ) { callback . onResponse ( ) ; } ToastUtil . success ( context . getString ( R . string . patent_data_synced_successfully ) ) ; } else { if ( callback != null ) { callback . onErrorResponse ( context . getString ( R . string . patient_cannot_be_synced_due_to_server_error_message , patient . getId ( ) , response . message ( ) ) ) ; } ToastUtil . error ( "Patient[" + patient . getId ( ) + "] cannot be synced due to server error" + response . message ( ) ) ; deferred . reject ( new RuntimeException ( "Patient cannot be synced due to server error: " + response . errorBody ( ) . toString ( ) ) ) ; } } @ Override public void onFailure ( @ NonNull Call < PatientDto > call , @ NonNull Throwable t ) { if ( callback != null ) { callback . onErrorResponse ( context . getString ( R . string . patient_cannot_be_synced_due_to_server_error_message , patient . getId ( ) , t . getMessage ( ) ) ) ; } ToastUtil . notify ( "Patient[ " + patient . getId ( ) + "] cannot be synced due to request error " + t . toString ( ) ) ; deferred . reject ( t ) ; } } ) ; } ) ; } else { if ( callback != null ) { callback . onNotifyResponse ( context . getString ( R . string . offline_mode_patient_data_saved_locally_notification_message ) ) ; } } return deferred . promise ( ) ; }
Sync patient simple promise.
Sync patient simple promise .