Experience life cycle and search rule

Here is the lesson code:

Experience SearchTreasure {
String textFirstSearch value("Search by points") as Label
Bool searchByPoints value(false) as Checkbox
Array points value([10, 20, 30, 50, 100]) as Select

String textSecondSearch value("Search by treasure status") as Label
Array statusTreasure value(["ALL", "FOUND", "NOT FOUND"]) as Select

Array listTreasures value([]) as List

OnResume {
SearchTreasure exp = broker.ui.getDataSource()

if (exp.listTreasures == null) {
exp.searchByPoint = false
exp.points = treasureScores
exp.statusTreasure = ["ALL", "FOUND", "NOT FOUND"]
exp.listTreasures = []
}
}
}

RuleContext SearchTreasureContext {
Rule search {
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))
}
}

Module Accounts {
Array searchTreasures (Bool searchByPoints, Integer points, String treasureStatus) {
Filter filter = Filter(modelName: "Treasure")

if (searchByPoint == true) {
filter.equalTo("points", points)
}

if (treasureStatus == "FOUND") {
filter.equalTo("hasBeenFound", true)

} else if (treasureStatus == "NOT FOUND") {
filter.equalTo("hasBeenFound", false)
}

Array treasures = broker.cloudPersistence.getWithFilter(filter)

Array treasuresSearch = []

for treasure in treasures {
treasuresSearch.add(TreasureSearch(name: treasure.name, points: treasure.points, hasBeenFound: treasure.hasBeenFound.toString(), creatorName: treasure.creator.name))
}

return treasuresSearch
}
}

> Next