Listen clause in rules and treasures visualization

 

Here is the lesson code:


RuleContext SearchTreasureContext {
Rule search listen (searchByPoints from SearchTreasure as searchTreasure) {
SearchTreasure exp = broker.ui.getDataSource()
Integer indexPoints = exp.points.selected().get(0)
Integer indexStatus = exp.statusTreasure.selected().get(0)

exp.listTreasures = Accounts.searchTreasures(exp.searchByPoints, exp.points.get(indexPoints), exp.statusTreasure.get(indexStatus))
}

Rule selectTreasure {
SearchTreasure searchTreasure = broker.ui.getDataSource()
Integer index = searchTreasure.listTreasures.selected().get(0)
searchTreasure.listTreasures.setSelected([])

TreasureSearch treasureSearch = searchTreasure.listTreasures.get(index)

Filter filter = Filter(modelName: “Treasure”)
filter.equalTo(“name”, treasureSearch.name)

Array treasures = broker.cloudPersistence.getWithFilter(filter)

broker.ui.push(“Treasure”, treasures.get(0))
}
}

RuleContext CreateTreasureContext {
Rule createTreasure {
CreateTreasure ct = broker.ui.getDataSource()

if (ct.name != null && ct.name != “” && ct.latitude != null && ct.longitude != null && ct.points.selected().size() > 0) {
if (Accounts.validateTreasureName(ct.name)) {
Treasure newTreasure = Treasure()
newTreasure.name = ct.name
newTreasure.latitude = ct.latitude
newTreasure.longitude = ct.longitude

Integer index = ct.points.selected().get(0)

newTreasure.points = ct.points.get(index)
newTreasure.win = User()
newTreasure.creator = loggedUser

broker.cloudPersistence.save(newTreasure)

broker.ui.showAlert(‘Yeah!’, “Thanks for adding a new treasure.”)

} else {
broker.ui.showAlert(“Oops”, “Please select a different name, this name is already in use.”)
}

} else {
broker.ui.showAlert(“Oops”, “Please, complete all the fields.”)
}
}
}

> Next