Search in sources :

Example 46 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity in project rulesservice by genny-project.

the class QRules method acceptJob.

public void acceptJob(QEventBtnClickMessage m) {
    /* Get beg.getCode(), username, userCode, userFullName */
    // Get Baseentity once so we don't need to keep
    BaseEntity beg = getBaseEntityByCode(m.getItemCode());
    // fetching...
    println("beg.getCode()  ::   " + beg.getCode());
    String userCode = getUser().getCode();
    println("usercode   ::   " + getUser().getCode());
    String userName = getAsString("preferred_username");
    println("username   ::   " + userName);
    String userFullName = getFullName(getUser());
    println("user fullName   ::   " + userFullName);
    String linkCode = "LNK_BEG";
    String linkOffer = "OFFER";
    String linkQuoter = "QUOTER";
    String linkOwner = "OWNER";
    String linkCreator = "CREATOR";
    Optional<String> optOwnerCode = beg.getValue("PRI_AUTHOR");
    String ownerCode = null;
    if (optOwnerCode.isPresent()) {
        ownerCode = optOwnerCode.get();
    } else {
        ownerCode = QwandaUtils.getSourceOrTargetForGroupLink("GRP_NEW_ITEMS", linkCode, beg.getCode(), linkOwner, false, getToken());
    }
    /* get BEG PRICEs */
    println("BEG Prices   ::   ");
    Money begPrice = beg.getLoopValue("PRI_PRICE", Money.of(0.00, "AUD"));
    Money ownerPriceExcGST = beg.getLoopValue("PRI_OWNER_PRICE_EXC_GST", Money.of(0.00, "AUD"));
    Money ownerPriceIncGST = beg.getLoopValue("PRI_OWNER_PRICE_INC_GST", Money.of(0.00, "AUD"));
    Money driverPriceExcGST = beg.getLoopValue("PRI_DRIVER_PRICE_EXC_GST", Money.of(0.00, "AUD"));
    Money driverPriceIncGST = beg.getLoopValue("PRI_DRIVER_PRICE_INC_GST", Money.of(0.00, "AUD"));
    Money feePriceExcGST = beg.getLoopValue("PRI_FEE_EXC_GST", Money.of(0.00, "AUD"));
    Money feePriceIncGST = beg.getLoopValue("PRI_FEE_INC_GST", Money.of(0.00, "AUD"));
    /* Create Offer BE */
    BaseEntity offer = createBaseEntityByCode(getUser().getCode(), "OFR", "Offer");
    println("OFFER CODE   ::   " + offer.getCode());
    RulesUtils.ruleLogger("OFFER Base Entity", offer);
    /* Send beg to driver and owner should see it as part of beg link */
    VertxUtils.subscribe(realm(), offer, getUser().getCode());
    VertxUtils.subscribe(realm(), offer, ownerCode);
    /* Save attributes for OFFER as answer */
    List<Answer> answerList = new ArrayList<Answer>();
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_PRICE", JsonUtils.toJson(begPrice)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_EXC_GST", JsonUtils.toJson(ownerPriceExcGST)));
    /*
		 * answerList .add(new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_INC_GST",
		 * JsonUtils.toJson(ownerPriceIncGST)));
		 */
    Answer owIncGST = new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_INC_GST", JsonUtils.toJson(ownerPriceIncGST));
    owIncGST.setChangeEvent(false);
    answerList.add(owIncGST);
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DRIVER_PRICE_EXC_GST", JsonUtils.toJson(driverPriceExcGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DRIVER_PRICE_INC_GST", JsonUtils.toJson(driverPriceIncGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_FEE_EXC_GST", JsonUtils.toJson(feePriceExcGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_FEE_INC_GST", JsonUtils.toJson(feePriceIncGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_CODE", offer.getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_CODE", getUser().getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_USERNAME", userName));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_FULLNAME", userFullName));
    answerList.add(new Answer(getUser(), offer, "PRI_BEG_CODE", beg.getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_NEXT_ACTION", linkOwner));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DATE", getCurrentLocalDateTime()));
    saveAnswers(answerList);
    /* Update the number of offers for BEG */
    Integer offerCount = beg.getLoopValue("PRI_OFFER_COUNT", 0);
    offerCount = offerCount + 1;
    println("Offer Count is   ::   " + offerCount);
    saveAnswer(new Answer(beg.getCode(), beg.getCode(), "PRI_OFFER_COUNT", offerCount.toString()));
    /* Determine the recipient code */
    String[] recipients = VertxUtils.getSubscribers(realm(), beg.getCode());
    println("BEG subscribers   ::   " + Arrays.toString(recipients));
    /* link BEG and OFFER BE || OFFER */
    createLink(beg.getCode(), offer.getCode(), linkCode, linkOffer, 1.0);
    /* link BEG and QUOTER BE || QUOTER */
    createLink(beg.getCode(), getUser().getCode(), linkCode, linkQuoter, 1.0);
    /* link OFFER and QUOTER BE || CREATOR */
    createLink(offer.getCode(), getUser().getCode(), "LNK_OFR", linkCreator, 1.0);
    /* set Status of the job */
    /* get Owner of the job */
    BaseEntity owner = getChildren(beg.getCode(), "LNK_BEG", "OWNER");
    // updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_STATUS",
    // "#FFA500");
    updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_" + getUser().getCode(), Status.NEEDS_ACTION.value());
    updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_" + owner.getCode(), Status.NEEDS_ACTION.value());
    /* SEND (OFFER, QUOTER, BEG) BaseEntitys to recipients */
    String[] offerRecipients = VertxUtils.getSubscribers(realm(), offer.getCode());
    println("OFFER subscribers   ::   " + Arrays.toString(offerRecipients));
    publishBaseEntityByCode(offer.getCode(), beg.getCode(), "LNK_BEG", offerRecipients);
    publishBaseEntityByCode(getUser().getCode(), beg.getCode(), "LNK_BEG", offerRecipients);
    publishBaseEntityByCode(beg.getCode(), "GRP_NEW_ITEMS", "LNK_CORE", offerRecipients);
    /* Messages */
    /* OWNER config */
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put("QUOTER", getUser().getCode());
    contextMap.put("JOB", beg.getCode());
    contextMap.put("OFFER", offer.getCode());
    RulesUtils.println("owner code ::" + ownerCode);
    String[] recipientArr = { ownerCode };
    /* Sending toast message to owner frontend */
    sendMessage("", recipientArr, contextMap, "MSG_CH40_ACCEPT_QUOTE_OWNER", "TOAST");
    /* QUOTER config */
    HashMap<String, String> contextMapForDriver = new HashMap<String, String>();
    contextMapForDriver.put("JOB", beg.getCode());
    contextMapForDriver.put("OWNER", ownerCode);
    contextMapForDriver.put("OFFER", offer.getCode());
    contextMapForDriver.put("QUOTER", getUser().getCode());
    String[] recipientArrForDriver = { getUser().getCode() };
    /* Sending toast message to driver frontend */
    sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_ACCEPT_QUOTE_DRIVER", "TOAST");
}
Also used : AttributeInteger(life.genny.qwanda.attribute.AttributeInteger) AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) Answer(life.genny.qwanda.Answer) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList)

Example 47 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity in project rulesservice by genny-project.

the class QRules method updateGPS.

public void updateGPS(QDataGPSMessage m) {
    GPS driverPosition = m.getItems()[0];
    Double driverLatitude = driverPosition.getLatitude();
    Double driverLongitude = driverPosition.getLongitude();
    if (driverLatitude != null && driverLongitude != null) {
        try {
            List<BaseEntity> jobsInTransit = getBaseEntitysByAttributeAndValue("STT_IN_TRANSIT", getUser().getCode());
            if (!jobsInTransit.isEmpty()) {
                println("###### GPS: for user " + getUser().getCode() + ":" + m);
                println(jobsInTransit.toString());
            }
            for (BaseEntity be : jobsInTransit) {
                Double deliveryLatitude = be.getValue("PRI_DROPOFF_ADDRESS_LATITUDE", 0.0);
                Double deliveryLongitude = be.getValue("PRI_DROPOFF_ADDRESS_LONGITUDE", 0.0);
                Double totalDistance = be.getValue("PRI_TOTAL_DISTANCE_M", 0.0);
                /* Call Google Maps API to know how far the driver is */
                Double distance = GPSUtils.getDistance(driverLatitude, driverLongitude, deliveryLatitude, deliveryLongitude);
                Double percentage = 100.0 * (totalDistance - distance) / (totalDistance);
                percentage = percentage < 0 ? 0 : percentage;
                /* update position of the beg */
                List<Answer> answers = new ArrayList<Answer>();
                answers.add(new Answer(be.getCode(), be.getCode(), "PRI_PROGRESS", percentage.toString()));
                answers.add(new Answer(be.getCode(), be.getCode(), "PRI_POSITION_LATITUDE", driverLatitude + ""));
                answers.add(new Answer(be.getCode(), be.getCode(), "PRI_POSITION_LONGITUDE", driverLongitude + ""));
                saveAnswers(answers);
            }
        } catch (NumberFormatException e) {
            println("GPS Error " + m);
        }
    }
}
Also used : Answer(life.genny.qwanda.Answer) BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList) GPS(life.genny.qwanda.GPS)

