static public void extractDiagramMetaData(Hashtable result, EA.Element root)
        {
            int    level     = 1;
            String prefix    = "";
            String filename  = "";
            String project   = "";
            String intertype = "n";


            String runState = root.RunState;

            logger.log("RunState is:" + runState);

            Dictionary <string, RunState> rs = ObjectManager.parseRunState(runState);

            if (rs.ContainsKey(RoundTripAddInClass.HIERARCHY_LEVEL))
            {
                level = Int32.Parse(rs[RoundTripAddInClass.HIERARCHY_LEVEL].value);
                result.Add(RoundTripAddInClass.HIERARCHY_LEVEL, level);
                logger.log("Level is:" + level);
            }


            if (rs.ContainsKey(RoundTripAddInClass.PREFIX))
            {
                prefix = rs[RoundTripAddInClass.PREFIX].value;
                result.Add(RoundTripAddInClass.PREFIX, prefix);
                logger.log("Prefix is:" + prefix);
            }

            if (rs.ContainsKey(RoundTripAddInClass.INCLUDE_INTERTYPE))
            {
                intertype = rs[RoundTripAddInClass.INCLUDE_INTERTYPE].value;
                logger.log("Include Intertype Links is:" + intertype);
            }
            result.Add(RoundTripAddInClass.INCLUDE_INTERTYPE, intertype);


            if (rs.ContainsKey(RoundTripAddInClass.FILENAME))
            {
                filename = rs[RoundTripAddInClass.FILENAME].value;
            }
            else
            {
                filename = root.Name;
            }
            result.Add(RoundTripAddInClass.FILENAME, filename);
            logger.log("FileName is:" + filename);

            if (rs.ContainsKey(RoundTripAddInClass.PROJECT))
            {
                project = rs[RoundTripAddInClass.PROJECT].value;
                result.Add(RoundTripAddInClass.PROJECT, project);
            }
            else
            {
                result.Add(RoundTripAddInClass.PROJECT, RoundTripAddInClass.EXPORT_PACKAGE);
            }
            logger.log("Project is:" + project);
        }
        private static void sync_population_runstate(EA.Repository Repository, EA.Element sample, EA.Element classifier, JObject jo, EA.Package pkg, DiagramCache diagramCache)
        {
            logger.log("Syncing JObject:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            sample.ClassifierID = classifier.ElementID;

            foreach (JProperty p in jo.Properties())
            {
                if (p.Name == RoundTripAddInClass.POPULATION_PROPERTY_GUID)
                {
                    continue;
                }
                if (p.Name == RoundTripAddInClass.POPULATION_PROPERTY_NAME)
                {
                    sample.Name = p.Value.ToString();
                    continue;
                }
                if (p.Name == RoundTripAddInClass.POPULATION_PROPERTY_NOTES)
                {
                    sample.Notes = p.Value.ToString();
                    continue;
                }

                if (p.Name == RoundTripAddInClass.POPULATION_PROPERTY_TYPE)
                {
                    string     classifierName = p.Value.ToString();
                    EA.Element clazz          = RepositoryHelper.queryClassifier(Repository, classifierName);
                    if (clazz != null)
                    {
                        sample.ClassifierID = clazz.ElementID;
                        continue;
                    }
                }
                //string rsv=null;
                if (p.Value.Type != JTokenType.Object && p.Value.Type != JTokenType.Array)
                {
                    //logger.log("Adding Property:" + sample.Name);
                    RunState r;
                    if (rs.ContainsKey(p.Name))
                    {
                        r = rs[p.Name];
                    }
                    else
                    {
                        r     = new RunState();
                        r.key = p.Name;
                    }
                    r.value = p.Value.ToString();

                    nrs.Add(r.key, r);
                }
            }

            sample.RunState = ObjectManager.renderRunState(nrs);
            //logger.log(sample.RunState);
            sample.Update();
        }
示例#3
0
        public static void addRunStateToJson(String rs, JObject jsonClass)
        {
            // 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;


                // 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);
                        }
                        if (jsonClass.GetValue(key) == null)
                        {
                            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());
                        if (jsonClass.GetValue(key) == null)
                        {
                            jsonClass.Add(new JProperty(key, o));
                        }
                    }
                }
                else
                {
                    // No classifier found so add as object serialized as string
                    //logger.log("Attr:" + key + "-" + o.ToString());
                    if (jsonClass.GetValue(key) == null)
                    {
                        jsonClass.Add(new JProperty(key, o));
                    }
                }
            }
        }
        private static void sync_mapping(EA.Repository Repository, EA.Diagram diagram, EA.Element sample, EA.Element classifier, JArray ja, EA.Package pkg, DiagramCache diagramCache)
        {
            logger.log("Syncing JArray:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            foreach (JObject jo in ja.Children <JObject>())
            {
                logger.log("Syncing Relationship:");

                EA.Element source = null;
                EA.Element target = null;

                JToken guidToken = null;
                if (jo.TryGetValue(RoundTripAddInClass.MAPPING_PROPERTY_SOURCE, out guidToken))
                {
                    String     guid = guidToken.ToString();
                    EA.Element el   = Repository.GetElementByGuid(guid);
                    if (el != null)
                    {
                        //logger.log("Found element for guid" + guid);
                        source = el;
                    }
                    else
                    {
                        logger.log("No element for guid" + guid);
                    }
                }
                if (jo.TryGetValue(RoundTripAddInClass.MAPPING_PROPERTY_TARGET, out guidToken))
                {
                    String     guid = guidToken.ToString();
                    EA.Element el   = Repository.GetElementByGuid(guid);
                    if (el != null)
                    {
                        //logger.log("Found element for guid" + guid);
                        target = el;
                    }
                    else
                    {
                        logger.log("No element for guid" + guid);
                    }
                }
                if (source != null && target != null)
                {
                    if (!checkRelationship(source, target))
                    {
                        sync_relationship(Repository, diagram, source, target, jo, pkg);
                    }
                }
            }
        }
示例#5
0
        private static void sync_constraint(EA.Repository Repository, EA.Diagram diagram, EA.Element sample, EA.Element classifier, JArray ja, EA.Package pkg, DiagramCache diagramCache)
        {
            //logger.log("Syncing Constraints from JArray:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            foreach (JObject jo in ja.Children <JObject>())
            {
                //logger.log("Syncing Constraint:");

                EA.Element constraint = reifyElement(Repository, diagramCache, jo, pkg);
                ObjectManager.sync_element_taggedvalue(Repository, constraint, null, jo, pkg, diagramCache);

                JArray sources = (JArray)jo.GetValue(RoundTripAddInClass.CONSTRAINT_PROPERTY_SOURCE);
                foreach (JObject relatedJo in sources)
                {
                    EA.Element related           = reifyElement(Repository, diagramCache, relatedJo, pkg);
                    JToken     connectorProperty = null;
                    String     connectorId       = null;
                    if (relatedJo.TryGetValue(RoundTripAddInClass.CONSTRAINT_PROPERTY_CONNECTOR_GUID, out connectorProperty))
                    {
                        connectorId = connectorProperty.ToString();
                    }
                    sync_relationship(Repository, diagram, connectorId, related, constraint, relatedJo, pkg);
                }
                JArray targets = (JArray)jo.GetValue(RoundTripAddInClass.CONSTRAINT_PROPERTY_TARGET);
                foreach (JObject relatedJo in targets)
                {
                    EA.Element related           = reifyElement(Repository, diagramCache, relatedJo, pkg);
                    JToken     connectorProperty = null;
                    String     connectorId       = null;
                    if (relatedJo.TryGetValue(RoundTripAddInClass.CONSTRAINT_PROPERTY_CONNECTOR_GUID, out connectorProperty))
                    {
                        connectorId = connectorProperty.ToString();
                    }
                    sync_relationship(Repository, diagram, connectorId, constraint, related, relatedJo, pkg);
                }
            }
        }
示例#6
0
        private static void sync_hierarchy(EA.Repository Repository, EA.Diagram diagram, EA.Element sample, JArray ja, EA.Package pkg, DiagramCache diagramCache)
        {
            logger.log("Syncing JArray:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            foreach (JObject jo in ja.Children <JObject>())
            {
                logger.log("Syncing Child:");
                JToken guidToken = null;
                if (jo.TryGetValue(RoundTripAddInClass.HIERARCHY_PROPERTY_ID, out guidToken))
                {
                    String     guid = guidToken.ToString();
                    EA.Element el   = diagramCache.elementGuidHash[guid];
                    if (el == null)
                    {
                        el = Repository.GetElementByGuid(guid);
                    }

                    if (el != null)
                    {
                        //logger.log("Found element for guid" + guid);
                        sync_hierarchy(Repository, diagram, el, jo, pkg, diagramCache);
                    }
                    else
                    {
                        logger.log("No element for id" + guid);
                    }
                }
                else
                {
                    logger.log("No id, adding element" + jo.ToString());
                    EA.Element el = pkg.Elements.AddNew("", "Object");
                    logger.log("No guid, adding element" + jo.ToString());
                    sync_hierarchy(Repository, diagram, el, jo, pkg, diagramCache);
                }
            }
        }
示例#7
0
        private static void sync_hierarchy(EA.Repository Repository, EA.Diagram diagram, EA.Element sample, JObject jo, EA.Package pkg, DiagramCache diagramCache)
        {
            logger.log("Syncing JObject:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            foreach (JProperty p in jo.Properties())
            {
                logger.log("Property:" + p.Name + ":" + p.Value.ToString());
                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_LEVEL)
                {
                    continue;
                }
                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_ID)
                {
                    continue;
                }
                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_NAME)
                {
                    sample.Name = p.Value.ToString();
                    continue;
                }
                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_DESCRIPTION)
                {
                    sample.Notes = p.Value.ToString();
                    continue;
                }

                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_TYPE)
                {
                    string     classifierName    = p.Value.ToString();
                    EA.Element elementClassifier = diagramCache.elementIDHash[sample.ClassifierID];
                    if (elementClassifier == null || elementClassifier.Name != classifierName)
                    {
                        EA.Element clazz = RepositoryHelper.queryClassifier(Repository, classifierName);
                        if (clazz != null)
                        {
                            sample.ClassifierID = clazz.ElementID;
                            continue;
                        }
                    }
                    else
                    {
                    }
                    continue;
                }
                if (p.Name == RoundTripAddInClass.HIERARCHY_PROPERTY_PARENT)
                {
                    string guid = p.Value.ToString();
                    if (guid == null || guid.Length == 0 || guid == "null")
                    {
                        continue;
                    }

                    EA.Element parent = null;
                    if (diagramCache.elementGuidHash.ContainsKey(guid))
                    {
                        parent = diagramCache.elementGuidHash[guid];
                    }
                    if (parent == null)
                    {
                        parent = Repository.GetElementByGuid(guid);
                    }
                    if (parent == null)
                    {
                        logger.log("missing parent");
                        continue;
                    }
                    else
                    {
                        linkToParent(Repository, diagram, sample, parent);
                    }
                    continue;
                }

                //string rsv=null;
                if (p.Value.Type != JTokenType.Object && p.Value.Type != JTokenType.Array)
                {
                    //logger.log("Adding Property:" + sample.Name);
                    RunState r;
                    if (rs.ContainsKey(p.Name))
                    {
                        r = rs[p.Name];
                    }
                    else
                    {
                        r     = new RunState();
                        r.key = p.Name;
                    }
                    r.value = p.Value.ToString();

                    nrs.Add(r.key, r);
                }
            }

            sample.RunState = ObjectManager.renderRunState(nrs);
            logger.log(sample.RunState);
            sample.Update();

            foreach (EA.Connector con in sample.Connectors)
            {
                EA.Element related = null;

                if (!DiagramManager.isVisible(con)) //skip not visiable
                {
                    continue;
                }

                //logger.log("Connector:" + con.SupplierEnd.Role);

                if (sample.ElementID == con.ClientID)
                {
                    if (diagramCache.elementIDHash.ContainsKey(con.SupplierID))
                    {
                        related = diagramCache.elementIDHash[con.SupplierID];
                    }

                    if (related == null)
                    {
                        related = Repository.GetElementByID(con.SupplierID);
                    }

                    JProperty p = jo.Property(con.SupplierEnd.Role);

                    if (p != null)
                    {
                        //logger.log("Found Json Property:" + con.SupplierEnd.Role);
                        if (p.Value.Type == JTokenType.Object)
                        {
                            JObject pjo = (JObject)p.Value;
                            sync_hierarchy(Repository, diagram, related, pjo, pkg, diagramCache);
                        }
                        else if (p.Value.Type == JTokenType.Array)
                        {
                            JArray ja = (JArray)p.Value;
                            if (ja.Count > 0)
                            {
                                JToken t = ja.ElementAt(0);
                                ja.RemoveAt(0);
                                if (t.Type == JTokenType.Object)
                                {
                                    sync_hierarchy(Repository, diagram, related, (JObject)t, pkg, diagramCache);
                                }
                                else
                                {
                                    MessageBox.Show("Arrays of types other than object not supported");
                                }
                            }
                        }
                    }
                }
            }
        }
        private static void sync_sample(EA.Repository Repository, EA.Element sample, JObject jo)
        {
            logger.log("Syncing:" + sample.Name);
            Dictionary <string, RunState> rs  = ObjectManager.parseRunState(sample.RunState);
            Dictionary <string, RunState> nrs = new Dictionary <string, RunState>();

            foreach (JProperty p in jo.Properties())
            {
                //string rsv=null;
                if (p.Value.Type != JTokenType.Object && p.Value.Type != JTokenType.Array)
                {
                    //logger.log("Adding Property:" + sample.Name);
                    RunState r;
                    if (rs.ContainsKey(p.Name))
                    {
                        r = rs[p.Name];
                    }
                    else
                    {
                        r     = new RunState();
                        r.key = p.Name;
                    }
                    r.value = p.Value.ToString();

                    nrs.Add(r.key, r);
                }
            }

            sample.RunState = ObjectManager.renderRunState(nrs);
            logger.log(sample.RunState);
            sample.Update();

            foreach (EA.Connector con in sample.Connectors)
            {
                logger.log("Connector:" + con.SupplierEnd.Role);
                EA.Element related = null;

                if (sample.ElementID == con.ClientID)
                {
                    related = Repository.GetElementByID(con.SupplierID);

                    JProperty p = jo.Property(con.SupplierEnd.Role);

                    if (p != null)
                    {
                        //logger.log("Found Json Property:" + con.SupplierEnd.Role);
                        if (p.Value.Type == JTokenType.Object)
                        {
                            JObject pjo = (JObject)p.Value;
                            sync_sample(Repository, related, pjo);
                        }
                        else if (p.Value.Type == JTokenType.Array)
                        {
                            JArray ja = (JArray)p.Value;
                            if (ja.Count > 0)
                            {
                                JToken t = ja.ElementAt(0);
                                ja.RemoveAt(0);
                                if (t.Type == JTokenType.Object)
                                {
                                    sync_sample(Repository, related, (JObject)t);
                                }
                                else
                                {
                                    MessageBox.Show("Arrays of types other than object not supported");
                                }
                            }
                        }
                    }
                }
            }
        }
        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);
        }