Пример #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();
                }
            }
        }
Пример #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());
                    }
                }
            }
        }
Пример #4
0
        static public EA.Element findContainer(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache, String stereotype)
        {
            logger.log("Finding container for diagram:" + diagram.Name);

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

            foreach (EA.Element sample in samples)
            {
                if (sample.Stereotype != null && sample.Stereotype == stereotype)
                {
                    logger.log("Constraint is identified by Constraint stereotype");

                    return(sample);
                }
            }
            throw new ModelValidationException("Unable to find Object stereotyped as " + stereotype + " on the diagram");
        }
Пример #5
0
        static public void exportSample(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            RepositoryHelper.createDiagramCache(Repository, diagram, diagramCache);

            Hashtable ht        = sampleToJObject(Repository, diagram, diagramCache.elementsList);
            string    sample    = (string)ht["sample"];
            string    clazz     = (string)ht["class"];
            JObject   container = (JObject)ht["json"];


            if (!diagram.Stereotype.Equals(RoundTripAddInClass.EA_STEREOTYPE_SAMPLEDIAGRAM))
            {
                logger.log("exportSample: Ignore diagam that isnt a sample diagram");
                return;
            }

            //KeyValuePair<string,JObject> kv = sampleToJObject(Repository, diagram);
            //JObject container = kv.Value;

            if (container == null)
            {
                MessageBox.Show("No object linked to root with classification sample declared nor  (older style) object of classification Request declared");
                return;
            }

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

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

            string sourcecontrolPackage = apiPackage.Name;

            if (MetaDataManager.isCDMPackage(Repository, apiPackage))
            {
                sourcecontrolPackage = "cdm";
            }

            if (fileManager != null)
            {
                fileManager.initializeAPI(sourcecontrolPackage);
                fileManager.setup(RoundTripAddInClass.RAML_0_8);
                fileManager.exportSample(sample, clazz, msg);
            }
        }
Пример #6
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)
        //{
        //    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();
                }
            }
        }
        ///
        /// Called Before EA starts to check Add-In Exists
        /// Nothing is done here.
        /// This operation needs to exists for the addin to work
        ///
        /// <param name="Repository" />the EA repository
        /// a string
        public String EA_Connect(EA.Repository Repository)
        {
            logger.setRepository(Repository);

            try
            {
                fileManager.setBasePath(Properties.Settings.Default.BasePath);
                fileManager.setDiagramPath(Properties.Settings.Default.DiagramPath);
            }
            catch (Exception)
            {
                logger.log("Did not find BasePath or DiagramPath in user settings");
            }

            MappingManager.setLogger(logger);
            MappingManager.setFileManager(fileManager);

            HierarchyManager.setLogger(logger);
            HierarchyManager.setFileManager(fileManager);

            PopulationManager.setLogger(logger);
            PopulationManager.setFileManager(fileManager);

            ConstraintManager.setLogger(logger);
            ConstraintManager.setFileManager(fileManager);

            DiagramManager.setLogger(logger);
            DiagramManager.setFileManager(fileManager);

            SchemaManager.setLogger(logger);
            SchemaManager.setFileManager(fileManager);
            SampleManager.setLogger(logger);
            SampleManager.setFileManager(fileManager);

            MetaDataManager.setLogger(logger);
            RepositoryHelper.setLogger(logger);
            ObjectManager.setLogger(logger);

            return("a string");
        }
Пример #8
0
        public static void syncSample(EA.Repository Repository, EA.Diagram diagram, DiagramCache diagramCache)
        {
            logger.log("Sync Sample");

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

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

            EA.Element container             = container = findContainer(Repository, diagram, diagramElements);
            EA.Element containerClassifierEl = Repository.GetElementByID(container.ClassfierID);
            string     containerName         = container.Name;
            string     containerClassifier   = containerClassifierEl.Name;


            //logger.log("Sample Root:" + container.Name);

            EA.Package samplePkg      = Repository.GetPackageByID(diagram.PackageID);
            EA.Package samplesPackage = Repository.GetPackageByID(samplePkg.ParentID);
            EA.Package apiPackage     = Repository.GetPackageByID(samplesPackage.ParentID);
            if (fileManager != null)
            {
                fileManager.initializeAPI(apiPackage.Name);
                fileManager.setup(RoundTripAddInClass.RAML_0_8);
                if (!fileManager.sampleExists(container.Name, containerClassifier))
                {
                    MessageBox.Show("No file exists at:" + fileManager.samplePath(container.Name, containerClassifier));
                    return;
                }
                else
                {
                    string  fullpath = fileManager.samplePath(containerName, containerClassifier);
                    JObject jo       = JObject.Parse(File.ReadAllText(fullpath));
                    sync_sample(Repository, container, jo);
                }
            }
        }
