Example #1
0
        private static void AddRelation(ConscienceContext context, BulkImportResult result, Dictionary <int, Character> characters, IRow row, Character character, string hostName, int relationColumn)
        {
            if (row.GetCell(relationColumn) == null)
            {
                return;
            }

            var relation = row.GetCell(relationColumn).ToCleanString().Trim();

            if (string.IsNullOrWhiteSpace(relation))
            {
                return;
            }

            var reader      = new StringReader(relation);
            var name        = reader.ReadLine().Trim();
            var description = reader.ReadToEnd();

            var relatedCharacter = characters.Values.FirstOrDefault(c => c.Name.ToLowerInvariant() == name.ToLowerInvariant());

            if (relatedCharacter == null)
            {
                result.Errors.Add(new BulkImportError {
                    Line = "Characrer relations - " + hostName, Error = "There is no character with name " + name
                });
                return;
            }

            character.Relations.Add(new CharacterRelation
            {
                Character   = relatedCharacter,
                Description = description
            });

            context.SaveChanges();
        }
Example #2
0
        public async Task <HttpResponseMessage> PostAsync(HttpRequestMessage request)
        {
            var result = new BulkImportResult();

            var childContainer = _container.CreateChildContainer();

            var rowIndex = 0;
            var name     = string.Empty;

            try
            {
                var identityService = childContainer.Resolve <IUsersIdentityService>();
                if (!identityService.CurrentUser.Roles.Any(r => r.Name == RoleTypes.Admin.ToString()))
                {
                    throw new UnauthorizedAccessException("Current user is not a platform Admin");
                }

                var httpRequest = HttpContext.Current.Request;
                if (httpRequest.Files.Count > 0)
                {
                    var file = httpRequest.Files[0];

                    //TODO: Use GraphQL instead of manually working with the Entity Framework context from a Controller
                    var context = childContainer.Resolve <ConscienceContext>();

                    ClearDb(context, identityService.CurrentUser);

                    var workbook = WorkbookFactory.Create(file.InputStream);

                    ISheet sheet;

                    var plots      = new Dictionary <int, Dictionary <string, Plot> >();
                    var characters = new Dictionary <int, Dictionary <int, Character> >();

                    for (int run = 0; run <= 1; run++)
                    {
                        sheet = workbook.GetSheet("employees");

                        for (rowIndex = 2; rowIndex <= sheet.LastRowNum; rowIndex++)
                        {
                            try
                            {
                                if (sheet.GetRow(rowIndex) != null && !string.IsNullOrWhiteSpace(sheet.GetRow(rowIndex).GetCell(0).ToCleanString())) //null is when the row only contains empty cells
                                {
                                    var row = sheet.GetRow(rowIndex);

                                    name = (run + 1) + "-" + row.GetCell(0).ToCleanString().Trim();
                                    var displayName = row.GetCell(1).ToCleanString().Trim();
                                    var password    = row.GetCell(2).ToCleanString().Trim();
                                    var role        = row.GetCell(3).ToCleanString().Trim();

                                    if (role.ToLowerInvariant() != RoleTypes.Host.ToString().ToLowerInvariant())
                                    {
                                        var account = await CreateOrUpdateAccount(identityService, context, name, password, role);

                                        if (account.Employee == null && role.ToLowerInvariant() != RoleTypes.Admin.ToString().ToLowerInvariant())
                                        {
                                            context.Employees.Add(new Employee {
                                                Name = displayName, Account = account
                                            });
                                            context.SaveChanges();
                                        }

                                        result.Successes.Add(rowIndex + " employee - " + name);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                result.Errors.Add(new BulkImportError {
                                    Line = rowIndex + " employee - " + name, Error = ex.ToString()
                                });
                            }
                        }

                        sheet      = workbook.GetSheet("plots");
                        plots[run] = new Dictionary <string, Plot>();

                        for (rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
                        {
                            try
                            {
                                if (sheet.GetRow(rowIndex) != null && !string.IsNullOrWhiteSpace(sheet.GetRow(rowIndex).GetCell(0).ToCleanString())) //null is when the row only contains empty cells
                                {
                                    var row = sheet.GetRow(rowIndex);

                                    var plotCode = row.GetCell(0).ToCleanString().Trim();
                                    name = row.GetCell(1).ToCleanString();
                                    var description   = row.GetCell(2).ToCleanString();
                                    var eventName     = row.GetCell(3).ToCleanString();
                                    var eventLocation = row.GetCell(4).ToCleanString();
                                    var eventTime     = row.GetCell(5).DateCellValue;

                                    var plot = new Plot
                                    {
                                        Code        = plotCode,
                                        Name        = name,
                                        Description = description
                                    };

                                    if (!string.IsNullOrWhiteSpace(eventName))
                                    {
                                        plot.Events.Add(new PlotEvent
                                        {
                                            Description = eventName,
                                            Location    = eventLocation,
                                            Hour        = eventTime.Hour,
                                            Minute      = eventTime.Minute
                                        });
                                    }

                                    context.Plots.Add(plot);
                                    context.SaveChanges();

                                    plots[run].Add(plotCode, plot);
                                    result.Successes.Add(rowIndex + " plot - " + name);
                                }
                            }
                            catch (Exception ex)
                            {
                                result.Errors.Add(new BulkImportError {
                                    Line = rowIndex + " plot - " + name, Error = ex.ToString()
                                });
                            }
                        }

                        characters[run] = new Dictionary <int, Character>();

                        sheet = workbook.GetSheet("hosts");

                        for (rowIndex = 2; rowIndex <= sheet.LastRowNum; rowIndex++)
                        {
                            try
                            {
                                if (sheet.GetRow(rowIndex) != null && sheet.GetRow(rowIndex).GetCell(0) != null && !string.IsNullOrWhiteSpace(sheet.GetRow(rowIndex).GetCell(0).ToCleanString())) //null is when the row only contains empty cells
                                {
                                    var row = sheet.GetRow(rowIndex);

                                    name = (run + 1) + "-" + row.GetCell(0).ToCleanString().Trim();
                                    var password = row.GetCell(1).ToCleanString().Trim();

                                    if (characters[run].Any(c => c.Value.CurrentHost.Host.Account.UserName.ToLowerInvariant() == name.ToLowerInvariant()))
                                    {
                                        throw new Exception("There is already a host with the name: " + name);
                                    }

                                    var account = await CreateOrUpdateAccount(identityService, context, name, password, RoleTypes.Host.ToString());

                                    var host = new Host {
                                        Account = account
                                    };
                                    context.Hosts.Add(host);

                                    foreach (var statName in Enum.GetNames(typeof(StatNames)))
                                    {
                                        host.Stats.Add(new Stats {
                                            Name = statName, Value = 10
                                        });
                                    }

                                    host.Stats.First(s => s.Name == StatNames.Apperception.ToString())
                                    .Value = int.Parse(row.GetCell(7).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Candor.ToString())
                                    .Value = int.Parse(row.GetCell(8).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Vivacity.ToString())
                                    .Value = int.Parse(row.GetCell(9).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Coordination.ToString())
                                    .Value = int.Parse(row.GetCell(10).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Meekness.ToString())
                                    .Value = int.Parse(row.GetCell(11).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Humility.ToString())
                                    .Value = int.Parse(row.GetCell(12).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Cruelty.ToString())
                                    .Value = int.Parse(row.GetCell(13).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.SelfPreservation.ToString())
                                    .Value = int.Parse(row.GetCell(14).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Patience.ToString())
                                    .Value = int.Parse(row.GetCell(15).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Decisiveness.ToString())
                                    .Value = int.Parse(row.GetCell(16).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Imagination.ToString())
                                    .Value = int.Parse(row.GetCell(17).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Curiosity.ToString())
                                    .Value = int.Parse(row.GetCell(18).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Agression.ToString())
                                    .Value = int.Parse(row.GetCell(19).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Loyalty.ToString())
                                    .Value = int.Parse(row.GetCell(20).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Empathy.ToString())
                                    .Value = int.Parse(row.GetCell(21).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Tenacity.ToString())
                                    .Value = int.Parse(row.GetCell(22).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Courage.ToString())
                                    .Value = int.Parse(row.GetCell(23).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Sensuality.ToString())
                                    .Value = int.Parse(row.GetCell(24).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Charm.ToString())
                                    .Value = int.Parse(row.GetCell(25).ToCleanString().Trim());
                                    host.Stats.First(s => s.Name == StatNames.Humor.ToString())
                                    .Value = int.Parse(row.GetCell(26).ToCleanString().Trim());

                                    var charName          = row.GetCell(2).ToCleanString().Trim();
                                    var genderValue       = row.GetCell(3).ToCleanString().Trim().ToLowerInvariant();
                                    var gender            = genderValue == "female" ? Genders.Female : (genderValue == "male" ? Genders.Male : Genders.NonGender);
                                    var age               = int.Parse(row.GetCell(4).ToCleanString().Trim());
                                    var assignedOnMonths  = int.Parse(row.GetCell(5).ToCleanString().Trim());
                                    var narrativeFunction = row.GetCell(27).ToCleanString().Trim();
                                    var story             = row.GetCell(28).ToCleanString().Trim();
                                    var memories          = row.GetCell(29).ToCleanString().Trim();
                                    var triggers          = row.GetCell(30).ToCleanString().Trim();

                                    var character = new Character
                                    {
                                        Name              = charName,
                                        Gender            = gender,
                                        Age               = age,
                                        NarrativeFunction = narrativeFunction,
                                        Story             = story
                                    };
                                    context.Characters.Add(character);
                                    characters[run].Add(rowIndex, character);

                                    host.Characters.Add(new CharacterInHost
                                    {
                                        Character  = character,
                                        AssignedOn = DateTime.Now - TimeSpan.FromDays(30 * assignedOnMonths)
                                    });

                                    using (var reader = new StringReader(memories))
                                    {
                                        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                                        {
                                            var memory = new Memory {
                                                Description = line
                                            };
                                            character.Memories.Add(memory);
                                        }
                                    }

                                    using (var reader = new StringReader(triggers))
                                    {
                                        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                                        {
                                            var trigger = new Trigger {
                                                Description = line
                                            };
                                            character.Triggers.Add(trigger);
                                        }
                                    }

                                    context.SaveChanges();

                                    AddCharacterInPlot(context, plots[run], row, character, 31);

                                    AddCharacterInPlot(context, plots[run], row, character, 36);

                                    AddCharacterInPlot(context, plots[run], row, character, 41);

                                    AddCharacterInPlot(context, plots[run], row, character, 46);

                                    AddCharacterInPlot(context, plots[run], row, character, 51);

                                    host.CoreMemory1 = GetCoreMemory(row, 66);

                                    host.CoreMemory2 = GetCoreMemory(row, 67);

                                    host.CoreMemory3 = GetCoreMemory(row, 68);

                                    context.SaveChanges();

                                    result.Successes.Add(rowIndex + " host - " + name);
                                }
                            }
                            catch (Exception ex)
                            {
                                result.Errors.Add(new BulkImportError {
                                    Line = rowIndex + " host - " + name, Error = ex.ToString()
                                });
                            }
                        }

                        for (rowIndex = 2; rowIndex <= sheet.LastRowNum; rowIndex++)
                        {
                            try
                            {
                                if (sheet.GetRow(rowIndex) != null && sheet.GetRow(rowIndex).GetCell(0) != null && !string.IsNullOrWhiteSpace(sheet.GetRow(rowIndex).GetCell(0).ToCleanString())) //null is when the row only contains empty cells
                                {
                                    var row = sheet.GetRow(rowIndex);

                                    name = row.GetCell(0).ToCleanString().Trim();
                                    var lastCharacter = row.GetCell(6).ToCleanString().Trim();

                                    if (!string.IsNullOrWhiteSpace(lastCharacter))
                                    {
                                        var relatedCharacter = characters[run].Values.FirstOrDefault(c => c.Name.ToLowerInvariant() == lastCharacter.ToLowerInvariant());

                                        if (relatedCharacter == null)
                                        {
                                            result.Errors.Add(new BulkImportError {
                                                Line = "Characrer relations - " + name, Error = "There is no character with name " + lastCharacter
                                            });
                                        }
                                    }

                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 56);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 57);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 58);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 59);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 60);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 61);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 62);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 63);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 64);
                                    AddRelation(context, result, characters[run], row, characters[run][rowIndex], name, 65);

                                    result.Successes.Add(rowIndex + " host relations - " + name);
                                }
                            }
                            catch (Exception ex)
                            {
                                result.Errors.Add(new BulkImportError {
                                    Line = rowIndex + " host - " + name, Error = ex.ToString()
                                });
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("Unable to open uploaded file");
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new BulkImportError {
                    Line = rowIndex + " " + name, Error = ex.ToString()
                });
            }
            finally
            {
                childContainer.Dispose();
            }

            FakeLocationsHosts();
            FakeLocationsForEmployees();

            var response = request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
            return(response);
        }