public async Task <DrupalPerson> Generate(SitePerson sitePerson) { // Get external source info for this person var sources = await this.dbContext.PeopleSources.Where(s => s.PersonId == sitePerson.PersonId).AsNoTracking().ToArrayAsync(); return(Generate(sitePerson, sources)); }
public async Task <ActionResult> Post(int personId, SitePerson sitePerson) { var dbSitePerson = await this.dbContext.SitePeople.Where(sp => sp.PersonId == personId && sp.SiteId == SiteId).SingleOrDefaultAsync(); if (dbSitePerson == null) { dbSitePerson = sitePerson; // TODO: copy properties dbSitePerson.Person = null; // don't overwrite person info this.dbContext.SitePeople.Add(dbSitePerson); } else { // existing site person, just update props dbSitePerson.FirstName = sitePerson.FirstName.NullIfEmpty(); dbSitePerson.LastName = sitePerson.LastName.NullIfEmpty(); dbSitePerson.Title = sitePerson.Title.NullIfEmpty(); dbSitePerson.Bio = sitePerson.Bio.NullIfEmpty(); dbSitePerson.Emails = sitePerson.Emails.NullIfEmpty(); dbSitePerson.Phones = sitePerson.Phones.NullIfEmpty(); dbSitePerson.Departments = sitePerson.Departments.NullIfEmpty(); dbSitePerson.Websites = sitePerson.Websites.NullIfEmpty(); dbSitePerson.Tags = sitePerson.Tags.NullIfEmpty(); dbSitePerson.PageUid = sitePerson.PageUid; dbSitePerson.ShouldSync = sitePerson.ShouldSync; } dbSitePerson.PersonId = personId; dbSitePerson.SiteId = SiteId; dbSitePerson.LastUpdate = DateTime.UtcNow; await this.dbContext.SaveChangesAsync(); return(CreatedAtAction(nameof(Get), new { sitePersonId = dbSitePerson.Id }, dbSitePerson)); }
public async Task <ActionResult> CreateSitePeople(int id) { var validSite = await this.dbContext.Sites.AnyAsync(s => s.Id == id); if (!validSite) { return(NotFound()); } // go find everyone who doesn't already have a matching site person, and make them one var peopleWithoutSitePeopleIds = await this.dbContext.People.Where(p => !p.SitePeople.Any(sp => sp.SiteId == id)).Select(p => p.Id).ToListAsync(); foreach (var personId in peopleWithoutSitePeopleIds) { var sitePerson = new SitePerson { PersonId = personId, SiteId = id, ShouldSync = false }; this.dbContext.Add(sitePerson); } await this.dbContext.SaveChangesAsync(); return(Json(peopleWithoutSitePeopleIds)); }
private string[] GetTags(SitePerson sitePerson, PersonSource[] sources) { if (!string.IsNullOrWhiteSpace(sitePerson.Tags)) { // site person entry overrides all return(sitePerson.Tags.Split('|')); } return(GetSourceTags(sources)); }
private string[] GetPhones(SitePerson sitePerson, PersonSource[] sources) { if (!string.IsNullOrWhiteSpace(sitePerson.Phones)) { // site person entry overrides all return(sitePerson.Phones.Split('|')); } else if (!string.IsNullOrWhiteSpace(sitePerson.Person.Phone)) { return(new[] { sitePerson.Person.Phone }); } // TODO: pull from sources return(new string[0]); }
public async Task <ActionResult> CreatePerson() { var site = new Site { Name = "Playground", Url = "https://playground.sf.ucdavis.edu" }; var person = new Person { FirstName = "Test", LastName = "Person", IamId = "123456789" }; var sitePerson = new SitePerson { Person = person, Site = site, Name = "Test R. Person" }; this.dbContext.Add(sitePerson); await this.dbContext.SaveChangesAsync(); return(Content("done")); }
public DrupalPerson Generate(SitePerson sitePerson, PersonSource[] sources) { var departmentValues = sitePerson.Departments ?? sitePerson.Person.Departments; var bio = string.IsNullOrWhiteSpace(sitePerson.Bio) ? GetBiography(sources) : sitePerson.Bio; var person = new DrupalPerson { FirstName = sitePerson.FirstName ?? sitePerson.Person.FirstName, LastName = sitePerson.LastName ?? sitePerson.Person.LastName, Title = sitePerson.Title ?? sitePerson.Person.Title, Emails = GetEmails(sitePerson, sources), Phones = GetPhones(sitePerson, sources), Departments = departmentValues?.Split("|").ToArray(), // TODO: should it be null or empty array if we don't have any? Tags = GetTags(sitePerson, sources), Websites = GetWebsites(sitePerson, sources), Bio = bio }; return(person); }
private DrupalWebsite[] GetWebsites(SitePerson sitePerson, PersonSource[] sources) { if (!string.IsNullOrWhiteSpace(sitePerson.Websites)) { // site person entry overrides all var websites = sitePerson.Websites.Split('|'); var websiteList = new List <DrupalWebsite>(); for (int i = 0; i < websites.Length; i += 2) { websiteList.Add(new DrupalWebsite { Uri = websites[i], Title = websites[i + 1] }); } return(websiteList.ToArray()); } // TODO: once we have websites from a person source, check and return here return(new DrupalWebsite[0]); }
// TODO: handle multiple sites? public async Task <string> PublishPerson(SitePerson sitePerson) { // TODO: get from site settings or query dynamically from site settings? var personTypes = new Dictionary <string, string> { { "faculty", "8338d79c-1105-4845-ae56-1919c8738249" }, { "emeriti", "14f01b6f-09af-422a-9b57-739a4b306d17" }, { "leadership", "eca6b30c-6c72-442b-af9a-dce57a0c8358" } }; // TODO: determine which site values to use for each property var drupalPerson = await this.biographyGenerationService.Generate(sitePerson); // Step 1: Sync tag taxonomy var tags = drupalPerson.Tags; var tagNodes = new List <object>(); await foreach (var tagId in this.SyncTags(tags)) { tagNodes.Add(new { type = "taxonomy_term--sf_tags", id = tagId }); } // now we have our tag relationship var field_sf_tags = new { data = tagNodes }; // pull in person type var field_sf_person_type = new { data = new { type = "taxonomy_term--sf_person_type", id = personTypes[sitePerson.Person.Classification] } }; // relationships are different if the person is newly created or existing dynamic relationships; if (sitePerson.PageUid.HasValue) { // existing users get to keep their images relationships = new { field_sf_person_type, field_sf_tags }; } else { // set to default image for people without an existing page relationships = new { field_sf_person_type, field_sf_primary_image = new { data = new { type = "file--file", id = "b1327c77-a018-4ede-b322-0bfd9f4ee79e", // TODO: get from site settings meta = new { title = "Profile Image" } } }, field_sf_tags }; } // Step 2: Compile and generate user page info var personData = new { data = new { type = "node--sf_person", id = sitePerson.PageUid, attributes = new { title = drupalPerson.Title, field_sf_first_name = drupalPerson.FirstName, field_sf_last_name = drupalPerson.LastName, field_sf_position_title = drupalPerson.Title, field_sf_emails = drupalPerson.Emails, field_sf_phone_numbers = drupalPerson.Phones, field_sf_unit = drupalPerson.Departments, field_sf_websites = drupalPerson.Websites.Select(w => new { uri = w.Uri, title = w.Title }), body = new { value = drupalPerson.Bio, format = "basic_html", summary = "" // TODO: do we need summary? } }, relationships }, }; var serialized = JsonSerializer.Serialize(personData); // Console.WriteLine(serialized); // Step 3: form POST/PATCH depending on prior existance of user page var resourceUrl = $"{this.config.ApiBase}/node/sf_person"; var method = HttpMethod.Post; if (sitePerson.PageUid.HasValue) { resourceUrl = resourceUrl + "/" + sitePerson.PageUid.Value; method = System.Net.Http.HttpMethod.Patch; } var request = new HttpRequestMessage(method, resourceUrl); request.Content = new StringContent(serialized, Encoding.UTF8, "application/vnd.api+json"); var response = await this.httpClient.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); // Console.WriteLine(content); // TODO: make models and deserialze properly dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(content); var id = json.data.id; if (sitePerson.PageUid.HasValue == false) { // if this is a new site entity, save the page uid for future updates sitePerson.PageUid = id; } sitePerson.LastSync = DateTime.UtcNow; await this.dbContext.SaveChangesAsync(); return(content); }