Skip to content

Quickstart Guide

Ryu Xin edited this page Oct 17, 2022 · 9 revisions

What you'll build

With this Quickstart Guide, you will complete:

  1. Definition of an extension point
  2. The custom logic implementation of the extension point by the business package
  3. Start Lattice and complete the call to the extension point

What you’ll need

  1. An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
  2. A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.

Maven dependency

<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-model</artifactId>
      <version>1.0.12</version>
</dependency>
<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-runtime</artifactId>
      <version>1.0.12</version>
</dependency>

Step 1: Define the "custom item unit price" extension point

Define a "custom item unit price" extension point, allowing the business to implement this extension point and return the custom unit price of the commodity.

public interface OrderLinePriceExt extends IBusinessExt {

    String EXT_ORDER_LINE_CUSTOM_UNIT_PRICE = "OrderLinePriceExt.EXT_ORDER_LINE_CUSTOM_UNIT_PRICE";

    @Extension(
            code = EXT_ORDER_LINE_CUSTOM_UNIT_PRICE,
            name = "Custom the Item's unit price of OrderLine",
            reduceType = ReduceType.FIRST
    )
    Long getCustomUnitPrice(OrderLine orderLine);
}

The extension point is identified with the @Extension annotation, and is assigned a globally unique extension point code, and the Reduce policy of the extension point is declared as FIRST. (Reduce strategy can be ignored in this chapter, and it will be explained in another article later)

Step 2: Define the "OrderLine Price Management" ability

@Ability(name = "OrderLine's Price Ability")
public class OrderLinePriceAbility extends BaseLatticeAbility<OrderLinePriceExt> {

    public OrderLinePriceAbility(OrderLine bizObject) {
        super(bizObject);
    }

    public Long getCustomUnitPrice(OrderLine orderLine) {
        return Optional.ofNullable(reduceExecute(
                        p -> p.getCustomUnitPrice(orderLine),
                        Reducers.firstOf(Objects::nonNull)))
                .orElse(orderLine.getUnitPrice());
    }

    @Override
    public BlankOrderLinePriceExt getDefaultRealization() {
        return new BlankOrderLinePriceExt();
    }
}

The definition of ability is marked with the @Ability annotation, and it is necessary to declare that the current set of extension points for capability management is OrderLinePriceExt. And, through the getDefaultRealization() method, a default implementation is provided.

Ability to use the reduceExecute() method to try to load the custom implementation logic of the business for the "custom item unit price" extension point.

Step 3: Write business custom implementation logic.

We define a business A, marked with the @Business annotation, and define the code of the business as "business.a", as follows:

@Business(code = "business.a", name = "Business A")
public class BusinessA extends BusinessTemplate {
}

Then, for business A, define its item custom unit price as 2000 (cent).

@Realization(codes = "business.a")
public class BusinessAExt extends BlankOrderLinePriceExt {
    @Override
    public Long getCustomUnitPrice(OrderLine orderLine) {
        return 2000L;
    }
}    

The custom implementation logic of the business is identified with @Realization and the business code is "business.a"

We can use a similar method to define another business B. At the same time, business B does not customize the unit price of the product, as follows:

@Business(code = "business.b", name = "Business B")
public class BusinessB extends BusinessTemplate {
}

@Realization(codes = "business.b")
public class BusinessBExt extends BlankOrderLinePriceExt {
    //do nothing.
}

Step 4: Start Lattice and invoke the extension point

public class LatticeQuickStart {

    public static void main(String[] args) {
        Lattice.getInstance().setSimpleMode(true);
        Lattice.getInstance().start();

        doBusinessA();
        doBusinessB();

    }

    private static void doBusinessA() {
        OrderLine orderLine = new OrderLine();
        orderLine.setUnitPrice(1000L);
        orderLine.setBizCode("business.a");
        OrderLinePriceAbility ability = new OrderLinePriceAbility(orderLine);
        Long unitPrice = ability.getCustomUnitPrice(orderLine);
        System.out.println("[Business A] unit price: " + unitPrice);
    }

    private static void doBusinessB() {
        OrderLine orderLine = new OrderLine();
        orderLine.setUnitPrice(1000L);
        orderLine.setBizCode("business.b");
        OrderLinePriceAbility ability = new OrderLinePriceAbility(orderLine);
        Long unitPrice = ability.getCustomUnitPrice(orderLine);
        System.out.println("[Business B] unit price: " + unitPrice);
    }
}

We simulate two business calls respectively, in which the default unit price of the item (the default price after the product is published in the database) is 1000L. Because business A has customized the unit price of the product, the final return value should be the customized 2000L; while business B has not done any customization, the return value should still be 1000L.

The console print result of running org.hiforce.lattice.sample.LatticeQuickStart#main is as follows:

[Business A] unit price: 2000
[Business B] unit price: 1000

Sample code URL

The sample code can be obtained by visiting: https://github.com/hiforce/lattice-sample/tree/main/lattice-quick-start


中文版:https://www.ryu.xin/2022/09/23/lattice-quickstart-guide/