Example of simple ordering (booking of items) application. Part 1, basic business logic.

Models.

This is a simple example, and so do these models:

  • Customer - has name

  • Merchandise - has name

  • Customer order - has date, customer, total, lines

  • Customer order's line - has merchandise, quantity, price, amount

Creating Maven project.

* All files must be in the UTF-8 format. I recommend Jeany editor.

Create a project folder "myapp" in the "programmer/java", then create a pom.xml in it:

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>


<groupId>org.myapp</groupId>

<version>1.0-SNAPSHOT</version>

<artifactId>ordering</artifactId>

<packaging>jar</packaging>


<name>My ordering business logic.</name>

<inceptionYear>2019</inceptionYear>


<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.7</java.version>

</properties>


<dependencies>

<dependency>

<groupId>org.beigesoft</groupId>

<artifactId>beige-blc</artifactId>

<version>1.0</version>

<exclusions>

<exclusion>

<groupId>com.zaxxer</groupId>

<artifactId>HikariCP</artifactId>

</exclusion>

</exclusions>

</dependency>

</dependencies>


<build>

<finalName>${project.artifactId}</finalName>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-install-plugin</artifactId>

<version>2.5.2</version>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-deploy-plugin</artifactId>

<version>2.8.2</version>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-resources-plugin</artifactId>

<version>2.7</version>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.1</version>

<configuration>

<useSystemClassLoader>false</useSystemClassLoader>

<useFile>false</useFile>

<trimStackTrace>false</trimStackTrace>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>${java.version}</source>

<target>${java.version}</target>

<compilerArgs>

<!--<arg>-verbose</arg>-->

<arg>-Xlint:all,-options,-path</arg>

</compilerArgs>

</configuration>

</plugin>

</plugins>

</build>

</project>

*pom.xml is the main file of any Maven project. It describes about this project artifact: its name, group, version, packaging (JAR in this case), used 3-d party libraries (dependencies). Any JAR file is a standard Java archive, that contains of all compiled Java classes and other files - properties in text or XML files, images, etc.

Then create these folders for the source code:

myapp - src - main - java - org - myapp - ordering

myapp - src - main - resources - dbcp

myapp - src - main - resources - sqlite

myapp - src - main - resources - uvd - clsCs

myapp - src - main - resources - uvd - fldTyFs

Implementing the models.

According to Basic information about using Beigesoft™ libraries you should use org.beigesoft.mdlp.AIdLnNm as the basic model for the "Customer". So, create a file "Customer.java" in the folder "myapp - src - main - java - org - myapp - ordering":

package org.myapp.ordering;


import org.beigesoft.mdlp.AIdLnNm;


public class Customer extends AIdLnNm {


}

* any Java class has fields (properties), e.g. for the "AIdLnNm", it's the nme property of type String that holds Name value. These properties is accessed via methods Getters and Setters, e.g. for the "nme" property, the "getNme()" method returns its value, and setNme(String pValue) sets its value.

* Java word extends means that this class extends another class (AIdLnNm in this case). As a result, an extending class inherits (will have) the all fields and methods of the extended class.

* as you can notice, a class name is the same as its file name, a package name matches its path from the "java" folder.

Class in this case is a data model of the business object "Customer". Another class can be a "service" that makes something, e.g. a service that saves a model into a database.

Assume, we have several databases (warehouse A, B) and a lot of goods, so we use org.beigesoft.mdlp.AOrIdNm for the "Merchandise":

package org.myapp.ordering;


import org.beigesoft.mdlp.AOrIdNm;


public class Merchandise extends AOrIdNm {


}

We need to import orders into the central database, so it will be:

package org.myapp.ordering;


import java.util.List;

import java.util.Date;

import java.math.BigDecimal;


import org.beigesoft.mdlp.AOrId;


public class COrder extends AOrId {


private Date itsDate;

private Customer customer;

private BigDecimal itsTotal;

private List<OrderLine> itsLines;


//Simple getters and setters:

/**

* <p>Getter for itsDate.</p>

* @return Date

**/

public final Date getItsDate() {

return this.itsDate;

}


/**

* <p>Setter for itsDate.</p>

* @param pItsDate reference

**/

public final void setItsDate(final Date pItsDate) {

this.itsDate = pItsDate;

}


/**

* <p>Getter for customer.</p>

* @return Customer

**/

public final Customer getCustomer() {

return this.customer;

}


/**

* <p>Setter for customer.</p>

* @param pCustomer reference

**/

public final void setCustomer(final Customer pCustomer) {

this.customer = pCustomer;

}


/**

* <p>Getter for itsTotal.</p>

* @return BigDecimal

**/

public final BigDecimal getItsTotal() {

return this.itsTotal;

}


/**

* <p>Setter for itsTotal.</p>

* @param pItsTotal reference

**/

public final void setItsTotal(final BigDecimal pItsTotal) {

this.itsTotal = pItsTotal;

}


/**

* <p>Getter for itsLines.</p>

* @return List<OrderLine>

**/

public final List<OrderLine> getItsLines() {

return this.itsLines;

}


/**

* <p>Setter for itsLines.</p>

* @param pItsLines reference

**/

public final void setItsLines(final List<OrderLine> pItsLines) {

this.itsLines = pItsLines;

}

}

