Listen en reglas y visualización de tesoros

 

Aquí te dejamos el código utilizado en esta lección:

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

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

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

        TreasureSearch treasureSearch = searchTreasure.listTreasure.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 indice = ct.points.selected().get(0)

                newTreasure.points = ct.points.get(indice)
                newTreasure.win = User()

                broker.cloudPersistence.save(newTreasure)
             
                broker.ui.showAlert('Yeah!', "Thanks for adding a new treasure.")
            } else {
                broker.ui.showAlert("Oops", "Please select a diferent name, this name is already in use.")
            }
        } else {
             broker.ui.showAlert("Oops", "Please, complete all the fields.")
        }
    }
}