dao.queryScene(campaignKey), FilterOperator.GREATER_THAN, timestamp), null);
// register the scenes.
if(scenesDB != null) {
for(Scene sceneDB : scenesDB) {
final SceneSDO sceneSDO = readSceneSDO(context, sceneDB, null, character);
mapScenes.put(sceneDB.getKey(), sceneSDO);
}
}
// search new messages.
final List<Message> messages =
dao.asListOfMessages(context, dao.addMessageFilterOnTimestamp(
dao.queryMessage(campaignKey), FilterOperator.GREATER_THAN, timestamp), null);
// keep only visible messages.
keepOnlyVisibleMessages(messages, character.getKey(), true);
// dispatch messages on scenes
if(messages != null) {
for(Message message : messages) {
final Key sceneKey = message.getKey().getParent();
SceneSDO sceneSDO = mapScenes.get(sceneKey);
if(sceneSDO == null) {
// scene was not already loaded, so ...
final Scene scene = dao.readScene(context, sceneKey);
if(scene == null) {
logger.severe("Invalid scene " + sceneKey);
continue;
}
sceneSDO = readSceneSDO(context, scene, null, character);
mapScenes.put(sceneKey, sceneSDO);
}
// add the message to the scene.
if(sceneSDO.getMessages() == null) {
sceneSDO.setMessages(new ArrayList<Message>());
}
sceneSDO.getMessages().add(message);
}
}
scenes.addAll(mapScenes.values());
} else {
final String characterIndex = String.valueOf(characterId);
final Key sceneKey;
if(lastSceneId == null) {
// use current scene as start
sceneKey = character.getScene();
} else {
// get the "last scene"'s previous scene
final Key lastSceneKey = Scene.createKey(campaignKey, lastSceneId);
final Scene lastScene = dao.readScene(context, lastSceneKey);
if(lastScene == null) {
logger.severe("Invalid scene " + lastSceneKey);
context.addError(Errors.NOT_FOUND_SCENE, lastSceneKey);
return null;
}
if(!lastScene.getNonNullCharacters().contains(characterKey)) {
logger.severe("LastScene " + lastSceneKey
+ " is not associated with " + characterKey);
context.addError(Errors.IMPOSSIBLE);
return null;
}
// get the previous scene "for the current character"
sceneKey = lastScene.getPrevious(characterIndex);
}
if(sceneKey == null) {
logger.severe("cannot find a starting scene for " + characterKey);
context.addError(Errors.IMPOSSIBLE);
return null;
}
// Load the scene.
Scene scene = dao.readScene(context, sceneKey);
if(scene == null) {
logger.severe("Invalid scene " + sceneKey);
context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
return null;
}
// Iterate over the scenes to reach (if possible), 25 messages
int count = 0;
while(scene != null && count < PAGINATION_MESSAGES) {
// select all the messages of the scene, always, and order them.
final List<Message> messages = order(dao.asListOfMessages(
context, dao.queryMessage(scene.getKey()), null), scene.getFirstMessage());
// keep only visible messages
keepOnlyVisibleMessages(messages, characterKey, false);
// note : even scenes with 0 messages must be returned because off the intro !
final SceneSDO sceneSDO = readSceneSDO(context, scene, messages, character);
scenes.add(0, sceneSDO);
if(messages != null) {
count += messages.size();