Search in sources :

Example 1 with QRules

use of life.genny.rules.QRules in project rulesservice by genny-project.

the class EBCHandlers method initMsg.

public static void initMsg(final String msgType, String ruleGroup, final Object msg, final EventBus eventBus) {
    Vertx.currentContext().owner().executeBlocking(future -> {
        Map<String, Object> adecodedTokenMap = new HashMap<String, Object>();
        Set<String> auserRoles = new HashSet<String>();
        auserRoles.add("admin");
        auserRoles.add("user");
        QRules qRules = new QRules(eventBus, token, adecodedTokenMap);
        qRules.set("realm", ruleGroup);
        List<Tuple2<String, Object>> globals = RulesLoader.getStandardGlobals();
        List<Object> facts = new ArrayList<Object>();
        facts.add(qRules);
        facts.add(msg);
        facts.add(adecodedTokenMap);
        facts.add(auserRoles);
        User currentUser = new User("user1", "User1", ruleGroup, "admin");
        usersSession.put("user", currentUser);
        facts.add(currentUser);
        Map<String, String> keyvalue = new HashMap<String, String>();
        keyvalue.put("token", token);
        if (!"GPS".equals(msgType)) {
            System.out.println("FIRE RULES (" + ruleGroup + ") " + msgType);
        }
        // String ruleGroupRealm = realm + (StringUtils.isBlank(ruleGroup)?"":(":"+ruleGroup));
        try {
            RulesLoader.executeStatefull(ruleGroup, eventBus, globals, facts, keyvalue);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        future.complete();
    }, res -> {
        if (res.succeeded()) {
        // System.out.println("Processed "+msgType+" Msg");
        }
    });
}
Also used : User(life.genny.qwanda.entity.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QRules(life.genny.rules.QRules) Tuple2(io.vavr.Tuple2) JsonObject(io.vertx.core.json.JsonObject) HashSet(java.util.HashSet)

Example 2 with QRules

use of life.genny.rules.QRules in project rulesservice by genny-project.

the class EBCHandlers method processMsg.

public static void processMsg(final String msgType, String ruleGroup, final Object msg, final EventBus eventBus, final String token) {
    Vertx.currentContext().owner().executeBlocking(future -> {
        Map<String, Object> adecodedTokenMap = RulesLoader.getDecodedTokenMap(token);
        Set<String> auserRoles = KeycloakUtils.getRoleSet(adecodedTokenMap.get("realm_access").toString());
        User userInSession = usersSession.get(adecodedTokenMap.get("preferred_username").toString());
        String preferredUName = adecodedTokenMap.get("preferred_username").toString();
        String fullName = adecodedTokenMap.get("name").toString();
        String realm = adecodedTokenMap.get("realm").toString();
        String accessRoles = adecodedTokenMap.get("realm_access").toString();
        QRules qRules = new QRules(eventBus, token, adecodedTokenMap);
        qRules.set("realm", realm);
        List<Tuple2<String, Object>> globals = RulesLoader.getStandardGlobals();
        List<Object> facts = new ArrayList<Object>();
        facts.add(qRules);
        facts.add(msg);
        facts.add(adecodedTokenMap);
        facts.add(auserRoles);
        if (userInSession != null)
            facts.add(usersSession.get(preferredUName));
        else {
            User currentUser = new User(preferredUName, fullName, realm, accessRoles);
            usersSession.put(adecodedTokenMap.get("preferred_username").toString(), currentUser);
            facts.add(currentUser);
        }
        Map<String, String> keyvalue = new HashMap<String, String>();
        keyvalue.put("token", token);
        if (!"GPS".equals(msgType)) {
            System.out.println("FIRE RULES (" + realm + ") " + msgType);
        }
        // String ruleGroupRealm = realm + (StringUtils.isBlank(ruleGroup)?"":(":"+ruleGroup));
        try {
            RulesLoader.executeStatefull(realm, eventBus, globals, facts, keyvalue);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        future.complete();
    }, res -> {
        if (res.succeeded()) {
        // System.out.println("Processed "+msgType+" Msg");
        }
    });
}
Also used : User(life.genny.qwanda.entity.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QRules(life.genny.rules.QRules) Tuple2(io.vavr.Tuple2) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with QRules

use of life.genny.rules.QRules in project rulesservice by genny-project.

the class FeeCalculationTest method ownerTest.

// @Test
public void ownerTest() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter OWNER Price   ::   ");
    String ownerPriceString = scanner.nextLine();
    Double ownerPriceDouble = Double.parseDouble(ownerPriceString);
    Money ownerPrice = Money.of(ownerPriceDouble, DEFAULT_CURRENCY_AUD);
    Money ownerFeeExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerFeeIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    QRules rules = new QRules(null, null, null);
    /* fee calculation */
    ownerFeeExcGST = rules.calcOwnerFee(ownerPrice);
    if (ownerPrice.compareTo(Money.of(300, DEFAULT_CURRENCY_AUD)) > 0) {
        if (ownerFeeExcGST.compareTo(Money.of(100, DEFAULT_CURRENCY_AUD)) < 0) {
            ownerFeeExcGST = Money.of(100, DEFAULT_CURRENCY_AUD);
        }
    }
    ownerFeeIncGST = rules.includeGSTMoney(ownerFeeExcGST);
    /* price calculation */
    ownerPriceExcGST = ownerPrice;
    ownerPriceIncGST = rules.includeGSTMoney(ownerPriceExcGST);
    driverPriceIncGST = ownerPriceIncGST.subtract(ownerFeeIncGST);
    driverPriceExcGST = ownerPriceExcGST.subtract(ownerFeeExcGST);
    System.out.println("-------------------------------------------");
    System.out.println("OWNER");
    System.out.println("-------------------------------------------");
    System.out.println("FEES");
    System.out.println("ownerFeeExcGST  ::  " + String.valueOf(ownerFeeExcGST.getNumber().doubleValue()));
    System.out.println("ownerFeeIncGST  ::  " + ownerFeeIncGST.getNumber().doubleValue());
    System.out.println("\n");
    System.out.println("PRICES");
    System.out.println("ownerPrice         ::  " + ownerPrice.getNumber().doubleValue());
    System.out.println("ownerPriceExcGST   ::  " + ownerPriceExcGST.getNumber().doubleValue());
    System.out.println("ownerPriceIncGST   ::  " + ownerPriceIncGST.getNumber().doubleValue());
    System.out.println("driverPriceExcGST  ::  " + driverPriceExcGST.getNumber().doubleValue());
    System.out.println("driverPriceIncGST  ::  " + driverPriceIncGST.getNumber().doubleValue());
}
Also used : Scanner(java.util.Scanner) Money(org.javamoney.moneta.Money) QRules(life.genny.rules.QRules)

Example 4 with QRules

use of life.genny.rules.QRules in project rulesservice by genny-project.

the class FeeCalculationTest method driverTest.

// @Test
public void driverTest() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter DRIVER Price   ::   ");
    String ownerPriceString = scanner.nextLine();
    Double driverPriceDouble = Double.parseDouble(ownerPriceString);
    Money driverPrice = Money.of(driverPriceDouble, DEFAULT_CURRENCY_AUD);
    Money driverFeeExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverFeeIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    QRules rules = new QRules(null, null, null);
    /* fee calculation */
    driverFeeExcGST = rules.calcOwnerFee(driverPrice);
    if (driverPrice.compareTo(Money.of(300, DEFAULT_CURRENCY_AUD)) > 0) {
        if (driverFeeExcGST.compareTo(Money.of(100, DEFAULT_CURRENCY_AUD)) < 0) {
            driverFeeExcGST = Money.of(100, DEFAULT_CURRENCY_AUD);
        }
    }
    driverFeeIncGST = rules.includeGSTMoney(driverFeeExcGST);
    /* price calculation */
    driverPriceExcGST = driverPrice;
    driverPriceIncGST = rules.includeGSTMoney(driverPriceExcGST);
    ownerPriceExcGST = driverPriceExcGST.add(driverFeeExcGST);
    ownerPriceIncGST = rules.includeGSTMoney(ownerPriceExcGST);
    System.out.println("-------------------------------------------");
    System.out.println("DRIVER");
    System.out.println("-------------------------------------------");
    System.out.println("FEES");
    System.out.println("driverFeeExcGST  ::  " + driverFeeExcGST.getNumber().doubleValue());
    System.out.println("driverFeeIncGST  ::  " + driverFeeIncGST.getNumber().doubleValue());
    System.out.println("\n");
    System.out.println("PRICES");
    System.out.println("driverPrice         ::  " + driverPrice.getNumber().doubleValue());
    System.out.println("ownerPriceExcGST   ::  " + ownerPriceExcGST.getNumber().doubleValue());
    System.out.println("ownerPriceIncGST   ::  " + ownerPriceIncGST.getNumber().doubleValue());
    System.out.println("driverPriceExcGST  ::  " + driverPriceExcGST.getNumber().doubleValue());
    System.out.println("driverPriceIncGST  ::  " + driverPriceIncGST.getNumber().doubleValue());
}
Also used : Scanner(java.util.Scanner) Money(org.javamoney.moneta.Money) QRules(life.genny.rules.QRules)

Example 5 with QRules

use of life.genny.rules.QRules in project rulesservice by genny-project.

the class FeeCalculationTest method generatePasscodeTest.

@Test
public void generatePasscodeTest() {
    QRules rules = new QRules(null, null, null);
    for (int i = 0; i <= 100; i++) {
        System.out.println("The passcode is::" + rules.generateVerificationCode());
    }
    System.out.println(String.format("%04d", 0));
    System.out.println(String.format("%04d", 12));
    System.out.println(String.format("%04d", 123));
    System.out.println(String.format("%04d", 1234));
}
Also used : QRules(life.genny.rules.QRules) Test(org.junit.Test)

Aggregations

QRules (life.genny.rules.QRules)5 Tuple2 (io.vavr.Tuple2)2 JsonObject (io.vertx.core.json.JsonObject)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Scanner (java.util.Scanner)2 User (life.genny.qwanda.entity.User)2 Money (org.javamoney.moneta.Money)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1