Skip to content

Instantly share code, notes, and snippets.

@jongyoul
Created September 28, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongyoul/5723ce59bf5df4791ff8d9d6fd614c29 to your computer and use it in GitHub Desktop.
Save jongyoul/5723ce59bf5df4791ff8d9d6fd614c29 to your computer and use it in GitHub Desktop.
private void runParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook,
Message fromMessage) throws IOException {
final String paragraphId = (String) fromMessage.get("id");
if (paragraphId == null) {
return;
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal,
userAndRoles, notebookAuthorization.getWriters(noteId));
return;
}
Paragraph p = note.getParagraph(paragraphId);
String text = (String) fromMessage.get("paragraph");
p.setText(text);
p.setTitle((String) fromMessage.get("title"));
if (!fromMessage.principal.equals("anonymous")) {
AuthenticationInfo authenticationInfo = new AuthenticationInfo(fromMessage.principal,
fromMessage.ticket);
p.setAuthenticationInfo(authenticationInfo);
} else {
p.setAuthenticationInfo(new AuthenticationInfo());
}
Map<String, Object> params = (Map<String, Object>) fromMessage
.get("params");
p.settings.setParams(params);
Map<String, Object> config = (Map<String, Object>) fromMessage
.get("config");
p.setConfig(config);
// if it's the last paragraph, let's add a new one
boolean isTheLastParagraph = note.isLastParagraph(p.getId());
if (!(text.trim().equals(p.getMagic()) || Strings.isNullOrEmpty(text)) &&
isTheLastParagraph) {
note.addParagraph();
}
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
note.persist(subject);
try {
note.run(paragraphId);
} catch (Exception ex) {
LOG.error("Exception from run", ex);
if (p != null) {
p.setReturn(
new InterpreterResult(InterpreterResult.Code.ERROR, ex.getMessage()),
ex);
p.setStatus(Status.ERROR);
broadcast(note.getId(), new Message(OP.PARAGRAPH).put("paragraph", p));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment