Search in sources :

Example 1 with Answer

use of life.genny.qwanda.Answer in project rulesservice by genny-project.

the class QRules method processDimensions.

public void processDimensions(QEventAttributeValueChangeMessage msg) {
    Answer newAnswer = msg.getAnswer();
    BaseEntity load = getBaseEntityByCode(newAnswer.getTargetCode());
    println("The laod value is " + load.toString());
    String value = newAnswer.getValue();
    println("The load " + msg.getData().getCode() + " is    ::" + value);
    /* Get the sourceCode(Job code) for this LOAD */
    BaseEntity job = getParent(newAnswer.getTargetCode(), "LNK_BEG");
    Answer jobDimensionAnswer = new Answer(getUser().getCode(), job.getCode(), msg.getData().getCode(), value);
    saveAnswer(jobDimensionAnswer);
}
Also used : Answer(life.genny.qwanda.Answer) BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 2 with Answer

use of life.genny.qwanda.Answer in project rulesservice by genny-project.

the class QRules method updateBaseEntityAttribute.

public void updateBaseEntityAttribute(final String sourceCode, final String beCode, final String attributeCode, final String newValue) {
    Answer newAnswer = new Answer(sourceCode, beCode, attributeCode, newValue);
    saveAnswer(newAnswer);
}
Also used : Answer(life.genny.qwanda.Answer)

Example 3 with Answer

use of life.genny.qwanda.Answer in project rulesservice by genny-project.

the class QRules method processAnswer2.

public void processAnswer2(QDataAnswerMessage m) {
    /* extract answers */
    List<Answer> answerList = new ArrayList<Answer>();
    Answer[] answers2 = m.getItems();
    for (Answer answer : answers2) {
        if (answer != null) {
            String attributeCode = answer.getAttributeCode();
            /* if this answer is actually an address another rule will be triggered */
            if (!attributeCode.contains("ADDRESS_FULL") && !attributeCode.contains("PRI_PAYMENT_METHOD")) {
                answerList.add(answer);
            }
        } else {
            println("Answer was null ");
        }
    }
    saveAnswers(answerList);
}
Also used : Answer(life.genny.qwanda.Answer) ArrayList(java.util.ArrayList)

Example 4 with Answer

use of life.genny.qwanda.Answer in project rulesservice by genny-project.

the class QRules method processAnswerRating.

public void processAnswerRating(QDataAnswerMessage m, final String finalAttributeCode) {
    /* extract answers */
    Answer[] answers = m.getItems();
    for (Answer answer : answers) {
        String sourceCode = answer.getSourceCode();
        String targetCode = answer.getTargetCode();
        answer.setSourceCode(answer.getTargetCode());
        String attributeCode = answer.getAttributeCode();
        String value = answer.getValue();
        if (attributeCode.equals("PRI_RATING_RAW")) {
            /* Saving PRI_RATING attribute */
            this.updateBaseEntityAttribute(sourceCode, targetCode, "PRI_RATING", value);
            /* we grab the old value of the rating as well as the current rating */
            String currentRatingString = getBaseEntityValueAsString(targetCode, finalAttributeCode);
            String numberOfRatingString = getBaseEntityValueAsString(targetCode, "PRI_NUMBER_RATING");
            if (currentRatingString == null)
                currentRatingString = "0";
            if (numberOfRatingString == null)
                numberOfRatingString = "0";
            if (currentRatingString != null && numberOfRatingString != null) {
                Double currentRating = Double.parseDouble(currentRatingString);
                Double numberOfRating = Double.parseDouble(numberOfRatingString);
                Double newRating = Double.parseDouble(value);
                /* we increment the number of current ratings */
                numberOfRating += 1;
                this.updateBaseEntityAttribute(sourceCode, targetCode, "PRI_NUMBER_RATING", Double.toString(numberOfRating));
                /* we compute the new rating */
                /*
					 * because for now we are not storing ALL the previous ratings, we calculate a
					 * rolling average
					 */
                Double newRatingAverage = currentRating / numberOfRating;
                newRatingAverage += newRating / numberOfRating;
                this.updateBaseEntityAttribute(sourceCode, targetCode, finalAttributeCode, Double.toString(newRatingAverage));
            }
        /* publishData(answer); */
        }
    }
}
Also used : Answer(life.genny.qwanda.Answer)

