Through broker.cloudPersistence.* services, we can have access to functions related to cloud storage.Mat|r offers a shared database in the cloud, in which each application has its own instance. Thus, each user can store and retrieve information shared by all other users. The methods of this service use Mat|r models on their signatures. Thus, the models of the applications constitute the information that will be stored in the cloud. This allows the developers to work with the same models used in their business policies, providing speed and transparency and avoiding unnecessary parsing.

PUBLIC METHODS

void broker.cloudPersistence.save (Model modelToSave)

It saves a model in the cloud storage. This is a blocking method, which displays a loading indicator while it is being executed. If there is no internet connection, the changes will be persisted locally and they will bestored in the cloud until internet connection is reestablished.

If modelAGuradar has attributes of the model type, they will be also persisted and referenced in said attributes. A call to this method over a previous stored model, updates the information. If modelToSave has attributed of model type, they will be also persisted and referenced in said attributes. A call to this method over a previous stored model, updates the information.

Arguments
modelToSave Model: model to be persisted in the cloud storage.
Exceptions
WrongTypeOfArgumentsInCall – Wrong {argumentNumber} argument type. Found ‘{paramType}‘, required; ‘{expectedType}’ in call of method ‘{methodName}’.

void broker.cloudPersistence.saveCollection(Array<Model> collection)

It saves an Array of models in the cloud storage. This is a blocking method, which displays a loading indicator while it is being executed. If there is no internet connection, the changes will be persisted locally and they will be stored in the cloud until internet connection is reestablished. The result of calling this method is exactly the same of calling broker.cloudPersistence.save(aModel) on each element of the array, but in fastest and efficient way.

Argumentos
collection Array<Model>:Array of models to be persisted in the cloud storage.
Excepciones
WrongTypeOfArgumentsInCall – Wrong {argumentNumber} argument type. Found ‘{paramType}‘, required; ‘{expectedType}’ in call of method ‘{methodName}’.

void broker.cloudPersistence.remove(Model modelToDelete)

It deletes a model from the cloud. This is a blocking method which displays a loading indicator while it is being executed. If there is no internet connection, the changes are saved locally and they will persist in the cloud until internet connection is reestablished. If modelToDelete has model type attributes, they will not be deleted from the database. Only its references will be deleted.

Arguments
modelToDelete Model: model to be deleted from the cloud storage.
Exceptions
WrongTypeOfArgumentsInCall – Wrong {argumentNumber} argument type. Found ‘{paramType}‘, required; ‘{expectedType}’ in call of method ‘{methodName}’.
CloudOperationError – Error when trying to remove a not cloud model value.

Array <Model> broker.cloudPersistence.get(String modelName)

It retrieves a collection of models from the cloud storage. This is a blocking method which displays a loading indicator while it is being executed. If there is no internet connection, it returns the stored data in the local cache of the device.

Arguments
modelName String: name of the models to be retrieved from the cloud storage.
Return
Array<Model> An array of models of the nombreModel type, retrieved from the cloud storage.
Exceptions
ModelNotFound – Model modelName not found.

Array <Model> broker.cloudPersistence.getWithFilter(Filter filter)

It retrieves a collection of models from the cloud storage, filtering the results obtained in the filter, previously set up.

Arguments
filter Filter: filter configured to retrieve models from the cloud storage.
Return
Array<Model> An array of filtered models of the modelName type, retrieved from the cloud storage.

Array<Model> broker.cloudPersistence.getNextPage(PageFetcher paginator)

It returns the following page as an array of models of the type specified in the constructor of the filter. Its size is defined at the moment PageFetcher is created. If it is executed for the first time, it runs a query and returns the first page.

Arguments
paginator PageFetcher: object in charge of the paginator configuration to retrieve data from the cloud storage.
Return
Array<Model> A filtered model array, of the size specified in the paginator, retrieved from the cloud storage.
Exceptions
CloudServicePageRequestOutOfBound – Error when trying to request a page in out of bound index. Index value: ‘{value}’, pages: ‘{pages}’.

