Search in sources :

Example 11 with BaseEntity

use of life.genny.qwanda.entity.BaseEntity 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)

Example 12 with BaseEntity

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

the class QRules method clearBaseEntity.

/* sets delete field to true so that FE removes the BE from their store */
public void clearBaseEntity(String baseEntityCode, String[] recipients) {
    BaseEntity be = getBaseEntityByCode(baseEntityCode);
    QDataBaseEntityMessage beMsg = new QDataBaseEntityMessage(be);
    beMsg.setDelete(true);
    publishData(beMsg, recipients);
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage) BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 13 with BaseEntity

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

the class QRules method processChat.

public void processChat(QEventMessage m) {
    String data = m.getData().getValue();
    JsonObject dataJson = new JsonObject(data);
    String text = dataJson.getString("value");
    String chatCode = dataJson.getString("itemCode");
    if (text != null && chatCode != null) {
        /* creating new message */
        BaseEntity newMessage = QwandaUtils.createBaseEntityByCode(QwandaUtils.getUniqueId(getUser().getCode(), null, "MSG", getToken()), "message", getQwandaServiceUrl(), getToken());
        if (newMessage != null) {
            List<BaseEntity> stakeholders = getBaseEntitysByParentAndLinkCode(chatCode, "LNK_USER");
            String[] recipientCodeArray = new String[stakeholders.size()];
            int counter = 0;
            for (BaseEntity stakeholder : stakeholders) {
                recipientCodeArray[counter] = stakeholder.getCode();
                counter += 1;
            }
            /*
				 * publishBaseEntityByCode(newMessage.getCode(), chatCode, "LNK_MESSAGES",
				 * recipientCodeArray);
				 */
            this.updateBaseEntityAttribute(newMessage.getCode(), newMessage.getCode(), "PRI_MESSAGE", text);
            this.updateBaseEntityAttribute(newMessage.getCode(), newMessage.getCode(), "PRI_CREATOR", getUser().getCode());
            QwandaUtils.createLink(chatCode, newMessage.getCode(), "LNK_MESSAGES", "message", 1.0, getToken());
            BaseEntity chatBE = getBaseEntityByCode(newMessage.getCode());
            publishBE(chatBE);
        }
    }
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity) JsonObject(io.vertx.core.json.JsonObject)

Example 14 with BaseEntity

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

the class QRules method createBaseEntityByCode2.

public BaseEntity createBaseEntityByCode2(final String beCode, final String name) {
    BaseEntity beg = QwandaUtils.createBaseEntityByCode(beCode, name, qwandaServiceUrl, getToken());
    addAttributes(beg);
    VertxUtils.writeCachedJson(beg.getCode(), JsonUtils.toJson(beg));
    return beg;
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 15 with BaseEntity

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

the class QRules method getUser.

public BaseEntity getUser() {
    BaseEntity be = null;
    String username = (String) getDecodedTokenMap().get("preferred_username");
    String code = "PER_" + QwandaUtils.getNormalisedUsername(username).toUpperCase();
    be = getBaseEntityByCode(code);
    /*
		 * if this situation happens it just means that QRules has not registered the
		 * user yet. setting it.
		 */
    if (isNull("USER") && be != null) {
        set("USER", be);
    }
    return be;
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity)

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