* To avoid names collisions (with SQL special words), you should name entities and its fields carefully, e.g. if you name this entity as "Order", then it will be the error.

Its line will be:

package org.myapp.ordering;


import java.math.BigDecimal;


import org.beigesoft.mdl.IOwnedOr;

import org.beigesoft.mdlp.AOrId;


public class OrderLine extends AOrId implements IOwnedOr<COrder> {

private COrder ownr;

private Merchandise product;

private BigDecimal itsQuantity;

private BigDecimal itsPrice;


private BigDecimal itsAmount;


/**

* <p>Getter for ownr.</p>

* @return COrder

**/

@Override

public final COrder getOwnr() {

return this.ownr;

}


/**

* <p>Setter for ownr.</p>

* @param pOwnr reference

**/

@Override

public final void setOwnr(final COrder pOwnr) {

this.ownr = pOwnr;

}


//Simple getters and setters:

/**

* <p>Getter for product.</p>

* @return Merchandise

**/

public final Merchandise getProduct() {

return this.product;

}


/**

* <p>Setter for product.</p>

* @param pProduct reference

**/

public final void setProduct(final Merchandise pProduct) {

this.product = pProduct;

}


/**

* <p>Getter for itsQuantity.</p>

* @return BigDecimal

**/

public final BigDecimal getItsQuantity() {

return this.itsQuantity;

}


/**

* <p>Setter for itsQuantity.</p>

* @param pItsQuantity reference

**/

public final void setItsQuantity(final BigDecimal pItsQuantity) {

this.itsQuantity = pItsQuantity;

}


/**

* <p>Getter for itsPrice.</p>

* @return BigDecimal

**/

public final BigDecimal getItsPrice() {

return this.itsPrice;

}


/**

* <p>Setter for itsPrice.</p>

* @param pItsPrice reference

**/

public final void setItsPrice(final BigDecimal pItsPrice) {

this.itsPrice = pItsPrice;

}


/**

* <p>Getter for itsAmount.</p>

* @return BigDecimal

**/

public final BigDecimal getItsAmount() {

return this.itsAmount;

}


/**

* <p>Setter for itsAmount.</p>

* @param pItsAmount reference

**/

public final void setItsAmount(final BigDecimal pItsAmount) {

this.itsAmount = pItsAmount;

}

}


Adding XML configuration for full database copy.

Just copy the conf.xml from the beige-blc/src/main/resources/dbcp into the "myapp - src - main - resources - dbcp", then add your models:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>My ordering DBCP configuration</comment>

<entry key="clss">org.beigesoft.mdlp.DbInf,

org.beigesoft.mdlp.UsTmc,

org.beigesoft.mdlp.UsRlTmc,

org.beigesoft.mdlp.Lng,

org.beigesoft.mdlp.Cntr,

org.beigesoft.mdlp.DcSp,

org.beigesoft.mdlp.DcGrSp,

org.beigesoft.mdlp.UsPrf,

org.beigesoft.mdlp.MaFrn,

org.beigesoft.mdlp.MaFrnLn,

org.beigesoft.mdlp.CsvMth,

org.beigesoft.mdlp.CsvCl,

org.beigesoft.mdlp.EmAdr,

org.beigesoft.mdlp.EmCon,

org.beigesoft.mdlp.EmInt,

org.beigesoft.mdlp.EmStr,

org.beigesoft.mdlp.EmMsg,

org.beigesoft.mdlp.EmRcp,

org.beigesoft.mdlp.EmAtch,

org.myapp.ordering.Customer,

org.myapp.ordering.Merchandise,

org.myapp.ordering.COrder,

org.myapp.ordering.OrderLine</entry>

<entry key="stgClsNms">exlFlds,idFlds</entry>

<entry key="stgFldNms"></entry>

<entry key="exlFlds">isNew</entry>

</properties>

The order is essential! E.g. the OrderLine is the last because of it contains a Merchandise and a COrder.

Adding XML configuration for Beige-ORM.

Copy the conf.xml from the beige-blc/sqlite into the "myapp - src - main - resources - sqlite", then add your models:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>My ordering SQLite configuration</comment>

<entry key="clss">org.beigesoft.mdlp.DbInf,

org.beigesoft.mdlp.UsTmc,

org.beigesoft.mdlp.UsRlTmc,

org.beigesoft.mdlp.Lng,

org.beigesoft.mdlp.Cntr,

org.beigesoft.mdlp.DcSp,

org.beigesoft.mdlp.DcGrSp,

org.beigesoft.mdlp.UsPrf,

org.beigesoft.mdlp.MaFrn,

org.beigesoft.mdlp.MaFrnLn,

