Search in sources :

Example 6 with QDataBaseEntityMessage

use of life.genny.qwanda.message.QDataBaseEntityMessage 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 7 with QDataBaseEntityMessage

use of life.genny.qwanda.message.QDataBaseEntityMessage 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 8 with QDataBaseEntityMessage

use of life.genny.qwanda.message.QDataBaseEntityMessage in project rulesservice by genny-project.

the class QRules method publishCmdToRecipients.

public void publishCmdToRecipients(final BaseEntity be, final String[] recipientsCode) {
    QDataBaseEntityMessage msg = new QDataBaseEntityMessage(be, null);
    msg.setRecipientCodeArray(recipientsCode);
    msg.setToken(getToken());
    publish("cmds", RulesUtils.toJsonObject(msg));
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage)

Example 9 with QDataBaseEntityMessage

use of life.genny.qwanda.message.QDataBaseEntityMessage in project rulesservice by genny-project.

the class QRules method publishCmd.

public void publishCmd(final List<BaseEntity> beList, final String parentCode, final String linkCode, String[] recipientCodes) {
    QDataBaseEntityMessage msg = new QDataBaseEntityMessage(beList.toArray(new BaseEntity[0]));
    msg.setParentCode(parentCode);
    msg.setLinkCode(linkCode);
    msg.setToken(getToken());
    if (recipientCodes != null) {
        msg.setRecipientCodeArray(recipientCodes);
    }
    publish("cmds", RulesUtils.toJsonObject(msg));
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage) BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 10 with QDataBaseEntityMessage

use of life.genny.qwanda.message.QDataBaseEntityMessage in project rulesservice by genny-project.

the class RulesUtils method getBaseEntitysByAttributeAndValue.

/**
 * @param qwandaServiceUrl
 * @param decodedToken
 * @param token
 * @return baseEntity user for the decodedToken passed
 */
public static List<BaseEntity> getBaseEntitysByAttributeAndValue(final String qwandaServiceUrl, Map<String, Object> decodedToken, final String token, final String attributeCode, final String value) {
    String beJson = getBaseEntityJsonByAttributeAndValue(qwandaServiceUrl, decodedToken, token, attributeCode, value, 1000);
    QDataBaseEntityMessage be = fromJson(beJson, QDataBaseEntityMessage.class);
    List<BaseEntity> items = null;
    try {
        items = new ArrayList<BaseEntity>(Arrays.asList(be.getItems()));
    } catch (Exception e) {
        println("Error: items is null");
    }
    return items;
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage) BaseEntity(life.genny.qwanda.entity.BaseEntity) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException)

Aggregations

QDataBaseEntityMessage (life.genny.qwanda.message.QDataBaseEntityMessage)20 BaseEntity (life.genny.qwanda.entity.BaseEntity)13 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 EntityAttribute (life.genny.qwanda.attribute.EntityAttribute)2 JsonParseException (com.google.gson.JsonParseException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Answer (life.genny.qwanda.Answer)1 AttributeBoolean (life.genny.qwanda.attribute.AttributeBoolean)1 AttributeMoney (life.genny.qwanda.attribute.AttributeMoney)1 Money (org.javamoney.moneta.Money)1