private void TransferStudy(ServerEntityKey studyKey, PatientInfo oldPatient, Patient newPatient)
		{
			Platform.Log(LogLevel.Info, "Transferring study from {0} [ID={1}] to {2} [ID={3}]",
			             oldPatient.Name, oldPatient.PatientId, newPatient.PatientsName, newPatient.PatientId);

            IAttachStudyToPatient attachStudyToPatientBroker = UpdateContext.GetBroker<IAttachStudyToPatient>();
			AttachStudyToPatientParamaters parms = new AttachStudyToPatientParamaters
			                                       	{
			                                       		StudyKey = studyKey,
			                                       		NewPatientKey = newPatient.GetKey()
			                                       	};
			attachStudyToPatientBroker.Execute(parms);            
		}
		private void UpdateDatabase()
		{
			// Reload the StudyStorage and Study tables.
			LoadEntities();

			UpdateEntity(_study);
			UpdateEntity(_curPatient);
			UpdateEntity(_storage);

			SetStudyEncoding(_study);

			var order = FindOrderForStudy();
			_study.OrderKey = order == null ? null : order.Key;

			// Update the Study table
			var studyUpdateBroker = UpdateContext.GetBroker<IStudyEntityBroker>();
			studyUpdateBroker.Update(_study);

			// Update the StudyStorage table
			var storageUpdateBroker = UpdateContext.GetBroker<IStudyStorageEntityBroker>();
			storageUpdateBroker.Update(_storage);

			// Update Patient level info. Different cases can occur here: 
			//      A) Patient demographic info is not changed ==> update the current patient
			//      B) New patient demographics matches (another) existing patient in the datbase 
			//              ==> Transfer the study to that patient. This means the study count on both patients must be updated.
			//                  The current patient should also be deleted if there's no more study attached to it after the transfer.
			//      C) New patient demographics doesn't match any patient in the database
			//              ==> A new patient should be created for this study. The study count on the current patient should be updated
			//                  and the patient should also be deleted if this is the only study attached to it.
			if (!_patientInfoChanged)
			{
				UpdatePatientDemographics(_curPatient.GetKey(), _newPatientInfo);
			}
			else if (_newPatient == null)
			{
				// No matching patient in the database. We should create a new patient for this study
				_newPatient = CreateNewPatient(_newPatientInfo);
				UpdatePatientDemographics(_newPatient.GetKey(), _newPatientInfo);
			}
			else
			{
				// There's already patient in the database with the new patient demographics
				// The study should be attached to that patient.
				TransferStudy(_study.Key, _oldPatientInfo, _newPatient);
				UpdatePatientDemographics(_newPatient.GetKey(), _newPatientInfo);
			}
		}