public static void syncSample(EA.Repository Repository, EA.Diagram diagram) { logger.log("Sync Sample"); IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagram); EA.Element container = container = findContainer(Repository, diagram); 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(APIAddinClass.RAML_0_8); if (!fileManager.sampleExists(container.Name, containerClassifier, fileManager.getNamespace(Repository, apiPackage))) { MessageBox.Show("No file exists at:" + fileManager.samplePath(container.Name, containerClassifier, fileManager.getNamespace(Repository, apiPackage))); return; } else { string fullpath = fileManager.samplePath(containerName, containerClassifier, fileManager.getNamespace(Repository, apiPackage)); JObject jo = JObject.Parse(File.ReadAllText(fullpath)); sync_sample(Repository, container, jo); } } }
/// /// 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"); } DiagramManager.setLogger(logger); DiagramManager.setFileManager(fileManager); APIManager.setLogger(logger); APIManager.setFileManager(fileManager); SchemaManager.setLogger(logger); SchemaManager.setFileManager(fileManager); SampleManager.setLogger(logger); SampleManager.setFileManager(fileManager); WSDLManager.setLogger(logger); MetaDataManager.setLogger(logger); return("a string"); }
static public void exportSample(EA.Repository Repository, EA.Diagram diagram) { Hashtable ht = sampleToJObject(Repository, diagram); string sample = (string)ht["sample"]; string clazz = (string)ht["class"]; JObject container = (JObject)ht["json"]; if (!diagram.Stereotype.Equals(APIAddinClass.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(APIAddinClass.RAML_0_8); fileManager.exportSample(sample, clazz, msg, fileManager.getNamespace(Repository, samplesPackage)); } }
static EA.Element findContainer(EA.Repository Repository, EA.Diagram diagram) { IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagram); foreach (EA.Element sample in samples) { if (sample.Stereotype != null && sample.Stereotype == APIAddinClass.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 == APIAddinClass.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 void exportAPI(EA.Repository Repository, EA.Diagram diagram, double version) { logger.log("Exporting an API to RAML"); REIFY_VERSION = version; EA.Element apiEl = MetaDataManager.diagramAPI(Repository, diagram); DiagramManager.captureDiagramLinks(diagram); { YamlMappingNode map = new YamlMappingNode(); REIFY_VERSION = version; reifyAPI(Repository, apiEl, map); YamlStream stream = new YamlStream(); YamlDocument yamlDoc = new YamlDocument(map); stream.Documents.Add(yamlDoc); StringWriter writer = new StringWriter(); stream.Save(writer, false /*no alias*/); string yaml = "#%RAML " + version.ToString("F1") + "\n" + writer.ToString(); EA.Package apiPackage = Repository.GetPackageByID(diagram.PackageID); fileManager.initializeAPI(apiPackage.Name); fileManager.setup(version); fileManager.exportAPI(apiPackage.Name, version, yaml, fileManager.getNamespace(Repository, apiPackage)); //fileManager.exportAPI(apiPackage.Name, APIAddinClass.RAML_0_8, yaml); //return map; } }
//static public KeyValuePair<string,JObject> sampleToJObject(EA.Repository Repository,EA.Diagram diagram) static public Hashtable sampleToJObject(EA.Repository Repository, EA.Diagram diagram) { Hashtable result = new Hashtable(); IList <EA.Element> clazzes = MetaDataManager.diagramClasses(Repository, diagram); IList <EA.Element> samples = MetaDataManager.diagramSamples(Repository, diagram); EA.Element root = findContainer(Repository, diagram); 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 == APIAddinClass.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 == APIAddinClass.EA_TYPE_ENUMERATION) { //if (con.SupplierEnd.Cardinality.Equals(APIAddinClass.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 == APIAddinClass.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(APIAddinClass.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(APIAddinClass.CARDINALITY_0_TO_MANY) || con.SupplierEnd.Cardinality.Equals(APIAddinClass.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); }
/// /// 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); EA.Diagram diagram = null; if (Repository.GetContextItemType() == ObjectType.otDiagram) { diagram = Repository.GetContextObject(); } switch (ItemName) { case menuExportAll: exportAll(Repository); break; case menuExportPackage: exportPackage(Repository); break; case menuExportDiagram: exportDiagram(Repository); break; case menuExportSchema: try { SchemaManager.exportSchema(Repository, diagram); 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); MetaDataManager.setAsSampleDiagram(Repository, diagram); break; case menuSyncSample: SampleManager.syncSample(Repository, diagram); break; case menuCreateSample: SchemaManager.generateSample(Repository); break; case menuUpdateClassFromInstance: SchemaManager.updateClassFromInstance(Repository); break; case menuUpdateInstanceFromClass: SchemaManager.operateOnSample(Repository, SchemaManager.updateSampleFromClass); break; case menuValidateDiagram: if (diagram != null) { if (diagram.Stereotype.Equals(APIAddinClass.EA_STEREOTYPE_SAMPLEDIAGRAM)) { SampleManager.validateDiagram(Repository, diagram); } else if (diagram.Stereotype.Equals(APIAddinClass.EA_STEREOTYPE_SCHEMADIAGRAM)) { SchemaManager.validateDiagram(Repository, diagram); } else if (diagram.Stereotype.Equals(APIAddinClass.EA_STEREOTYPE_APIDIAGRAM)) { APIManager.validateDiagram(Repository, diagram); } } break; case menuExportAPI: APIManager.exportAPI(Repository, diagram); MetaDataManager.setAsAPIDiagram(Repository, diagram); break; case menuExportAPIRAML1: APIManager.exportAPI_RAML1(Repository, diagram); MetaDataManager.setAsAPIDiagram(Repository, diagram); break; case menuToggleLogging: logger.toggleLogging(Repository); break; case menuWeb: this.callWeb(Repository); break; case menuImportSOA: WSDLManager.importWSDLs(Repository, diagram); break; case menuExportSOA: WSDLManager.exportWSDLs(Repository, diagram); break; } }