Javascript Codegenerator

The Javascript code generator creates a javascript library out of a Java service object model.

Example for a gradle build configuration:

// ------------------------------------------------------
// Javascript API generation
// ------------------------------------------------------
task generateJavascript (type: JavaExec) {
	main = 'org.resoa.codegen.CodeGenerator'
	classpath = sourceSets.main.runtimeClasspath
	// first argument is the folder, containing the resoa service domain jars.
	// second argument is the target folder for your javascript API library
	// third argument is the path to a xml file with root element JSFunctionList, defining your extended API functionality
	args = ['build/libs', 'build/javascript', 'build/resources/main/prototype.function.xml'].toList()

The Codegenerator will define /generate the following:

  • namespace according to the java namespace. The package name itself will be a self executing function, returning all defined classes.
  • javascript class for every java bean
  • property and prototype get/set functions for every java bean property of a class
  • prototype functions getInstance(), instanceOf(jsonString), isClassProperty(propName)
  • additional prototype functions and constructor, specified by a xml file (default prototype.function.xml)
  • a type definition class (name is the extension of the java class name by _ClassDef). Use this type to add a default behaviour of your properties in datatables etc. Set JSFunctionList attribute generateClassDef to false, if you don’t need these descriptor types.
  • simple minify of the generate javsascrupt library. Set ‘JSFunctionList attribute shrink to false, if you don’t need the .min.js files.

Extend your API by JSFunctionList – prototype.function.xml

Declare additional functionality to your javascript API. Here’s the default file:

<?xml version="1.0" encoding="UTF-8"?>
<JSFunctionList xmlns="" generateClassDef="true" shrink="true">

<!-- Define additional constructor function with jsonString as parameter -->
	<Code>if (jsonstring != null){var jsonobj = JSON.parse(jsonstring)[this];
		if (jsonobj == null)return;for (var p in jsonobj){if (jsonobj[p] != null)this[p] = jsonobj[p];};};</Code>

<!-- Define additional functions -->
<!-- *************************** -->

<!-- toJSON function: create an Jquery/AJAX read JSON object for a resoa service request. Arguments:
	- method: The service method, you like to call
	- callback: The name of the callback function if you use JSONP
	- priority: the priority, the service request will be handled within the resoa grid
	- async: JQUERY Ajax option for synchronized or async request processing -->
<Functions name="toJson">
	<Code>var tmp={};tmp[this]=this;tmp.method=method;
		if(callback != null)tmp.callback = callback;if(priority != null)tmp.priority = priority;if(async != null)tmp.async = true;
		return JSON.stringify(tmp);</Code>

<!-- fromForm function: Fill an instance out of an HTML form.
Mapping concvention:The HTML form name attribute must be the business object class name, extended by _ and the object attribute name
Argument: the form
<Functions name="fromForm">
	<Code><![CDATA[for (var p in this){var fp = form[this + "_" + p];if (fp != null && fp.value != null && fp.value.length > 0) this[p] = fp.value;}]]></Code>

<!-- fillForm function: Set values within an HTML form from a service instance
Mapping concvention:The HTML form name attribute must be the business object class name, extended by _ and the object attribute name
<Functions name="fillForm">
	<Code><![CDATA[if (form == null)return;if (form.size != null){if (form.size() > 0)form = form[0];else return;}
		for (var p in this){if (! this.isClassProperty(p) || this[p] == null || form.elements[this.toString() + "_" + p] == null) continue;form.elements[this.toString() + "_" + p].value = this[p].toString();}]]></Code>

<!-- Wrap function for wrapping JSON into a javascript api obejct instance -->
<Functions name="wrap">
	<Code><![CDATA[var inst = this.getInstance();if (obj == null)return inst;
		for (var p in inst){if (! this.isClassProperty(p) || obj[p] == null)continue;inst[p] = obj[p];} return inst;]]></Code>

<!-- _ClassDef Descriptor type area
optionally generated by CodeGenerator
define descriptor properties, which should be added to javascript types as default
As an example: Add behaviour of a property within a jquery datatable -->
<Properties javascriptType="string" name = "searchable" value="true" />
<Properties javascriptType="string" name = "sortable" value="true" />
<Properties javascriptType="string" name = "defaultContent" value="&quot;&quot;" />
<Properties javascriptType="date" name = "eventFunction" value="org.resoa.functions.handleDateString" />
<Properties javascriptType="date" name = "sortable" value="true" />

<!-- You also can define properties for Javascript fields. Use the system generated fieldName as identifier
example: <property fieldName="XSDType_XSDAttribute" name = "visible" value="false" /> -->
<Properties fieldName="Student_id" name = "visible" value="false" />


js file dependencies and utilities

Check the root, here we added relevant javascript files:

  • Import the the json.min.js when using auto-generated libraries

Resoa JSON functionality uses XMLGregorianCalendar for date serialization. Check jsutil.min.js, there are utilities like

  • JSUTIL.XMLDateToJavascriptDate: Helper function vor date handling
  • Hashcode generator functions
  • Cookie get/set/check helper functions