Пример #9
0
        static EA.Element findContainer(EA.Repository Repository, EA.Diagram diagram, IList <EA.Element> diagramElements)
        {
            IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagramElements);

            foreach (EA.Element sample in samples)
            {
                if (sample.Stereotype != null && sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_SAMPLE)
                {
                    logger.log("Sample is identified by SAMPLE classified root");

                    if (sample.Connectors.Count != 1)
                    {
                        MessageBox.Show("root Sample should refer to a single class");
                        break;
                    }
                    else
                    {
                        int        id   = sample.Connectors.GetAt(0).SupplierID;
                        EA.Element root = Repository.GetElementByID(id);
                        return(root);
                    }
                }
            }

            //Deprecated mechanism
            foreach (EA.Element sample in samples)
            {
                if (sample.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_REQUEST)
                {
                    return(sample);
                }
            }


            //MessageBox.Show("object stereotyped as Sample needs to be linked to schema root");
            throw new ModelValidationException("object stereotyped as Sample needs to be linked to schema root");
        }
        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);
        }
Пример #11
0
        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);
        }
        ///
        /// Called when user makes a selection in the menu.
        /// This is your main exit point to the rest of your Add-in
        ///
        /// <param name="Repository" />the repository
        /// <param name="Location" />the location of the menu
        /// <param name="MenuName" />the name of the menu
        /// <param name="ItemName" />the name of the selected menu item
        public void EA_MenuClick(EA.Repository Repository, string Location, string MenuName, string ItemName)
        {
            logger.enable(Repository);

            DiagramCache diagramCache = new DiagramCache();

            EA.Diagram diagram = null;
            if (Repository.GetContextItemType() == ObjectType.otDiagram)
            {
                diagram = Repository.GetContextObject();
            }

            switch (ItemName)
            {
            case menuExportMapping:
                MappingManager.exportMapping(Repository, diagram, diagramCache);
                break;

            case menuSyncMapping:
                MappingManager.syncMapping(Repository, diagram, diagramCache);
                break;

            case menuExportHierarchy:
                logger.log("Menu Export Hierarchy");
                HierarchyManager.exportHierarchy(Repository, diagram, diagramCache);

                break;

            case menuExportPopulation:
                PopulationManager.exportPopulation(Repository, diagram, diagramCache);

                break;

            case menuSyncPopulation:
                PopulationManager.syncPopulation(Repository, diagram, diagramCache);
                break;

            case menuSyncHierarchy:
                HierarchyManager.syncHierarchy(Repository, diagram, diagramCache);
                break;


            case menuExportConstraint:
                ConstraintManager.exportConstraint(Repository, diagram, diagramCache);
                break;

            case menuSyncConstraint:
                ConstraintManager.syncConstraint(Repository, diagram, diagramCache);
                break;

            case menuExportAll:
                exportAll(Repository, diagramCache);
                break;

            case menuExportPackage:
                exportPackage(Repository, diagramCache);
                break;

            case menuExportDiagram:
                exportDiagram(Repository);
                break;

            case menuExportSchema:
                try
                {
                    SchemaManager.exportSchema(Repository, diagram, diagramCache);
                    MetaDataManager.setAsSchemaDiagram(Repository, diagram);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                break;

            //case menuExportCanonical:
            //    try
            //    {
            //        SchemaManager.exportCanonical(Repository, diagram);
            //        //MetaDataManager.setAsCanonicalDiagram(Repository, diagram);
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //    }
            //    break;

            case menuExportSample:
                SampleManager.exportSample(Repository, diagram, diagramCache);
                MetaDataManager.setAsSampleDiagram(Repository, diagram);
                break;

            case menuSyncSample:
                SampleManager.syncSample(Repository, diagram, diagramCache);
                break;

            case menuCreateSample:
                SchemaManager.generateSample(Repository, diagramCache);
                break;

            case menuUpdateClassFromInstance:
                SchemaManager.updateClassFromInstance(Repository);
                break;

            case menuUpdateInstanceFromClass:
                SchemaManager.operateOnSample(Repository, SchemaManager.updateSampleFromClass);
                break;

            case menuValidateDiagram:
                if (diagram != null)
                {
                    if (diagram.Stereotype.Equals(RoundTripAddInClass.EA_STEREOTYPE_SAMPLEDIAGRAM))
                    {
                        SampleManager.validateDiagram(Repository, diagram, diagramCache);
                    }
                    else if (diagram.Stereotype.Equals(RoundTripAddInClass.EA_STEREOTYPE_SCHEMADIAGRAM))
                    {
                        SchemaManager.validateDiagram(Repository, diagram, diagramCache);
                    }
                }

                break;



            case menuToggleLogging:
                logger.toggleLogging(Repository);
                break;

            case menuWeb:
                this.callWeb(Repository);
                break;
            }
        }
Пример #13
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);
        }
