/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.alamoraes.eaintegration;
import java.util.ArrayList;
import java.util.List;
import org.sparx.Attribute;
import org.sparx.Connector;
import org.sparx.Element;
import org.sparx.Method;
import org.sparx.Repository;
/**
*
* Esta classe exporta uma classe de um projeto do EA para uma arquivo javascript.
* Contendo a estrutura que ser utilizada posteriormente nos templates
* @author Andr Moraes
*/
public class EAClasse extends EAObject {
private Element ele;
private EAClasse superClass;
private List<EARelation> relations;
private List<EAAttribute> attributes;
private List<EAMethod> methods;
public EAClasse(Repository repo, Element clzEle) {
super(repo);
this.ele = clzEle;
this.relations = new EAArrayList<EARelation>();
this.attributes = new EAArrayList<EAAttribute>();
this.methods = new EAArrayList<EAMethod>();
}
public String getPackage() {
String name = getPackageName(repo.GetPackageByID(ele.GetPackageID()));
return name.substring(1);
}
private String getPackageName(org.sparx.Package pkg) {
if (pkg.GetIsModel()) {
return "";
} else {
return getPackageName(repo.GetPackageByID(pkg.GetParentID())) + "." + pkg.GetName().toLowerCase();
}
}
public void generate() {
info.put("name", ele.GetName());
if ("".equals(ele.GetAlias())) {
info.put("table", ele.GetName().toUpperCase());
} else {
info.put("table", ele.GetAlias().toUpperCase());
}
ArrayList<String> imports = new ArrayList<String>();
info.put("imports", imports);
info.put("package", getPackage());
if (this.superClass != null) {
if (!this.getPackage().equals(this.superClass.getPackage())) {
imports.add(this.superClass.getPackage());
}
}
info.put("baseclass", (this.superClass != null ? this.superClass.getName() : "Object"));
info.put("implements", new String[] { "Serializable" });
generateAttributes();
generateMethods();
generateRelations();
}
public void generateAttributes() {
List<Object> attrs = new ArrayList<Object>();
for(EAAttribute attr: this.attributes)
attrs.add(attr.export());
info.put("attributes", attrs);
}
public void generateMethods() {
List<Object> meths = new ArrayList<Object>();
for(EAMethod meth: this.methods)
meths.add(meth.export());
info.put("methods", meths);
}
public void generateRelations() {
List<Object> relations = new ArrayList<Object>();
for(EARelation rel: this.relations)
relations.add(rel.export());
info.put("relations", relations);
}
public void extract() {
extractAttributes();
extractMethods();
extractRelations();
}
public String getName() {
return ele.GetName();
}
public void extractAttributes() {
for(Attribute att: ele.GetAttributes()) {
EAAttribute eattr = new EAAttribute(repo, att);
this.attributes.add(eattr);
}
}
public void extractMethods() {
for(Method meth: ele.GetMethods()) {
EAMethod eam = new EAMethod(repo, meth);
this.methods.add(eam);
}
}
public void extractRelations() {
System.out.println("Relations for: " + ele.GetName());
for(Connector conn: ele.GetConnectors()) {
System.out.println("extractRelations @ " + conn.GetName());
if (!EARelation.isClassRelation(conn, repo))
continue;
if (EARelation.isInheritance(conn, this.ele, repo)) {
extractSuperClass(conn);
} else if (EARelation.isAssociation(conn, repo, true)) {
EARelationDirection dir = EARelation.guessMyDirection(conn, ele, repo);
if (dir == EARelationDirection.Both) {
EARelation rel = new EARelation(repo, conn, EARelationDirection.SourceToTarget);
EARelation reverse = new EARelation(repo, conn, EARelationDirection.TargeToSource);
this.relations.add(rel);
this.relations.add(reverse);
} else {
EARelation ear = new EARelation(repo, conn, dir);
this.relations.add(ear);
}
}
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EAClasse) {
EAClasse other = (EAClasse) obj;
return other.ele.GetElementID() ==
this.ele.GetElementID();
} else return false;
}
@Override
public int hashCode() {
return this.ele.hashCode();
}
private void extractSuperClass(Connector conn) {
Element parent = repo.GetElementByID(conn.GetSupplierID());
this.superClass = new EAClasse(repo, parent);
EAProject.getGenerationForThread().push(this.superClass);
}
Element getElement() {
return this.ele;
}
public static boolean isClass(Element element) {
return "Class".equals(element.GetType());
}
}
|