Support > Repository > Business logic > Model basics (1)
ja | en

I will explain how Wagby's model is structured. I will explain how to operate the basic store model among them.

The database table definition (CREATE DDL statement) and the class definition (Java class) are automatically generated from the design information (repository) of "model" entered in Designer.This relationship is shown in Figure 1.

Figure 1 Relationship between model definitions and tables, class diagrams

All "models" can be operated as Java objects.From the viewpoint of a relational database, one data (one record) becomes the entity of one model.

This entity is called an "object".

Sometimes we call it "instance".In this manual, it is unified by the name of "object".

The script can directly manipulate the object of interest.For example, to handle the model ID customer, you can use a variable name "customer" directly in the script.This is an object.

"Item ID" can be referred to as "object name +". "+ Item ID".For example, if you have prepared the item name, you can write "customer.name".

"Item ID" isCamel notationUse.For example, customer.kana_name is described as customer.kanaName in the script.

It was defined in the Designer from the "model", a plurality of classes shown in FIG. 2 is generated.

Figure 2 Types of models
Classification name Description
container Store model There is a 1: 1 relationship with database records.
Condition model Store search conditions.
List model Store list display items.
Presentation model Presentation models are prepared for each store/condition/list.A model corresponding to Web form (input form displayed on screen).All values ​​are string type.
operation service Perform business processing.
Data manipulation (DAO) Perform basic exchange (CRUD) between the store model and the database.
Helper Processing accompanying model operations such as model initialization, calculation, and resolution of reference linking is performed.It also converts between models and presentation models.
DAO: Abbreviation for Data Access Object.[Wikipedia...]
CRUD: Abbreviation for Create/Read/Update/Delete.It shows basic operations such as registration, retrieval, update, and deletion of data.[Wikipedia...]
JAXB: Abbreviation for Java Architecture for XML Binding.Converts between Java and XML.[Wikipedia...]
Wagby Framework: Framework bundled with Wagby.Provide the base part of each automatically generated class.
Spring: Spring Framework.Standard foundation technology for enterprise application development by Java.[Wikipedia...]

In the script, it can be used with the following variable name.

nameRepresentation in scriptAn exampleDescription
Store model Model name customer Object stored in the database.
Presentation model Model name _ p customer_p Value entered from web form.
Condition model Model name _ c customer_c An object that stores search conditions.
Presentation model of condition Model name _ cp customer_cp The value of the search condition entered from the Web form.
List model Model name _ l customer_l An object that contains the values ​​to be listed.
List presentation model Model name _ lp customer_lp For screen output of list display object.(Input is not prepared)
Objects other than the store model may not exist.For example, if it is not a search screen operation, the condition model is not included.
Depending on the situation, it may not be possible to acquire a store model.For example, in the case of deletion processing, no store model is provided.

The store model is an object for storing information of "1 record" of "table".It can be used in most scripts.

I extract a part of the code generated by Wagby.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "customer")
@XmlType(propOrder={ "customerid_", "name_", "namekana_", "email_", "tel_", "fax_", "companyname_", "companynamekana_", "companytel_", "companyfax_", "companyzipcode_", "companyaddress_", "companyurl_", "officename_", "officenamekana_", "officetel_", "officefax_", "officezipcode_", "officeaddress_", "keyperson_", "retiredate_", "note_" })
public class Customer extends jp.jasminesoft.jfc.app.ContainerBase<Customer> implements java.io.Serializable, Cloneable {
    @XmlElement(name="customerid")
    private int customerid_;
    @XmlElement(name="name")
    private String name_;
    ...
    public final String getName() {
        return (name_);
    }
    public final void setName(String name) {
        this.name_ = name;
    }
    ...
}

