Ciclo de vida de la experiencia y regla de búsqueda

 

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

Experience SearchTreasure {
    String textFirstSearch value("Search by points") as Label
    Bool searchByPoints as checkbox
    Array points as Select

    String textSecondSarch value("Search by treasure status") as Label
    Array statusTreasure as Select

    Array listTreasures 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)

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

Module Accounts {
    Array searchTreasures (Bool searchByPoint, Integer point, String treasureStatus) {
        Filter filter = Filter(modelName: “Treasure”)        

        if (searchByPoint == true) {
            filter.equalTo(“points”, point)
        }

        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
    }
}