Пример #14
0
        static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram, IList <EA.Element> diagramElements)
        {
            Hashtable result = new Hashtable();

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

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

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

            EA.Element rootClassifier = Repository.GetElementByID(root.ClassifierID);

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

            instances.Add(root.ElementID, container);

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

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

                EA.Element clazz = null;
                if (sample.ClassifierID != 0)
                {
                    clazz = Repository.GetElementByID(sample.ClassifierID);
                }
                else
                {
                    logger.log("Classifier is null");
                }

                JObject jsonClass = null;


                if (!instances.TryGetValue(sample.ElementID, out jsonClass))
                {
                    jsonClass = new JObject();
                    instances.Add(sample.ElementID, jsonClass);
                }

                string rs = sample.RunState;

                // Loop through all attributes in run state and add to json
                Dictionary <string, RunState> runstate = ObjectManager.parseRunState(rs);
                foreach (string key in runstate.Keys)
                {
                    logger.log("Adding property:" + key + " =>" + runstate[key].value);
                    object o = runstate[key].value;

                    // Find classifier attribute specified in run state
                    string attrType       = null;
                    string attrUpperBound = null;
                    if (clazz != null)
                    {
                        foreach (EA.Attribute a in clazz.Attributes)
                        {
                            if (a.Name.Equals(key))
                            {
                                attrType       = a.Type;
                                attrUpperBound = a.UpperBound;
                                break;
                            }
                        }

                        // Check if attribuite is defined as related enumeration. When cardinaltity is 0..* then set the attribute cardinality so we serialize as an array
                        foreach (EA.Connector con in clazz.Connectors)
                        {
                            // Check relation is named the same as the run state attribute name and is an enumeration
                            EA.Element related = Repository.GetElementByID(con.SupplierID);
                            if (con.SupplierEnd.Role == key && related.Type == RoundTripAddInClass.EA_TYPE_ENUMERATION)
                            {
                                //if (con.SupplierEnd.Cardinality.Equals(RoundTripAddInClass.CARDINALITY_0_TO_MANY))
                                //{
                                //logger.log("  matching enum with 0..*:" + con.SupplierEnd.Cardinality);
                                //}
                                attrType       = related.Type;
                                attrUpperBound = con.SupplierEnd.Cardinality;
                                break;
                            }
                        }

                        // Check if attribute is defined as related DataItem
                        foreach (EA.Connector con in clazz.Connectors)
                        {
                            // Check relation is named the same as the run state attribute name and is an enumeration
                            EA.Element related = Repository.GetElementByID(con.SupplierID);
                            if (con.SupplierEnd.Role == key && related.Stereotype == RoundTripAddInClass.EA_STEREOTYPE_DATAITEM)
                            {
                                attrType       = SchemaManager.getDataItemType(related);
                                attrUpperBound = con.SupplierEnd.Cardinality;
                                break;
                            }
                        }
                    }

                    // Add attribute to json as either value or array
                    if (attrType != null)
                    {
                        //logger.log("  upper bound:" + key + " =>" + attrUpperBound);
                        if (attrUpperBound.Equals("*") || attrUpperBound.Equals(RoundTripAddInClass.CARDINALITY_0_TO_MANY))
                        {
                            // Create array and split values separated by commas
                            JArray ja = new JArray();
                            foreach (string value in runstate[key].value.Split(','))
                            {
                                o = convertEATypeToValue(attrType, value);
                                ja.Add(o);
                            }
                            jsonClass.Add(new JProperty(key, ja));
                        }
                        else
                        {
                            // Not array so convert and add attribute and formatted value
                            o = convertEATypeToValue(attrType, runstate[key].value);
                            logger.log("Attr:" + attrType + " " + o.ToString());
                            jsonClass.Add(new JProperty(key, o));
                        }
                    }
                    else
                    {
                        // No classifier found so add as object serialized as string
                        logger.log("Attr:" + key + "-" + o.ToString());
                        jsonClass.Add(new JProperty(key, o));
                    }
                }
            }

            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);
        }