org.beigesoft.mdlp.CsvMth,

org.beigesoft.mdlp.CsvCl,

org.beigesoft.mdlp.EmAdr,

org.beigesoft.mdlp.EmCon,

org.beigesoft.mdlp.EmInt,

org.beigesoft.mdlp.EmStr,

org.beigesoft.mdlp.EmMsg,

org.beigesoft.mdlp.EmRcp,

org.beigesoft.mdlp.EmAtch,

org.myapp.ordering.Customer,

org.myapp.ordering.Merchandise,

org.myapp.ordering.COrder,

org.myapp.ordering.OrderLine</entry>

<entry key="stgClsNms">exlFlds,idFlds,vrAlg,cnstr</entry>

<entry key="stgFldNms">def,nul</entry>

<entry key="exlFlds">isNew</entry>

</properties>

ORM means that you will not care about making SQL commands by yourself. For example to insert a new Merchandise into the database just write this code:

Merchandise product1 = new Merchandise();

product1.setNme("Product 1"); //ID will be generated automatically

this.orm.insIdLn(product1); //ORM makes SQL insert command by itself, and sets a new generated ID for this product

Then copy the cmnst.xml from the beige-blc/sqlite into the "myapp - src - main - resources - sqlite", then change the database name:

...

<entry key="dbUrl">jdbc:sqlite:#currentDir#myordering.sqlite</entry>

...


Adding XML configuration for Beige-WEB interface.

Copy the conf.xml from the beige-blc/uvd into the "myapp - src - main - resources - uvd", then add your models:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>UVD configuration</comment>

<entry key="clss">org.beigesoft.mdlp.UsTmc,

org.beigesoft.mdlp.UsRlTmc,

org.beigesoft.mdlp.Lng,

org.beigesoft.mdlp.Cntr,

org.beigesoft.mdlp.DcSp,

org.beigesoft.mdlp.DcGrSp,

org.beigesoft.mdlp.UsPrf,

org.beigesoft.mdlp.MaFrn,

org.beigesoft.mdlp.MaFrnLn,

org.beigesoft.mdlp.CsvMth,

org.beigesoft.mdlp.CsvCl,

org.beigesoft.mdlp.EmAdr,

org.beigesoft.mdlp.EmCon,

org.beigesoft.mdlp.EmInt,

org.beigesoft.mdlp.EmStr,

org.beigesoft.mdlp.EmMsg,

org.beigesoft.mdlp.EmRcp,

org.beigesoft.mdlp.EmAtch,

org.myapp.ordering.Customer,

org.myapp.ordering.Merchandise,

org.myapp.ordering.COrder,

org.myapp.ordering.OrderLine</entry>

<entry key="stgClsNms">exlFlds,idFlds,owdEnts,frmFds,lstFds,pickFds,selFds,selDpl</entry>

<entry key="stgFldNms">cnToSt,cnFrSt,inp,str,wde</entry>

<entry key="exlFlds"></entry>

</properties>

Then add this COrder.xml into the "myapp - src - main - resources - uvd - clsCs":

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="frmFds">iid,idOr,dbOr,itsDate,customer,itsTotal</entry>

<entry key="owdEnts">org.myapp.ordering.OrderLine</entry>

</properties>

This file describes which fields will be retrieved into the list and they order. It also says that owned lists will be listed in the form of this entity.

Then add this OrderLine.xml into the "myapp - src - main - resources - uvd - clsCs":

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="frmFds">iid,idOr,dbOr,product,itsQuantity,itsPrice,itsAmount</entry>

</properties>

Then copy the cnToSt.xml from the beige-blc into the "myapp - src - main - resources - uvd - fldTyFs" and add BigDecimal entry:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="java.util.Date">CnvDtTmStr</entry>

<entry key="java.math.BigDecimal">CnvPriStr</entry>

</properties>

This file says that it will be used "converter to string" (service) named "CnvPriStr" for all fields of type "BigDecimal".

Then copy the str.xml from the beige-blc into the "myapp - src - main - resources - uvd - fldTyFs" and edit it:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="java.lang.Long">str</entry>

<entry key="java.math.BigDecimal">str</entry>

<entry key="java.util.Date">dtTm</entry>

</properties>

This file describes which "HTML to string widget" (JSP) will be used for a field depending on its type.

Then copy the inp.xml from the beige-blc into the "myapp - src - main - resources - uvd - fldTyFs" and edit it:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="org.beigesoft.mdl.IHasNm">nme</entry>

<entry key="org.beigesoft.mdl.IHasId">hsid</entry>

<entry key="java.util.Date">dtTm</entry>

<entry key="java.lang.String">str</entry>

<entry key="java.lang.Long">int</entry>

<entry key="java.lang.Boolean">bln</entry>

<entry key="java.math.BigDecimal">pri</entry>

</properties>

This file describes which "HTML input widget" (JSP) will be used for a field depending on its type.

This is all about the basic business logic at this time.