// author's key is null for GM
final Key authorKey = entry.getKey() == null ? null
: Data.Character.createKey(campaignKey, entry.getKey());
// read the current note from this author
CharacterNote noteDB = dao.getCharacterNoteByAuthor(context, characterKey, authorKey);
if(noteDB == null) {
noteDB = new CharacterNote(character);
noteDB.setAuthor(authorKey);
}
// Notes are never deleted, just set to null (usefull when checking deltas)
noteDB.setContent(entry.getValue());
dao.save(context, noteDB);
}
}
if(dead != null) {
character.setDead(dead);
}
// Only PCs can be locked
if(locked != null && !npc) {
character.setLocked(locked);
}
dao.save(context, character);
} else {
if(notes != null) {
if(byCharacterId == null) {
logger.severe("user " + context.getUser().getKey()
+ " has tried to update GM note on " + characterKey);
context.addError(USER_USURPATION_GM);
return null;
}
// players can only update their own.
final Key authorKey = Data.Character.createKey(campaignKey, byCharacterId);
// check the writer exists
final Data.Character author = dao.readCharacter(context, authorKey);
if(author == null) {
logger.severe("character not found " + characterKey);
context.addError(NOT_FOUND_CHARACTER, characterId);
return null;
}
// check the user is the writer's owner
if(!isOwner(context, author)) {
logger.severe("user " + context.getUser().getKey() + " has tried to update "
+ author.getOwner() + "'s note on " + characterKey);
context.addError(Errors.USER_USURPATION);
return null;
}
// update or create the note if not an NPC
CharacterNote noteDB = dao.getCharacterNoteByAuthor(context, characterKey, authorKey);
if(noteDB == null) {
noteDB = new CharacterNote(character);
noteDB.setAuthor(authorKey);
}
noteDB.setContent(notes.get(byCharacterId));
dao.save(context, noteDB);
}
}
tx.commit();