Exemple #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);
        }
Exemple #2
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());
                    }
                }
            }
        }
        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");
        }
        ///
        /// 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();
                }
            }
        }
        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);
                }
            }
        }
        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, 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);
        }