Initialize data (data initialization ):
It is mainly used to add records. There are two methods:
1. Set the Default value of Page item during Design time.
2. Set it programmatically during Run time.
The setting of Default value is not mentioned here. It will be discussed later when "going deep into UI item.
This article mainly describes how to use programming methods to set up.
After EO is created, if you select to generate a JAVA file and a java class, we will find a <Your EO name> EOImpl. java node under EO,
After you double-click it, there are already some methods in it, mainly including the following:
DML Operation Class
Set Attribute Value Class
Get Attribute Value Class
Validation Class
Then we need to initialize it. It is obvious that we need to use the Set Attribute Value Class. These methods are named set + <Attribute Name>. For example:
Public void setHeaderId (Number value)
{
SetAttributeInternal (HEADERID, value );
}
The input parameter is value, and then the value is assigned to HEADERID using the setAttributeInternal method (note,
This HEADERID is the alias of EO Attributes. There are two methods to assign values to Attribute in EO:
SetAttribute (int index, String lang, object val)
Index indicates the position of the Attribute in EO (note that it is only sorted in the Attribute displayed on the Page). It can also be replaced by the alias of EO Attributes.
Val indicates the input value, and lang indicates the language, which can be omitted. For example:
Number price = new Number (100 );
SetAttribute (5, price );
According to FORM's habits, we may write setAttribute (5,100 ),
Note that this is incorrect, Because JAVA considers this 100 as the int type in the basic type, rather than the object type.
SetAttributeInternal (int index, Object val)
Except for no language distinction, it is consistent with setAttribute.
In this way, we can write a CLASS and set all the Attributes to be initialized as follows:
Public void setallAttribute ()
{
Number v_id = new Number (100 );
SetLineId (v_id );
String v_desc = new String ("ABCD ");
SetItemDesc (v_desc );
...
}
Then add the above method to one of the DML Operation class: create:
Public void create (AttributeList attributeList)
{
Super. create (attributeList );
SetallAttribute ();
}
In this way, when you execute the new record action on the Page, you will find that some fields have the initial value...
In the definition of an initial value, there are several special values:
1. Initialization of Primary Key
2. WHO field Initialization
3. fields need to be dynamically specified for initialization.
Primary Key initialization
Call OADBTransaction to obtain the Sequence Value and assign the Primary Key through SetAttribute (). The Code is as follows:
Public void create (AttributeList attributeList)
{
Super. create (attributeList );
OADBTransaction transaction = getOADBTransaction ();
Number lineid = transaction. getSequenceValue ("xx_order_line_s ");
SetLineId (lineid );
}
The above code is written in the create () method of EO. In this way, when you add an empty record, an initial value is added to the lineid field, that is, the sequence.
Value obtained. If you do not save this record, there will be a gap. Therefore, you can put this statement in the commit method.
WHO field Initialization
Under normal circumstances, if your EO contains the WHO field, OAF will automatically help you initialize the WHO field when you submit it. You do not need to write code,
If you need to obtain the WHO Information by yourself, you can still use the OADBTransaction method to obtain it, mainly including the following.
OADBTransaction transaction = getOADBTransaction ();
Transaction. getUserId ()
Transaction. getCurrentDBDate ()
Transaction. getResponsibilityApplicationId ()
Transaction. getResponsibilityId ()
Initialize a dynamic Value Field
In this case, we often encounter forms, that is, to extract a certain information from the database as the field value in real time.
If it cannot be extracted, an error message should be reported and the record cannot be saved. How can this problem be achieved in OAF?
I thought about the following methods:
1. Using VO (sometimes VO is equivalent to FORM Recordgroup), you need to dynamically define the WHERE condition for VO and obtain the VO query results.
2. Implemented Using JDBC or SQLJ. Both modes support using standard SQL statements directly in JAVA.
The VO method will wait until "going into VO". Now we will first study how to implement it using JDBC.
JDBC Developer's guide for http://download-uk.oracle.com/docs/cd/B10501_01/java.920/a96654/toc.htm Oracle.
We already know how to use the OADBTransaction method executeCommand () to execute an SQL statement,
However, it is limited to DML statements, such as Update, Delete, and Insert. here we need to execute an SQL query and return a result set.
Then, assign a field in the result set to an Attribute of EO. I think this will be very useful in future OAF development.
After research, the pseudo code is successfully implemented as follows:
--------------------------------------------------------------------
Import oracle. jdbc. OracleResultSet; // result set
Import oracle. jdbc. OracleConnection; // database connection
Import oracle. jdbc. OracleStatement; // statement
Import java. SQL. SQLException; // exception
Public void create (AttributeList attributeList)
{
Super. create (attributeList );
Try
{
OracleConnection conn = (OracleConnection) transaction. getJdbcConnection (); // obtain the current connection
OracleStatement stmt = (OracleStatement) conn. createStatement (); // generate a statement
OracleResultSet rset = (OracleResultSet)stmt.exe cuteQuery ("select msi. segment1 from mtl_system_items_ B msi"); // execute the query
While (rset. next () // forward transfer pointer
{SetItemDesc (rset. getString (1); // obtain the first field in the result set.
Break; // assume that only one record is obtained.
}
Rset. close (); // close the result set.
}
Catch (SQLException ex ){}
}
------------------------------------------------------------------------
Note: The JDBC Method usually throws an exception, which is java. SQL. SQLException. If no capture is performed, OAF reports an error.
By adjusting the SQL statements, we can implement more flexible functions.
Author: "Soy Milk"