com.strongloop.android.loopback

## Class UserRepository<U extends User>

- java.lang.Object
- - com.strongloop.android.remoting.Repository<T>
  - - com.strongloop.android.loopback.RestRepository<T>
    - - com.strongloop.android.loopback.ModelRepository<U>
      - - com.strongloop.android.loopback.UserRepository<U>

    public class UserRepository<U extends User>
    extends ModelRepository<U>

A base class implementing `ModelRepository` for the built-in User type.

    UserRepository<MyUser> userRepo = new UserRepository<MyUser>("user", MyUser.class);

Most application are extending the built-in User model and adds new properties like address, etc. You should create your own Repository class by extending this base class in such case.

    public class Customer extends User {
       // your custom properties and prototype (instance) methods
     }

     public class CustomerRepository extends UserRepository<Customer> {
         public interface LoginCallback extends LoginCallback<Customer> {
         }

         public CustomerRepository() {
             super("customer", null, Customer.class);
         }

         // your custom static methods
     }

### Nested Class Summary

Nested Classes
`static interface` `UserRepository.LoginCallback<U>` Callback passed to loginUser to receive success and newly created `U` user instance or thrown error.

### Nested classes/interfaces inherited from class com.strongloop.android.loopback.ModelRepository

`ModelRepository.FindAllCallback<T extends Model>, ModelRepository.FindCallback<T extends Model>`

### Field Summary

Fields
`static java.lang.String` `PROPERTY_CURRENT_USER_ID`
`static java.lang.String` `SHARED_PREFERENCES_NAME`

### Constructor Summary

Constructors
Constructor and Description
`UserRepository(java.lang.String className, java.lang.Class<U> userClass)` Creates a new UserRepository, associating it with the static `U` user class and the user class name.
`UserRepository(java.lang.String className, java.lang.String nameForRestUrl, java.lang.Class<U> userClass)` Creates a new UserRepository, associating it with the static `U` user class and the user class name.

### Method Summary

Methods
`RestContract` `createContract()` Creates a `RestContract` representing the user type's custom routes.
`U` `createUser(java.lang.String email, java.lang.String password)` Creates a `U` user instance given an email and a password.
`U` `createUser(java.lang.String email, java.lang.String password, java.util.Map<java.lang.String,? extends java.lang.Object> parameters)` Creates a new `U` user given the email, password and optional parameters.
`void` `findCurrentUser(ObjectCallback<U> callback)` Fetch the data of the currently logged in user.
`U` `getCachedCurrentUser()` Get the cached value of the currently logged in user.
`java.lang.Object` `getCurrentUserId()`
`void` `loginUser(java.lang.String email, java.lang.String password, UserRepository.LoginCallback<U> callback)` Login a user given an email and password.
`void` `logout(VoidCallback callback)` Logs the current user out of the server and removes the access token from the system.
`protected void` `setCurrentUserId(java.lang.Object currentUserId)`

### Methods inherited from class com.strongloop.android.loopback.ModelRepository

`createModel, createObject, findAll, findById, getNameForRestUrl`

### Methods inherited from class com.strongloop.android.loopback.RestRepository

`getApplicationContext, getRestAdapter`

### Methods inherited from class com.strongloop.android.remoting.Repository

`getAdapter, getClassName, invokeStaticMethod, invokeStaticMethod, setAdapter`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### SHARED_PREFERENCES_NAME

    public static final java.lang.String SHARED_PREFERENCES_NAME

#### PROPERTY_CURRENT_USER_ID

    public static final java.lang.String PROPERTY_CURRENT_USER_ID

See Also:
  Constant Field Values

### Constructor Detail

#### UserRepository

    public UserRepository(java.lang.String className,
                  java.lang.Class<U> userClass)

Creates a new UserRepository, associating it with the static `U` user class and the user class name.

Parameters:
  `className` - The remote class name.
  `userClass` - The User (sub)class. It must have a public no-argument constructor.

#### UserRepository

    public UserRepository(java.lang.String className,
                  java.lang.String nameForRestUrl,
                  java.lang.Class<U> userClass)

Creates a new UserRepository, associating it with the static `U` user class and the user class name.

Parameters:
  `className` - The remote class name.
  `nameForRestUrl` - The pluralized class name to use in REST transport. Use `null` for the default value, which is the plural form of className.
  `userClass` - The User (sub)class. It must have a public no-argument constructor.

### Method Detail

#### getCurrentUserId

    public java.lang.Object getCurrentUserId()

Returns:
  Id of the currently logged in user. null when there is no user logged in.

#### setCurrentUserId

    protected void setCurrentUserId(java.lang.Object currentUserId)

#### findCurrentUser

    public void findCurrentUser(ObjectCallback<U> callback)

Fetch the data of the currently logged in user. Invokes `callback.onSuccess(null)` when no user is logged in. The data is cached, see `getCachedCurrentUser()`

Parameters:
  `callback` - success/error callback

#### getCachedCurrentUser

    public U getCachedCurrentUser()

Get the cached value of the currently logged in user. The value is updated by `findCurrentUser(ObjectCallback)`, `loginUser(String, String, LoginCallback)` and `logout(VoidCallback)`

The typical usage:

- At the application start up and after a successfull login, `findCurrentUser(ObjectCallback)` is called to pre-load the data.
- All other places call `getCachedCurrentUser()` to access the data of the currently logged in user.

Returns:
  The current user or null.

#### createUser

    public U createUser(java.lang.String email,
               java.lang.String password)

Creates a `U` user instance given an email and a password.

Parameters:
  `email` -
  `password` -
Returns:
  A `U` user instance.

#### createContract

    public RestContract createContract()

Creates a `RestContract` representing the user type's custom routes. Used to extend an `Adapter` to support user. Calls super `ModelRepository` createContract first.

Overrides:
  `createContract` in class `ModelRepository<U extends User>`
Returns:
  A `RestContract` for this model type.

#### createUser

    public U createUser(java.lang.String email,
               java.lang.String password,
               java.util.Map<java.lang.String,? extends java.lang.Object> parameters)

Creates a new `U` user given the email, password and optional parameters.

Parameters:
  `email` - - user email
  `password` - - user password
  `parameters` - - optional parameters
Returns:
  A new `U` user instance.

#### loginUser

    public void loginUser(java.lang.String email,
                 java.lang.String password,
                 UserRepository.LoginCallback<U> callback)

Login a user given an email and password. Creates a `AccessToken` and `U` user models if successful.

Parameters:
  `email` - - user email
  `password` - - user password
  `callback` - - success/error callback

#### logout

    public void logout(VoidCallback callback)

Logs the current user out of the server and removes the access token from the system.

Parameters:
  `callback` - The callback to be executed when finished.