Example #1
0
        static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            Hashtable result = new Hashtable();

            IList <EA.Element> clazzes = MetaDataManager.diagramClasses(Repository, diagramCache.elementsList);

            IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramCache.elementsList);

            EA.Element root = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_CONSTRAINT);

            logger.log("MetaData container:" + root.Name);

            EA.Element rootClassifier = MetaDataManager.extractSelection(diagramCache, root);

            MetaDataManager.extractDiagramMetaData(result, root);

            logger.log("Export container:" + rootClassifier.Name);

            String prefix = (string)result[RoundTripAddInClass.PREFIX];


            Dictionary <int, JObject> instances = new Dictionary <int, JObject>();
            JArray container           = new JArray();
            string containerName       = root.Name;
            string containerClassifier = rootClassifier.Name;

            IList <int>        visited          = new List <int>();
            IList <EA.Element> parents          = new List <EA.Element>();
            IList <int>        sampleIds        = new List <int>();
            IList <int>        relationsVisited = new List <int>();

            foreach (EA.Element sample in samples)
            {
                sampleIds.Add(sample.ElementID);

                if (sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_CONSTRAINT)
                {
                    continue;
                }

                if (sample.ClassfierID != root.ClassfierID)
                {
                    //skip root elements that are the population elements.
                    continue;
                }

                visited.Add(sample.ElementID);
                parents.Add(sample);
            }

            parentsToJObject(Repository, diagram, container, sampleIds, null, parents, visited, relationsVisited, diagramCache);

            string msg = prefix + JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented) + "\n";

            result.Add("sample", containerName);
            result.Add("class", containerClassifier);
            result.Add("json", msg);
            //result.Add("export", root.Name);
            return(result);
        }
        public static void syncPopulation(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            logger.log("Sync Population");
            RepositoryHelper.createDiagramCache(Repository, diagram, diagramCache);
            IList <EA.Element> diagramElements = diagramCache.elementsList;

            //IList<EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramElements);
            IList <EA.Element> samples = diagramElements;

            EA.Element container = container = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_POPULATION);

            logger.log("Classifier ID:" + container.ClassifierID);


            string containerName       = container.Name;
            string containerClassifier = "Classes";

            EA.Element containerClassifierEl = null;
            if (container.ClassifierID != 0)
            {
                containerClassifierEl = Repository.GetElementByID(container.ClassfierID);
                containerName         = container.Name;
                containerClassifier   = containerClassifierEl.Name;
            }

            EA.Package samplePkg = Repository.GetPackageByID(diagram.PackageID);

            Hashtable ht = new Hashtable();

            MetaDataManager.extractDiagramMetaData(ht, container);

            string project = (String)ht[RoundTripAddInClass.PROJECT];

            if (project == null)
            {
                MessageBox.Show("No project defined in the Population stereotyped element. Please enter project name where population should be exported.");
                return;
            }

            if (fileManager != null)
            {
                fileManager.initializeAPI(project);
                fileManager.setDataName(RoundTripAddInClass.POPULATION_PATH);
                fileManager.setup(RoundTripAddInClass.RAML_0_8);
                if (!fileManager.populationExists(container.Name, containerClassifier, RoundTripAddInClass.POPULATION_PATH, container.Name))
                {
                    MessageBox.Show("No file exists at:" + fileManager.exportPath(container.Name, containerClassifier, RoundTripAddInClass.POPULATION_PATH, container.Name));
                    return;
                }
                else
                {
                    string fullpath = fileManager.exportPath(containerName, containerClassifier, RoundTripAddInClass.POPULATION_PATH, container.Name);
                    JArray jo       = JArray.Parse(File.ReadAllText(fullpath));
                    sync_population(Repository, container, containerClassifierEl, jo, samplePkg, diagramCache);
                    samplePkg.Update();
                }
            }
        }
