示例#1
0
        private void ImportElements([NotNull] ThreatModel source, [NotNull] IThreatModel target, float dpiFactor,
                                    out int diagrams, out int externalInteractors, out int processes, out int dataStores, out int trustBoundaries)
        {
            diagrams            = 0;
            externalInteractors = 0;
            processes           = 0;
            dataStores          = 0;
            trustBoundaries     = 0;

            var elements = source.Elements?
                           .OrderBy(x => (int)x.Value.ElementType)
                           .ToArray();

            if (elements?.Any() ?? false)
            {
                var schemaManager = new ObjectPropertySchemaManager(target);

                IEntity                entity;
                ITrustBoundary         boundary;
                IEntityTemplate        entityTemplate;
                ITrustBoundaryTemplate trustBoundaryTemplate;
                IPropertySchema        schema;
                IPropertySchema        secondarySchema;
                var baseEISchema = new BaseExternalInteractorPropertySchemaManager(target).GetSchema();
                var basePSchema  = new BaseProcessPropertySchemaManager(target).GetSchema();
                var baseDSSchema = new BaseDataStorePropertySchemaManager(target).GetSchema();
                var baseTBSchema = new BaseTrustBoundaryPropertySchemaManager(target).GetSchema();

                foreach (var element in elements)
                {
                    switch (element.Value.ElementType)
                    {
                    case ElementType.StencilRectangle:
                        entityTemplate = target.EntityTemplates?.FirstOrDefault(x =>
                                                                                string.CompareOrdinal(schemaManager.GetObjectId(x), element.Value.TypeId) == 0);
                        entity = entityTemplate != null?entityTemplate.CreateEntity(element.Value.Name) :
                                     target.AddEntity <IExternalInteractor>(element.Value.Name);

                        boundary        = null;
                        schema          = baseEISchema;
                        secondarySchema = entityTemplate != null?
                                          target.GetSchema(entityTemplate.Name, Resources.DefaultNamespace) : null;

                        externalInteractors++;
                        break;

                    case ElementType.StencilEllipse:
                        entityTemplate = target.EntityTemplates?.FirstOrDefault(x =>
                                                                                string.CompareOrdinal(schemaManager.GetObjectId(x), element.Value.TypeId) == 0);
                        entity = entityTemplate != null?entityTemplate.CreateEntity(element.Value.Name) :
                                     target.AddEntity <IProcess>(element.Value.Name);

                        boundary        = null;
                        schema          = basePSchema;
                        secondarySchema = entityTemplate != null?
                                          target.GetSchema(entityTemplate.Name, Resources.DefaultNamespace) : null;

                        processes++;
                        break;

                    case ElementType.StencilParallelLines:
                        entityTemplate = target.EntityTemplates?.FirstOrDefault(x =>
                                                                                string.CompareOrdinal(schemaManager.GetObjectId(x), element.Value.TypeId) == 0);
                        entity = entityTemplate != null?entityTemplate.CreateEntity(element.Value.Name) :
                                     target.AddEntity <IDataStore>(element.Value.Name);

                        boundary        = null;
                        schema          = baseDSSchema;
                        secondarySchema = entityTemplate != null?
                                          target.GetSchema(entityTemplate.Name, Resources.DefaultNamespace) : null;

                        dataStores++;
                        break;

                    case ElementType.BorderBoundary:
                    case ElementType.LineBoundary:
                        trustBoundaryTemplate = target.TrustBoundaryTemplates?.FirstOrDefault(x =>
                                                                                              string.CompareOrdinal(schemaManager.GetObjectId(x), element.Value.TypeId) == 0);
                        entity   = null;
                        boundary = trustBoundaryTemplate != null?trustBoundaryTemplate.CreateTrustBoundary(element.Value.Name) :
                                       target.AddGroup <ITrustBoundary>(element.Value.Name);

                        schema          = baseTBSchema;
                        secondarySchema = trustBoundaryTemplate != null?
                                          target.GetSchema(trustBoundaryTemplate.Name, Resources.DefaultNamespace) : null;

                        trustBoundaries++;
                        break;

                    default:
                        entity          = null;
                        boundary        = null;
                        schema          = null;
                        secondarySchema = null;
                        break;
                    }

                    if (entity != null)
                    {
                        schemaManager.SetObjectId(entity, element.Value.TypeId);
                        schemaManager.SetInstanceId(entity, element.Key.ToString());
                        var properties = element.Value.Properties?.ToArray();
                        AddProperties(target, schema, secondarySchema, entity, properties);

                        var diagram = target.Diagrams?.FirstOrDefault(x =>
                                                                      string.CompareOrdinal(x.Name, element.Value.Page) == 0);
                        if (diagram == null)
                        {
                            diagram = target.AddDiagram(element.Value.Page);
                            diagrams++;
                        }

                        diagram.AddEntityShape(entity.Id,
                                               new PointF((element.Value.Position.X + element.Value.Size.Width / 2f) * dpiFactor, (element.Value.Position.Y + element.Value.Size.Height / 2f) * dpiFactor));
                    }
                    else if (boundary != null)
                    {
                        schemaManager.SetObjectId(boundary, element.Value.TypeId);
                        schemaManager.SetInstanceId(boundary, element.Key.ToString());
                        var properties = element.Value.Properties?.ToArray();
                        AddProperties(target, schema, secondarySchema, entity, properties);

                        if (element.Value.ElementType == ElementType.BorderBoundary)
                        {
                            var diagram = target.Diagrams?.FirstOrDefault(x =>
                                                                          string.CompareOrdinal(x.Name, element.Value.Page) == 0);
                            if (diagram == null)
                            {
                                diagram = target.AddDiagram(element.Value.Page);
                                diagrams++;
                            }

                            diagram.AddGroupShape(boundary.Id,
                                                  new PointF((element.Value.Position.X + element.Value.Size.Width / 2f) * dpiFactor, (element.Value.Position.Y + element.Value.Size.Height / 2f) * dpiFactor),
                                                  new SizeF(element.Value.Size.Width * dpiFactor, element.Value.Size.Height * dpiFactor));
                        }
                    }
                }
            }
        }