private string GetTicketGrantingUrl(UsfCasTicket query, WorkReportBuilder reportBuilder)
        {
            var username  = query.Service.Login;
            var password  = query.Service.Password;
            var ticketUrl = query.Service.TicketUrl;

            reportBuilder.Report("Obtained username and password for ticket URL '{0}'.", ticketUrl);

            var data = new NameValueCollection
            {
                { "username", username },
                { "password", password },
            };

            // ticket responses should be fast, but can be unreliable. use a 10 second timeout with retry.
            reportBuilder.Report("Posting username and password to ticket URL.");
            var ticketGrantingResource = _httpConsumer.Upload <string>(ticketUrl, "POST", data, Timeout, Retries);

            reportBuilder.Report("Obtained ticket granting resource response:");
            reportBuilder.Report(ticketGrantingResource);
            reportBuilder.Report("Extracting ticket granting URL from ticket granting resource.");
            var ticketGrantingUrl = ExtractTicketGrantingUrl(ticketGrantingResource, reportBuilder);

            reportBuilder.Report("Obtained ticket granting URL '{0}' from ticket granting resource.", ticketGrantingUrl);
            return(ticketGrantingUrl);
        }
        public void Perform(LoadPacificOceanToponyms job)
        {
            var reportBuilder = new WorkReportBuilder("Load Pacific Ocean Toponyms");

            try
            {
                var geoNameIds = new[] { 8411083, 2363254 };
                var pacifics   = _entities.Query <GeoNamesToponym>().Where(x => geoNameIds.Contains(x.GeoNameId));
                reportBuilder.Report("Found {0} Pacific Ocean toponyms", pacifics.Count());
                if (pacifics.Count() < 2)
                {
                    reportBuilder.Report("Seeding Pacific Ocean toponyms.");

                    _queryProcessor.Execute(new SingleGeoNamesToponym(8411083));
                    _queryProcessor.Execute(new SingleGeoNamesToponym(2363254));

                    reportBuilder.Report("There is/are now {0} Pacific Ocean toponyms.", pacifics.Count());
                }
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
        public void Perform(FixDuplicateMexicos job)
        {
            var reportBuilder = new WorkReportBuilder("Fix Duplicate Mexicos");

            try
            {
                // one is country, other is Estado de Mexico
                var mexicos = _entities.Query <Place>()
                              .EagerLoad(_entities, new Expression <Func <Place, object> >[]
                {
                    x => x.Ancestors.Select(y => y.Ancestor),
                    x => x.Offspring.Select(y => y.Offspring),
                    x => x.GeoPlanetPlace,
                    x => x.GeoNamesToponym,
                })
                              .Where(x => x.OfficialName.Equals("Mexico", StringComparison.OrdinalIgnoreCase)).ToArray();
                var mexico1 = mexicos[0];
                var mexico2 = mexicos[1];
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
        public void Perform(CanonilizeOceans job)
        {
            var reportBuilder = new WorkReportBuilder("Canonilize Oceans");

            try
            {
                var atlantic     = _entities.Query <Place>().Single(x => x.OfficialName == AtlanticText);
                var subAtlantics = _entities.Query <Place>()
                                   .Where(x => x.OfficialName == SouthAtlanticText || x.OfficialName == NorthAtlanticText)
                                   .Where(x => x.ParentId != atlantic.RevisionId);

                reportBuilder.Report("There is/are {0} sub-atlantic oceans that do not have atlantic for a parent.", subAtlantics.Count());

                if (subAtlantics.Any())
                {
                    reportBuilder.Report("Canonilizing sub-atlantics.");
                    var subAtlanticsArray = subAtlantics.ToArray();
                    foreach (var subAtlantic in subAtlanticsArray)
                    {
                        _updatePlace.Handle(new UpdatePlace(subAtlantic.RevisionId, atlantic.RevisionId));
                    }
                    reportBuilder.Report("There is/are now {0} sub-atlantic oceans that do not have atlantic for a parent.", subAtlantics.Count());
                    reportBuilder.Report("");
                }

                var pacific     = _entities.Query <Place>().Single(x => x.OfficialName == PacificText);
                var subPacifics = _entities.Query <Place>()
                                  .Where(x => x.OfficialName == SouthPacificText || x.OfficialName == NorthPacificText)
                                  .Where(x => x.ParentId != pacific.RevisionId);

                reportBuilder.Report("There is/are {0} sub-pacific oceans that do not have pacific for a parent.", subPacifics.Count());

                if (subPacifics.Any())
                {
                    reportBuilder.Report("Canonilizing sub-pacifics.");
                    var subPacificsArray = subPacifics.ToArray();
                    foreach (var subPacific in subPacificsArray)
                    {
                        _updatePlace.Handle(new UpdatePlace(subPacific.RevisionId, pacific.RevisionId));
                    }
                    reportBuilder.Report("There is/are now {0} sub-pacific oceans that do not have pacific for a parent.", subPacifics.Count());
                }
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
        public void Perform(RepairPlaceHierarchies job)
        {
            lock (Lock)
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                var reportBuilder = new WorkReportBuilder("Repair Place Hierarchies");
                reportBuilder.Report("{0}ms: Checking for Places with invalid ancestral hierarchies.", stopwatch.ElapsedMilliseconds);
                try
                {
                    var brokenPlaces = _entities.Query <Place>()
                                       .Where(x => x.Parent != null)
                                       .Count(x => x.Ancestors.Count <= x.Parent.Ancestors.Count || // place should always have more ancestors than its parent
                                              !x.Ancestors.Select(y => y.AncestorId).Contains(x.ParentId.Value) // place ancestors should always contain the parent
                                                                                                                // place should have 1 more ancestor than its closest ancestor does
                                              || x.Ancestors.Count != x.Ancestors.OrderBy(y => y.Separation).FirstOrDefault().Ancestor.Ancestors.Count + 1)
                    ;
                    if (brokenPlaces < 1)
                    {
                        reportBuilder.Report("{0}ms: No Places appear to have invalid ancestral hierarchies.", stopwatch.ElapsedMilliseconds);
                    }
                    else
                    {
                        reportBuilder.Report("{0}ms: A total of {1} Places appear to have invalid hierarchies.", stopwatch.ElapsedMilliseconds, brokenPlaces);
                    }
                    reportBuilder.Report("");

                    var command = new EnsurePlaceHierarchies();
                    _placeNodes.Handle(command);

                    reportBuilder.Report("{0}ms: Place ancestral hierarchy repair command ran.", stopwatch.ElapsedMilliseconds);
                    reportBuilder.Report("At total of {0} Places have had their ancestral hierarchies repaired.", command.EnsuredPlaceNames.Count);
                    foreach (var ensured in command.EnsuredPlaceNames)
                    {
                        reportBuilder.Report("{0} (PlaceId {1})", ensured.Value, ensured.Key);
                    }

                    reportBuilder.Report("");
                    reportBuilder.Report("Job completed in {0} minutes.", stopwatch.Elapsed.TotalMinutes);
                }
                catch (Exception ex)
                {
                    reportBuilder.Report("");
                    reportBuilder.Report("JOB FAILED!");
                    reportBuilder.Report(ex.GetType().Name);
                    reportBuilder.Report(ex.Message);
                    reportBuilder.Report(ex.StackTrace);
                    _exceptionLogger.Log(ex);
                }
                finally
                {
                    reportBuilder.Send(_mailSender);
                }
            }
        }
        public void Perform(SeedAtlanticOcean job)
        {
            var reportBuilder = new WorkReportBuilder("Seed Atlantic Ocean");

            try
            {
                var atlantics = _queryProcessor.Execute(new PlacesWithName
                {
                    MaxResults        = 5,
                    Term              = AtlanticOceanText,
                    TermMatchStrategy = StringMatchStrategy.Equals,
                });
                if (atlantics.Any())
                {
                    reportBuilder.Report("At least {0} Place(s) named '{1}' already exist(s).", atlantics.Count(), AtlanticOceanText);
                }
                else
                {
                    reportBuilder.Report("There are no Places named '{0}'.", AtlanticOceanText);
                    reportBuilder.Report("Seeding '{0}'.", AtlanticOceanText);

                    var geoPlanetPlaces   = _geoPlanet.Places(AtlanticOceanText);
                    var geoPlanetToponyms = _geoNames.Search(new SearchOptions(SearchType.NameEquals, AtlanticOceanText));

                    var geoNamesToponym = geoPlanetToponyms.FirstOrDefault(x => x.FeatureClassCode == "H");
                    var geoPlanetPlace  = geoPlanetPlaces.FirstOrDefault(x => x.Type.Code == 37 || x.Type.Code == 38);

                    if (geoPlanetPlace != null && geoNamesToponym != null)
                    {
                        _queryProcessor.Execute(new PlaceByWoeId(geoPlanetPlace.WoeId, geoNamesToponym.GeoNameId));
                    }

                    reportBuilder.Report("There is/are now {0} Place(s) named '{1}'.", atlantics.Count(), AtlanticOceanText);
                }
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
Exemple #7
0
        private void WaitOnProgress(ImportUsfEstablishments command, WorkReportBuilder reportBuilder)
        {
            var        ticks    = DateTime.UtcNow.Ticks;
            long       duration = 0;
            const long timeout  = 5 * 60 * 1000; // five minutes

            reportBuilder.Report("Waiting up to '{0}' milliseconds.", timeout);
            while (command.Service.Status == UsfFacultyProfileAttribute.InProgress.ToString() && duration <= timeout)
            {
                Thread.Sleep(5000);
                _entities.Reload(command.Service.Integration);
                duration = (DateTime.UtcNow.Ticks - ticks) / TimeSpan.TicksPerMillisecond;
            }
            reportBuilder.Report("Finished waiting for progress, total duration was {0} milliseconds.", duration);
        }
        public void Perform(MigrateUsfAgreementData job)
        {
            var reportBuilder = new WorkReportBuilder("Migrate USF Agreement Data");

            try
            {
                // for USF, move from description into content
                // do not preserve any titles as names.
                const int usfEstablishmentId = 3306;
                var       usfAgreements      = _entities.Get <Agreement>()
                                               .Where(x => x.Participants.Any(y => y.IsOwner
                                                                              &&
                                                                              (
                                                                                  y.EstablishmentId == usfEstablishmentId
                                                                                  ||
                                                                                  y.Establishment.Ancestors.Any(z => z.AncestorId == usfEstablishmentId)
                                                                              )
                                                                              ))
                ;

                foreach (var usfAgreement in usfAgreements)
                {
                    var html = usfAgreement.Description.ToHtml();
                    usfAgreement.Name    = null;
                    usfAgreement.Content = html;
                }
                _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
        public void Perform(ProjectEmployeeSummaryViews job)
        {
            var reportBuilder = new WorkReportBuilder("Cook Activity Data");

            try
            {
                reportBuilder.Report("Getting relevant tenants.");
                var establishmentIds = _placesBuilder.GetEstablishmentIdsWithData().ToArray();
                reportBuilder.Report("Operating on {0} tenants.", establishmentIds.Length);
                foreach (var establishmentId in establishmentIds)
                {
                    var placesViews       = _placesBuilder.Build(establishmentId);
                    var activityCountView = _activityCountsBuilder.Build(establishmentId);
                    reportBuilder.Report("Places view has {0} records., projecting...", placesViews.Length);
                    _placesProjector.Set(placesViews, establishmentId);
                    reportBuilder.Report("Places projected into view manager for establishment #{0}.", establishmentId);
                    _activityCountsProjector.Set(activityCountView, establishmentId);
                }
                reportBuilder.Report("Activity data cooked.");
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
                if (!_mailSent)
                {
                    reportBuilder.Send(_mailSender);
                }
                _mailSent = true;
            }
            //finally // do not want to receive emails indicating success every 10 minutes
            //{
            //    if (!_mailSent)
            //        reportBuilder.Send(_mailSender);
            //    _mailSent = true;
            //}
        }
        private static string ExtractTicketGrantingUrl(string ticketGrantingResource, WorkReportBuilder reportBuilder)
        {
            #region Sample Ticket Granting Resource Response

            /*
             * <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
             * <html>
             *  <head>
             *      <title>201 The request has been fulfilled and resulted in a new resource being created</title>
             * </head>
             * <body>
             *  <h1>TGT Created</h1>
             *  <form action="https://authtest.it.usf.edu:444/v1/tickets/TGT-1152-OBHag3uftwa4gbCNffGo0o5MHpwfNI7Le9UUruzrpt30MtJfeo-IMPERS" method="POST">
             *      Service:
             *      <input type="text" name="service" value=""><br>
             *      <input type="submit" value="Submit">
             *  </form>
             * </body>
             * </html>
             */
            #endregion

            const string pattern = "action=\"";
            reportBuilder.Report("Looking for string pattern '{0}' in ticket granting response.", pattern);
            var startIndex = ticketGrantingResource.IndexOf(pattern, StringComparison.Ordinal) + pattern.Length;
            reportBuilder.Report("Found matching string pattern at index '{0}'.", startIndex);
            reportBuilder.Report("Computing string length for the end of the URL pattern.");
            var length = ticketGrantingResource.IndexOf("\"", startIndex, StringComparison.Ordinal) - startIndex;
            reportBuilder.Report("Length of string for URL was computed to be '{0}'.", length);
            var tgtUrl = ticketGrantingResource.Substring(startIndex, length);
            reportBuilder.Report("Target URL to be used is '{0}'.", tgtUrl);
            return(tgtUrl);
        }
        public void Perform(ComposeRegions job)
        {
            var reportBuilder = new WorkReportBuilder("Compose Regions");

            try
            {
                var regions = _entities.Get <Place>()
                              .Where(x => x.IsRegion && !x.IsWater && !x.Components.Any());

                reportBuilder.Report("There is/are {0} uncomposed region(s).", regions.Count());

                if (regions.Any())
                {
                    var mutated      = false;
                    var regionsArray = regions.ToArray();
                    foreach (var region in regionsArray)
                    {
                        reportBuilder.Report("");
                        reportBuilder.Report("Composing '{0}'...", region.OfficialName);

                        //if (!region.IsRegion) continue;
                        var woeId      = region.GeoPlanetPlace.WoeId;
                        var components = _entities.Get <Place>()
                                         .Where(x => x.GeoPlanetPlace != null &&
                                                x.GeoPlanetPlace.BelongTos.Select(y => y.BelongToWoeId).Contains(woeId));
                        foreach (var component in components)
                        {
                            if (!component.IsCountry && !component.IsWater)
                            {
                                continue;
                            }
                            if (component.IsRegion)
                            {
                                continue;
                            }
                            reportBuilder.Report("    Component: '{0}'", component.OfficialName);
                            if (region.Components.All(x => x.RevisionId != component.RevisionId))
                            {
                                region.Components.Add(component);
                                if (!mutated)
                                {
                                    mutated = true;
                                }
                            }
                        }
                    }
                    if (mutated)
                    {
                        _entities.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                reportBuilder.Send(_mailSender);
            }
        }
        public override void Handle(UserCreated e)
        {
            // is this a usf user?
            var usf    = _queryProcessor.Execute(new EstablishmentByUrl("www.usf.edu"));
            var tenant = _queryProcessor.Execute(new EstablishmentById(e.TenantId));

            if (tenant == null || usf == null || !usf.Equals(tenant))
            {
                return;
            }

            var reportBuilder = new WorkReportBuilder("Handle USF User Created Event");
            var logging       = UsfFacultyProfileAttribute.MailLog.ToString();

            try
            {
                // get the service integration
                var usfFacultyProfile = UsfFacultyProfileAttribute.UsfFacultyProfile.ToString();
                var integration       = _entities.Get <ServiceIntegration>()
                                        .EagerLoad(_entities, new Expression <Func <ServiceIntegration, object> >[]
                {
                    x => x.StringAttributes,
                })
                                        .SingleOrDefault(x => x.TenantId == e.TenantId &&
                                                         x.Name.Equals(usfFacultyProfile));
                if (integration == null)
                {
                    throw new InvalidOperationException(string.Format(
                                                            "Found no service integration for '{0}_{1}'.", usfFacultyProfile, e.TenantId));
                }

                reportBuilder.Report("Wrapping integration data into USF Faculty Profile Service object.");
                var service = new UsfFacultyProfileService(integration);
                logging = service.Logging;

                reportBuilder.Report("Invoking command to import USF person data.");
                _commandHandler.Handle(new ImportUsfPerson(e.Principal, service, e.UserId)
                {
                    ReportBuilder = reportBuilder,
                });
            }
            catch (Exception ex)
            {
                reportBuilder.Report("");
                reportBuilder.Report("JOB FAILED!");
                reportBuilder.Report(ex.GetType().Name);
                reportBuilder.Report(ex.Message);
                reportBuilder.Report(ex.StackTrace);
                _exceptionLogger.Log(ex);
            }
            finally
            {
                _entities.DiscardChanges();
                var usfFacultyProfile = UsfFacultyProfileAttribute.UsfFacultyProfile.ToString();
                var integration       = _entities.Get <ServiceIntegration>()
                                        .EagerLoad(_entities, new Expression <Func <ServiceIntegration, object> >[]
                {
                    x => x.StringAttributes,
                })
                                        .SingleOrDefault(x => x.TenantId == e.TenantId &&
                                                         x.Name.Equals(usfFacultyProfile));

                if (integration == null)
                {
                    reportBuilder.Report("Found no service integration for '{0}_{1}'.", usfFacultyProfile, e.TenantId);
                    reportBuilder.Send(_mailSender);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(logging))
                    {
                        integration.LogEntries.Add(new ServiceLogEntry
                        {
                            IntegrationName = integration.Name,
                            TenantId        = integration.TenantId,
                            Subject         = reportBuilder.Subject,
                            Log             = reportBuilder.Message.ToString(),
                        });
                        _entities.SaveChanges();
                    }
                    if (logging == UsfFacultyProfileAttribute.MailLog.ToString())
                    {
                        reportBuilder.Send(_mailSender);
                    }
                }
            }
        }