Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Populating and Recollecting Example

Adrián Rivero edited this page May 7, 2017 · 2 revisions

In this example we'll log in an User to an application which will show his data and will permit to edit it and upload it to the server. Let's declare the User Model.

@LocalDBModel
@ServerModel(
    baseUrl="http://example.com",
    put={
        @ServerRequest(
            name = "auth"
            action = "/user/auth"
        ),
        @ServerRequest(
            name = "update",
            action = "/user/update/{query}"
        )
    }
)
public class User extends Model {
    int remote_id;
    
    @NotEmpty
    @Column 
    String name;

    @Column 
    String image;

    @NotEmpty @Email
    @Column 
    String email;

    @Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
    String password;

    boolean successful;
    String message;

    static void getResponse(Response response) {
        successful = response.isSuccessful();
        message = response.message();
    }
}

When the user is logged in the application, the data of the user will be downloaded from the server and stored in the Local Data Base, when this data is modified in the application it will be uploaded to the server and also stored in the Database. If an error occurs while authenticating, it will be shown in a Toast.

The recollection of data is also validated.

public class LoginActivity extends Activity {
    @Model 
    @UseModel
    @Recollect(validate=true)
    User_ user;

    @Click
    void btnLogin() {

        $PutModel(user).orderBy("auth");

        if (user.isSuccessful()) {
            $MainActivity();
            finish();
        } else {
            $Toast(user.getMessage());
        }

    }
}
public class MainActivity extends Activity {
    @Model
    @Populate
    @Recollect(validate=true)
    User user;

    @Click
    void btnSave() {
        $PutModel(user).query("user.getRemoteId()").orderBy("update");
        
        if (user.isSuccessful()) {
            $Toast("User Data has been saved");
        } else {
            $Toast(user.getMessage());
        }
    }
}