示例#1
0
            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return(false);
                }
                if (ReferenceEquals(this, obj))
                {
                    return(true);
                }
                if (obj.GetType() != typeof(SourceItemModel))
                {
                    return(false);
                }
                SourceItemModel other = (SourceItemModel)obj;

                return(GitUrl == other.GitUrl);
            }
        public async Task <IEnumerable <string> > Restore(MemoryStream input)
        {
            var logs      = new List <string>();
            var createdBy = this._identityGateway.GetIdentity();
            var createdOn = this._datetimeGateway.GetCurrentDateTime();

            await this.ClearData();

            using (var package = new ExcelPackage(input))
            {
                var securityThreatSheet = package.Workbook.Worksheets[SHEET_SecurityThreatsName];
                var securityThreats     = SecurityThreatModel.Build(new SheetRowAdapter(securityThreatSheet));
                foreach (var item in securityThreats)
                {
                    var temp = await this._securityRiskComponent.CreateThreat(new SecurityThreatPostRp()
                    {
                        Name = item.Name
                    });

                    await this._securityRiskComponent.UpdateThreat(temp.Id, item);
                }

                var reliabilityThreatSheet = package.Workbook.Worksheets[SHEET_ReliabilityThreats];
                var reliabilityThreats     = ReliabilityThreatModel.Build(new SheetRowAdapter(reliabilityThreatSheet));
                foreach (var item in reliabilityThreats)
                {
                    var temp = await this._reliabilityRiskComponent.CreateThreat(new ReliabilityThreatPostRp()
                    {
                        Name = item.Name
                    });

                    await this._reliabilityRiskComponent.UpdateThreat(temp.Id, item);
                }

                var usersSheet = package.Workbook.Worksheets["Users"];
                for (int row = 2; row <= usersSheet.Dimension.Rows; row++)
                {
                    var name   = usersSheet.Cells[row, 1].GetValue <string>();
                    var avatar = usersSheet.Cells[row, 2].GetValue <string>();
                    var email  = usersSheet.Cells[row, 3].GetValue <string>();
                    var slack  = usersSheet.Cells[row, 4].GetValue <string>();
                    await this._userComponent.CreateOrUpdate(email, name, avatar, slack);
                }

                var users = await this._dbContext.Users.ToListAsync();

                var organizationSheet = package.Workbook.Worksheets["Organizations"];
                for (int row = 2; row <= organizationSheet.Dimension.Rows; row++)
                {
                    var name    = organizationSheet.Cells[row, 1].GetValue <string>();
                    var avatar  = organizationSheet.Cells[row, 2].GetValue <string>();
                    var leaders = organizationSheet.Cells[row, 3].GetValue <string>();
                    await this._customerComponent.CreateOrUpdate(name, avatar, leaders);
                }

                var organizations = await this._dbContext.Customers.ToListAsync();

                var productsSheet = package.Workbook.Worksheets["Products"];
                for (int row = 2; row <= productsSheet.Dimension.Rows; row++)
                {
                    var organization = productsSheet.Cells[row, 1].GetValue <string>();
                    var name         = productsSheet.Cells[row, 2].GetValue <string>();
                    var description  = productsSheet.Cells[row, 3].GetValue <string>();
                    var leaders      = productsSheet.Cells[row, 4].GetValue <string>();
                    var avatar       = productsSheet.Cells[row, 5].GetValue <string>();
                    await this._productComponent.CreateOrUpdate(organizations.Single(c => c.Name == organization),
                                                                name, description, avatar, leaders);
                }

                var products = await this._dbContext.Products.ToListAsync();

                foreach (var item in products)
                {
                    item.Customer = organizations.Single(c => c.Id == item.CustomerId);
                }

                var anchorSheet = package.Workbook.Worksheets["Anchors"];
                for (int row = 2; row <= anchorSheet.Dimension.Rows; row++)
                {
                    var organization = anchorSheet.Cells[row, 1].GetValue <string>();
                    var product      = anchorSheet.Cells[row, 2].GetValue <string>();
                    var name         = anchorSheet.Cells[row, 3].GetValue <string>();
                    var target       = DateTime.Parse(anchorSheet.Cells[row, 4].GetValue <string>());
                    await this._productComponent.CreateOrUpdateAnchor(
                        products.Single(c => c.Name == product && c.Customer.Name == organization).Id.Value,
                        name, target);
                }

                var squadSheet = package.Workbook.Worksheets["Squads"];
                for (int row = 2; row <= squadSheet.Dimension.Rows; row++)
                {
                    var organization = squadSheet.Cells[row, 1].GetValue <string>();
                    var name         = squadSheet.Cells[row, 2].GetValue <string>();
                    var avatar       = squadSheet.Cells[row, 3].GetValue <string>();
                    var description  = squadSheet.Cells[row, 4].GetValue <string>();
                    var leaders      = squadSheet.Cells[row, 5].GetValue <string>();
                    await this._squadComponent.CreateOrUpdate(organizations.Single(c => c.Name == organization),
                                                              name, description, avatar, leaders);
                }

                var squads = await this._dbContext.Squads.ToListAsync();

                foreach (var item in squads)
                {
                    item.Customer = organizations.Single(c => c.Id == item.CustomerId);
                }

                var membersSheet = package.Workbook.Worksheets["Members"];
                for (int row = 2; row <= membersSheet.Dimension.Rows; row++)
                {
                    var organization = membersSheet.Cells[row, 1].GetValue <string>();
                    var email        = membersSheet.Cells[row, 2].GetValue <string>();
                    var squad        = membersSheet.Cells[row, 3].GetValue <string>();
                    await this._squadComponent.RegisterMember(
                        squads.Where(c => c.Customer.Name == organization && c.Name == squad).Single().Id.Value,
                        users.Single(c => c.Email == email).Id.Value);
                }

                var journeysSheet = package.Workbook.Worksheets["Journeys"];
                for (int row = 2; row <= journeysSheet.Dimension.Rows; row++)
                {
                    var organization    = journeysSheet.Cells[row, 1].GetValue <string>();
                    var product         = journeysSheet.Cells[row, 2].GetValue <string>();
                    var name            = journeysSheet.Cells[row, 3].GetValue <string>();
                    var group           = journeysSheet.Cells[row, 4].GetValue <string>();
                    var availabilitySlo = journeysSheet.Cells[row, 5].GetValue <decimal>();
                    var latencySlo      = journeysSheet.Cells[row, 6].GetValue <decimal>();
                    var experienceSlo   = journeysSheet.Cells[row, 7].GetValue <decimal>();
                    var description     = journeysSheet.Cells[row, 8].GetValue <string>();
                    var avatar          = journeysSheet.Cells[row, 9].GetValue <string>();
                    var leaders         = journeysSheet.Cells[row, 10].GetValue <string>();
                    var slaValue        = new SLAValue(
                        journeysSheet.Cells[row, 11].GetValue <decimal>(),
                        journeysSheet.Cells[row, 12].GetValue <decimal>()
                        );

                    await this._journeyComponent.CreateOrUpdate(
                        organizations.Single(c => c.Name == organization), product, name,
                        description, avatar, availabilitySlo, latencySlo, experienceSlo,
                        slaValue,
                        leaders, group);
                }

                var journeys = await this._dbContext.Journeys.ToListAsync();

                foreach (var item in journeys)
                {
                    item.Product = products.Single(c => c.Id == item.ProductId);
                }

                var featuresSheet = package.Workbook.Worksheets["Features"];

                for (int row = 2; row <= featuresSheet.Dimension.Rows; row++)
                {
                    var organization = featuresSheet.Cells[row, 1].GetValue <string>();
                    var product      = featuresSheet.Cells[row, 2].GetValue <string>();
                    var name         = featuresSheet.Cells[row, 3].GetValue <string>();
                    var avatar       = featuresSheet.Cells[row, 4].GetValue <string>();
                    var description  = featuresSheet.Cells[row, 5].GetValue <string>();
                    await this._featureComponent.CreateOrUpdate(
                        organizations.Single(c => c.Name == organization), product, name, description, avatar);
                }

                var features = await this._dbContext.Features.ToListAsync();

                foreach (var item in features)
                {
                    item.Product = products.Single(c => c.Id == item.ProductId);
                }

                var jouneryMapSheet = package.Workbook.Worksheets["JourneyMaps"];

                for (int row = 2; row <= jouneryMapSheet.Dimension.Rows; row++)
                {
                    var organization = jouneryMapSheet.Cells[row, 1].GetValue <string>();
                    var product      = jouneryMapSheet.Cells[row, 2].GetValue <string>();
                    var journey      = jouneryMapSheet.Cells[row, 3].GetValue <string>();
                    var feature      = jouneryMapSheet.Cells[row, 4].GetValue <string>();
                    var description  = jouneryMapSheet.Cells[row, 5].GetValue <string>();
                    await this._journeyMapComponent.CreateMap(
                        organizations.Single(c => c.Name == organization).Id.Value,
                        product, journey, feature);
                }

                var sourcesSheet = package.Workbook.Worksheets["Sources"];

                var sourcesInstances = SourceModel.Build(organizations, createdOn, createdBy,
                                                         new SheetRowAdapter(sourcesSheet));

                await this._dbContext.Sources.AddRangeAsync(sourcesInstances);

                await this._dbContext.SaveChangesAsync();

                var sources = await this._dbContext.Sources.ToListAsync();

                foreach (var item in sources)
                {
                    item.Product = products.Single(c => c.Id == item.ProductId);
                }

                var indicatorsSheet = package.Workbook.Worksheets["Indicators"];

                for (int row = 2; row <= indicatorsSheet.Dimension.Rows; row++)
                {
                    var organization = indicatorsSheet.Cells[row, 1].GetValue <string>();
                    var product      = indicatorsSheet.Cells[row, 2].GetValue <string>();
                    var feature      = indicatorsSheet.Cells[row, 3].GetValue <string>();
                    var source       = indicatorsSheet.Cells[row, 4].GetValue <string>();
                    await this._indicatorComponent.Create(
                        features.Single(c => c.Name == feature && c.Product.Name == product && c.Product.Customer.Name == organization).Id.Value,
                        sources.Single(c => c.Name == source && c.Product.Name == product && c.Product.Customer.Name == organization).Id.Value
                        );
                }

                var squadFeaturesSheet = package.Workbook.Worksheets["SquadFeatures"];

                for (int row = 2; row <= squadFeaturesSheet.Dimension.Rows; row++)
                {
                    var organization = squadFeaturesSheet.Cells[row, 1].GetValue <string>();
                    var product      = squadFeaturesSheet.Cells[row, 2].GetValue <string>();
                    var feature      = squadFeaturesSheet.Cells[row, 3].GetValue <string>();
                    var squad        = squadFeaturesSheet.Cells[row, 4].GetValue <string>();

                    var featureId = features.Where(c => c.Name == feature && c.Product.Name == product && c.Product.Customer.Name == organization).Single().Id.Value;
                    var squadId   = squads.Where(c => c.Name == squad && c.Customer.Name == organization).Single().Id.Value;
                    await this._featureComponent.RegisterSquad(new SquadFeaturePostRp()
                    {
                        FeatureId = featureId,
                        SquadId   = squadId,
                    });
                }


                var risksSources = await this._dbContext.Sources.Include(c => c.Product).ThenInclude(c => c.Customer)
                                   .ToListAsync();

                var securityRiskSheet = package.Workbook.Worksheets[SHEET_SecurityRisks];
                var securityRisks     = SecurityRiskModel.Build(new SheetRowAdapter(securityRiskSheet), risksSources);
                foreach (var item in securityRisks)
                {
                    var temp = await this._securityRiskComponent.Create(item.Item1);

                    await this._securityRiskComponent.UpdateRisk(temp.Id, item.Item2);
                }

                var reliabilityRiskSheet = package.Workbook.Worksheets[SHEET_ReliabilityRisks];
                var reliabilityRisks     = ReliabilityRiskModel.Build(new SheetRowAdapter(reliabilityRiskSheet), risksSources);
                foreach (var item in reliabilityRisks)
                {
                    var temp = await this._reliabilityRiskComponent.Create(item.Item1);

                    await this._reliabilityRiskComponent.UpdateRisk(temp.Id, item.Item2);
                }

                var sourceItemsSheet = package.Workbook.Worksheets["SourceItems"];
                var sourceItems      = SourceItemModel.Build(organizations, createdOn, createdBy, new SheetRowAdapter(sourceItemsSheet));
                await this._sourceItemComponent.BulkInsert(sourceItems);
            }

            return(logs);
        }