Search in sources :

Example 41 with BaseEntity

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

the class QRules method saveJob.

public void saveJob(BaseEntity job) {
    String jobCode = job.getCode();
    /*
		 * We create a new attribute "PRI_TOTAL_DISTANCE" for this BEG. TODO: should be
		 * triggered in another rule
		 */
    Double pickupLatitude = job.getValue("PRI_PICKUP_ADDRESS_LATITUDE", 0.0);
    Double pickupLongitude = job.getValue("PRI_PICKUP_ADDRESS_LONGITUDE", 0.0);
    Double deliveryLatitude = job.getValue("PRI_DROPOFF_ADDRESS_LATITUDE", 0.0);
    Double deliveryLongitude = job.getValue("PRI_DROPOFF_ADDRESS_LONGITUDE", 0.0);
    /* Add author to the load */
    List<Answer> answers = new ArrayList<Answer>();
    answers.add(new Answer(getUser().getCode(), jobCode, "PRI_POSITION_LATITUDE", pickupLatitude + ""));
    answers.add(new Answer(getUser().getCode(), jobCode, "PRI_POSITION_LONGITUDE", pickupLongitude + ""));
    Double totalDistance = GPSUtils.getDistance(pickupLatitude, pickupLongitude, deliveryLatitude, deliveryLongitude);
    if (totalDistance > 0) {
        Answer totalDistanceAnswer = new Answer(jobCode, jobCode, "PRI_TOTAL_DISTANCE_M", totalDistance + "");
        answers.add(totalDistanceAnswer);
    }
    /* Adding Offer Count to 0 */
    Answer offerCountAns = new Answer(getUser().getCode(), jobCode, "PRI_OFFER_COUNT", "0");
    /* Publish Answer */
    answers.add(offerCountAns);
    /* set Status of the job */
    answers.add(new Answer(getUser().getCode(), jobCode, "STA_STATUS", Status.NEEDS_NO_ACTION.value()));
    // Setting color to green for new jobs for both driver and owner
    /*
		 * answers.add(new Answer(getUser().getCode(), jobCode, "STA_" +
		 * getUser().getCode(), Status.NEEDS_NO_ACTION.value()));
		 */
    BaseEntity updatedJob = this.getBaseEntityByCode(job.getCode());
    Long jobId = updatedJob.getId();
    answers.add(new Answer(getUser().getCode(), jobCode, "PRI_JOB_ID", jobId + ""));
    saveAnswers(answers);
    /* Determine the recipient code */
    String[] recipientCodes = VertxUtils.getSubscribers(realm(), "GRP_NEW_ITEMS");
    println("Recipients for Job/Load " + Arrays.toString(recipientCodes));
    /*
		 * Send newly created job with its attributes to all drivers so that it exists
		 * before link change
		 */
    BaseEntity newJobDetails = getBaseEntityByCode(jobCode);
    println("The newly submitted Job details     ::     " + newJobDetails.toString());
    publishData(newJobDetails, recipientCodes);
    /* publishing to Owner */
    publishBE(newJobDetails);
    /* Moving the BEG */
    Link link = new Link("GRP_DRAFTS", jobCode, "LNK_CORE");
    try {
        String output = QwandaUtils.apiPostEntity(getQwandaServiceUrl() + "/qwanda/baseentitys/move/GRP_NEW_ITEMS", JsonUtils.toJson(link), getToken());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /* Get the sourceCode(Company code) for this User */
    BaseEntity company = getParent(getUser().getCode(), "LNK_STAFF");
    /* link newly created Job to GRP_LOADS */
    BaseEntity load = getChildren(jobCode, "LNK_BEG", "LOAD");
    String loadCode = load.getCode();
    Link newLoadLinkToLoadList = QwandaUtils.createLink("GRP_LOADS", loadCode, "LNK_LOAD", company.getCode(), (double) 1, getToken());
    println("The load has been added to the GRP_LOADS ");
    /* SEND LOAD BE */
    /* Try sending different types of links to the frontend to get it to display */
    publishBaseEntityByCode(loadCode, jobCode, "LNK_BEG", recipientCodes);
    /* publishing to Owner */
    publishBE(getBaseEntityByCode(loadCode));
    QEventLinkChangeMessage msgLnkBegLoad = new QEventLinkChangeMessage(new Link(jobCode, load.getCode(), "LNK_BEG"), null, getToken());
    publishData(msgLnkBegLoad, recipientCodes);
    publishBaseEntityByCode(jobCode, "GRP_NEW_ITEMS", "LNK_CORE", recipientCodes);
    /* publishing to Owner */
    publishBE(getBaseEntityByCode(jobCode));
    if (!newJobDetails.getValue("PRI_JOB_IS_SUBMITTED", false)) {
        /* Sending Messages */
        println("new job");
        HashMap<String, String> contextMap = new HashMap<String, String>();
        contextMap.put("JOB", jobCode);
        contextMap.put("OWNER", getUser().getCode());
        println("The String Array is ::" + Arrays.toString(recipientCodes));
        /* Sending toast message to owner frontend */
        sendMessage("", recipientCodes, contextMap, "MSG_CH40_NEW_JOB_POSTED", "TOAST");
        /* Sending message to BEG OWNER */
        sendMessage("", recipientCodes, contextMap, "MSG_CH40_NEW_JOB_POSTED", "EMAIL");
    }
}
Also used : QEventLinkChangeMessage(life.genny.qwanda.message.QEventLinkChangeMessage) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BaseEntity(life.genny.qwanda.entity.BaseEntity) IOException(java.io.IOException) Answer(life.genny.qwanda.Answer) Link(life.genny.qwanda.Link)

Example 42 with BaseEntity

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

the class QRules method fastClearBaseEntity.

/* sets delete field to true so that FE removes the BE from their store */
public void fastClearBaseEntity(String baseEntityCode, String[] recipients) {
    BaseEntity be = new BaseEntity(baseEntityCode, "FastBE");
    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 43 with BaseEntity

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

the class QRules method updateCachedBaseEntity.

public BaseEntity updateCachedBaseEntity(final Answer answer) {
    BaseEntity cachedBe = this.getBaseEntityByCode(answer.getTargetCode());
    // Add an attribute if not already there
    try {
        answer.setAttribute(RulesUtils.attributeMap.get(answer.getAttributeCode()));
        if (answer.getAttribute() == null) {
            log.error("Null Attribute");
        } else
            cachedBe.addAnswer(answer);
        VertxUtils.writeCachedJson(answer.getTargetCode(), JsonUtils.toJson(cachedBe));
    } catch (BadDataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return cachedBe;
}
Also used : BadDataException(life.genny.qwanda.exception.BadDataException) BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 44 with BaseEntity

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

the class QRules method getBaseEntityValueAsString.

public String getBaseEntityValueAsString(final String baseEntityCode, final String attributeCode) {
    String attrValue = null;
    if (baseEntityCode != null) {
        BaseEntity be = getBaseEntityByCode(baseEntityCode);
        attrValue = getBaseEntityAttrValueAsString(be, attributeCode);
    }
    return attrValue;
}
Also used : BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 45 with BaseEntity

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

the class QRules method getBaseEntityByCode.

public BaseEntity getBaseEntityByCode(final String code) {
    BaseEntity be = null;
    be = VertxUtils.readFromDDT(code, getToken());
    if (be == null) {
        println("ERROR - be (" + code + ") fetched is NULL ");
    } else {
        addAttributes(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