Characteristic

  • "Item ID" corresponds to "Field" (of Java class).
  • All fields are declared private.
  • An accessor (setter/getter) method corresponding to the field is prepared.
  • Annotations beginning with "@ Xml" are provided by JAXB.This realizes mutual conversion between objects and XML expressions.(Example: An xml file exchanged by Wagby's import export function is realized by this annotation.)
  • The relational database table and column correspond to the store model class name and field name respectively.This correspondence processing is managed by middleware called Hibernate (included in Wagby).[Wikipedia...]

Class name, field name naming convention

name of the class

  • Make the model ID capital letter.If originally capitalized, it is intact.
  • Remove the underscore (_) and capitalize the next letter.Sales_slip is SalesSlip.

Field name

  • A field corresponding to the item ID is prepared.
  • Include an underscore at the end of the item name.Id is id_.
  • Remove the underscore (_) and capitalize the next letter.Customer_id is customerId_.
  • A getter to get the data and a setter method to set the value are prepared.For example, getCustomerId () or setCustomerId (argument).
  • When working from scripts, getter/setter methods are always used.For example, when you write customer.customerId in a script, the getCustomerId () method is called internally.In other words, we do not manipulate the fields of the model directly in the script.

Java type

The relationship between the type specified in Designer and the type of Java used in the actually generated code is as follows.

Type specified in Designer Java type
Integer type Integer (non-mandatory)
Int (required)
1, 2, 4, 8 byte integer Byte, Short, Integer, Long (non-essential)
Byte, short, integer, long (required)
4 byte decimal Float (non-essential)
Float (required)
8 byte decimal Double (non-mandatory)
Double (required)
String, file, mail, URL, zip code String
date java.sql.Date
Times of Day java.sql.Time
Date Time java.sql.Timestamp
The date and time are java.sql package, not java.util package.
There is no definition (from Wagby Designer) that corresponds to Java's boolean type.Replace with int.
There is no definition (from Wagby Desginer) that corresponds to the Java BigInteger/BigDecimal class.Functions provided by WagbyADD,SUB,MUL,DIVInternally performs calculation using BigInteger.The result is saved as long or double type.

Handling of mandatory items

If you set "Required" in Designer, the generated code will change.

Type of Designer Classification Java type initial value Confirmation method not yet entered table
Integer type Non-mandatory Integer null Check method NOT NULL is granted
Integer type Required int 0 None -

If integral items are non-mandatory, a check method is provided.Specifically, it is a method called check item ID ().The return value is of type boolean, the value istrueOrfalseIt is either.You can use this to check whether it is not entered.

When the get method (get item ID) is used, "-1" is returned if the value is null.

Difference between Integer and int/Difference between null and 0

Integer and int are handled differently.Although Integer has a state null (uninputted), int always has some value, it does not allow null.The initial value of int is 0, but it can not be determined whether the input value is 0 or not input yet the initial value is 0.

The table definition changes

If it is a required item, the table definition of that model is changed.NOT NULL is added to the corresponding item.

A single value and an array

The way the script handles depends on the model item type.

Item typeTreatment
String type, numeric type, date type, model reference (list box, radio button, search)Have one value
Model reference (check box), repeating item, repeating container itemIt becomes an array

I will explain the details later.

Prepare the customer model, set the execution timing to "registration" with "Screen> Script" and enter the following script code.

var customerid = customer.customerid; /* 主キー(整数型) */
var name = customer.name; /* 文字列型 */
var companyname = customer.companyname; /* モデル参照 */
var companynamekana = customer.companynamekana; /* 参照連動 */
var retiredate = customer.retiredate; /* 日付型 */

stdout.println("customerid="+customerid);
stdout.println("name="+name);
stdout.println("companyname="+companyname);
stdout.println("compnaynamekana="+companynamekana);
stdout.println("retiredate="+retiredate);

When the customer model registration is successful, the console running Tomcat will display as follows.

Customerid = 1000 name = Taro Yamada companyname = 1000 compnaynamekana = Sharpness retardate = 2010-03-31

For numbers, character strings and dates, the values ​​stored in the database are output as they are.

The model reference item companyname shows that the "primary key" of the referenced model is stored.Also, you can see that the reference linked item companynamekana contains the reference data.If you do not refer to it, the value of companyname is "-1".(When the primary key type item of the referenced model is an integer type)

The details of model reference items are explained below.

[Example] Set the value

The above example was to display the value that was originally stored.Here is a script to set the value.

customer.age = 20; /* 整数型 */
customer.name = "鈴木 一郎"; /* 文字列型 */
customer.retiredate = ExcelFunction.DATEVALUE("1972-5-15"); /* 日付型 */

An integer type item can specify a numerical value directly.
For string type items, specify values ​​with double quotation marks.Japanese characters can also be specified.
Date type items specify Java date objects.Here, functions provided by Wagby DATEVALUE We will introduce examples using.To use a function in a script, give the prefix ExcelFunction.

For one store model, one DAO (Data Access Object) is prepared. You can read one data from the database as an object using the get method provided by DAO.Here is an example of usage in script.

// 顧客ID(主キー)が1000の顧客データを読み込む。
var customerDao = p.appctx.getBean("CustomerDao");
var customer = customerDao.get(1000);

The entity of DAO is an object of Java.In p.appctx.getBean ("model IDDao") ;, obtain the object.

In a script, we usually do not get a customer object using DAO or (using Java's new operator).In many cases the script is invokedBeforeWe have created objects from the values ​​Wagby read from the database and are ready for use in scripts at once.What are the objects currently handled?anotherPlease obtain the value from DAO when it is necessary to use the object.In this case, you need the primary key of the data you want to retrieve.

Repeat item

When "Repeat" is specified in Designer, it is internally handled as follows.

  • Repeat items can be specified as "character string" "number" "date, time, date time".
  • Internally it holds multiple values ​​in the collection called List.
    @ XmlElement (name = "tel") private java.util.List <String> tel _ = new java.util.ArrayList <String> ();
    
  • A separate table is prepared in the database.For example, when the item tel of the customer model is repeatedly specified, a separate table "customer $ tel" is prepared.
  • When an object is acquired from DAO, the value is in the List.(That is, table join is always done.)

Repeat container

If you specify "repeating container" in Designer, it is internally handled as follows.

  • Repeating containers become independent classes (container names).For example, when preparing an item called a repeating container "cont", a class called Cont is generated.
  • Internally it holds multiple containers in the collection called List.
    @ XmlElement (name = "cont") private java.util.List <Cont> cont_ = new java.util.ArrayList <Cont> ();
    
  • A separate table "customer $ cont" is prepared in the database.It will automatically become a foreign key relationship.
  • When an object is acquired from DAO, the value is in the List.(That is, table join is always done.)
  • Nesting of repeated containers is not possible.
Because of this constraint, the problem of performance degradation due to deep nest structure does not occur.

Script example: reference

In scripts, repeating containers can be treated as "arrays". The acquired repeating container can be operated as an object like a model.

The following example is a script that gets the repeating container "reason (memo)" included in the leave model.

var array = leave.memo; /* memoは繰り返しコンテナ。arrayは配列形式となっている。*/
for (var i=0; i<array.length; i++) {
    stdout.println("memo="+array[i]);
    stdout.println("memo.memo_id="+array[i].memoId);
    stdout.println("memo.memo_reason="+array[i].memoReason);
}

Script example: seek sum

The next example is a code that manipulates "precord" contained in the sales slip model (salesslip) and finds the sum of unit price and quantity.

var total = 0;
var array = salesslip.precord; /* 繰り返しコンテナ */
for (var i=0; i<array.length; i++) {
    if (条件が成立) {
        total = total + (array[i].PPrice * array[i].PNumber);
    }
}
salesslip.totalPrice = total; /* 値を代入(セット)*/

As shown in the above code, you can assign the calculation result to the object (model item).

Wagby provides as standard SUM functionIt can also correspond to complicated business rules (conditional expressions for computation as shown above) which are difficult to describe.

Script example: Add new

The next example is a script to add three repetitive containers "precord" at the timing of opening the quotation model new registration screen.

var PRecordClass = Java.type("jp.jasminesoft.wagby.model.quotation.Precord");
var array = new Array(3);
array[0] = new PRecordClass();
array[1] = new PRecordClass();
array[2] = new PRecordClass();

(各配列への、具体的な値の設定コードは省略)

quotation.precord = array;
Java.type is a special notation.By writing Java.type (class name in FQCN format), you can prepare a variable representing Java class.With this variable you can now create an object with the new operator.
FQCN: Abbreviation for Fully Qualified Class Name.It is expressed as a fully qualified class name and class given a package name.

transaction

A repeating container is a separate table (on the database), but in the store model, the structure information contains item information in one piece of data.Here, registration, update, and deletion of models that contain repeating containers are performed in sets of two tables.That is, implicit transaction processing is done.

Figure 3 Relationship between Java class and table

Remarks on deletion

In the above figure, if you delete the order model, the order item is also deleted at the same time.In other words, repeating containers are strongly connected.

Model reference (list box, radio button, search window) holds the primary key of the model you are referring to.

Figure 4 Model reference relationship

When importing a model from "order DAO", the value of "customer ID" as a foreign key (eg "1000") is included, but the information of "customer" itself is not included.By using the customer ID as an argument, "customer" data can be acquired by using "customer DAO".

var customerDao = p.appctx.getBean("CustomerDao");
var customer = customerDao.get(order.customerid);

Remarks on deletion

Even if you delete the "order" model, the "customer" model associated with the key will not be deleted. On the other hand, if you delete the "customer" model, the reference destination of the "order" model becomes undefined.

For model reference (check box), hold the primary key of the referencing model in the List.

Fig. 5 Model reference relation (check box)

When loading a model from "Questionnaire DAO", a list containing the value of "Option ID" which is a foreign key is prepared.In a script, you can get this as an array.

Reference value

In the enq model, a checkbox item options is prepared.The code for checking what kind of value is as follows.

var array = enq.options; /* チェックボックスは配列として取得できる */
...
for (i=0; i<array.length; i++) {
  stdout.println("option="+array[i]);
}

Only those checked are included in the array array.For example, it is displayed as follows.

... option = 1 ...

Even for values ​​that are arrayed like this, always prepare "var type" in scripts. Since it is an array, you can refer to the value using a for loop or the like.

Acquire the content part, not the ID

As shown in the above code, the store model does not include the information of "option" itself.You can obtain "choice" data using "choice DAO".

var optionDao = p.appctx.getBean("OptionDao");
var array = enq.options;
for (int i=0; i<array.length; i++) {
  var option = optionDao.get(array[i]);
  stdout.println(option.content);/* 内容部を表示する */
}

Manipulate checkboxes individually

You can add (delete) an ID (checkbox) to enq.options.Use addXXX, removeXXX, clearXXX, setXXX, size methods.These are methods that are always generated automatically.

/* test 1:値を一つずつ追加する */
enq.addOptions(1);
enq.addOptions(2);
stdout.println("test1 "+enq.options);

/* test 2:値を消去する */
/* 添字は0から始まる。以下は最初にチェックされていたものを解除する */
enq.removeOptions(0);
stdout.println("test2 "+enq.options);

/* test 3:すべての値を消去する */
enq.clearOptions();
stdout.println("test3 "+enq.options);

/* test 4:複数の値をまとめてセットする */
var IntArrayType = Java.type("int[]");
var arr = new IntArrayType(3);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
enq.setOptions(arr);
stdout.println("test4 "+enq.options);

/* いくつチェックされているかを知る */
var size = enq.sizeOptions();
stdout.println("size="+size);

In parent-child relationship, a relationship called child (N) is established for parent (1).There is no special description in the parent, and the child holds the key of the parent.

Figure 6 Foreign key

From this, there is no difference in class definition between Wagby's "model reference" and "foreign key".By specifying "Foreign key", a function such as displaying a list of children on the parent detail screen is automatically given.

Remarks on deletion

By default, if you delete the "parent" model, the "child" model attached with the key will also be deleted at the same time.HoweverDepending on the setting, you can also leave the child model.Parent-child relationship in this case is a weak reference relationship.

Reference linkage is the function of posting the value of the reference source model to the reference destination.For automatically generated code, data is read from DAO and set value processing is done.

See Figure 7 Interlocking

Role of store model

The store model can include not only "information held in the database" but also "information displayed on the screen".Reference linkage is a mechanism prepared for that.

Figure 8 Role of store model
  • File type internally consists of two fields (and also two columns on the table).
  • The file name is entered in the item name defined in Designer.
  • "Item name _jshfilename" automatically prepared internally stores the actual path information (character string) of the file.
  • File information itself is not managed as BLOB.It is managed as a file name and a real path to the last.Therefore, if the physical file is deleted, a reference error occurs.

By checking the automatically generated Java source code directly, you can know what kind of method is prepared.

For example, the store model for the customer model is the following file.

wagbydesigner/webapps/wagbydesigner/WEB-INF/env/work/srcgen/jp/jasminesoft/wagby/model/customer/Customer.java
"The folder corresponding to the package name (jp/jasminesoft/wagby in this example)" in the above file is changed in the project where the package name is specified.Please read it properly.

For each classification, the folder of the source code of the generated model changes.(The starting point of the folder is wagbydesigner/webapps/wagbydesigner/WEB-INF/env/work/srcgen.)

Here is an example when model ID is taken as customer.

Store model
FQCNjp.jasminesoft.wagby.model.customer.Customer
Filejp/jasminesoft/wagby/model/customer/Customer.java
Presentation model
FQCNjp.jasminesoft.wagby.model.customer_p.CustomerP
Filejp/jasminesoft/wagby/model/customer_p/CustomerP.java
Condition model
FQCNjp.jasminesoft.wagby.model.customer_c.CustomerC
Filejp/jasminesoft/wagby/model/customer_c/CustomerC.java
Presentation model of condition
FQCNjp.jasminesoft.wagby.model.customer_cp.CustomerCp
Filejp/jasminesoft/wagby/model/customer_cp/CustomerCp.java
List model
FQCNjp.jasminesoft.wagby.model.customer_l.CustomerL
Filejp/jasminesoft/wagby/model/customer_l/CustomerL.java
List presentation model
FQCNjp.jasminesoft.wagby.model.customer_lp.CustomerLp
Filejp/jasminesoft/wagby/model/customer_lp/CustomerLp.java
FQCN: Abbreviation for Fully Qualified Class Name.It is expressed as a fully qualified class name and class given a package name.
Repetitive containers are also independent classes, so they can be expressed as above.For example, if it is a repeat container cont of the customer model, it is jp.jasminesoft.wagby.model.customer.Cont.The file is jp/jasminesoft/wagby/model/customer/Cont.java.