Array<Model> broker.cloudPersistence.getPreviousPage(PageFetcher paginator)

It returns the previous page as an array of models of the type specified in the constructor of the filter. The size is specified at the moment the PageFetcher is created. If it is executed for the first time, it runs a query and returns the first page.

Arguments
paginator PageFetcher: object in charge of the paginator configuration to retrieve data from the cloud storage.
Return
Array<Model> A filtered array of models, with the size specified in the paginator, retrieved from the cloud storage.
Excepciones
CloudServicePageRequestOutOfBound – Error when trying to request a page in out of bound index. Index value: ‘{value}’, pages: ‘{pages}’.

Array<Model> broker.cloudPersistence.resetPages(PageFetcher paginator)

It resets the page fetcher so that a query is run again and the first page is loaded.

Arguments
paginator PageFetcher: object in charge of the paginator configuration to retrieve data from the cloud storage.
Return
Array<Model> A filtered array of models, with the size specified in the paginator, retrieved from the cloud storage.

Model broker.cloudPersistence.appendToArray(Model model, String arrayAttributeName, Any valueToAdd)

It adds the valueToAdd in the array referenced in the attribute arrayAttributeName, present in the model. Although other users have modified the array, it is guaranteed that the object is added at the end of the array in the database. This means that integrity of the data will not be lost. The method returns the model, which has just been modified, updated.

Arguments
model Model: model (with the arrayAttributeName attribute) in which we want to add the object.
arrayAttributeName String: name of the attribute (of Array type) where we want to add the object.
valueToAdd [Model,String,Double,Integer,Bool,Date]: object to be added in model.arrayAttributeName.
Return
Model It returns the updated model after the adding operation.
Exceptions
AttributeNotFound – Attribute arrayAttributeName not found in model model.
WrongTypeOfArgumentsInCall – Error trying to appendToArray a value in a not collection attribute arrayAttributeName.
CloudOperationError – Error when trying to appendToArray a not cloud model value.

Model broker.cloudPersistence.removeFromArray(Model model, String arrayAttributeName, Any valueToDelete)

It deletes all the valueToDelete occurrences with an array referenced in the attribute arrayAttributeName present in the model. It is guaranteed that the modified array is the last image in the database. This means that the integrity of the data will not be lost. The method returns the model, which has just been modified, updated.

Arguments
model Model: model (with the attribute arrayAttributeName) in which we want to delete the object.
arrayAttributeName String: name of the attribute ( Array type ) in which we want to delete the object.
valueToDelete [Model,String,Double,Integer,Bool,Date]: object to be deleted in model.arrayAttributeName.
Return
Model It returns the updated model after the delete operation.
Exceptions
AttributeNotFound – Attribute arrayAttributeName not found in model model
WrongTypeOfArgumentsInCall – Error trying to removeFromArray a value in a not collection attribute arrayAttributeName.
CloudOperationError – Error when trying to removeFromArray a not cloud model value.

Model broker.cloudPersistence.appendToMap(Model model, String mapAttributeName, String keyValueToAdd, Any valueToAdd)

It adds the valueToAdd in the map referenced in the attribute mapAttributeName present in the model, with the key key. It is guaranteed that the modified array is the last image in the database. This means that integrity of the data will not be lost. The method returns the model, which has just been modified, updated.

Arguments
model Model: model (with the attribute mapAttributeName) in which we want to add the object.
mapAttributeName String:name of the attribute (map type) in which we want to add the object.
keyValueToAdd String: name of the key of the map in which we want to add the object.
valueToAdd [Model,String,Double,Integer,Bool,Date]: object to be added in model.mapAttributeName.
Return
Model It returns the updated model after the adding operation.
Exceptions
AttributeNotFound – Attribute mapAttributeName not found in model model
WrongTypeOfArgumentsInCall – Error trying to appendToMap a value in a not collection attribute mapAttributeName.
CloudOperationError – Error when trying to appendToMap a not cloud model value.

