Porting Basic Script To Java
Porting Basic Script To Java
Presentation vs. business logic
Basic Script files contain both the presentation code (view definitions, dialogs, code that handles user actions, etc) and the business logic code. In Web Central all presentation features are implemented in AXVW views and corresponding JS files, and the business logic in implemented as workflow rules in Java.
Most Basic Script files have a structure that makes it easy to distinguish between presentation and business logic. There are Basic Script procedures that display views and dialogs, and these procedures do not contain any real business logic code, but call other procedures that do.
Application functional specifications should explicitly list the business service methods that are called from views, along with their input and output parameters.
Examining Basic Script code
A good editor with syntax highlighting, such as Programmer’s Notepad (http://www.pnotepad.org/), makes it easier to examine Basic Script code.
Executing UPDATE or INSERT SQL
Basic Script provides the ProjDb.ExecuteSql() method to execute an UPDATE or an INSERT SQL.
The equivalent method in the Web Central API is SqlUtils.executeUpdate():
String sql = "UPDATE cost_tran SET chrgbck_status = 'CA' WHERE chrgbck_status = 'CS'";
SqlUtils.executeUpdate("cost_tran", sql);
The SQL can contain expressions such as ${concat} or ${sql.isNull}. For example, to format a Date object for SQL in a database-specific format:
Date dateDue = ...;
String sql = "INSERT INTO cost_tran_sched (date_due, ...) "
+ "SELECT ${sql.date(" + dateDue + ")}, ... ";
SqlUtils.executeUpdate("cost_tran", sql);
The SqlUtils class also provides some of the helper methods ported from the sup.abs, for example:
String makeLiteralOrBlank(String value)
String formatSqlReplace0WithHuge(String value)
See additional help topics:
Querying records
Basic Script code queries records using the RecordSet object:
Dim costTransRecurId As String
Dim rs As New RecordSet
rs.Open "SELECT cost_tran_recur_id FROM cost_tran_recur WHERE ..."
Do While Not rs.EOF
costTranRecurId = rs.Fields('cost_tran_recur_id').Value
...
rs.MoveNext ' scroll the ResultSet to the next record
Loop
The corresponding Java code looks like this:
String table = "cost_tran_recur";
String[] fields = {"cost_tran_recur_id"};
DataSource ds = DataSourceFactory.createDataSourceForFields(table, fields);
// for all records retreived by the DataSource
for (DataRecord record: ds.getRecords()) {
String costTranRecurId = record.getString("cost_tran_recur.cost_tran_recur_id");
...
}
Field values retrieved by the DataSource are already converted to Java object types that match Archibus field types. For example, record.getString("cost_tran.date_paid") returns the java.util.Date object – not a string.
See additional help topics: Read Multiple Data Records
Batch updates
Both BasicScript API and Web Central API provide the FieldOperation and FieldFormula objects that perform batch updates of the owner table field values, based on the assigned table field values. These objects generate database-independent SQL queries.
FieldOperation
The FieldOperation object calculates total values of the assigned field using an aggregation operation, such as SUM or COUNT. The example below updates the Room Area for all Floor records by calculating the sum of Area of all Room records that are assigned to each floor.
Basic Script:
Dim fo As New FieldOperation
fo.Owner = "fl"
fo.Assigned = "rm"
fo.Calculate "fl.area_rm", "SUM", "rm.area"
Java:
FieldOperation fo = new FieldOperation();
fo.setOwner("fl");
fo.setAssigned("rm");
fo.calculate("fl.area_rm", "SUM", "rm.area");
FieldFormula
The FieldFormula object calculates assigned field values using an SQL expression.
BasicScript:
Dim ff As New FieldFormula
ff.Assigned = "fl"
ff.Calculate "fl.area_remain", "fl.area_gross_int – fl.area_rm"
Java:
FieldFormula ff = new FieldFormula();
ff.setAssigned("rm");
ff.calculate("fl.area_remain", "fl.area_gross_int – fl.area_rm");
Restricting assigned records
You can apply a restriction on the assigned records that are used in the calculation.
BasicScript:
fo.InpFilter = "rm.dp_id IS NOT NULL"
Java:
fo.setAssignedRestriction("rm.dp_id IS NOT NULL ");
Restricting owner records in FieldOperation
When using FieldOperation, you can also apply a restriction on the owner records that will be updated.
BasicScript:
fo.OutFilter = "bl.site_id = 'ND'"
Java:
fo.setAssignedRestriction("bl.site_id = 'ND'");
See additional help topic: Batch_Updates
Language differences
Function parameters and return values
In Basic Script, function arguments can be passed by reference (ByRef) or by value (ByVal). Arguments passed by reference can be changed inside the function, and the changed values will be visible to the function caller – they effectively work as additional function return values.
In Java, function arguments of the primitive types (eq.int, long, double, etc) are always passed by value. If they are changed inside the function, the changes are not visible to the function caller.
When function arguments of the simple object types (eq. String, Integer, Double) are passed to the function, the function gets not the object itself but a reference variable. The reference variable can be changed inside the function, but this change will not be visible outside of the function. In addition, most simple object types are immutable, which means that once the variable is created, its value or content cannot be changed.
The recommended Java practice is to only have a single return value. If the function must return multiple values, it can return a java.util.Collection or a custom Java class instance.
Constants
Basic Script:
Const kElevatorMsg$ = "Elevator"
Java
static final String kElevatorMsg = "Elevator";
Strings
Basic Script string literals can contain the back-slash character: "Division\Department"
In Java this character is used to escape other special characters, so the same string would look like: "Division\\Department"
String concatenation
Basic Script:
message = "Work request " & wr_id
Java:
String message = "Work request " + wr_id