private bool importObjects(ObjectType[] objects, MAXImporterWorkingMemory wm) { // first do objects without a parent foreach (ObjectType maxObj in objects.Where(maxObj => string.IsNullOrEmpty(maxObj.parentId))) { importObject(maxObj, wm); } List <ObjectType> leftOver = new List <ObjectType>(); foreach (ObjectType maxObj in objects.Where(maxObj => !string.IsNullOrEmpty(maxObj.parentId))) { if (eaElementDict.ContainsKey(maxObj.parentId)) { importObject(maxObj, wm); } else { leftOver.Add(maxObj); } } foreach (ObjectType maxObj in leftOver) { importObject(maxObj, wm); } return(wm.issues); }
public bool import(EA.Repository Repository, EA.Package selectedPackage, string fileName, bool deleteRelationships = true) { bool issues = false; try { this.Repository = Repository; progress.Show(); Repository.EnableUIUpdates = false; Repository.BatchAppend = true; Repository.EnableCache = true; XmlSerializer serializer = new XmlSerializer(typeof(ModelType)); StreamReader stream = new StreamReader(fileName); // make sure file gets closed using (stream) { ModelType model = (ModelType)serializer.Deserialize(stream); int max = 0; if (model.objects != null) { max += model.objects.Count(); } if (model.relationships != null) { max += model.relationships.Count(); } progress.setup(max); // Don't use Linq constructs those are reeeeaaalllyyyy slow, create a helper dictionary for quick lookup // This construct also makes sure there will be unique elements based on the ID|MAX::ID eaElementDict.Clear(); eaPackageDict.Clear(); updateDicts(selectedPackage); // do objects if (model.objects != null && model.objects.Any()) { MAXImporterWorkingMemory wm = new MAXImporterWorkingMemory(); wm.eaPackage = selectedPackage; issues |= importObjects(model.objects, wm); } // now do relationships if (model.relationships != null && model.relationships.Any()) { issues |= importRelationships(selectedPackage, model.relationships, deleteRelationships); } // Add/update import metadata to the package EA.TaggedValue tvImportDate = (EA.TaggedValue)selectedPackage.Element.TaggedValues.GetByName("MAX::ImportDate"); if (tvImportDate == null) { tvImportDate = (EA.TaggedValue)selectedPackage.Element.TaggedValues.AddNew("MAX::ImportDate", ""); } tvImportDate.Value = DateTime.Now.ToString(); tvImportDate.Update(); EA.TaggedValue tvImportFile = (EA.TaggedValue)selectedPackage.Element.TaggedValues.GetByName("MAX::ImportFile"); if (tvImportFile == null) { tvImportFile = (EA.TaggedValue)selectedPackage.Element.TaggedValues.AddNew("MAX::ImportFile", ""); } tvImportFile.Value = fileName; tvImportFile.Update(); } Repository.EnableUIUpdates = true; Repository.BatchAppend = false; Repository.RefreshModelView(selectedPackage.PackageID); } finally { progress.Close(); } return(issues); }
private void importObject(ObjectType maxObj, MAXImporterWorkingMemory wm) { if (maxObj.id == null) { Repository.WriteOutput("MAX", string.Format("Skipped {0} because id missing.", maxObj.name), 0); wm.issues = true; return; } string id = maxObj.id.Trim().ToUpper(); string name = maxObj.name; EA.Element eaElement; // if this element is unwanted then delete it or ignore it when it is not anymore in the model if ("#UNWANTED#".Equals(name)) { if (eaElementDict.ContainsKey(id)) { eaElement = eaElementDict[id]; short idx = 0; foreach (EA.Element pkgElement in wm.eaPackage.Elements) { if (pkgElement.ElementGUID == eaElement.ElementGUID) { wm.eaPackage.Elements.DeleteAt(idx, true); return; } idx++; } } return; } // check if element already in package // if not create, otherwise use existing and update if (eaElementDict.ContainsKey(id)) { eaElement = eaElementDict[id]; eaElement.Name = name; // Only change if not Package type (cannot be changed) or given if (maxObj.typeSpecified && !"Package".Equals(eaElement.Type)) { eaElement.Type = Enum.GetName(typeof(ObjectTypeEnum), maxObj.type); } } else { // when object doesnot exist, then default to "Class" string type = "Class"; if (maxObj.typeSpecified) { type = Enum.GetName(typeof(ObjectTypeEnum), maxObj.type); } string parentId = maxObj.parentId; if ("Package".Equals(type)) { // Name cannot be empty when creating a new Package bool emptyPackageNameWorkaround = false; if (string.IsNullOrEmpty(name)) { emptyPackageNameWorkaround = true; name = "_"; } EA.Package eaPackage; if (!string.IsNullOrEmpty(parentId)) { parentId = parentId.Trim().ToUpper(); if (eaPackageDict.ContainsKey(parentId)) { eaPackage = (EA.Package)eaPackageDict[parentId].Packages.AddNew(name, type); } else { Repository.WriteOutput("MAX", string.Format("Parent Package(id={0}) for Package(id={1}) not created yet. To correct: adjust the order in the MAX XML file. Fallback to selected Package.", parentId, id), 0); wm.issues = true; eaPackage = (EA.Package)wm.eaPackage.Packages.AddNew(name, type); } } else { eaPackage = (EA.Package)wm.eaPackage.Packages.AddNew(name, type); } eaPackage.TreePos = wm.pkgPos++; eaPackage.Update(); if (emptyPackageNameWorkaround) { eaPackage.Name = String.Empty; eaPackage.Update(); } eaPackageDict[id] = eaPackage; eaElement = eaPackage.Element; } else { if (!string.IsNullOrEmpty(parentId)) { parentId = parentId.Trim().ToUpper(); EA.Element parentElement; if (eaElementDict.ContainsKey(parentId)) { parentElement = eaElementDict[parentId]; } else { Repository.WriteOutput("MAX", string.Format("Parent Element(id={0}) for Element(id={1}) not created yet. To correct: adjust the order in the MAX XML file. Fallback to selected Package.", parentId, id), 0); wm.issues = true; parentElement = wm.eaPackage.Element; } if ("Package".Equals(parentElement.Type)) { EA.Package parentPackage; if (eaPackageDict.ContainsKey(parentId)) { parentPackage = eaPackageDict[parentId]; } else { Repository.WriteOutput("MAX", string.Format("Parent Package(id={0}) for Element(id={1}) not created yet. To correct: adjust the order in the MAX XML file. Fallback to selected Package.", parentId, id), 0); wm.issues = true; parentPackage = wm.eaPackage; } eaElement = (EA.Element)parentPackage.Elements.AddNew(name, type); } else { eaElement = (EA.Element)parentElement.Elements.AddNew(name, type); } } else { eaElement = (EA.Element)wm.eaPackage.Elements.AddNew(name, type); } } eaElement.TreePos = wm.objPos++; eaElementDict[id] = eaElement; } if (maxObj.alias != null) { eaElement.Alias = maxObj.alias; } if (maxObj.stereotype != null) { eaElement.StereotypeEx = maxObj.stereotype; } if (maxObj.notes != null && maxObj.notes.Text != null && maxObj.notes.Text.Length > 0) { eaElement.Notes = maxObj.notes.Text[0].Trim().Replace("\n", "\r\n"); } if (maxObj.isAbstractSpecified) { eaElement.Abstract = maxObj.isAbstract ? "1" : "0"; } eaElement.Update(); EA.TaggedValue tvID = (EA.TaggedValue)eaElement.TaggedValues.GetByName(TV_MAX_ID); if (tvID == null) { tvID = (EA.TaggedValue)eaElement.TaggedValues.AddNew(TV_MAX_ID, ""); } tvID.Value = id; tvID.Update(); if (maxObj.modifiedSpecified) { eaElement.Modified = maxObj.modified; } if (maxObj.tag != null) { // First delete existing TaggedValues for (short i = (short)(eaElement.TaggedValues.Count - 1); i > 0; i--) { eaElement.TaggedValues.DeleteAt(i, true); } eaElement.TaggedValues.Refresh(); // Now add TaggedValues in the import foreach (TagType maxTag in maxObj.tag) { string tagName = maxTag.name.Trim(); EA.TaggedValue tv = (EA.TaggedValue)eaElement.TaggedValues.AddNew(tagName, ""); if (maxTag.value != null) { tv.Value = maxTag.value; } if (maxTag.Text != null && maxTag.Text.Length > 0) { tv.Notes = maxTag.Text[0].Trim().Replace("\n", "\r\n"); } tv.Update(); } } if (maxObj.attribute != null) { int attPos = 0; eaElement.Attributes.Refresh(); foreach (AttributeType maxAtt in maxObj.attribute) { string attName = maxAtt.name.Trim(); if ("#UNWANTED#".Equals(attName)) { short idx = 0; foreach (EA.Attribute eaAtt in eaElement.Attributes) { if (eaAtt.AttributeID.ToString().Equals(maxAtt.id)) { eaElement.Attributes.DeleteAt(idx, true); continue; } idx++; } continue; } // Prefer att id, but that is not always known EA.Attribute att = getAttributeByIdOrName(eaElement, maxAtt.id, attName);// (EA.Attribute)eaElement.Attributes.GetByName(attName); if (att == null) { att = (EA.Attribute)eaElement.Attributes.AddNew(attName, ""); } att.Pos = attPos++; att.Name = attName; if (maxAtt.alias != null) { att.Alias = maxAtt.alias; } if (maxAtt.type != null) { att.Type = maxAtt.type; } if (maxAtt.minCard != null) { att.LowerBound = maxAtt.minCard; } if (maxAtt.maxCard != null) { att.UpperBound = maxAtt.maxCard; } if (maxAtt.value != null) { att.Default = maxAtt.value; } if (maxAtt.Text != null && maxAtt.Text.Length > 0) { att.Notes = maxAtt.Text[0].Trim().Replace("\n", "\r\n"); } if (maxAtt.stereotype != null) { att.StereotypeEx = maxAtt.stereotype; } if (maxAtt.isReadOnlySpecified) { att.IsConst = maxAtt.isReadOnly; } att.Update(); if (maxAtt.tag != null) { foreach (TagType maxTag in maxAtt.tag) { EA.AttributeTag attTag = (EA.AttributeTag)att.TaggedValues.AddNew(maxTag.name, ""); attTag.Value = maxTag.value; if (maxTag.Text != null && maxTag.Text.Length > 0) { attTag.Notes = maxTag.Text[0].Trim().Replace("\n", "\r\n"); } attTag.Update(); } } } } progress.step(); }