/// <summary> /// Get information about species observations /// that has changed in the specified date range or /// from a specified changeId. /// Only date part of parameters changedFrom and changedTo /// are used. It does not matter what time of day that is set /// in parameters changedFrom and changedTo. /// </summary> /// <param name="changedFrom"> /// Changed from date. /// </param> /// <param name="isChangedFromSpecified"> /// Is changed from specified. /// </param> /// <param name="changedTo"> /// Changed to date. /// </param> /// <param name="isChangedToSpecified"> /// Is changed to specified. /// </param> /// <param name="changeId"> /// From witch change id. /// </param> /// <param name="isChangeIdspecified"> /// Is changed id specified. /// </param> /// <param name="maxReturnedChanges"> /// Max number of observations returned. /// </param> /// <param name="mappings"> /// The mapping list. /// </param> /// <param name="context"> /// The web service context. /// </param> /// <param name="connectorServer"> /// The connector service. /// </param> public void GetSpeciesObservationChange(DateTime changedFrom, Boolean isChangedFromSpecified, DateTime changedTo, Boolean isChangedToSpecified, Int64 changeId, Boolean isChangeIdspecified, Int64 maxReturnedChanges, List <HarvestMapping> mappings, WebServiceContext context, IConnectorServer connectorServer) { SpeciesObservationChange speciesObservationChange = new SpeciesObservationChange { CreatedSpeciesObservations = new List <HarvestSpeciesObservation>(), UpdatedSpeciesObservations = new List <HarvestSpeciesObservation>() }; var stopwatch = Stopwatch.StartNew(); int sumNoOfCreated = 0, sumNoOfCreatedErrors = 0, sumNoOfUpdated = 0, sumNoOfUpdatedErrors = 0, sumNoOfDeleted = 0, sumNoOfDeletedErrors = 0; KulProcess kulProcess = new KulProcess(); WebSpeciesObservationDataProvider dataProvider = GetSpeciesObservationDataProvider(context); Stopwatch stopwatch1 = Stopwatch.StartNew(); WebSpeciesObservationChange webSpeciesObservationChange = WebServiceProxy.KulService.GetSpeciesObservationChangeAsSpecies(changedFrom, isChangedFromSpecified, changedTo, isChangedToSpecified, changeId, isChangeIdspecified, maxReturnedChanges); Debug.WriteLine("Harvest: " + DateTime.Now.ToLongTimeString() + " - Time: " + TimeSpan.FromMilliseconds(stopwatch1.ElapsedMilliseconds).TotalMinutes + " Minutes"); // LOOP OVER CHANGEID // Denna verkar inte klara mer än ca 100 obsar så det behövs en loop över dessa... Int32 readSize = 0; Int64 currentChangeId = 0; int j = 0; while ((readSize++ < 50) && (currentChangeId < webSpeciesObservationChange.MaxChangeId)) { currentChangeId = webSpeciesObservationChange.MaxChangeId; context.GetSpeciesObservationDatabase().SetMaxChangeId(dataProvider.Id, webSpeciesObservationChange.MaxChangeId); // CREATED AddNewSpeciesObservations(webSpeciesObservationChange, speciesObservationChange, kulProcess, mappings, context, connectorServer, dataProvider, out sumNoOfCreated, out sumNoOfCreatedErrors); /* int i = 0; * int noOfCreated, noOfCreatedErrors; * foreach (Proxy.KulService.WebSpeciesObservation kulSpeciesObservation in webSpeciesObservationChange.CreatedSpeciesObservations) * { * WebData webData = new WebData { DataFields = new List<WebDataField>() }; * * foreach (WebSpeciesObservationField webSpeciesObservationField in kulSpeciesObservation.Fields) * { * WebDataField webDataField = new WebDataField * { * Information = webSpeciesObservationField.Information, * Name = webSpeciesObservationField.Property.Id.ToString(), * Type = (WebDataType)webSpeciesObservationField.Type, * Unit = webSpeciesObservationField.Unit, * Value = webSpeciesObservationField.Value.CheckInjection() * }; * * if (webDataField.IsNotNull()) * { * webData.DataFields.Add(webDataField); * } * } * * // map webdata * HarvestSpeciesObservation harvestSpeciesObservation = kulProcess.ProcessObservation(webData, mappings, context); * * speciesObservationChange.CreatedSpeciesObservations.Add(harvestSpeciesObservation); * * if (decimal.Remainder(++i, 1000) != 0) * { * continue; * } * * // write every 10000 observation to database to avoid memory problems * connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.CreatedSpeciesObservations, dataProvider, out noOfCreated, out noOfCreatedErrors); * * sumNoOfCreated += noOfCreated; * sumNoOfCreatedErrors += noOfCreatedErrors; * speciesObservationChange.CreatedSpeciesObservations.Clear(); * } * * connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.CreatedSpeciesObservations, dataProvider, out noOfCreated, out noOfCreatedErrors); * * sumNoOfCreated += noOfCreated; * sumNoOfCreatedErrors += noOfCreatedErrors; * speciesObservationChange.CreatedSpeciesObservations.Clear(); */ // UPDATED int i = 0; int noOfUpdated, noOfUpdatedErrors; foreach (Proxy.KulService.WebSpeciesObservation kulSpeciesObservation in webSpeciesObservationChange.UpdatedSpeciesObservations) { WebData webData = new WebData { DataFields = new List <WebDataField>() }; foreach (WebSpeciesObservationField webSpeciesObservationField in kulSpeciesObservation.Fields) { WebDataField webDataField = new WebDataField { Information = webSpeciesObservationField.Information, Name = webSpeciesObservationField.Property.Id.ToString(), Type = (WebDataType)webSpeciesObservationField.Type, Unit = webSpeciesObservationField.Unit, Value = webSpeciesObservationField.Value.CheckInjection() }; if (webDataField.IsNotNull()) { webData.DataFields.Add(webDataField); } } // map webdata HarvestSpeciesObservation harvestSpeciesObservation = kulProcess.ProcessObservation(webData, mappings, context); speciesObservationChange.UpdatedSpeciesObservations.Add(harvestSpeciesObservation); if (decimal.Remainder(++i, 1000) != 0) { continue; } // write every 10000 observation to database to avoid memory problems connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.UpdatedSpeciesObservations, dataProvider, out noOfUpdated, out noOfUpdatedErrors); sumNoOfUpdated += noOfUpdated; sumNoOfUpdatedErrors += noOfUpdatedErrors; speciesObservationChange.UpdatedSpeciesObservations.Clear(); } connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.UpdatedSpeciesObservations, dataProvider, out noOfUpdated, out noOfUpdatedErrors); sumNoOfCreated += noOfUpdated; sumNoOfCreatedErrors += noOfUpdatedErrors; speciesObservationChange.UpdatedSpeciesObservations.Clear(); // DELETED this.RemoveDeletedSpeciesObservations(webSpeciesObservationChange, speciesObservationChange, kulProcess, mappings, context, connectorServer, dataProvider, out sumNoOfDeleted, out sumNoOfDeletedErrors); /* * i = 0; * int noOfDeleted = 0, noOfDeletedErrors = 0; * speciesObservationChange.DeletedSpeciesObservationGuids = new List<String>(); * * foreach (String kulSpeciesObservation in webSpeciesObservationChange.DeletedSpeciesObservationGuids) * { * // if (adsSpeciesObservation.DatabaseId == 99) continue; * String id = kulSpeciesObservation.Substring(kulSpeciesObservation.LastIndexOf('.') + 1); * speciesObservationChange.DeletedSpeciesObservationGuids.Add(id); * * if (decimal.Remainder(++i, 1000) != 0) * { * continue; * } * * // write every 10000 observation to database to avoid memory problems * connectorServer.DeleteSpeciesObservations( * context, * speciesObservationChange.DeletedSpeciesObservationGuids, * dataProvider, * out noOfDeleted, * out noOfDeletedErrors); * sumNoOfDeleted += noOfDeleted; * sumNoOfDeletedErrors += noOfDeletedErrors; * speciesObservationChange.DeletedSpeciesObservationGuids.Clear(); * } * * // write remaining observations to database * connectorServer.DeleteSpeciesObservations( * context, * speciesObservationChange.DeletedSpeciesObservationGuids, * dataProvider, * out noOfDeleted, * out noOfDeletedErrors); * sumNoOfDeleted += noOfDeleted; * sumNoOfDeletedErrors += noOfDeletedErrors; * speciesObservationChange.DeletedSpeciesObservationGuids.Clear(); */ // HANDLE LOOP OVER CHANGEID webSpeciesObservationChange = WebServiceProxy.KulService.GetSpeciesObservationChangeAsSpecies(changedFrom, isChangedFromSpecified, changedTo, isChangedToSpecified, webSpeciesObservationChange.MaxChangeId + 1, isChangeIdspecified, maxReturnedChanges); if (decimal.Remainder(++j, 10) != 0) { continue; } Debug.WriteLine(webSpeciesObservationChange.MaxChangeId + " : count created: " + sumNoOfCreated); } Debug.WriteLine("end :" + webSpeciesObservationChange.MaxChangeId + " : count created: " + sumNoOfCreated); // Log latest harvest date for the data provider // Use changeFrom since changedTo is calculated (+1 day) context.GetSpeciesObservationDatabase().SetDataProviderLatestHarvestDate(dataProvider.Id, changedFrom); context.GetSpeciesObservationDatabase() .LogHarvestRead(context, dataProvider, changedFrom, changedTo, stopwatch.ElapsedMilliseconds, sumNoOfCreated, sumNoOfCreatedErrors, sumNoOfUpdated, sumNoOfUpdatedErrors, sumNoOfDeleted, sumNoOfDeletedErrors, currentChangeId); stopwatch.Stop(); }
/// <summary> /// Get information about species observations that has changed. /// The service contains observations that are aggregated and has start /// and end dates that are set to the first and last day of every month. /// </summary> /// <param name="changedFrom">Start date for changes.</param> /// <param name="changedTo">End date for changes.</param> /// <param name="mappings">The mappings.</param> /// <param name="context">Web service context.</param> /// <param name="connectorServer">The connector server.</param> /// <returns> /// Returns true if there are more species /// observations to retrieve for current date. /// </returns> public Boolean GetSpeciesObservationChange(DateTime changedFrom, DateTime changedTo, List <HarvestMapping> mappings, WebServiceContext context, IConnectorServer connectorServer) { Dictionary <String, WebSpeciesObservation> updatedSpeciesObservations; Int32 minutes; Int64 maxChangeId; // Wait for MVM service to be ready for use. for (minutes = 0; minutes < 60; minutes++) { if (WebServiceProxy.MvmService.IsReadyToUse()) { break; } else { // Wait one minute for MVM service to be ready for use. Thread.Sleep(60000); } } if ((minutes == 60) && !(WebServiceProxy.MvmService.IsReadyToUse())) { // Can not wait any longer for MVM service to be ready. return(false); } SpeciesObservationChange speciesObservationChange = new SpeciesObservationChange(); speciesObservationChange.CreatedSpeciesObservations = new List <HarvestSpeciesObservation>(); WebSpeciesObservationChange webSpeciesObservationChange; webSpeciesObservationChange = new WebSpeciesObservationChange(); webSpeciesObservationChange.CreatedSpeciesObservations = new List <WebSpeciesObservation>(); webSpeciesObservationChange.DeletedSpeciesObservationGuids = new List <String>(); webSpeciesObservationChange.UpdatedSpeciesObservations = new List <WebSpeciesObservation>(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); MvmProcess mvmProcess = new MvmProcess(); WebSpeciesObservationDataProvider dataProvider = GetSpeciesObservationDataProvider(context); maxChangeId = dataProvider.MaxChangeId; for (Int32 index = 0; index < 10; index++) { WebSpeciesObservationChange webSpeciesObservationChangeTemp = WebServiceProxy.MvmService.GetSpeciesObservationChangeAsSpecies(DateTime.Now, false, DateTime.Now, false, maxChangeId, true, Settings.Default.MaxSpeciesObservationsFromMvm); if (webSpeciesObservationChangeTemp.CreatedSpeciesObservations.IsNotEmpty()) { webSpeciesObservationChange.CreatedSpeciesObservations.AddRange(webSpeciesObservationChangeTemp.CreatedSpeciesObservations); } if (webSpeciesObservationChangeTemp.DeletedSpeciesObservationGuids.IsNotEmpty()) { webSpeciesObservationChange.DeletedSpeciesObservationGuids.AddRange(webSpeciesObservationChangeTemp.DeletedSpeciesObservationGuids); } if (webSpeciesObservationChangeTemp.UpdatedSpeciesObservations.IsNotEmpty()) { webSpeciesObservationChange.UpdatedSpeciesObservations.AddRange(webSpeciesObservationChangeTemp.UpdatedSpeciesObservations); } if (maxChangeId < webSpeciesObservationChangeTemp.MaxChangeId) { maxChangeId = webSpeciesObservationChangeTemp.MaxChangeId; } else { // No more species observation changes are available. break; } } // Run all created and updated together as one list. updatedSpeciesObservations = new Dictionary <String, WebSpeciesObservation>(); AddSpeciesObservations(updatedSpeciesObservations, webSpeciesObservationChange.UpdatedSpeciesObservations); AddSpeciesObservations(updatedSpeciesObservations, webSpeciesObservationChange.CreatedSpeciesObservations); List <WebSpeciesObservation> createdAndUpdatedObservations = new List <WebSpeciesObservation>(); createdAndUpdatedObservations.AddRange(updatedSpeciesObservations.Values); // Handle created and updated species observations. int noOfCreated = 0, noOfCreatedErrors = 0; if (createdAndUpdatedObservations.IsNotEmpty()) { foreach (WebSpeciesObservation mvmSpeciesObservation in createdAndUpdatedObservations) { WebData webData = new WebData { DataFields = new List <WebDataField>() }; foreach (WebSpeciesObservationField webSpeciesObservationField in mvmSpeciesObservation.Fields) { WebDataField webDataField = new WebDataField(); webDataField.LoadData(webSpeciesObservationField); webData.DataFields.Add(webDataField); } // Map and add webdata to the 'created collection' speciesObservationChange.CreatedSpeciesObservations.Add(mvmProcess.ProcessObservation(webData, mappings, context)); } connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.CreatedSpeciesObservations, dataProvider, out noOfCreated, out noOfCreatedErrors); } // Handle deleted species observtions. int noOfDeleted = 0, noOfDeletedErrors = 0; speciesObservationChange.DeletedSpeciesObservationGuids = new List <String>(); if (webSpeciesObservationChange.DeletedSpeciesObservationGuids.IsNotEmpty()) { foreach (String mvmSpeciesObservation in webSpeciesObservationChange.DeletedSpeciesObservationGuids) { String id = mvmSpeciesObservation.Substring(mvmSpeciesObservation.LastIndexOf(':') + 1); speciesObservationChange.DeletedSpeciesObservationGuids.Add(id); } connectorServer.DeleteSpeciesObservations(context, speciesObservationChange.DeletedSpeciesObservationGuids, dataProvider, out noOfDeleted, out noOfDeletedErrors); } Debug.WriteLine(maxChangeId + " : count created: " + noOfCreated); context.GetSpeciesObservationDatabase().LogHarvestRead( context, dataProvider, changedFrom, changedTo, stopwatch.ElapsedMilliseconds, noOfCreated, noOfCreatedErrors, 0, 0, noOfDeleted, noOfDeletedErrors, maxChangeId); stopwatch.Stop(); if (maxChangeId > 0) { context.GetSpeciesObservationDatabase().SetMaxChangeId(dataProvider.Id, maxChangeId); } // else: No changes was retrieved. There are no more changes available right now. return(false); }
/// <summary> The add new species observations. </summary> /// <param name="webSpeciesObservationChange"> The web species observation change. </param> /// <param name="speciesObservationChange"> The species observation change. </param> /// <param name="kulProcess"> The kul Process. </param> /// <param name="mappings"> The mappings. </param> /// <param name="context"> The context. </param> /// <param name="connectorServer"> The connector Server. </param> /// <param name="dataProvider"> The data Provider. </param> /// <param name="sumNoOfCreated"> The sum No Of Created. </param> /// <param name="sumNoOfCreatedErrors"> The sum No Of Created Errors. </param> private void AddNewSpeciesObservations( WebSpeciesObservationChange webSpeciesObservationChange, SpeciesObservationChange speciesObservationChange, KulProcess kulProcess, List <HarvestMapping> mappings, WebServiceContext context, IConnectorServer connectorServer, WebSpeciesObservationDataProvider dataProvider, out int sumNoOfCreated, out int sumNoOfCreatedErrors) { int observationCount = 0; int noOfCreated, noOfCreatedErrors; sumNoOfCreated = 0; sumNoOfCreatedErrors = 0; foreach (Proxy.KulService.WebSpeciesObservation kulSpeciesObservation in webSpeciesObservationChange.CreatedSpeciesObservations) { WebData webData = new WebData { DataFields = new List <WebDataField>() }; foreach (WebSpeciesObservationField webSpeciesObservationField in kulSpeciesObservation.Fields) { WebDataField webDataField = new WebDataField { Information = webSpeciesObservationField.Information, Name = webSpeciesObservationField.Property.Id.ToString(), Type = (WebDataType)webSpeciesObservationField.Type, Unit = webSpeciesObservationField.Unit, Value = webSpeciesObservationField.Value.CheckInjection() }; webData.DataFields.Add(webDataField); } // map webdata HarvestSpeciesObservation harvestSpeciesObservation = kulProcess.ProcessObservation(webData, mappings, context); speciesObservationChange.CreatedSpeciesObservations.Add(harvestSpeciesObservation); if (decimal.Remainder(++observationCount, 1000) != 0) { continue; } // write every 10000 observation to database to avoid memory problems connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.CreatedSpeciesObservations, dataProvider, out noOfCreated, out noOfCreatedErrors); sumNoOfCreated += noOfCreated; sumNoOfCreatedErrors += noOfCreatedErrors; speciesObservationChange.CreatedSpeciesObservations.Clear(); } connectorServer.UpdateSpeciesObservations(context, speciesObservationChange.CreatedSpeciesObservations, dataProvider, out noOfCreated, out noOfCreatedErrors); sumNoOfCreated += noOfCreated; sumNoOfCreatedErrors += noOfCreatedErrors; speciesObservationChange.CreatedSpeciesObservations.Clear(); }