Example 5 with Answer

use of life.genny.qwanda.Answer in project rulesservice by genny-project.

the class QRules method makePayment.

public void makePayment(QDataAnswerMessage m) {
    /* Save Payment-related answers as user/BEG attributes */
    String userCode = getUser().getCode();
    BaseEntity userBe = getBaseEntityByCode(userCode);
    String begCode = PaymentUtils.processPaymentAnswers(getQwandaServiceUrl(), m, getToken());
    String assemblyAuthKey = PaymentUtils.getAssemblyAuthKey();
    String assemblyId = userBe.getValue("PRI_ASSEMBLY_USER_ID", null);
    if (begCode != null && assemblyId != null) {
        /* GET beg Base Entity */
        BaseEntity beg = getBaseEntityByCode(begCode);
        String offerCode = beg.getLoopValue("STT_HOT_OFFER", null);
        if (offerCode != null) {
            /* Make payment */
            showLoading("Processing payment...");
            Boolean isMakePaymentSucceeded = PaymentUtils.makePayment(getQwandaServiceUrl(), offerCode, begCode, assemblyAuthKey, getToken());
            println("isMakePaymentSucceeded ::" + isMakePaymentSucceeded);
            /* GET offer Base Entity */
            BaseEntity offer = getBaseEntityByCode(offerCode);
            String quoterCode = offer.getLoopValue("PRI_QUOTER_CODE", null);
            if (!isMakePaymentSucceeded) {
                /* TOAST :: FAIL */
                println("Sending error toast since make payment failed");
                HashMap<String, String> contextMap = new HashMap<String, String>();
                contextMap.put("DRIVER", quoterCode);
                contextMap.put("JOB", begCode);
                contextMap.put("QUOTER", quoterCode);
                String[] recipientArr = { userCode };
                /* Need to display error toast if make payment fails */
                sendMessage(null, recipientArr, contextMap, "MSG_CH40_MAKE_PAYMENT_FAILED", "TOAST");
                /* sending cmd BUCKETVIEW */
                drools.setFocus("SendLayoutsAndData");
            }
            if (isMakePaymentSucceeded) {
                /* GET attributes of OFFER BE */
                Money offerPrice = offer.getLoopValue("PRI_OFFER_PRICE", null);
                Money ownerPriceExcGST = offer.getLoopValue("PRI_OFFER_OWNER_PRICE_EXC_GST", null);
                Money ownerPriceIncGST = offer.getLoopValue("PRI_OFFER_OWNER_PRICE_INC_GST", null);
                Money driverPriceExcGST = offer.getLoopValue("PRI_OFFER_DRIVER_PRICE_EXC_GST", null);
                Money driverPriceIncGST = offer.getLoopValue("PRI_OFFER_DRIVER_PRICE_INC_GST", null);
                Money feePriceExcGST = offer.getLoopValue("PRI_OFFER_FEE_EXC_GST", null);
                Money feePriceIncGST = offer.getLoopValue("PRI_OFFER_FEE_INC_GST", null);
                /* Update BEG's prices with offer's prices */
                /*
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_PRICE",
					 * QwandaUtils.getMoneyString(offerPrice)); updateBaseEntityAttribute(begCode,
					 * begCode, "PRI_OWNER_PRICE_EXC_GST",
					 * QwandaUtils.getMoneyString(ownerPriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_OWNER_PRICE_INC_GST",
					 * QwandaUtils.getMoneyString(ownerPriceIncGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_DRIVER_PRICE_EXC_GST",
					 * QwandaUtils.getMoneyString(driverPriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_DRIVER_PRICE_INC_GST",
					 * QwandaUtils.getMoneyString(driverPriceIncGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_FEE_EXC_GST",
					 * QwandaUtils.getMoneyString(feePriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_FEE_INC_GST",
					 * QwandaUtils.getMoneyString(feePriceIncGST));
					 */
                List<Answer> answers = new ArrayList<Answer>();
                answers.add(new Answer(begCode, begCode, "PRI_PRICE", QwandaUtils.getMoneyString(offerPrice)));
                answers.add(new Answer(begCode, begCode, "PRI_OWNER_PRICE_EXC_GST", QwandaUtils.getMoneyString(ownerPriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_OWNER_PRICE_INC_GST", QwandaUtils.getMoneyString(ownerPriceIncGST)));
                answers.add(new Answer(begCode, begCode, "PRI_DRIVER_PRICE_EXC_GST", QwandaUtils.getMoneyString(driverPriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_DRIVER_PRICE_INC_GST", QwandaUtils.getMoneyString(driverPriceIncGST)));
                answers.add(new Answer(begCode, begCode, "PRI_FEE_EXC_GST", QwandaUtils.getMoneyString(feePriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_FEE_INC_GST", QwandaUtils.getMoneyString(feePriceIncGST)));
                // fetch the job to ensure the cache has caught up
                BaseEntity begBe = null;
                try {
                    begBe = QwandaUtils.getBaseEntityByCode(begCode, getToken());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                /* Update BEG to have DRIVER_CODE as an attribute */
                answers.add(new Answer(begCode, begCode, "STT_IN_TRANSIT", quoterCode));
                saveAnswers(answers);
                /* TOAST :: SUCCESS */
                println("Sending success toast since make payment succeeded");
                HashMap<String, String> contextMap = new HashMap<String, String>();
                contextMap.put("DRIVER", quoterCode);
                contextMap.put("JOB", begCode);
                contextMap.put("QUOTER", quoterCode);
                contextMap.put("OFFER", offer.getCode());
                String[] recipientArr = { userCode };
                /* TOAST :: PAYMENT SUCCESS */
                sendMessage("", recipientArr, contextMap, "MSG_CH40_MAKE_PAYMENT_SUCCESS", "TOAST");
                // sendMessage("", recipientArr, contextMap, "MSG_CH40_CONFIRM_QUOTE_OWNER",
                // "TOAST");
                sendMessage("", recipientArr, contextMap, "MSG_CH40_CONFIRM_QUOTE_OWNER", "EMAIL");
                /* QUOTER config */
                HashMap<String, String> contextMapForDriver = new HashMap<String, String>();
                contextMapForDriver.put("JOB", begCode);
                contextMapForDriver.put("OWNER", userCode);
                contextMapForDriver.put("OFFER", offer.getCode());
                String[] recipientArrForDriver = { quoterCode };
                /* Sending messages to DRIVER - Email and sms enabled */
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "TOAST");
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "SMS");
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "EMAIL");
                /* Update link between BEG and OFFER to weight= 0 */
                // updateLink(begCode, offerCode, "LNK_BEG", "OFFER", 1.0);
                /* Allocate QUOTER as Driver */
                updateLink(begCode, quoterCode, "LNK_BEG", "DRIVER", 1.0);
                /* Update link between BEG and Accepted OFFER to weight= 100 */
                updateLink(begCode, offerCode, "LNK_BEG", "OFFER", 100.0);
                /* Set PRI_NEXT_ACTION to Disabled for all other Offers */
                // get all offers
                List<BaseEntity> offers = getAllChildrens(begCode, "LNK_BEG", "OFFER");
                println("All the Offers for the load " + begCode + " are: " + offers.toString());
                for (BaseEntity be : offers) {
                    if (!(be.getCode().equals(offerCode))) {
                        println("The BE is : " + be.getCode());
                        /* Update PRI_NEXT_ACTION to Disabled */
                        updateBaseEntityAttribute(getUser().getCode(), be.getCode(), "PRI_NEXT_ACTION", "DISABLED");
                    }
                }
                /* SEND (OFFER, QUOTER, BEG) BaseEntitys to recipients */
                String[] offerRecipients = VertxUtils.getSubscribers(realm(), offer.getCode());
                println("OFFER subscribers   ::   " + Arrays.toString(offerRecipients));
                publishBaseEntityByCode(userCode, begCode, "LNK_BEG", offerRecipients);
                /* OWNER */
                publishBaseEntityByCode(quoterCode, begCode, "LNK_BEG", offerRecipients);
                publishBaseEntityByCode(offerCode, begCode, "LNK_BEG", offerRecipients);
                publishBaseEntityByCode("GRP_NEW_ITEMS", begCode, "LNK_CORE", offerRecipients);
                /* Set progression of LOAD delivery to 0 */
                Answer updateProgressAnswer = new Answer(begCode, begCode, "PRI_PROGRESS", Double.toString(0.0));
                saveAnswer(updateProgressAnswer);
                /* We ask FE to monitor GPS */
                geofenceJob(begCode, getUser().getCode(), 10.0);
                /* GET all the driver subsribers */
                String[] begRecipients = VertxUtils.getSubscribers(realm(), "GRP_NEW_ITEMS");
                println("ALL BEG subscribers   ::   " + Arrays.toString(begRecipients));
                println("quoter code ::" + quoterCode);
                Set<String> unsubscribeSet = new HashSet<>();
                for (String begRecipient : begRecipients) {
                    if (!begRecipient.equals(quoterCode)) {
                        unsubscribeSet.add(begRecipient);
                    }
                }
                println("unsubscribe set ::" + unsubscribeSet);
                String[] unsubscribeArr = new String[unsubscribeSet.size()];
                int i = 0;
                for (String code : unsubscribeSet) {
                    unsubscribeArr[i++] = code;
                }
                println("unsubscribe arr ::" + Arrays.toString(unsubscribeArr));
                /* Move BEG to GRP_APPROVED */
                fastClearBaseEntity(begCode, unsubscribeArr);
                BaseEntity begbe = getBaseEntityByCode(begCode);
                println("be   ::   " + begbe);
                Set<EntityAttribute> attributes = begbe.getBaseEntityAttributes();
                begbe.setBaseEntityAttributes(attributes);
                QDataBaseEntityMessage beMsg = new QDataBaseEntityMessage(begbe);
                beMsg.setDelete(true);
                publishData(beMsg, unsubscribeArr);
                VertxUtils.unsubscribe(realm(), "GRP_NEW_ITEMS", unsubscribeSet);
                moveBaseEntity(begCode, "GRP_NEW_ITEMS", "GRP_APPROVED", "LNK_CORE");
                // publishBaseEntityByCode(begCode, "GRP_NEW_ITEMS", "LNK_CORE",
                // newbegRecipients);
                // publishBaseEntityByCode(begCode, "GRP_APPROVED", "LNK_CORE",
                // newbegRecipients);
                /* Update PRI_NEXT_ACTION = OWNER */
                Answer begNextAction = new Answer(userCode, offerCode, "PRI_NEXT_ACTION", "NONE");
                saveAnswer(begNextAction);
                /* Update the Job status */
                updateBaseEntityAttribute(getUser().getCode(), begCode, "STA_" + quoterCode, Status.NEEDS_ACTION.value());
                updateBaseEntityAttribute(getUser().getCode(), begCode, "STA_" + getUser().getCode(), Status.NEEDS_NO_ACTION.value());
                /* sending cmd BUCKETVIEW */
                drools.setFocus("SendLayoutsAndData");
            /*	List<BaseEntity> listBe = new ArrayList<>();
					listBe.add(getUser());
					listBe.add(getBaseEntityByCode(quoterCode));
					listBe.add(getBaseEntityByCode(offerCode));
					listBe.add(getBaseEntityByCode(begCode));

					publishCmd(listBe, "GRP_ROOT", "LNK_CORE");
					sendSublayout("BUCKET_DASHBOARD", "dashboard_channel40.json", "GRP_DASHBOARD");
					setLastLayout( "BUCKET_DASHBOARD", "GRP_DASHBOARD" );  */
            }
            setState("PAYMENT_DONE");
        }
    }
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage) EntityAttribute(life.genny.qwanda.attribute.EntityAttribute) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) Answer(life.genny.qwanda.Answer) AttributeBoolean(life.genny.qwanda.attribute.AttributeBoolean) HashSet(java.util.HashSet)

Aggregations

Answer (life.genny.qwanda.Answer)16 BaseEntity (life.genny.qwanda.entity.BaseEntity)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 IOException (java.io.IOException)4 JsonObject (io.vertx.core.json.JsonObject)2 AttributeMoney (life.genny.qwanda.attribute.AttributeMoney)2 EntityAttribute (life.genny.qwanda.attribute.EntityAttribute)2 BadDataException (life.genny.qwanda.exception.BadDataException)2 QDataAnswerMessage (life.genny.qwanda.message.QDataAnswerMessage)2 Money (org.javamoney.moneta.Money)2 JsonArray (io.vertx.core.json.JsonArray)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 GPS (life.genny.qwanda.GPS)1 Link (life.genny.qwanda.Link)1 AttributeBoolean (life.genny.qwanda.attribute.AttributeBoolean)1