Once we have created the page and bean corresponding to it, its quite easy to
Insert Record in the database table referred by EO. Following code can be used to insert the records:
ApplicationModule am = Configuration.createRootApplicationModule(AMName, ConfigName);
ViewObject myVO= am.findViewObject("MyViewObject1");
Row newEmp = myVO.createRow();
myVO.insertRow(newEmp);
newEmp.setAttribute("Ename","Steve");
newEmp.setAttribute("Empno", new Number(1234));
am.getTransaction().commit();
*You can simply write bc4jclient and press ctrl+Enter and Application module code will be generated automatically.
Another important feature is to provide your
Application Module as a Webservice. This can easily be done using following line of codes.
Scenario: Suppose you want to provide a service method to find the Employee's year of service.We will create a new method in AM say 'EmpServiceYears' and will bind the where clause of VO to employees id passed by employee.
public long EmpServiceYears(long empId) {
ViewObject emps = findViewObject("Employee1");
emps.setWhereClause("empno = :0");
emps.setWhereClauseParam(0, new Number(empId));
emps.executeQuery();
Row emp = emps.first();
return emp == null ? 0 : ((Number)emp.getAttribute("YearsOfService")).longValue();
}
After writting code, deploy it as J2EE web service and select the method to include in client interface panel of AM.
Following code can help to find an Entity object based on primary key:
// Custom method in AppModuleImpl.java
public String findServiceRequestStatus(long requestId) {
String entityName = "devguide.model.entities.ServiceRequest";
// 1. Find the entity definition for the ServiceRequest entity
EntityDefImpl svcReqDef = EntityDefImpl.findDefObject(entityName);
// 2. Create the key
Key svcReqKey = new Key(new Object[]{requestId});
// 3. Find the entity object instance using the key
EntityImpl svcReq = svcReqDef.findByPrimaryKey(getDBTransaction(),svcReqKey);
if (svcReq != null) {
// 4. Return the Status attribute of the ServiceRequest
return (String)svcReq.getAttribute("Status");
}
else {
return null;
}
}
To create a new Entity Row:
//Custom method in AppModuleImpl.java
public long createProduct(String name, String description) {
String entityName = "devguide.model.entities.Product";
// 1. Find the entity definition for the Product entity
EntityDefImpl productDef = EntityDefImpl.findDefObject(entityName);
// 2. Create a new instance of a Product entity
EntityImpl newProduct = productDef.createInstance2(getDBTransaction(),null);
// 3. Set attribute values
newProduct.setAttribute("Name",name);
newProduct.setAttribute("Description",description);
try {
// 4. Commit the transaction
getDBTransaction().commit();
}
catch (JboException ex) {
getDBTransaction().rollback();
throw ex;
}
// 5. Access the database trigger assigned ProdId value and return it
DBSequence newIdAssigned = (DBSequence)newProduct.getAttribute("ProdId");
return newIdAssigned.getSequenceNumber().longValue();
}
To access the Application Module Client Interface in a JSF Backing Bean:
public class YourBackingBean {
public String commandButton_action() {
// 1. Access the FacesContext
FacesContext fc = FacesContext.getCurrentInstance();
// 2. Create value binding for the #{data} EL expression
ValueBinding vb = fc.getApplication().createValueBinding("#{data}");
// 3. Evaluate the value binding, casting the result to BindingContext
BindingContext bc = (BindingContext)vb.getValue(fc);
// 4. Find the data control by name from the binding context
DCDataControl dc = bc.findDataControl("SRServiceDataControl");
// 5. Access the application module data provider
ApplicationModule am = (ApplicationModule)dc.getDataProvider();
// 6. Cast the ApplicationModule to its client interface
SRService service = (SRService)am;
// 7. Call a method on the client interface
service.doSomethingInteresting();
return "SomeNavigationRule";
}
}
To get Application module instance independent of EO or VO::
/***
* This method returns the current instance of the session DBTransaction object.
* The method below is implemented from a Singleton Object for utilitarian purposes.
*/
public static synchronized DBTransaction getDBTransaction(){
FacesContext ctx = FacesContext.getCurrentInstance();
ValueBinding vb = ctx.getApplication().createValueBinding("#{data}");
BindingContext bc = (BindingContext)vb.getValue(ctx.getCurrentInstance());
DataControl dc = bc.findDataControl("MyApplicationModuleControl");
ApplicationModuleImpl am = ((ApplicationModuleImpl)(ApplicationModule)dc.getDataProvider());
return am.getDBTransaction();
}
.......................
Reference: ADF Developer’s Guide For Forms/4GL Developers