Monday, May 25, 2015

ADF Technical Document Generator


Let me admit, most annoying part of full development life cycle is when I need to write Technical documentation. To avoid this recurring task(boring as well ;)...) I created Technical document generator utility. Thanks to my colleagues(Prahlad & Raj) who also contributed to this work.
I'm sure even they would appreciate writing a blog on this task.

Let me be clear what you will get with this utility.
It would list down;

  1. All the model artifacts, e.g Entity object, view object, application module ,package structure, class files.
  2. All(almost all) validation rules defined on entity objects.
  3. View accessors & list of values defined on an entity/view objects.
  4. Entity/View object attributes, any expressions defined over them.


No software/utility is perfect & cover all the scenarios. So you may need to modify & use as per your requirement. But this blog should give you fair enough idea on how to get around them.

Steps to uptake this utility:

  • Create a temporary application module & add all the view objects in the application.
  • Generate AMImpl for the temporary application module.
  • Copy paste code below.
  • Expose client interface methods writeAllEntityObjects & writeAllViewObjects.
  • Run them directly from Application module tester.
  • All your code is generated in server console. Copy code to a file & save as html.
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import oracle.jbo.AttributeDef;
import oracle.jbo.AttributeHints;
import oracle.jbo.ViewObject;
import oracle.jbo.rules.JboMethodValidator;
import oracle.jbo.rules.JboRegExpValidator;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.AttributeDefImpl;
import oracle.jbo.server.EntityDefImpl;
import oracle.jbo.server.EntityReference;
import oracle.jbo.server.JboUniqueKeyValidator;
import oracle.jbo.server.ViewAccessorDef;
import oracle.jbo.server.ViewAttributeDefImpl;
import oracle.jbo.server.ViewDefImpl;
import oracle.jbo.server.ViewObjectImpl;
public class HTMLDocumentGeneratorAMImpl extends ApplicationModuleImpl {
public HTMLDocumentGeneratorAMImpl() {
super();
}
public void writeAllEntityObjects() {
String string;
ViewObject[] allviewObjects = getViewObjects();
if (allviewObjects != null) {
string = "<!DOCTYPE html>\n" +
"<html>\n" +
"<body>\n" +
"\n";
String newline = "\n";
Map<String, ViewObject> entityMap = new TreeMap<String, ViewObject>();
for (ViewObject viewObj : allviewObjects) {
ViewObjectImpl viewObject = (ViewObjectImpl)viewObj;
EntityReference[] entities = ((ViewDefImpl)(viewObject.getDef())).getEntityUsages();
if (entities != null) {
for (EntityReference entity : entities) {
entityMap.put(entity.getName(), viewObject);
}
}
}
for (Map.Entry<String, ViewObject> entry : entityMap.entrySet()) {
ViewObjectImpl viewObject = (ViewObjectImpl)entry.getValue();
EntityReference[] entities = ((ViewDefImpl)(viewObject.getDef())).getEntityUsages();
if (entities != null) {
for (EntityReference entity : entities) {
if (!((String)entry.getKey()).equals(entity.getName())) {
break;
}
string = string + "<h2>Entity Name : " + entity.getName() + "</h2>\n";
string = string + "<h3>Entity Object Definition : </h3>\n\n";
string = string + "<table>";
string = string + "<tr><td>Package Name</td><td>" + entity.getEntityDef().getFullName() + "</td></tr>";
string = string + "<tr><td>EO Base Class</td><td>" + breakString(entity.getEntityDef().getRowClass().getName()) + "</td></tr>";
string = string + "<tr><td>Phycial Table Name</td><td>" + breakString(entity.getEntityDef().getSource()) + "</td></tr>";
string = string + "<tr><td>VO Primary Key Attribute</td><td>" + getVOPrimaryKeyAttributes(entity) + "</td></tr>";
string = string + "</table>\n\n";
//accessors
string = string + "<h3>View Accessors </h3>\n\n";
ArrayList<ViewAccessorDef> listOfAccessors = entity.getEntityDef().getViewAccessorDefs();
if (listOfAccessors != null) {
string = string + "<table>";
string = string + "<tr><td>View Accessors Name</td><td>View Object</td></tr>";
for (ViewAccessorDef accessorDef : listOfAccessors) {
string = string + "<tr><td>" + accessorDef.getName() + "</td><td>" + accessorDef.getViewDefFullName() + "</td></tr>";
}
string = string + "</table>\n\n";
}
// validators
string = string + "<h3>Validators </h3>\n\n";
ArrayList entityValidatorsList = entity.getEntityDef().getValidators();
if (entityValidatorsList != null) {
Iterator entityValidatoritr = entityValidatorsList.iterator();
if (entityValidatoritr.hasNext()) {
string = string + "<table>";
string = string + "<tr><td>Validator Type</td><td>Attributes</td><td>Expression/Other</td></tr>";
List entityValidatorsStrList = returnValidatorsList(entityValidatoritr);
Iterator entityValidatorStrItr = entityValidatorsStrList.iterator();
while (entityValidatorStrItr.hasNext()) {
String strArray[] = (String[])entityValidatorStrItr.next();
string = string + "<tr><td>" + strArray[0] + "</td><td>" + strArray[1] + "</td><td>" + strArray[2] + "</td></tr>";
}
// attribute validators
AttributeDef[] attDefVals = entity.getEntityDef().getAttributeDefs();
if (attDefVals != null) {
for (AttributeDef def : attDefVals) {
AttributeDefImpl attrDefImpl = (AttributeDefImpl)def;
ArrayList attrValidatorsList = attrDefImpl.getValidators();
Iterator attrValidatorItr = attrValidatorsList.iterator();
if (attrValidatorItr.hasNext()) {
List attrValidatorsStrList = returnValidatorsList(attrValidatorItr);
Iterator attrValidatorStrItr = attrValidatorsStrList.iterator();
while (attrValidatorStrItr.hasNext()) {
String strArray[] = (String[])attrValidatorStrItr.next();
string = string + "<tr><td>" + strArray[0] + "</td><td>" + strArray[1] + "</td><td>" + strArray[2] + "</td></tr>";
}
}
}
string = string + "</table>\n\n";
}
}
}
// entity Attributes
string = string + "<h3>Entity Attributes </h3>\n\n";
string = string + "<table>";
AttributeDef[] attDef = entity.getEntityDef().getAttributeDefs();
if (attDef != null) {
string = string + "<tr><td>Name</td><td>Type</td><td>Column Name</td><td>Expressions</td></tr>";
for (AttributeDef def : attDef) {
string = string + "<tr>";
AttributeDefImpl defImpl = (AttributeDefImpl)def;
String dataType = def.getJavaType().getName().substring(def.getJavaType().getName().lastIndexOf(".") + 1, def.getJavaType().getName().length());
String defaultValue = null, sqlExpr = null, groovyExpr = null, recalCondition = null;
if (!(dataType.contains("RowIterator") || dataType.contains("Object") || dataType.contains("CAPEntityImpl"))) {
string = string + "<td>" + def.getName() + "</td><td>" + dataType + "</td>";
//default value
if (defImpl.getDefaultValue() != null)
defaultValue = "Default: " + getStringValue(defImpl.getDefaultValue());
if (defImpl.getAttributeKind() == AttributeDef.ATTR_SQL_DERIVED) {
//col name
string = string + "<td>" + "" + "</td>";
//sql expr
if (defImpl.getColumnName() != null)
sqlExpr = "SQL Expr: " + getStringValue(defImpl.getColumnName());
} else {
string = string + "<td>" + defImpl.getColumnName() + "</td>";
//groovy
if (defImpl.getTransientExpression() != null)
groovyExpr = "Groovy: " + getStringValue(defImpl.getTransientExpression());
}
if (defImpl.getRecalcExpression() != null)
recalCondition = "Recal Condition: " + getStringValue(defImpl.getRecalcExpression());
//build the string
String str = "";
str = defaultValue != null ? defaultValue : str;
if (sqlExpr != null && !sqlExpr.isEmpty()) {
str = str + newline;
str = str + (sqlExpr != null ? breakString(sqlExpr) : "");
}
if (groovyExpr != null && !groovyExpr.isEmpty()) {
str = str + newline;
str = str + (groovyExpr != null ? breakString(groovyExpr) : "");
}
if (recalCondition != null && !recalCondition.isEmpty()) {
str = str + newline;
str = str + (recalCondition != null ? breakString(recalCondition) : "");
}
if (str != null && !str.isEmpty())
string = string + "<td>" + str + "</td>";
else
string = string + "<td>" + "" + "</td>";
}
string = string + "</tr>";
}
}
string = string + "</table>";
}
}
}
string = string + "</body>\n";
string = string + "</html>\n";
System.out.println(string);
}
}
private String getVOPrimaryKeyAttributes(EntityReference entity) {
String voPrimaryKeyAttributes = "";
//vo primary keys
AttributeDef[] keyDefs = ((EntityDefImpl)entity.getEntityDef()).getPrimaryKeys();
if (keyDefs != null) {
for (AttributeDef keyDef : keyDefs) {
if ("".equals(voPrimaryKeyAttributes)) {
voPrimaryKeyAttributes += keyDef.getName();
} else {
voPrimaryKeyAttributes += "," + keyDef.getName();
}
}
}
return voPrimaryKeyAttributes;
}
private List returnValidatorsList(Iterator attrItr) {
List validatorsList = new ArrayList();
while (attrItr.hasNext()) {
Object obj = attrItr.next();
Class classObj = obj.getClass();
String strValidatorTypeName = classObj.getName().substring(classObj.getName().lastIndexOf(".") + 1, classObj.getName().length());
if (strValidatorTypeName.equals("JboUniqueKeyValidator")) {
JboUniqueKeyValidator validator = (JboUniqueKeyValidator)obj;
String values = "";
if (validator.getTriggeringAttributeNames() != null)
for (String attribName : validator.getTriggeringAttributeNames()) {
if ("".equals(values)) {
values += attribName;
} else {
values += "," + attribName;
}
}
String validatorStrArray[] = { "Unique Key Validator", values, "" };
validatorsList.add(validatorStrArray);
} else if (strValidatorTypeName.equals("JboMethodValidator")) {
JboMethodValidator validator = (JboMethodValidator)obj;
String validatorStrArray[] = { "Method Validator", getStringValue(validator.getValidatingAttributeName()), "()" };
validatorsList.add(validatorStrArray);
} else if (strValidatorTypeName.equals("JboRegExpValidator")) {
JboRegExpValidator validator = (JboRegExpValidator)obj;
String validatorStrArray[] = { "RegExp Validator", validator.getValidatingAttributeName(), validator.getPattern() };
validatorsList.add(validatorStrArray);
}
}
return validatorsList;
}
public void writeAllViewObjects() {
ViewObject[] allviewObjects = (ViewObject[])getViewObjects();
String string = new String();
if (allviewObjects != null) {
string = "<!DOCTYPE html>\n" +
"<html>\n" +
"<body>\n" +
"\n";
Map<String, ViewObject> viewObjectMap = new TreeMap<String, ViewObject>();
for (ViewObject viewObj : allviewObjects) {
ViewObjectImpl viewObject = (ViewObjectImpl)viewObj;
viewObjectMap.put(viewObject.getName(), viewObject);
}
for (Map.Entry<String, ViewObject> entry : viewObjectMap.entrySet()) {
ViewObjectImpl viewObject = (ViewObjectImpl)entry.getValue();
String quotes = "'";
String newline = "\n";
///////////////VIEWOBJECT START/////////////////
string = string + "<h2>View Object Name : " + viewObject.getDef().getName() + "</h2>\n";
string = string + "<h3>View Object Definition </h3>\n\n";
string = string + "<table>";
string = string + "<tr><td>Package Name</td><td>" + ((ViewDefImpl)(viewObject.getDef())).getFullName() + "</td></tr>";
string = string + "<tr><td>VO Base Class</td><td>" + ((ViewDefImpl)(viewObject.getDef())).getRowClass() + "</td></tr>";
//vo primary keys
AttributeDef[] keyDefs = viewObject.getKeyAttributeDefs();
String voPrimaryKeyAttribute = "";
if (keyDefs != null) {
for (AttributeDef keyDef : keyDefs) {
if ("".equals(voPrimaryKeyAttribute)) {
voPrimaryKeyAttribute += keyDef.getName();
} else {
voPrimaryKeyAttribute += "," + keyDef.getName();
}
}
string = string + "<tr><td>VO Primary Key Attribute</td><td>" + voPrimaryKeyAttribute + "</td></tr>";
//entities
EntityReference[] entities = ((ViewDefImpl)(viewObject.getDef())).getEntityUsages();
String entitiesStr = "";
if (entities != null) {
for (EntityReference entity : entities) {
if ("".equals(entitiesStr)) {
entitiesStr += entity.getName();
} else {
entitiesStr += "," + entity.getName();
}
}
}
string = string + "<tr><td>Entities</td><td>" + entitiesStr + "</td></tr>";
string = string + "</table>\n\n";
string = string + "<h3>View Accessors </h3>\n\n";
//view accessors
ArrayList<ViewAccessorDef> listOfAccessors = ((ViewDefImpl)(viewObject.getDef())).getViewAccessorDefs();
if (listOfAccessors != null) {
string = string + "<table>";
string = string + "<tr><td>View Accessors Name</td><td>View Object</td></tr>";
for (ViewAccessorDef accessorDef : listOfAccessors) {
string = string + "<tr><td>" + accessorDef.getName() + "</td><td>" + accessorDef.getViewDefFullName() + "</td></tr>";
}
string = string + "</table>\n\n";
}
string = string + "<h3>View Attributes </h3>\n\n";
AttributeDef[] attDef = viewObject.getAttributeDefs();
if (attDef != null) {
string = string + "<table>";
string = string + "<tr><td>Name</td><td>Type</td><td>EntityObject.EnityAttribute</td><td>Expressions</td></tr>";
for (AttributeDef def : attDef) {
string = string + "<tr>";
String dataType = def.getJavaType().getName().substring(def.getJavaType().getName().lastIndexOf(".") + 1, def.getJavaType().getName().length());
String defaultValue = null, sqlExpr = null, groovyExpr = null, recalCondition = null, controlType = null;
if (!(dataType.contains("RowIterator") || dataType.contains("Object"))) {
ViewAttributeDefImpl viewDef = (ViewAttributeDefImpl)def;
string = string + "<td>" + def.getName() + "</td>";
//default value
if (viewDef.getDefaultValue() != null)
defaultValue = "Default: " + getStringValue(viewDef.getDefaultValue());
string = string + "<td>" + dataType + "</td>";
if (viewDef.getEntityAttributeDef() != null && viewDef.getEntityAttributeDef().getEntityDef() != null) {
string =
string + "<td>" + viewDef.getEntityAttributeDef().getEntityDef().getName() + "." + viewDef.getEntityAttributeDef().getName() + "</td>";
} else {
string = string + "<td>" + getStringValue(null) + "</td>";
}
//sql expr
if (viewDef.getColumnName() != null)
sqlExpr = "SQL Expr: " + getStringValue(viewDef.getColumnName());
//groovy
if (viewDef.getTransientExpression() != null)
groovyExpr = "Groovy: " + getStringValue(viewDef.getTransientExpression());
if (viewDef.getRecalcExpression() != null)
recalCondition = "Recal Condition: " + getStringValue(viewDef.getRecalcExpression());
if (def.getProperty(AttributeHints.ATTRIBUTE_CTL_TYPE) != null)
controlType = "Control Type: " + getStringValue(def.getProperty(AttributeHints.ATTRIBUTE_CTL_TYPE));
//build the string
String str = "";
str = defaultValue != null ? defaultValue : str;
if (sqlExpr != null && !sqlExpr.isEmpty()) {
str = str + newline;
str = str + (sqlExpr != null ? breakString(sqlExpr) : "");
}
if (groovyExpr != null && !groovyExpr.isEmpty()) {
str = str + newline;
str = str + (groovyExpr != null ? breakString(groovyExpr) : "");
}
if (recalCondition != null && !recalCondition.isEmpty()) {
str = str + newline;
str = str + (recalCondition != null ? breakString(recalCondition) : "");
}
if (controlType != null && !controlType.isEmpty()) {
str = str + newline;
str = str + (controlType != null ? breakString(controlType) : "");
}
if (str != null && !str.isEmpty())
string = string + "<td>" + str + "</td>";
else
string = string + "<td></td>";
}
string = string + "</tr>";
}
string = string + "</table>";
}
}
}
/////////////VIEWOBJECT END/////////////////
string = string + "</body>\n";
string = string + "</html>\n";
}
System.out.println(string);
}
private String breakString(String str) {
String retStr = "";
if (str != null) {
while (str.length() > 20) {
retStr = retStr + str.substring(0, 20);
str = str.substring(20);
retStr = retStr + "\n";
}
retStr += str;
}
return retStr;
}
private String getStringValue(Object str) {
if(str instanceof BigDecimal){
BigDecimal strBigDecimal = (BigDecimal)str;
return strBigDecimal.toString();
}
return str == null ? "" : (String)str;
}
}
Hope this helps!!!

No comments:

Post a Comment