Exemplo n.º 1
0
        private async void Download()
        {
            try
            {
                //TODO: check if connectivity
                if (!App.IsConnected)
                {
                    await App.CurrentApp.MainPage.DisplayAlert(AppResources.NoInternetText, "", AppResources.OKButton);
                }
                else
                {
                    //TODO: if any locally saved data, then uplo
                    _hudProvider.DisplayProgress(AppResources.WorkingText);
                    var observations = _observationRepository.All(mn => mn.ModifiedLocally == true);
                    if (observations != null && observations.Count > 0)
                    {
                        var userinfo = _userInfoService.GetSavedInfo();
                        foreach (var ob in observations)
                        {
                            ob.Begin_Date = OffSetDateTimeZone((DateTime)ob.Begin_Date);
                            ob.End_Date   = OffSetDateTimeZone((DateTime)ob.End_Date);
                            var obsResponse = await _observationService.ObservationSave(userinfo.Email, ob);

                            _observationRepository.Upsert(obsResponse.observation);
                        }
                    }
                    var user     = _userInfoService.GetSavedInfo();
                    var response = await _observationService.GetAllData(user.Email, LanguageId);

                    if (response != null)
                    {
                        //Need to determine if observations need to be uploaded
                        _siteRepository.DeleteAll();
                        _activityRepository.DeleteAll();
                        _indicatorRepository.DeleteAll();
                        _siteIndicatorRepository.DeleteAll();
                        _observationRepository.DeleteAll();
                        _observationEntryRepository.DeleteAll();
                        _observationChangeRepository.DeleteAll();
                        _observationCommentRepository.DeleteAll();
                        _observationAttachmentRepository.DeleteAll();
                        _indicatorAgeRepository.DeleteAll();

                        //have response object so need to insert into database
                        if (response.Sites != null)
                        {
                            foreach (var item in response.Sites)
                            {
                                _siteRepository.Upsert(item);
                            }
                        }
                        if (response.Activities != null)
                        {
                            foreach (var activity in response.Activities)
                            {
                                _activityRepository.Upsert(activity);
                            }
                        }
                        if (response.Indicators != null)
                        {
                            foreach (var indicator in response.Indicators)
                            {
                                _indicatorRepository.Upsert(indicator);
                            }
                        }
                        if (response.SiteIndicators != null)
                        {
                            foreach (var si in response.SiteIndicators)
                            {
                                _siteIndicatorRepository.Upsert(si);
                            }
                        }
                        if (response.Observations != null)
                        {
                            foreach (var obs in response.Observations)
                            {
                                //changes
                                var changes = response.ObservationChanges.ToList().Where(m => m.Observation_id == obs.Observation_id).ToList();
                                obs.Changes = new ObservableCollection <ObservationChange>(changes);

                                // comments
                                var comments = response.ObservationComments.ToList().Where(m => m.Observation_Id == obs.Observation_id).ToList();
                                obs.Comments = new ObservableCollection <ObservationComment>(comments);

                                // attachments
                                var attachments = response.ObservationAttachments.ToList().Where(m => m.Observation_Id == obs.Observation_id).ToList();
                                obs.Attachments = new ObservableCollection <ObservationAttachment>(attachments);

                                // Entries
                                var entries = response.ObservationEntries.ToList().Where(m => m.Observation_id == obs.Observation_id).ToList();
                                obs.ObservationEntries = new ObservableCollection <ObservationEntry>(entries);


                                //Need to get entries, changes, comments, and attachments
                                _observationRepository.Upsert(obs);
                            }
                        }

                        if (response.IndicatorAgePeriods != null)
                        {
                            foreach (var age in response.IndicatorAgePeriods)
                            {
                                _indicatorAgeRepository.Upsert(age);
                            }
                        }
                        ////save to database
                        //LastInformationText = AppResources.LastInformationText;
                        //var ci = DependencyService.Get<ILocale>().GetCurrentCultureInfo();
                        //var date = string.Format(ci, "{0}", DateTime.Now);
                        //LastInformationText = String.Format(AppResources.LastInformationText, date);

                        user.LastDownload = DateTime.Now.ToString();
                        _userInfoService.SaveUserInfo(user);
                        var last = DateTime.Parse(user.LastDownload);
                        var ci   = DependencyService.Get <ILocale>().GetCurrentCultureInfo();
                        LastInformationTextDate = user.LastDownload;                         //string.Format(ci, "{0}", last);
                        LastInformationText     = AppResources.LastInformationText;
                    }



                    //}
                }
                //return RateOptionsSubmissionResult.Failure;
            }
            catch (Exception ex)
            {
                var x = ex.ToString();
                //return RateOptionsSubmissionResult.Failure; //tell view to pop alert
                //display error
            }
            finally
            {
                _hudProvider.Dismiss();
            }
        }
        private List <IndicatorVM> GetIndicators()
        {
            List <IndicatorVM> list = new List <IndicatorVM>();
            //site indicators
            var siteIndicators = _siteIndicatoryRepository.All(m => m.SiteId == Site.SiteId).ToList();

            //Loop through siteIndicators and load up IndicatorVM

            if (siteIndicators != null)
            {
                foreach (var siteIndicator in siteIndicators)
                {
                    //Need to get indicator information and observations and observationsEntry
                    //Indicator
                    var indicator = _indicatorRepository.GetIndicatorModel(siteIndicator.IndicatorId);

                    var activity = _activitiesRepository.GetActivityModel(siteIndicator.ActivityId);

                    //Getting observations

                    //Need to loop through periods and add ones that don't belong
                    //if exists than create IndicatorVM
                    IndicatorVM vm = new IndicatorVM();
                    vm.Activity              = activity == null ? new ActivityModel() : activity;
                    vm.AgePeriods            = _indicatorAgeRespository.GetAgePeriods(siteIndicator.IndicatorId);
                    vm.IndicatorId           = indicator.IndicatorId;
                    vm.Aim                   = indicator.Aim;
                    vm.Created               = indicator.Created;
                    vm.Definition            = indicator.Definition;
                    vm.DenominatorName       = indicator.DenominatorName;
                    vm.DenominatorDefinition = indicator.DenominatorDefinition;
                    vm.DisaggregateByAge     = indicator.DisaggregateByAge;
                    vm.DisaggregateBySex     = indicator.DisaggregateBySex;
                    vm.Frequency             = indicator.Frequency;
                    vm.IndicatorType         = indicator.IndicatorType;
                    vm.Name                  = indicator.Name;
                    vm.NumeratorName         = indicator.NumeratorName;
                    vm.NumeratorDefinition   = indicator.NumeratorDefinition;
                    vm.StartDate             = siteIndicator.StartDate;


                    //var ob3s = _observationRepository.GetObservations(Site.SiteId, siteIndicator.IndicatorId);
                    var obs = _observationRepository.All(m => m.Indicator_id == siteIndicator.IndicatorId).ToList();
                    vm.Observations = obs;
                    list.Add(vm);
                }
            }


            //var activities = _activitiesRepository.All().ToList();

            //var allIndicators = _indicatorRepository.All().ToList();

            //if (allIndicators != null)
            //{
            //	foreach (var item in allIndicators)
            //	{
            //		//need to loop through all indicators and only take the ones that are in siteIndicators
            //		var siteI = siteIndicators.Find(m => (m.IndicatorId.ToString() == item.IndicatorId) && (m.SiteId == Site.SiteId));
            //		//(c => (c.ProductID == ProductID) && (c.ProductName == "ABS001"));

            //		var activity = activities.Find(m => m.ActivityId == siteI.ActivityId);
            //		if (siteI != null)
            //		{
            //			var obs = _observationRepository.GetObservations(Site.SiteId, Int32.Parse(item.IndicatorId));

            //			//create periods
            //			List<Period> periods = new List<Period>();
            //			foreach (var ob in obs)
            //			{
            //				periods.Add(new Period { BeginDate = ob.Begin_Date, EndDate = ob.End_Date });
            //			}

            //		}

            //	}
            //}


            return(list);
        }