Model broker.cloudPersistence.removeFromMap(Model model, String mapAttributeName, String keyValueToDelete)

It deletes the object from map, corresponding to keyValueToDelete in the map referenced in the attribute mapAttributeName present in the model. It is guaranteed that the modified map is the last image in the database. This means that integrity of the data will not be lost. The method returns the model, which has just been modified, updated.

Arguments
model Model: model (with the attribute mapAttributeName) in which we want to delete the object.
mapAttributeName String: name of the attribute (map-type) in which we want to delete the object.
keyValueToDelete String: key of the object to be deleted in model.mapAttributeName.
Return
Model It returns the updated model after the delete operation.
Exceptions
AttributeNotFound – Attribute mapAttributeName not found in model model
WrongTypeOfArgumentsInCall – Error trying to removeFromMap a value in a not collection attribute mapAttributeName.
CloudOperationError – Error when trying to removeFromMap a not cloud model value.

Model broker.cloudPersistence.increment(Model model, String attributeName, Any valueToAdd)

It adds up the valueToAdd to the attribute attributeName present in the model. It is guarantedd that the addition will be done over the updated data, notwithstanding if other user have modified the attribute. This means that integrity of the data will not be lost. The method returns the model, which has just been modified, updated.

Arguments
model Model: model (with the attribute attributeName) in which we want to add.
attributeName String: name of the attribute in which we want to add the value valueToAdd.
valueToAdd [Double,Integer]: value to be added to model.attributeName.
Return
Model It returns the updated model after the adding operation.
Exceptions
AttributeNotFound – Attribute attributeName not found in model model
CloudOperationError – Error when trying to appendToArray a not cloud model value.

Example:

Experience Main {
    String name label("Main name")
    Decision rule1 action("MainContext.savePerson")      label("Save person")
    Decision rule2 action("MainContext.fetchAllPersons")   label("Fetch all persons")
    Decision rule3 action("MainContext.deletePerson")       label("Delete person")
    Decision rule4 action("MainContext.fetchWithFilter")  label("Fetch persons which ages are greater than 15")
    Decision rule5 action("MainContext.addTeam")       label("Add a team transactionally")
    Decision rule6 action("MainContext.incrementarAge")     label("Increment Age")
}

Model Person {
    String name
    Integer age
    Array<String> teams
}

RuleContext MainContext {

    Rule savePerson {
        Person p = Person(name: "pepe", age: 18, teams: [])
        //save a person model instance in cloud storage
        broker.cloudPersistence.save(p)
    }
    
    Rule fetchAllPersons {
        //fetch all value of type Person from cloud storage
        Array<Person> persons = broker.cloudPersistence.get("Person")
        broker.ui.showAlert("Number of persons: ", persons.size().toString())
    }
    
    Rule deletePerson {
        Array<Person> persons = broker.cloudPersistence.get("Person")
        Person firstPerson = persons.get(0)
        //delete a person model instance in cloud storage
        broker.cloudPersistence.remove(firstPerson)
    }
    
    Rule fetchWithFilter {
        Filter filter = Filter(modelName:"Person")
        filter.greaterThan("age", 15)
        //fetch persons which ages are greater than 15
        Array<Person> persons = broker.cloudPersistence.getWithFilter(filter)
        broker.ui.showAlert("Number of persons which ages are greater than 15 años", persons.size().toString())
    }
    
    Rule addTeam {
        Array<Person> persons = broker.cloudPersistence.get("Person")
        Person firstPerson = persons.get(0)
        //add transactionally a tean in the array of the first person
        broker.cloudPersistence.appendToArray(firstPerson, "teams", "Barcelona")
    }
    
    Rule incrementarAge {
        Array<Person> persons = broker.cloudPersistence.get("Person")
        Person firstPerson = persons.get(0)
        
        //update the age of firsPerson model instance
        Person updatedPerson = broker.cloudPersistence.increment(firstPerson, "age", 1)
        broker.ui.showAlert("updated person age", updatedPerson.age.toString())
    }

}