/// <summary>
        /// Create, update, delete requirements in EA Package from Requirement DataTable.
        /// </summary>
        /// <param name="eaObjectType"></param>
        /// <param name="eaStereotype"></param>
        /// <param name="stateNew"></param>
        /// <param name="stateChanged"></param>
        /// <param name="importFile"></param>
        private void CreateUpdateDeleteEaRequirements(string eaObjectType, string eaStereotype, string stateNew,
                                                      string stateChanged, string importFile)
        {
            Count        = 0;
            CountChanged = 0;
            CountNew     = 0;
            List <int> parentElementIdsPerLevel = new List <int> {
                0
            };
            int parentElementId = 0;
            int lastElementId   = 0;

            int    oldLevel    = 0;
            string notesColumn = Settings.AttrNotes ?? "";

            foreach (DataRow row in DtRequirements.Rows)
            {
                Count += 1;
                string     objectId   = row["Id"].ToString();
                SpecObject specObject = (SpecObject)row["specObject"];


                int objectLevel = Int32.Parse(row["Object Level"].ToString()) - 1;

                // Maintain parent ids of level
                // get parent id
                if (objectLevel > oldLevel)
                {
                    if (parentElementIdsPerLevel.Count <= objectLevel)
                    {
                        parentElementIdsPerLevel.Add(lastElementId);
                    }
                    else
                    {
                        parentElementIdsPerLevel[objectLevel] = lastElementId;
                    }
                    parentElementId = lastElementId;
                }

                if (objectLevel < oldLevel)
                {
                    parentElementId = parentElementIdsPerLevel[objectLevel];
                }

                oldLevel = objectLevel;

                CombineAttrValues(Settings.AliasList, row, out string alias, ShortNameLength, makeName: true);
                CombineAttrValues(Settings.AttrNameList, row, out string name, ShortNameLength, makeName: true);
                string notes = GetStringAttrValue(notesColumn != "" ? row[notesColumn].ToString() : row[1].ToString());


                // Check if requirement with Doors ID already exists
                bool isExistingRequirement = DictPackageRequirements.TryGetValue(objectId, out int elId);


                EA.Element el;
                if (isExistingRequirement)
                {
                    el = Rep.GetElementByID(elId);
                    if (el.Alias != alias ||
                        el.Name != name ||
                        el.Notes != notes)
                    {
                        if (stateChanged != "")
                        {
                            el.Status = stateChanged;
                        }
                        CountChanged += 1;
                    }
                }
                else
                {
                    el = (EA.Element)Pkg.Elements.AddNew(name, "Requirement");
                    if (stateNew != "")
                    {
                        el.Status = stateNew;
                    }
                    CountChanged += 1;
                }


                try
                {
                    el.Alias        = alias;
                    el.Name         = name;
                    el.Multiplicity = objectId;
                    el.Notes        = notes;
                    el.TreePos      = Count * 10;
                    el.PackageID    = Pkg.PackageID;
                    el.ParentID     = parentElementId;
                    el.Type         = eaObjectType;
                    el.Stereotype   = eaStereotype;

                    el.Update();
                    Pkg.Elements.Refresh();
                    lastElementId = el.ElementID;
                }
                catch (Exception e)
                {
                    if (MessageBox.Show($@"Name: '{name}'
Alias: '{alias}
ObjectId/Multiplicity: '{objectId}

{e}", @"Error update EA Element, skip!",
                                        MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }

                // handle the remaining columns/ tagged values
                var cols = from c in DtRequirements.Columns.Cast <DataColumn>()
                           join v in specObject.SpecType.SpecAttributes on c.ColumnName equals
                           v.LongName  // specObject.Values on c.ColumnName equals v.AttributeDefinition.LongName
                           where !ColumnNamesNoTaggedValues.Any(n => n == c.ColumnName)
                           select new
                {
                    Name    = c.ColumnName,
                    Value   = row[c].ToString(),
                    AttrDef = v
                }
                ;
                // Handle *.rtf/*.docx content
                string rtfValue = CombineRtfAttrValues(Settings.RtfNameList, row);

                // Update EA linked documents by graphics and embedded elements
                UpdateLinkedDocument(el, rtfValue, importFile);

                // Update/Create Tagged value
                DeleteWritableTaggedValuesForElement(el);
                // over all columns
                foreach (var c in cols)
                {
                    // suppress column if already schown in notes
                    if (notesColumn != c.Name)
                    {
                        // Enum with multivalue
                        if (c.AttrDef is AttributeDefinitionEnumeration attrDefinitionEnumeration &&
                            attrDefinitionEnumeration.IsMultiValued)
                        {
                            // Enum values available
                            var arrayEnumValues = ((DatatypeDefinitionEnumeration)c.AttrDef.DatatypeDefinition)
                                                  .SpecifiedValues
                                                  .Select(x => x.LongName).ToArray();
                            Regex rx     = new Regex(@"\r\n| ");
                            var   values = rx.Replace(c.Value, ",").Split(',');
                            var   found  = from all in arrayEnumValues
                                           from s1 in values.Where(xxx => all == xxx).DefaultIfEmpty()
                                           select new { All = all, Value = s1 };
                            var value = "";
                            var del   = "";
                            foreach (var f in found)
                            {
                                if (f.Value == null)
                                {
                                    value = $"{value}{del}0";
                                }
                                else
                                {
                                    value = $"{value}{del}1";
                                }
                                del = ",";
                            }
                            CreateUpdateTaggedValueDuringInput(el, c.Name, c.Value, c.AttrDef);
                        }
                        else
                        {
                            // handle roundtrip attributes
                            CreateUpdateTaggedValueDuringInput(el, c.Name, c.Value, c.AttrDef);
                        }
                    }
                }
            }
        /// <summary>
        /// Import and update ReqIF Requirements. You can set EA ObjectType like "Requirement" or EA Stereotype like "FunctionalRequirement"
        /// </summary>
        /// async Task
        public override bool ImportForFile(string eaObjectType = "Requirement",
                                           string eaStereotype = "",
                                           string stateNew     = "",
                                           string stateChanged = "")
        {
            bool result = true;

            Rep.BatchAppend     = true;
            Rep.EnableUIUpdates = false;

            // Deserialize
            var reqIf = DeSerializeReqIf(ImportModuleFile, validate: Settings.ValidateReqIF);

            if (reqIf == null)
            {
                return(false);
            }

            InitializeDoorsRequirementsTable(reqIf);


            //reqIf.CoreContent[0].Specifications.Dump();
            // over all submodules
            Specification elModule = reqIf.CoreContent[0].Specifications[0];

            AddRequirements(DtRequirements, elModule.Children, 1);



            base.ReadEaPackageRequirements();
            CreateEaPackageDeletedObjects();



            Count        = 0;
            CountChanged = 0;
            CountNew     = 0;
            List <int> parentElementIdsPerLevel = new List <int> {
                0
            };
            int parentElementId = 0;
            int lastElementId   = 0;

            int oldLevel = 0;

            foreach (DataRow row in DtRequirements.Rows)
            {
                Count += 1;
                string objectId     = row["Id"].ToString();
                string reqAbsNumber = objectId;
                //string reqAbsNumber = GetAbsoluteNumerFromDoorsId(objectId);
                int    objectLevel   = Int32.Parse(row["Object Level"].ToString()) - 1;
                string objectNumber  = row["Object Number"].ToString();
                string objectType    = row["Object Type"].ToString();
                string objectHeading = row["Object Heading"].ToString();


                // Maintain parent ids of level
                // get parent id
                if (objectLevel > oldLevel)
                {
                    if (parentElementIdsPerLevel.Count <= objectLevel)
                    {
                        parentElementIdsPerLevel.Add(lastElementId);
                    }
                    else
                    {
                        parentElementIdsPerLevel[objectLevel] = lastElementId;
                    }
                    parentElementId = lastElementId;
                }
                if (objectLevel < oldLevel)
                {
                    parentElementId = parentElementIdsPerLevel[objectLevel];
                }

                oldLevel = objectLevel;
                string name;
                string notes;

                // Estimate if header
                if (objectType == "headline" || !String.IsNullOrWhiteSpace(objectHeading))
                {
                    name  = $"{objectNumber} {objectHeading}";
                    notes = row["Object Heading"].ToString();
                }
                else
                {
                    notes = row["Object Text"].ToString();
                    string objectShorttext = GetTextExtract(notes);
                    objectShorttext = objectShorttext.Length > ShortNameLength?objectShorttext.Substring(0, ShortNameLength) : objectShorttext;

                    name = objectShorttext;
                    //name = $"{reqAbsNumber.PadRight(7)} {objectShorttext}";
                }
                // Check if requirement with Doors ID already exists
                bool isExistingRequirement = DictPackageRequirements.TryGetValue(reqAbsNumber, out int elId);


                EA.Element el;
                if (isExistingRequirement)
                {
                    el = (EA.Element)Rep.GetElementByID(elId);
                    if (el.Alias != objectId ||
                        el.Name != name ||
                        el.Notes != notes ||
                        el.Type != eaObjectType ||
                        el.Stereotype != eaStereotype)
                    {
                        if (stateChanged != "")
                        {
                            el.Status = stateChanged;
                        }
                        CountChanged += 1;
                    }
                }
                else
                {
                    el = (EA.Element)Pkg.Elements.AddNew(name, "Requirement");
                    if (stateNew != "")
                    {
                        el.Status = stateNew;
                    }
                    CountChanged += 1;
                }


                el.Alias        = objectId;
                el.Name         = name;
                el.Multiplicity = reqAbsNumber;
                el.Notes        = notes;
                el.TreePos      = Count * 10;
                el.PackageID    = Pkg.PackageID;
                el.ParentID     = parentElementId;
                el.Type         = eaObjectType;
                el.Stereotype   = eaStereotype;

                el.Update();
                Pkg.Elements.Refresh();
                lastElementId = el.ElementID;

                // handle the remaining columns/ tagged values
                var cols = from c in DtRequirements.Columns.Cast <DataColumn>()
                           where !ColumnNamesNoTaggedValues.Any(n => n == c.ColumnName)
                           select new
                {
                    Name  = c.ColumnName,
                    Value = row[c].ToString()
                }

                ;
                // Update/Create Tagged value
                foreach (var c in cols)
                {
                    TaggedValue.SetUpdate(el, c.Name, c.Value);
                }
            }

            MoveDeletedRequirements();
            UpdatePackage();

            Rep.BatchAppend     = false;
            Rep.EnableUIUpdates = true;
            Rep.ReloadPackage(Pkg.PackageID);
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Import and update Requirements. You can set EA ObjectType like "Requirement" or EA Stereotype like "FunctionalRequirement"
        /// </summary>
        /// async Task
        public override bool ImportForFile(string eaObjectType = "Requirement",
                                           string eaStereotype = "",
                                           string stateNew     = "",
                                           string stateChanged = "")
        {
            bool result = true;

            Rep.BatchAppend     = true;
            Rep.EnableUIUpdates = false;

            // Read xml file
            XElement xElFile;

            try
            {
                xElFile = XElement.Parse(HoUtil.ReadAllText(ImportModuleFile));
            }
            catch (Exception e)
            {
                Rep.BatchAppend     = false;
                Rep.EnableUIUpdates = true;
                MessageBox.Show($@"File: {ImportModuleFile}{Environment.NewLine}{Environment.NewLine}{e}", @"Can't import structured *.xml");
                return(false);
            }

            InitializeXmlStructTable(xElFile);

            // Go through hierarchy and store in DataTable
            var level    = 1;
            var children = xElFile.Descendants(xmlChildrenName).FirstOrDefault(); //.Dump();

            if (OutputChildren(children, level) == false)
            {
                return(false);
            }



            ReadEaPackageRequirements();
            CreateEaPackageDeletedObjects();

            Count        = 0;
            CountChanged = 0;
            CountNew     = 0;
            List <int> parentElementIdsPerLevel = new List <int> {
                0
            };
            int parentElementId = 0;
            int lastElementId   = 0;

            int oldLevel = 0;

            string notesColumn = _settings.AttrNotes ?? "";

            foreach (DataRow row in DtRequirements.Rows)
            {
                Count += 1;



                int objectLevel = Int32.Parse(row["Object Level"].ToString()) - 1;

                // Maintain parent ids of level
                // get parent id
                if (objectLevel > oldLevel)
                {
                    if (parentElementIdsPerLevel.Count <= objectLevel)
                    {
                        parentElementIdsPerLevel.Add(lastElementId);
                    }
                    else
                    {
                        parentElementIdsPerLevel[objectLevel] = lastElementId;
                    }
                    parentElementId = lastElementId;
                }

                if (objectLevel < oldLevel)
                {
                    parentElementId = parentElementIdsPerLevel[objectLevel];
                }

                oldLevel = objectLevel;

                string objectId  = CombineAttrValues(_settings.IdList, row, ShortNameLength);
                string alias     = CombineAttrValues(_settings.AliasList, row, ShortNameLength);
                string name      = CombineAttrValues(_settings.AttrNameList, row, ShortNameLength);
                string notes     = notesColumn != "" ? row[notesColumn].ToString() : row[1].ToString();
                string nameShort = name.Length > ShortNameLength?name.Substring(0, ShortNameLength) : name;

                // Check if requirement with Doors ID already exists
                bool isExistingRequirement = DictPackageRequirements.TryGetValue(objectId, out int elId);


                EA.Element el;
                if (isExistingRequirement)
                {
                    el = Rep.GetElementByID(elId);
                    if (el.Alias != alias ||
                        el.Name != nameShort ||
                        el.Notes != notes)
                    {
                        if (stateChanged != "")
                        {
                            el.Status = stateChanged;
                        }
                        CountChanged += 1;
                    }
                }
                else
                {
                    el = (EA.Element)Pkg.Elements.AddNew(name, "Requirement");
                    if (stateNew != "")
                    {
                        el.Status = stateNew;
                    }
                    CountChanged += 1;
                }


                el.Alias        = alias;
                el.Name         = name;
                el.Multiplicity = objectId;
                el.Notes        = notes;
                el.TreePos      = Count * 10;
                el.PackageID    = Pkg.PackageID;
                el.ParentID     = parentElementId;
                el.Type         = eaObjectType;
                el.Stereotype   = eaStereotype;

                el.Update();
                Pkg.Elements.Refresh();
                lastElementId = el.ElementID;

                // handle the remaining columns/ tagged values
                var cols = from c in DtRequirements.Columns.Cast <DataColumn>()
                           where !ColumnNamesNoTaggedValues.Any(n => n == c.ColumnName)
                           select new
                {
                    Name  = c.ColumnName,
                    Value = row[c].ToString()
                }

                ;


                // Update/Create Tagged value
                foreach (var c in cols)
                {
                    if (notesColumn != c.Name)
                    {
                        TaggedValue.SetUpdate(el, c.Name, c.Value ?? "");
                    }
                }
            }

            MoveDeletedRequirements();
            UpdatePackage(xElFile);

            Rep.BatchAppend     = false;
            Rep.EnableUIUpdates = true;
            Rep.ReloadPackage(Pkg.PackageID);
            return(result);
        }