separate getAllNotes() and getAllNotes(subject)

This commit is contained in:
Khalid Huseynov 2016-09-17 15:11:40 +09:00
parent 17e2d4c0c5
commit b7f19c918b
3 changed files with 35 additions and 29 deletions

View file

@ -332,9 +332,7 @@ public class NotebookServer extends WebSocketServlet implements
private void broadcastToNoteBindedInterpreter(String interpreterGroupId,
Message m) {
Notebook notebook = notebook();
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
List<Note> notes = notebook.getAllNotes(subject);
List<Note> notes = notebook.getAllNotes();
for (Note note : notes) {
List<String> ids = notebook.getInterpreterFactory().getInterpreters(note.getId());
for (String id : ids) {
@ -859,9 +857,7 @@ public class NotebookServer extends WebSocketServlet implements
if (global) { // broadcast change to all web session that uses related
// interpreter.
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
for (Note n : notebook.getAllNotes(subject)) {
for (Note n : notebook.getAllNotes()) {
List<InterpreterSetting> settings = notebook.getInterpreterFactory()
.getInterpreterSettings(note.getId());
for (InterpreterSetting setting : settings) {
@ -1539,9 +1535,7 @@ public class NotebookServer extends WebSocketServlet implements
return;
}
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
List<Note> notes = notebook.getAllNotes(subject );
List<Note> notes = notebook.getAllNotes();
for (Note note : notes) {
if (object.getNoteId() != null && !note.getId().equals(object.getNoteId())) {
continue;
@ -1566,9 +1560,7 @@ public class NotebookServer extends WebSocketServlet implements
@Override
public void onRemove(String interpreterGroupId, String name, String noteId, String paragraphId) {
Notebook notebook = notebook();
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
List<Note> notes = notebook.getAllNotes(subject);
List<Note> notes = notebook.getAllNotes();
for (Note note : notes) {
if (noteId != null && !note.getId().equals(noteId)) {
continue;

View file

@ -578,6 +578,27 @@ public class Notebook implements NoteEventListener {
}
}
public List<Note> getAllNotes() {
synchronized (notes) {
List<Note> noteList = new ArrayList<>(notes.values());
Collections.sort(noteList, new Comparator<Note>() {
@Override
public int compare(Note note1, Note note2) {
String name1 = note1.getId();
if (note1.getName() != null) {
name1 = note1.getName();
}
String name2 = note2.getId();
if (note2.getName() != null) {
name2 = note2.getName();
}
return name1.compareTo(name2);
}
});
return noteList;
}
}
public List<Note> getAllNotes(AuthenticationInfo subject) {
synchronized (notes) {
List<Note> noteList = getAuthorizedNotes(subject);
@ -760,7 +781,7 @@ public class Notebook implements NoteEventListener {
}
}
List<Note> notes = getAllNotes(subject);
List<Note> notes = getAllNotes();
List<Map<String, Object>> notesInfo = new LinkedList<>();
for (Note note : notes) {
boolean isNotebookRunning = false;

View file

@ -135,15 +135,12 @@ public class NotebookTest implements JobListenerFactory{
File destDir = new File(notebookDir.getAbsolutePath() + "/2A94M5J1Z");
FileUtils.copyDirectory(srcDir, destDir);
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
// when load
notebook.reloadAllNotes(null);
assertEquals(1, notebook.getAllNotes(subject).size());
assertEquals(1, notebook.getAllNotes().size());
// then interpreter factory should be injected into all the paragraphs
Note note = notebook.getAllNotes(subject).get(0);
Note note = notebook.getAllNotes().get(0);
assertNull(note.getParagraphs().get(0).getRepl(null));
}
@ -166,17 +163,14 @@ public class NotebookTest implements JobListenerFactory{
logger.error(e.toString(), e);
}
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
// doesn't have copied notebook in memory before reloading
List<Note> notes = notebook.getAllNotes(subject);
List<Note> notes = notebook.getAllNotes();
assertEquals(notes.size(), 0);
// load copied notebook on memory when reloadAllNotes() is called
Note copiedNote = notebookRepo.get("2A94M5J1Z", null);
notebook.reloadAllNotes(null);
notes = notebook.getAllNotes(subject);
notes = notebook.getAllNotes();
assertEquals(notes.size(), 2);
assertEquals(notes.get(1).getId(), copiedNote.getId());
assertEquals(notes.get(1).getName(), copiedNote.getName());
@ -189,12 +183,12 @@ public class NotebookTest implements JobListenerFactory{
}
// keep notebook in memory before reloading
notes = notebook.getAllNotes(subject);
notes = notebook.getAllNotes();
assertEquals(notes.size(), 2);
// delete notebook from notebook list when reloadAllNotes() is called
notebook.reloadAllNotes(null);
notes = notebook.getAllNotes(subject);
notes = notebook.getAllNotes();
assertEquals(notes.size(), 0);
}
@ -211,7 +205,7 @@ public class NotebookTest implements JobListenerFactory{
logger.info("Loaded {} notes", notes1.size());
Note note = notebook.createNote(user1);
setNotePermissions(note.id(), user1.getUser(), true, true, true);
setNotePermissions(note.getId(), user1.getUser(), true, true, true);
notebook.reloadAllNotes(user1);
notes1 = notebook.getAllNotes(user1);
notebook.reloadAllNotes(user2);
@ -250,9 +244,8 @@ public class NotebookTest implements JobListenerFactory{
Notebook notebook2 = new Notebook(
conf, notebookRepo, schedulerFactory,
new InterpreterFactory(conf, null, null, null, depResolver), this, null, null, null);
//TODO(khalid): anonymous or specific user notes?
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
assertEquals(1, notebook2.getAllNotes(subject).size());
assertEquals(1, notebook2.getAllNotes().size());
}
@Test