Example 48 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity in project rulesservice by genny-project.

the class QRules method getProject.

public BaseEntity getProject() {
    BaseEntity be = null;
    String projectCode = "PRJ_" + getAsString("realm").toUpperCase();
    be = getBaseEntityByCode(projectCode);
    if (isNull("PROJECT") && be != null) {
        set("PROJECT", be);
    }
    return be;
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 49 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity in project rulesservice by genny-project.

the class QRules method getOfferBaseEntity.

public BaseEntity getOfferBaseEntity(String groupCode, String linkCode, String linkValue, String quoterCode, String token) {
    List linkList = getLinkList(groupCode, linkCode, linkValue, token);
    String quoterCodeForOffer = null;
    BaseEntity offer = null;
    if (linkList != null) {
        for (Object linkObj : linkList) {
            Link link = JsonUtils.fromJson(linkObj.toString(), Link.class);
            BaseEntity offerBe = getBaseEntityByCode(link.getTargetCode());
            if (offerBe != null) {
                quoterCodeForOffer = offerBe.getValue("PRI_QUOTER_CODE", null);
                if (quoterCode.equals(quoterCodeForOffer)) {
                    offer = offerBe;
                    return offerBe;
                }
            }
        }
    }
    return offer;
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(io.vertx.core.json.JsonObject) JSONObject(org.json.simple.JSONObject) Link(life.genny.qwanda.Link)

Example 50 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity in project rulesservice by genny-project.

the class QRules method sendLayoutsAndData.

public void sendLayoutsAndData() {
    boolean doCache = false;
    /* Show loading indicator */
    showLoading("Loading your interface...");
    BaseEntity user = getUser();
    List<BaseEntity> root = getBaseEntitysByParentAndLinkCode("GRP_ROOT", "LNK_CORE", 0, 20, doCache);
    List<BaseEntity> toRemove = new ArrayList<BaseEntity>();
    /* Removing GRP_DRAFTS be if user is a Driver */
    if (((user.is("PRI_DRIVER")))) {
        for (BaseEntity be : root) {
            if (be.getCode().equalsIgnoreCase("GRP_DRAFTS") || be.getCode().equalsIgnoreCase("GRP_BIN")) {
                toRemove.add(be);
                println("GRP_DRAFTS & GRP_BIN has been added to remove list");
            }
        }
        root.removeAll(toRemove);
        println("GRP_DRAFTS & GRP_BIN have been removed from root");
    }
    publishCmd(root, "GRP_ROOT", "LNK_CORE");
    println(root);
    List<BaseEntity> reportsHeader = getBaseEntitysByParentAndLinkCode("GRP_REPORTS", "LNK_CORE", 0, 20, false);
    List<BaseEntity> reportsHeaderToRemove = new ArrayList<BaseEntity>();
    println("User is Admin" + hasRole("admin"));
    // Checking for driver role
    if ((user.is("PRI_DRIVER"))) {
        for (BaseEntity be : reportsHeader) {
            if (be.getCode().equalsIgnoreCase("GRP_REPORTS_OWNER")) {
                reportsHeaderToRemove.add(be);
            }
        }
    } else // Checking for owner role
    if ((user.is("PRI_OWNER"))) {
        for (BaseEntity be : reportsHeader) {
            if (be.getCode().equalsIgnoreCase("GRP_REPORTS_DRIVER")) {
                reportsHeaderToRemove.add(be);
            }
        }
    }
    // checking for admin role
    if (!(hasRole("admin"))) {
        for (BaseEntity be : reportsHeader) {
            if (be.getCode().equalsIgnoreCase("GRP_REPORTS_ADMIN")) {
                reportsHeaderToRemove.add(be);
            }
        }
    }
    // Removing reports not related to the user based on their role
    reportsHeader.removeAll(reportsHeaderToRemove);
    println("Unrelated reports have been removed ");
    publishCmd(reportsHeader, "GRP_REPORTS", "LNK_CORE");
    List<BaseEntity> admin = getBaseEntitysByParentAndLinkCode("GRP_ADMIN", "LNK_CORE", 0, 20, false);
    publishCmd(admin, "GRP_ADMIN", "LNK_CORE");
    /*
		 * if(hasRole("admin")){ List<BaseEntity> reports =
		 * getBaseEntitysByParentAndLinkCode("GRP_REPORTS", "LNK_CORE", 0, 20, false);
		 * publishCmd(reports, "GRP_REPORTS", "LNK_CORE"); }
		 */
    if (!user.is("PRI_DRIVER")) {
        List<BaseEntity> bin = getBaseEntitysByParentLinkCodeAndLinkValue("GRP_BIN", "LNK_CORE", user.getCode(), 0, 20, doCache);
        publishCmd(bin, "GRP_BIN", "LNK_CORE");
    }
    List<BaseEntity> buckets = getBaseEntitysByParentAndLinkCode("GRP_DASHBOARD", "LNK_CORE", 0, 20, doCache);
    publishCmd(buckets, "GRP_DASHBOARD", "LNK_CORE");
    println(buckets);
    for (BaseEntity bucket : buckets) {
        println(bucket);
        List<BaseEntity> begs = new ArrayList<BaseEntity>();
        if (hasRole("admin")) {
            List<BaseEntity> driverbegs = getBaseEntitysByParentAndLinkCode(bucket.getCode(), "LNK_CORE", 0, 500, false);
            begs.addAll(driverbegs);
        } else {
            if (user.is("PRI_DRIVER") && bucket.getCode().equals("GRP_NEW_ITEMS")) {
                List<BaseEntity> driverbegs = getBaseEntitysByParentAndLinkCode(bucket.getCode(), "LNK_CORE", 0, 500, doCache);
                begs.addAll(driverbegs);
                VertxUtils.subscribe(realm(), bucket, user.getCode());
            /* monitor anything in first bucket */
            } else {
                if (user.is("PRI_DRIVER")) {
                    List<BaseEntity> driverbegs = getBaseEntitysByParentAndLinkCode(bucket.getCode(), "LNK_CORE", 0, 500, false, user.getCode());
                    begs.addAll(driverbegs);
                    VertxUtils.subscribe(realm(), driverbegs, user.getCode());
                }
            }
            if (user.is("PRI_OWNER")) {
                List<BaseEntity> ownerbegs = getBaseEntitysByParentAndLinkCode(bucket.getCode(), "LNK_CORE", 0, 500, false, user.getCode());
                begs.addAll(ownerbegs);
                VertxUtils.subscribe(realm(), ownerbegs, user.getCode());
            }
        }
        println("FETCHED " + begs.size() + " JOBS FOR " + user.getCode());
        publishCmd(begs, bucket.getCode(), "LNK_CORE");
        for (BaseEntity beg : begs) {
            List<BaseEntity> begKids = getBaseEntitysByParentAndLinkCode(beg.getCode(), "LNK_BEG", 0, 20, doCache);
            List<BaseEntity> filteredKids = new ArrayList<BaseEntity>();
            for (BaseEntity begKid : begKids) {
                if (begKid.getCode().startsWith("OFR_")) {
                    if (user.is("PRI_OWNER")) {
                        filteredKids.add(begKid);
                        VertxUtils.subscribe(realm(), begKid.getCode(), user.getCode());
                    }
                    if (user.is("PRI_DRIVER")) {
                        Optional<String> quoterCode = begKid.getLoopValue("PRI_QUOTER_CODE");
                        if (quoterCode.isPresent()) {
                            if (user.getCode().equals(quoterCode.get())) {
                                filteredKids.add(begKid);
                                VertxUtils.subscribe(realm(), begKid.getCode(), user.getCode());
                            }
                        }
                    }
                } else {
                    filteredKids.add(begKid);
                }
                println(bucket.getCode() + ":" + begKid.getCode());
            }
            publishCmd(filteredKids, beg.getCode(), "LNK_BEG");
        }
    }
    /* Sending Draft Datas for the Owners */
    if (user.is("PRI_OWNER")) {
        List<BaseEntity> ownerDraftBegs = getBaseEntitysByParentAndLinkCode("GRP_DRAFTS", "LNK_CORE", 0, 500, false, user.getCode());
        publishCmd(ownerDraftBegs, "GRP_DRAFTS", "LNK_BEG");
    }
    /*
		 * Send messages to user if they belong to the conversation. TODO: to optimize
		 */
    /*
		 * publishBaseEntitysByParentAndLinkCodeWithAttributes("GRP_MESSAGES",
		 * "LNK_CHAT", 0, 100, true);
		 */
    List<BaseEntity> conversations = getBaseEntitysByParentAndLinkCode("GRP_MESSAGES", "LNK_CHAT", 0, 100, true);
    List<BaseEntity> userConversations = new ArrayList<BaseEntity>();
    if (conversations != null) {
        for (BaseEntity convo : conversations) {
            List<BaseEntity> users = getBaseEntitysByParentAndLinkCode(convo.getCode(), "LNK_USER", 0, 100, true);
            if (users != null) {
                for (BaseEntity linkedUser : users) {
                    /* if user is a stackholder of this conversation we send it */
                    if (linkedUser.getCode().equals(getUser().getCode())) {
                        userConversations.add(convo);
                    }
                }
            }
        }
    }
    publishCmd(userConversations, "GRP_MESSAGES", "LNK_CHAT");
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList)

Aggregations

BaseEntity (life.genny.qwanda.entity.BaseEntity)54 QDataBaseEntityMessage (life.genny.qwanda.message.QDataBaseEntityMessage)13 ArrayList (java.util.ArrayList)11 JsonObject (io.vertx.core.json.JsonObject)9 Answer (life.genny.qwanda.Answer)8 EntityAttribute (life.genny.qwanda.attribute.EntityAttribute)8 BadDataException (life.genny.qwanda.exception.BadDataException)7 IOException (java.io.IOException)6 AttributeInteger (life.genny.qwanda.attribute.AttributeInteger)6 JsonArray (io.vertx.core.json.JsonArray)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 AttributeBoolean (life.genny.qwanda.attribute.AttributeBoolean)5 Link (life.genny.qwanda.Link)4 AttributeMoney (life.genny.qwanda.attribute.AttributeMoney)4 AttributeText (life.genny.qwanda.attribute.AttributeText)4 Money (org.javamoney.moneta.Money)3 HashSet (java.util.HashSet)2 List (java.util.List)2 JSONObject (org.json.simple.JSONObject)2