public class AlexaSessionStateHandler extends Object implements AlexaStateHandler
Constructor and Description |
---|
AlexaSessionStateHandler(com.amazon.speech.speechlet.Session session)
Initializes a handler and applies an Alexa Session to persist models.
|
Modifier and Type | Method and Description |
---|---|
<TModel extends AlexaStateModel> |
createModel(Class<TModel> modelClass)
Creates an instance of your model class and binds it to this handler.
|
<TModel extends AlexaStateModel> |
createModel(Class<TModel> modelClass,
String id)
Creates an instance of your model class and binds it to this handler.
|
<TModel extends AlexaStateModel> |
exists(Class<TModel> modelClass)
Looks for a model in the persistence store.
|
<TModel extends AlexaStateModel> |
exists(Class<TModel> modelClass,
AlexaScope scope)
Looks for a model in the persistence store in a given scope.
|
<TModel extends AlexaStateModel> |
exists(Class<TModel> modelClass,
String id)
Looks for a model with given id in the persistence store.
|
<TModel extends AlexaStateModel> |
exists(Class<TModel> modelClass,
String id,
AlexaScope scope)
Looks for a model with the given id in the persistence store in a given scope.
|
boolean |
exists(String id)
Looks for a state object with the given id.
|
boolean |
exists(String id,
AlexaScope scope)
Looks for a state object with the given id in the given scope.
|
com.amazon.speech.speechlet.Session |
getSession()
Returns the Alexa Session object which was given to this handler.
|
String |
getUserId()
Gets the userId used as a key when storing user-scoped model-state.
|
<TModel extends AlexaStateModel> |
readModel(Class<TModel> modelClass)
Reads out the model from the persistence store of this handler.
|
<TModel extends AlexaStateModel> |
readModel(Class<TModel> modelClass,
String id)
Reads out the model from the persistence store of this handler.
|
<TModel extends AlexaStateModel> |
readModels(Class<TModel> modelClass,
Collection<String> ids)
Reads out models from the persistence store of this handler.
|
Optional<AlexaStateObject> |
readValue(String id)
Reads a single object value from the persistence store.
|
Optional<AlexaStateObject> |
readValue(String id,
AlexaScope scope)
Reads a single object value from the persistence store.
|
Map<String,AlexaStateObject> |
readValues(Collection<String> ids)
Reads multiple single object values from the persistence store.
|
Map<String,AlexaStateObject> |
readValues(Collection<String> ids,
AlexaScope scope)
Reads multiple single object values from the persistence store in a specific scope.
|
Map<String,AlexaStateObject> |
readValues(Map<String,AlexaScope> idsInScope)
Reads multiple single object values from the persistence store in scopes.
|
void |
removeModel(AlexaStateModel model)
The given model will be removed from the persistence store.
|
void |
removeModels(Collection<? extends AlexaStateModel> models)
The given models will be removed from the persistence store.
|
void |
removeValue(String id)
Removes a single value state from the persistence store with the given key used
when single value was written in store.
|
void |
removeValues(Collection<String> ids)
Removes multiple value states from the persistence store with given keys used
when those values were written in store.
|
void |
setUserId(String userId)
sets the userId used as a key when storing user-scoped model-state
If no userId is provided the handler will use userId coming in with the session
Note, the userId from Alexa will change when a user re-enables your skill
|
AlexaStateHandler |
withUserId(String userId)
sets the userId used as a key when storing user-scoped model-state
If no userId is provided the handler will use userId coming in with the session
Note, the userId from Alexa will change when a user re-enables your skill
|
void |
writeModel(AlexaStateModel model)
The given model will be saved in the persistence store according to its
AlexaStateSave -annotations. |
void |
writeModels(Collection<? extends AlexaStateModel> models)
The given models will be saved in the persistence store according to their
AlexaStateSave -annotations. |
void |
writeValue(AlexaStateObject stateObject)
The value of a given state object will be saved with its id in the persistence store.
|
void |
writeValue(String id,
Object value)
The given value will be saved with the given id in the persistence store.
|
void |
writeValue(String id,
Object value,
AlexaScope scope)
The given value will be saved with the given id in the persistence store.
|
void |
writeValues(Collection<? extends AlexaStateObject> stateObjects)
Values of given state objects will be saved with their ids in the persistence store.
|
public AlexaSessionStateHandler(com.amazon.speech.speechlet.Session session)
session
- The Alexa session of your current skill invocation.public com.amazon.speech.speechlet.Session getSession()
getSession
in interface AlexaStateHandler
public final void setUserId(String userId)
userId
- userId used as a key when storing user-scoped model-statepublic final String getUserId()
public AlexaStateHandler withUserId(String userId)
userId
- userId used as a key when storing user-scoped model-statepublic <TModel extends AlexaStateModel> TModel createModel(Class<TModel> modelClass)
AlexaStateModel.saveState()
or AlexaStateModel.removeState()
)
This really much does the same as what you can achieve with AlexaStateModelFactory.createModel(Class, AlexaStateHandler)
.
Of course you can initialize your models with its own constructor but make sure you setHandler() before
calling aforementioned methods of this model.
By using this method you are not assigning an id to the model. Your model behaves like a singleton within
its AlexaScope (Session, User or Application) and is overridden on AlexaStateModel.saveState()
if already existent.createModel
in interface AlexaStateHandler
TModel
- type derived from AlexaStateModel
modelClass
- type of your model you want to initialize. Model has to derive from AlexaStateModel
public <TModel extends AlexaStateModel> TModel createModel(Class<TModel> modelClass, String id)
AlexaStateModel.saveState()
or AlexaStateModel.removeState()
)
This really much does the same as what you can achieve with AlexaStateModelFactory.createModel(Class, AlexaStateHandler)
.
Of course you can initialize your models with its own constructor but make sure you AlexaStateModel.setHandler(AlexaStateHandler)
before
calling aforementioned methods of this model.
By using this method you will assign an id to the model. From now on this id is used to read and
write to the persistence store associated with the handler. It gives you freedom to have more than one of a kind
of your model within its AlexaScope
(Session, User, Application)createModel
in interface AlexaStateHandler
TModel
- type derived from AlexaStateModel
modelClass
- type of your model you want to initialize. Model has to derive from AlexaStateModel
id
- An id unique within a model's AlexaScope
. If a model with this id is already existing in the persistence store it will be overridden on AlexaStateModel.saveState()
.public void writeModel(AlexaStateModel model) throws AlexaStateException
AlexaStateSave
-annotations.
If you set up an Id for the model it will be accessible with it on later reads to the handler.writeModel
in interface AlexaStateHandler
model
- Your model which needs to be a type of AlexaStateModel
AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void writeModels(Collection<? extends AlexaStateModel> models) throws AlexaStateException
AlexaStateSave
-annotations.
If you set up an Id for the model it will be accessible with it on later reads to the handler.
If you have multiple models to save always prefer this method as it tries to batch process
the models so it reduces the number of write-transactions to one.writeModels
in interface AlexaStateHandler
models
- list of models to save stateAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void writeValue(String id, Object value) throws AlexaStateException
writeValue
in interface AlexaStateHandler
id
- the key used when writing value to the storevalue
- the actual object whose value will be savedAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void writeValue(String id, Object value, AlexaScope scope) throws AlexaStateException
writeValue
in interface AlexaStateHandler
id
- the key used when writing value to the storevalue
- the actual object whose value will be savedscope
- the scope the value is savedAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void writeValue(AlexaStateObject stateObject) throws AlexaStateException
writeValue
in interface AlexaStateHandler
stateObject
- the state object to write to the persistence storeAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void writeValues(Collection<? extends AlexaStateObject> stateObjects) throws AlexaStateException
writeValues
in interface AlexaStateHandler
stateObjects
- the state objects to write to the persistence storeAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void removeModel(AlexaStateModel model) throws AlexaStateException
removeModel
in interface AlexaStateHandler
model
- Your model which needs to be a type of AlexaStateModel
AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void removeModels(Collection<? extends AlexaStateModel> models) throws AlexaStateException
AlexaStateHandler
removeModels
in interface AlexaStateHandler
models
- Your models which need to be a type of AlexaStateModel
AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void removeValue(String id) throws AlexaStateException
removeValue
in interface AlexaStateHandler
id
- the key of a single value object whose state you want to remove from the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic void removeValues(Collection<String> ids) throws AlexaStateException
AlexaStateHandler
removeValues
in interface AlexaStateHandler
ids
- the keys of single value objects whose state you want to remove from the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> Optional<TModel> readModel(Class<TModel> modelClass) throws AlexaStateException
AlexaScope
s configured in
the AlexaStateSave
annotations it will possibly collect data from more than one persistence store. (most
likely from the Alexa session and maybe from the permanent persistence store associated with this handler.
You are not providing an id with this call. Thus it will look for the single representative without an id.readModel
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to read out. It needs to be of type AlexaStateModel
.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> Optional<TModel> readModel(Class<TModel> modelClass, String id) throws AlexaStateException
AlexaScope
s configured in
the AlexaStateSave
annotations it will possibly collect data from more than one persistence store. (most
likely from the Alexa session and maybe from the permanent persistence store associated with this handler.
You are providing an id with this call. If you never saved the model with this id in the store you won't get anything.readModel
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to read out. It needs to be of type AlexaStateModel
.id
- The id of an existing instance of your model in the persistence store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> Map<String,TModel> readModels(Class<TModel> modelClass, Collection<String> ids) throws AlexaStateException
AlexaStateHandler
AlexaScope
s configured in
the AlexaStateSave
annotations it will possibly collect data from more than one persistence store. (most
likely from the Alexa session and maybe from the permanent persistence store associated with this handler.)
You are providing ids with this call. If you never saved any model with one of the ids in the store you won't get them.readModels
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to read out. It needs to be of type AlexaStateModel
.ids
- Collection of ids of existing instances of your model in the persistence store. If you provide ids of non-existing items they just won't show up in the returned map.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> boolean exists(Class<TModel> modelClass) throws AlexaStateException
exists
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to check for existence. It needs to be of type AlexaStateModel
.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> boolean exists(Class<TModel> modelClass, String id) throws AlexaStateException
exists
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to check for existence. It needs to be of type AlexaStateModel
.id
- the id of a modelAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> boolean exists(Class<TModel> modelClass, AlexaScope scope) throws AlexaStateException
exists
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to check for existence. It needs to be of type AlexaStateModel
.scope
- scope in which to test existence of modelAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic <TModel extends AlexaStateModel> boolean exists(Class<TModel> modelClass, String id, AlexaScope scope) throws AlexaStateException
exists
in interface AlexaStateHandler
TModel
- Type derived from AlexaStateModel
modelClass
- Type of the model you would like to check for existence. It needs to be of type AlexaStateModel
.id
- the id of a modelscope
- scope in which to test existence of modelAlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic boolean exists(String id) throws AlexaStateException
exists
in interface AlexaStateHandler
id
- id of a state-object whose existence you want to be checked in the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic boolean exists(String id, AlexaScope scope) throws AlexaStateException
exists
in interface AlexaStateHandler
id
- id of a state-object whose existence you want to be checked in the store.scope
- look for state-object in that scope.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic Optional<AlexaStateObject> readValue(String id) throws AlexaStateException
readValue
in interface AlexaStateHandler
id
- id of a state-object whose state you want to read from the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic Optional<AlexaStateObject> readValue(String id, AlexaScope scope) throws AlexaStateException
readValue
in interface AlexaStateHandler
id
- id of a state-object whose state you want to read from the store.scope
- read state-object in that scope.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic Map<String,AlexaStateObject> readValues(Collection<String> ids) throws AlexaStateException
readValues
in interface AlexaStateHandler
ids
- ids of state-objects whose state you want to read from the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic Map<String,AlexaStateObject> readValues(Collection<String> ids, AlexaScope scope) throws AlexaStateException
readValues
in interface AlexaStateHandler
ids
- ids of state-objects whose state you want to read from the store.scope
- read state-objects in that scope.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelpublic Map<String,AlexaStateObject> readValues(Map<String,AlexaScope> idsInScope) throws AlexaStateException
readValues
in interface AlexaStateHandler
idsInScope
- ids with scope of state-objects whose state you want to read from the store.AlexaStateException
- Wraps all inner exceptions and gives you context related to handler and modelCopyright © 2017. All rights reserved.