Ejemplo n.º 1
0
        protected override async void OnClick() => await ThreadService.RunOnBackground(async() => {
            /*
             * The UICAreaOfReview tbl has a field NoArtPenDate.
             * This is the date it was determined that there were no artificial penetrations
             * If this field is empty there should be at least one UICArtPen well associated with the AOR.
             *
             * Also, remember there is a many to many relationship between UICAreaOfReview and UICArtPen
             * so there isn't an AOR_FK in the UICArtPen record.
             */

            Log.Debug("Running area of review missing artificial penetration validation");

            var progressDialog = new ProgressDialog("🔍 Finding issues...", "Cancel", 100, false);
            var progressor     = new CancelableProgressorSource(progressDialog).Progressor;
            progressDialog.Show();

            var tableName = "UICAreaOfReview";
            using (var table = LayerService.GetTableFromLayersOrTables(tableName, MapView.Active.Map)) {
                progressor.Value = 10;

                if (table == null)
                {
                    NotificationService.NotifyOfMissingLayer(tableName);

                    progressDialog.Hide();

                    return;
                }

                var filter = new QueryFilter {
                    SubFields   = "OBJECTID",
                    WhereClause = "NoArtPenDate IS NULL"
                };

                Log.Verbose("searching for area of review records with no art pen date");

                var problems = new List <long>();
                using (var gdb = table.GetDatastore() as Geodatabase) {
                    if (gdb == null)
                    {
                        Log.Warning("Could not get geodatabase object");

                        progressDialog.Hide();
                    }

                    Log.Verbose("Got datastore as a geodatabase");

                    Log.Verbose("Opening relationship class and selecting {table} records", tableName);

                    var dbSchema = LayerService.GetDbSchema(MapView.Active.Map);

                    Log.Verbose("Using db and schema {schema}", dbSchema);

                    using (var relationshipClass = gdb.OpenDataset <RelationshipClass>($"{dbSchema}AreaOfReviewToArtPen"))
                        using (var selection = table.Select(filter, SelectionType.ObjectID, SelectionOption.Normal)) {
                            progressor.Value = 40;

                            var ids = selection.GetObjectIDs().ToList();

                            if (ids.Count == 0)
                            {
                                NotificationService.NotifyOfValidationSuccess();

                                progressDialog.Hide();

                                return;
                            }

                            Log.Verbose("Finding related records to {ids}", ids);

                            foreach (var id in ids)
                            {
                                var rows = relationshipClass.GetRowsRelatedToDestinationRows(new[] { id });
                                if (!rows.Any())
                                {
                                    problems.Add(id);
                                }
                                else
                                {
                                    foreach (var row in rows)
                                    {
                                        row.Dispose();
                                    }
                                }
                            }

                            progressor.Value = 75;
                        }

                    if (problems.Count == 0)
                    {
                        NotificationService.NotifyOfValidationSuccess();

                        progressDialog.Hide();

                        return;
                    }

                    var layerName = "UICAreaOfReview";
                    var layer     = LayerService.GetLayer(layerName, MapView.Active.Map);

                    if (layer == null)
                    {
                        NotificationService.NotifyOfMissingLayer(layerName);

                        progressDialog.Hide();

                        return;
                    }

                    MapView.Active.Map.SetSelection(new Dictionary <MapMember, List <long> > {
                        { layer, problems }
                    });

                    progressor.Value = 100;

                    progressDialog.Hide();

                    NotificationService.NotifyOfValidationFailure(problems.Count);

                    Log.Verbose("Zooming to selected");

                    await MapView.Active.ZoomToSelectedAsync(TimeSpan.FromSeconds(1.5));

                    Log.Debug("Finished aor artpen Validation");
                }
            }
        });