Skip to content

Instantly share code, notes, and snippets.

@Romiko
Created September 27, 2011 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romiko/1243867 to your computer and use it in GitHub Desktop.
Save Romiko/1243867 to your computer and use it in GitHub Desktop.
using System;
using MyStory.Logic.Models;
using MyStory.Logic.Models.ImportSchema;
using Neo4jClient;
using Omu.ValueInjecter;
namespace MyStory.Logic.Services.Import
{
public class ImportClientService : IImportClientService
{
readonly IClientService clientService;
public ImportClientService(
IClientService clientService)
{
this.clientService = clientService;
}
public NodeReference<Client> ImportClient(Clients client, Node<Agency> agency)
{
var neo4JClient = new Client();
neo4JClient.InjectFrom(client);
neo4JClient.DateOfBirth = client.DateOfBirth.HasValue
? new DateTimeOffset(client.DateOfBirth.Value, TimeSpan.Zero)
: (DateTimeOffset?)null;
var indexEntries = clientService.GetIndexEntries(agency.Data, neo4JClient, null);
return clientService.CreateClient(neo4JClient, agency, indexEntries);
}
}
}
public NodeReference<Client> CreateClient(Client client, Node<Agency> agencyNode, IEnumerable<IndexEntry> indexEntries)
{
var uniqueIdGenerator = uniqueIdGeneratorCallback();
client.UniqueId = uniqueIdGenerator.NextId(UniqueIdScopes.Clients(agencyNode.Data));
var clientsWithSameUniqueId = graphClient
.RootNode
.Out<Agency>(Hosts.TypeKey, a => a.Key == agencyNode.Data.Key)
.In<Client>(ClientBelongsTo.TypeKey, c => c.UniqueId == client.UniqueId)
.GremlinCount();
if (clientsWithSameUniqueId > 0)
throw new ApplicationException(string.Format("Create New Client failed, uniqueid {0} already exists.", client.UniqueId));
var clientNodeReference = graphClient.Create(
client,
new[] {new ClientBelongsTo(agencyNode.Reference)}, indexEntries);
return clientNodeReference;
}
public IEnumerable<IndexEntry> GetIndexEntries(Agency agency, Client client, IEnumerable<AlsoKnownAs> alsoKnownAses)
{
var indexKeyValues = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(AgencyClientIndexKeys.Gender.ToString(), client.Gender)
};
if (client.DateOfBirth.HasValue)
{
var dateOfBirthUtcTicks = client.DateOfBirth.Value.UtcTicks;
indexKeyValues.Add(new KeyValuePair<string, object>(AgencyClientIndexKeys.DateOfBirth.ToString(), dateOfBirthUtcTicks));
}
var names = new List<string>
{
client.GivenName,
client.FamilyName,
client.PreferredName
};
if (!string.IsNullOrWhiteSpace(client.MiddleNames))
{
var middleNames = client.MiddleNames.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
names.AddRange(middleNames);
}
if (alsoKnownAses != null)
{
names.AddRange(alsoKnownAses.Where(a => !string.IsNullOrEmpty(a.Name)).Select(aka => aka.Name));
}
indexKeyValues.AddRange(names.Select(name => new KeyValuePair<string, object>(AgencyClientIndexKeys.Name.ToString(), name)));
return new[]
{
new IndexEntry
{
Name = IndexNames.Clients(agency),
KeyValues = indexKeyValues.Where(v => v.Value != null)
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment