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