Example #3
0
        ///
        /// Validate all object run state keys correspond to classifier attributes
        ///
        //static public void validateDiagram(EA.Repository Repository,EA.Diagram diagram)
        //{
        //    IList<string> messages = diagramValidation(Repository,diagram);

        //    logger.log("**ValidationResults**");
        //    if(messages!=null)
        //    {
        //        foreach (string m in messages)
        //        {
        //            logger.log(m);
        //        }
        //    }
        //}

        //static public IList<string> diagramValidation(EA.Repository Repository, EA.Diagram diagram,IList<EA.Element> diagramElements)
        //{
        //    JSchema jschema = null;
        //    JObject json = null;
        //    try
        //    {
        //        //logger.log("Validate Sample");
        //        json = (JObject)sampleToJObject(Repository, diagram, diagramElements)["json"];

        //        //logger.log("JObject formed");

        //        EA.Package samplePkg = Repository.GetPackageByID(diagram.PackageID);
        //        EA.Package samplesPackage = Repository.GetPackageByID(samplePkg.ParentID);
        //        EA.Package apiPackage = Repository.GetPackageByID(samplesPackage.ParentID);

        //        EA.Package schemaPackage = null;

        //        foreach (EA.Package p in apiPackage.Packages)
        //        {
        //            if (p!=null && p.Name.Equals(RoundTripAddInClass.API_PACKAGE_SCHEMAS))
        //            {
        //                schemaPackage = p;
        //            }
        //        }
        //        if (schemaPackage == null)
        //        {
        //            throw new Exception("No Schema package found");
        //        }

        //        EA.Diagram schemaDiagram = null;
        //        foreach (EA.Diagram d in schemaPackage.Diagrams)
        //        {
        //            if (d.Stereotype != null && d.Stereotype.Equals(RoundTripAddInClass.EA_STEREOTYPE_SCHEMADIAGRAM))
        //            {
        //                schemaDiagram = d;
        //            }
        //        }



        //        jschema = SchemaManager.schemaToJsonSchema(Repository, schemaDiagram,diagramElements).Value;
        //    }
        //    catch (ModelValidationException ex)
        //    {
        //        return ex.errors.messages;
        //    }

        //    IList<string> messages;

        //    if (!json.IsValid(jschema, out messages))
        //    {
        //        logger.log("Sample is not valid:");
        //        return messages;
        //    }
        //    else{
        //        logger.log("Sample is Valid!");
        //        return null;
        //    }

        //}


        public static void syncHierarchy(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            logger.log("Sync Hierarchy");

            RepositoryHelper.createDiagramCache(Repository, diagram, diagramCache);
            //IList<EA.Element> diagramElements = diagramCache.elementsList;

            IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramCache.elementsList);

            EA.Element container             = container = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_HIERARCHY);
            EA.Element containerClassifierEl = diagramCache.elementIDHash[container.ClassfierID];
            string     containerName         = container.Name;
            string     containerClassifier   = containerClassifierEl.Name;

            Hashtable ht = new Hashtable();

            MetaDataManager.extractDiagramMetaData(ht, container);

            EA.Package samplePkg = Repository.GetPackageByID(diagram.PackageID);

            string sourcecontrolPackage = (string)ht[RoundTripAddInClass.PROJECT];


            if (fileManager != null)
            {
                fileManager.initializeAPI(sourcecontrolPackage);
                fileManager.setDataName(RoundTripAddInClass.HIERARCHY_PATH);
                fileManager.setup(RoundTripAddInClass.RAML_0_8);
                if (!fileManager.populationExists(container.Name, containerClassifier, RoundTripAddInClass.HIERARCHY_PATH, container.Name))
                {
                    MessageBox.Show("No file exists at:" + fileManager.exportPath(container.Name, containerClassifier, RoundTripAddInClass.HIERARCHY_PATH, container.Name));
                    return;
                }
                else
                {
                    string fullpath = fileManager.exportPath(containerName, containerClassifier, RoundTripAddInClass.HIERARCHY_PATH, container.Name);
                    JArray jo       = JArray.Parse(File.ReadAllText(fullpath));
                    sync_hierarchy(Repository, diagram, container, jo, samplePkg, diagramCache);
                    samplePkg.Update();
                    diagram.DiagramLinks.Refresh();
                    if (!diagram.Update())
                    {
                        logger.log(diagram.GetLastError());
                    }
                }
            }
        }
        ///
        /// Validate all object run state keys correspond to classifier attributes
        ///
        //static public void validateDiagram(EA.Repository Repository, EA.Diagram diagram)
        //{
        //    IList<string> messages = diagramValidation(Repository, diagram);

        //    logger.log("**ValidationResults**");
        //    if (messages != null)
        //    {
        //        foreach (string m in messages)
        //        {
        //            logger.log(m);
        //        }
        //    }
        //}

        //static public IList<string> diagramValidation(EA.Repository Repository, EA.Diagram diagram)
        //{
        //    JSchema jschema = null;
        //    JObject json = null;
        //    try
        //    {
        //        //logger.log("Validate Sample");
        //        json = (JObject)sampleToJObject(Repository, diagram)["json"];

        //        //logger.log("JObject formed");

        //        EA.Package samplePkg = Repository.GetPackageByID(diagram.PackageID);
        //        EA.Package samplesPackage = Repository.GetPackageByID(samplePkg.ParentID);
        //        EA.Package apiPackage = Repository.GetPackageByID(samplesPackage.ParentID);

        //        EA.Package schemaPackage = null;

        //        foreach (EA.Package p in apiPackage.Packages)
        //        {
        //            if (p != null && p.Name.Equals(RoundTripAddInClass.API_PACKAGE_SCHEMAS))
        //            {
        //                schemaPackage = p;
        //            }
        //        }
        //        if (schemaPackage == null)
        //        {
        //            throw new Exception("No Schema package found");
        //        }

        //        EA.Diagram schemaDiagram = null;
        //        foreach (EA.Diagram d in schemaPackage.Diagrams)
        //        {
        //            if (d.Stereotype != null && d.Stereotype.Equals(RoundTripAddInClass.EA_STEREOTYPE_SCHEMADIAGRAM))
        //            {
        //                schemaDiagram = d;
        //            }
        //        }



        //        jschema = SchemaManager.schemaToJsonSchema(Repository, schemaDiagram).Value;
        //    }
        //    catch (ModelValidationException ex)
        //    {
        //        return ex.errors.messages;
        //    }

        //    IList<string> messages;

        //    if (!json.IsValid(jschema, out messages))
        //    {
        //        logger.log("Sample is not valid:");
        //        return messages;
        //    }
        //    else
        //    {
        //        logger.log("Sample is Valid!");
        //        return null;
        //    }

        //}


        public static void syncMapping(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            logger.log("Sync Mapping");
            RepositoryHelper.createDiagramCache(Repository, diagram, diagramCache);
            IList <EA.Element> diagramElements = diagramCache.elementsList;
            IList <EA.Element> samples         = MetaDataManager.diagramSamples(Repository, diagramElements);

            EA.Element container             = container = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_MAPPING);
            EA.Element containerClassifierEl = diagramCache.elementIDHash[container.ClassfierID];
            string     containerName         = container.Name;
            string     containerClassifier   = containerClassifierEl.Name;

            EA.Package samplePkg = Repository.GetPackageByID(diagram.PackageID);


            Hashtable ht = new Hashtable();

            MetaDataManager.extractDiagramMetaData(ht, container);

            string project = (String)ht[RoundTripAddInClass.PROJECT];


            if (fileManager != null)
            {
                fileManager.initializeAPI(project);
                fileManager.setDataName(RoundTripAddInClass.MAPPING_PATH);
                fileManager.setup(RoundTripAddInClass.RAML_0_8);
                if (!fileManager.populationExists(container.Name, containerClassifier, RoundTripAddInClass.MAPPING_PATH, container.Name))
                {
                    MessageBox.Show("No file exists at:" + fileManager.exportPath(container.Name, containerClassifier, RoundTripAddInClass.MAPPING_PATH, container.Name));
                    return;
                }
                else
                {
                    string fullpath = fileManager.exportPath(containerName, containerClassifier, RoundTripAddInClass.MAPPING_PATH, container.Name);
                    JArray jo       = JArray.Parse(File.ReadAllText(fullpath));
                    sync_mapping(Repository, diagram, container, containerClassifierEl, jo, samplePkg, diagramCache);
                    samplePkg.Update();
                }
            }
        }
        static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramElements)
        {
            Hashtable result = new Hashtable();

            //logger.log("sampleToObject");

            //IList<EA.Element> clazzes = MetaDataManager.diagramClasses(Repository, diagramElements.elementsList);
            //logger.log("GetClazzes" + clazzes.Count);

            //IList<EA.Element> components = MetaDataManager.diagramComponents(Repository, diagramElements.elementsList);
            //logger.log("GetComponents" + components.Count);


            //IList<EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramElements.elementsList);
            //logger.log("GetSamples" + samples.Count);

            //samples = samples.Concat(clazzes).ToList();
            //samples = samples.Concat(components).ToList();
            IList <EA.Element> samples = diagramElements.elementsList;

            logger.log("All" + samples.Count);

            EA.Element root = MetaDataManager.findContainer(Repository, diagram, diagramElements, RoundTripAddInClass.EA_STEREOTYPE_POPULATION);

            MetaDataManager.extractDiagramMetaData(result, root);

            logger.log("Classifier ID:" + root.ClassifierID);

            Dictionary <int, JObject> instances = new Dictionary <int, JObject>();
            JArray container           = new JArray();
            string containerName       = root.Name;
            string containerClassifier = "Classes";

            if (root.ClassifierID != 0)
            {
                EA.Element rootClassifier = Repository.GetElementByID(root.ClassifierID);
                containerName       = root.Name;
                containerClassifier = rootClassifier.Name;
            }

            foreach (EA.Element sample in samples)
            {
                //logger.log("Sample Name:" + sample.Name);

                if (sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_POPULATION)
                {
                    continue;
                }

                if (root.ClassifierID != 0 && sample.ClassfierID != root.ClassfierID)
                {
                    //skip root elements that are the population elements.
                    continue;
                }

                //logger.log("Sample Name2:" + sample.Name);

                String     type  = "";
                EA.Element clazz = null;
                if (sample.ClassifierID != 0)
                {
                    clazz = diagramElements.elementIDHash[sample.ClassifierID];
                    type  = clazz.Name;
                }
                else
                {
                    logger.log("Classifier is null");
                }

                EA.Package package = diagramElements.packageIDHash[sample.PackageID];

                JObject jsonClass = null;

                {
                    jsonClass = new JObject();
                    jsonClass.Add(new JProperty(RoundTripAddInClass.POPULATION_PROPERTY_GUID, sample.ElementGUID));
                    jsonClass.Add(new JProperty(RoundTripAddInClass.POPULATION_PROPERTY_NAME, sample.Name));
                    jsonClass.Add(new JProperty(RoundTripAddInClass.POPULATION_PROPERTY_NOTES, sample.Notes));
                    jsonClass.Add(new JProperty(RoundTripAddInClass.POPULATION_PROPERTY_PACKAGE, package.Name));
                    if (clazz != null)
                    {
                        jsonClass.Add(new JProperty(RoundTripAddInClass.POPULATION_PROPERTY_TYPE, clazz.Name));
                    }

                    container.Add(jsonClass);
                }

                string rs = sample.RunState;

                ObjectManager.addRunStateToJson(rs, jsonClass);
                ObjectManager.addTagsToJson(sample, jsonClass);
            }

            logger.log("Export container:" + containerName);

            foreach (EA.Element clazz in samples)
            {
                JObject jsonClass = null;
                if (!instances.TryGetValue(clazz.ElementID, out jsonClass))
                {
                    continue;
                }
                if (jsonClass != null)
                {
                    logger.log("Found jsonClass:" + clazz.Name);
                    foreach (EA.Connector con in clazz.Connectors)
                    {
                        //logger.log("Found connector:");
                        EA.Element related = null;
                        if (clazz.ElementID == con.ClientID)
                        {
                            related = Repository.GetElementByID(con.SupplierID);

                            try
                            {
                                object o = instances[related.ElementID];
                            }
                            catch (KeyNotFoundException)
                            {
                                //Object is in package but not on the diagram
                                continue;
                            }

                            if (related != null && instances[related.ElementID] != null)
                            {
                                if (con.SupplierEnd.Cardinality.Equals(RoundTripAddInClass.CARDINALITY_0_TO_MANY) ||
                                    con.SupplierEnd.Cardinality.Equals(RoundTripAddInClass.CARDINALITY_1_TO_MANY)
                                    )
                                {
                                    //logger.log("Found array");

                                    string propertyName = related.Name;
                                    //Override with the connection supplier end
                                    try {
                                        if (con.SupplierEnd.Role.Length > 0)
                                        {
                                            propertyName = con.SupplierEnd.Role;
                                        }
                                    } catch (Exception) { }

                                    JProperty p = jsonClass.Property(propertyName);
                                    if (p == null)
                                    {
                                        JArray ja = new JArray();
                                        ja.Add(instances[related.ElementID]);
                                        //logger.log("Adding array property:"+ related.Name);
                                        jsonClass.Add(new JProperty(propertyName, ja));
                                    }
                                    else
                                    {
                                        JArray ja = (JArray)p.Value;
                                        //logger.log("Adding to array property");
                                        ja.Add(instances[related.ElementID]);
                                    }
                                }
                                else
                                {
                                    string propertyName = related.Name;
                                    //Override with the connection supplier end
                                    try {
                                        if (con.SupplierEnd.Role.Length > 0)
                                        {
                                            propertyName = con.SupplierEnd.Role;
                                        }
                                    } catch (Exception) { }
                                    //logger.log("Adding property:" + related.Name);
                                    jsonClass.Add(new JProperty(propertyName, instances[related.ElementID]));
                                }
                            }
                        }
                    }
                }
            }

            //KeyValuePair<string,JObject> kv = new KeyValuePair<string,JObject>(containerName,container);
            //return kv;

            //logger.log("REturning result");
            result.Add("sample", containerName);
            result.Add("class", containerClassifier);
            result.Add("json", container);
            return(result);
        }
        static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            logger.log("SampeToJObject");
            Hashtable result = new Hashtable();

            //IList<EA.Element> clazzes = MetaDataManager.diagramClasses(Repository, diagramCache.elementsList);

            //IList<EA.Element> components = MetaDataManager.diagramComponents(Repository, diagramCache.elementsList);

            //IList<EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramCache.elementsList);

            //samples = samples.Concat(clazzes).ToList();
            //samples = samples.Concat(components).ToList();

            IList <EA.Element> samples = diagramCache.elementsList;

            logger.log("Samples:" + samples.Count);

            EA.Element root = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_MAPPING);

            logger.log("MetaData container:" + root.Name);

            EA.Element rootClassifier           = null;
            Dictionary <int, JObject> instances = new Dictionary <int, JObject>();
            JArray container           = new JArray();
            string containerName       = "ALL";
            string containerClassifier = "Class";

            if (root.ClassifierID != 0)
            {
                rootClassifier      = diagramCache.elementIDHash[root.ClassifierID];
                containerName       = root.Name;
                containerClassifier = rootClassifier.Name;
                logger.log("Export container:" + rootClassifier.Name);
            }

            MetaDataManager.extractDiagramMetaData(result, root);


            String prefix    = (string)result[RoundTripAddInClass.PREFIX];
            String intertype = (string)result[RoundTripAddInClass.INCLUDE_INTERTYPE];


            IList <int>        visited          = new List <int>();
            IList <EA.Element> parents          = new List <EA.Element>();
            IList <int>        sampleIds        = new List <int>();
            IList <int>        relationsVisited = new List <int>();

            foreach (EA.Element sample in samples)
            {
                sampleIds.Add(sample.ElementID);

                if (sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_HIERARCHY)
                {
                    continue;
                }

                if (root.ClassifierID != 0 && sample.ClassfierID != root.ClassfierID)
                {
                    continue;
                }


                visited.Add(sample.ElementID);
                parents.Add(sample);
            }

            parentsToJObject(Repository, diagram, container, sampleIds, null, parents, visited, relationsVisited, diagramCache, intertype);

            string msg = prefix + JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented) + "\n";

            result.Add("sample", containerName);
            result.Add("class", containerClassifier);
            result.Add("json", msg);
            //result.Add("export", root.Name);
            return(result);
        }
