How to retrieve comments of entities with openAPI

Hello,

I am currently wiritng an API scripts which retrieves entities, columns, stereotypes and comments of a diagram but I am stuck in retrieving the comments of an entity (IDBTableUIModel). Could you please suggest how to do it.

The Entity has a “SQL Mapping” comment with a SQL in its description. That is the info I like to get

image

I could not find a method which gets the comments (IComment Array) of IModelElement

public class ERDAction implements VPActionController {
@Override
public void performAction(VPAction arg0) {
IProject project = ApplicationManager.instance().getProjectManager().getProject();
IDiagramUIModel[] diagrams = project.toDiagramArray();
if (diagrams != null) {
for (IDiagramUIModel diagram : diagrams) {
if (diagram instanceof IERDiagramUIModel && “AnyDiagram”.equals(diagram.getName())) {
IDiagramUIModelComment[] comments = diagram.toCommentArray();
if (comments != null) {
// Diagra comment is OK
for (IDiagramUIModelComment comment : comments) {
ApplicationManager.instance().getViewManager().showMessage(" ->COMMENTS…" + comment.getAuthor());

					}
				}
				-- Get Entities
				if (elements != null) {
					for (IDiagramElement element : elements) {

						IModelElement modElement = element.getModelElement();

						if (element instanceof IDBTableUIModel) {
						// Entitiy Found

							//Retrieve Table StereoType 
							ApplicationManager.instance().getViewManager().showMessage("	->Has StereoType:" + element.getModelElement().hasStereotype("coloumn1_"));
							ApplicationManager.instance().getViewManager().showMessage("	->Has StereoType:" + element.getModelElement().hasStereotype("test"));
							//Get all stereoTypes of entity
							IStereotype[] stereotypes = element.getModelElement().toStereotypeModelArray();
							if (stereotypes != null) {
								for (IStereotype stereotype : stereotypes) {
									ApplicationManager.instance().getViewManager().showMessage("	->got StereoType " + stereotype.getName() + " for table " + modElement.getName());
								}
							}
							
							
							// Get columns
							IModelElement[] tableElements = element.getModelElement().toChildArray();

							if (tableElements != null) {

								for (IModelElement tableelement : tableElements) {
									ApplicationManager.instance().getViewManager().showMessage("	-> table Element " + tableelement.getName());

								}
							}


						}
					}

Thanks for your gıidance is advance

Best Regards
seyhmus

Hi seyhmus,

Thank you for your inquiry. Your question has been forwarded to our engineers. I will let you know when there is any update.

Best regards,
Jick Yeung

Hi Seyhmus,

Since not all model types support the comments’ property, functions such as toCommentArray() etc… are not supported by IModelElement directly.

You may:

A. Cast your model element to IDBTable (model element of an ERD entity), which supports the functions:
public void addComment(IComment comment);
public void removeComment(IComment comment);
public IComment getCommentByIndex(int index);
public void removeCommentByIndex(int index);
public int commentCount();
public Iterator commentIterator();
public IComment[] toCommentArray();

B. Use:
IModelElement.getModelPropertyByName(IDBTable.PROP_COMMENTS) : IModelProperty
IModelProperty.getValueAsModelCollection() : IModelElement[] <----- this array contains the IComment

PS: the constants PROP_COMMENT is supported in any model type that supports comments property (e.g. IClass.PROP_COMMENTS).

Best regards,
Jick Yeung

1 Like

Hello Jick, Thanks a lot for quick solution. Both suggestions worked. This saved me a lot time :pray:

My pleasure. :slight_smile:

Hi Seyhmus, I’m facing the same issue like you before but I’m not a professional java programmer. So perhaps you can help me and post here the relevant coding lines or maybe the program? It would be a great help for me.
Best regards,
Dieter

Hello Dieter,

Below is the piece of code which retrieves the individual comments of an Entity, based on Jick’s suggested methods.

Hope it helps

public String performExport(IProject project, String lexportDir){
String outputFile =“”;
IDiagramUIModel diagrams = project.toDiagramArray();

    if (diagrams != null) {
        //Diagram
        for (IDiagramUIModel diagram : diagrams) {
            if (diagram instanceof IERDiagramUIModel) {

                this.addERDiagram(diagram.getName(),((diagram.getParentModel() == null) ? "" : diagram.getParentModel().getName()));

                ApplicationManager.instance().getViewManager().showMessage("Diagram:" + diagram.getName());
                ApplicationManager.instance().getViewManager().showMessage("	-> getparent " +diagram.getName() + "-" +   ((diagram.getParentModel() == null) ? "" : diagram.getParentModel().getName()) );
                // Get Diagram Comments

                IDiagramUIModelComment[] comments = diagram.toCommentArray();
                if (comments != null) {
                    for (IDiagramUIModelComment comment : comments) {
                        ApplicationManager.instance().getViewManager().showMessage("	->COMMENTS..." + comment.getAuthor());

                    }
                }
                // table gezinme kısmı
                IDiagramElement[] elements = diagram.toDiagramElementArray();
                int i = 0;
                //Diagram Elements
                if (elements != null) {
                    for (IDiagramElement element : elements) {
  				    // PArse the Entity
                        parseTable(this,element);
                    }
                    ApplicationManager.instance().getViewManager().showMessage(diagram.getName() + ":" + ((IERDiagramUIModel) diagram).getDataModel());
                }
            }
        }
        //JSON: Creating the Gson object
        outputFile = this.OutputDiagram(lexportDir);

    }
    return outputFile;
}

public void parseTable(ERDOutput erd, IDiagramElement element) {
IModelElement modElement = element.getModelElement();

    // Tables
    if (element instanceof IDBTableUIModel) {
        IDBTable itab = (IDBTable) modElement;
        IModelProperty prop = modElement.getModelPropertyByName(IDBTable.PROP_COMMENTS);


        erd.addERDEntity(modElement.getName(), modElement.getDocumentation(), ((itab.getSchemaLookup() == null) ? "" : itab.getSchemaLookup().getName()), ((itab.getDomain() == null) ? "" : itab.getDomain().getName()));

        //Tablo Commentleri (ETL SQLleri burada olacak)
        IModelElement[] propElements = prop.getValueAsModelCollection();
        if (propElements != null) {

            for (IModelElement propElement : propElements) {
  		     //Here we have our comments of an Entity in ERD 
                erd.addERDEntityComments(propElement.getName(), propElement.getDescription());
            }
        }
  }


}

Comments of a column is retrieved with similiar approach:

public void parseColumn(ERDOutput erd, IDBTable itab) {
    IDBColumn[] columns = itab.toDBColumnArray();
    if (columns != null) {

        for (IDBColumn column : columns) {
            erd.addERDEntityColumns(column.getName(), column.getTypeInText(), column.getLength(), column.getDescription());
             IStereotype[] columnStereotypes = column.toStereotypeModelArray();
            if (columnStereotypes != null) {
                for (IStereotype columnStereotype : columnStereotypes) {
                    erd.addERDColumnStereotypes(columnStereotype.getName(), "");
                 }
            }
            //Column comments more than one
            IComment[] columnComments = column.toCommentArray();
            if (columnComments != null) {
                for (IComment columnComment : columnComments) {
                    erd.addERDColumnComments(columnComment.getName(), columnComment.getDocumentation());
                }
            }

Regards
seyhmus

Dear Seyhmus, thanks for help.
Best regards from Colombia
Dieter

Hi Dieter,
I’m glad that it helped. Please let me know if you need further help. Regards from Istanbul

mit freundlichen Gruessen :slight_smile:
seyhmus

Hi Seyhum, it seems you are very experienced in VP API Programming. And I’m just a beginner. Are you interested in a collaboration? If so you can answer me directly using dieter.goebel@leonsulting.com
Best regards from Colombia

Dear Seyhum, once again thanks for your help.
I modified your code in order to get the comments belonging to a specific model element, in this case an ArchiMateApplicationComponent:

public IComment[] exportapplicationComponentComments(IArchiMateApplicationComponent applicationComponent, String lexportDir){ // export comments belonging to a model element
String commentName = “”;
String commentAuthor = “”;
String commentDescription = “”;
IComment[] comments = applicationComponent.toCommentArray();
if (comments != null) {
//Diagram
for (IComment comment : comments) {
if (comments != null) {
commentDescription = comment.getDescription();
commentName = comment.getName();
commentAuthor = comment.getAuthor();
ApplicationManager.instance().getViewManager().showMessage(" ->COMMENTS…" + commentName);
}
}
}
return comments;
}