Example #7
0
        static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            Hashtable result = new Hashtable();

            //IList<EA.Element> samples = MetaDataManager.diagramElements(Repository, diagram);

            logger.log("Elements size:" + diagramCache.elementsList.Count);

            //DiagramElements diagramCache = RepositoryHelper.getDiagramElements(Repository, diagram.DiagramObjects);
            //IList<EA.Element> diagramElements = diagramCache.elementsList;

            EA.Element root = MetaDataManager.findContainer(Repository, diagram, diagramCache, RoundTripAddInClass.EA_STEREOTYPE_HIERARCHY);

            MetaDataManager.extractDiagramMetaData(result, root);

            int level = (int)result[RoundTripAddInClass.HIERARCHY_LEVEL];

            //String prefix = "";
            //String filename = "";

            EA.Element rootClassifier = diagramCache.elementIDHash[root.ClassifierID];

            logger.log("Export container:" + rootClassifier.Name);

            Dictionary <int, JObject> instances = new Dictionary <int, JObject>();
            JArray container           = new JArray();
            string containerName       = root.Name;
            string containerClassifier = rootClassifier.Name;

            IList <int>        visited   = new List <int>();
            IList <EA.Element> parents   = new List <EA.Element>();
            IList <int>        sampleIds = new List <int>();

            foreach (EA.Element sample in diagramCache.elementsList)
            {
                sampleIds.Add(sample.ElementID);

                if (sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_HIERARCHY)
                {
                    continue;
                }

                if (sample.ClassfierID != root.ClassfierID)
                {
                    //skip root elements that are the population elements.
                    continue;
                }

                visited.Add(sample.ElementID);
                parents.Add(sample);
            }

            parentsToJObject(Repository, diagram, container, sampleIds, null, parents, visited, level, diagramCache);


            string msg = result[RoundTripAddInClass.PREFIX] + JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented) + "\n";

            String filename = (String)result[RoundTripAddInClass.FILENAME];

            if (filename.Length == 0)
            {
                filename = root.Name;
                result.Remove(RoundTripAddInClass.FILENAME);
                result.Add(RoundTripAddInClass.FILENAME, filename);
            }

            result.Add("sample", containerName);
            result.Add("class", containerClassifier);
            result.Add("json", msg);

            return(result);
        }