mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
ZEPPELIN-511 Address @prabhjyotsingh reviews
Conflicts: zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java
This commit is contained in:
parent
01feed41bb
commit
59f5f61c1c
3 changed files with 47 additions and 21 deletions
|
|
@ -301,11 +301,15 @@ public class NotebookRestApi {
|
|||
return new JsonResponse(Status.NOT_FOUND, "paragraph not found.").build();
|
||||
}
|
||||
|
||||
note.moveParagraph(paragraphId, Integer.parseInt(newIndex));
|
||||
note.persist();
|
||||
notebookServer.broadcastNote(note);
|
||||
try {
|
||||
note.moveParagraph(paragraphId, Integer.parseInt(newIndex), true);
|
||||
|
||||
return new JsonResponse(Status.OK, "").build();
|
||||
note.persist();
|
||||
notebookServer.broadcastNote(note);
|
||||
return new JsonResponse(Status.OK, "").build();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return new JsonResponse(Status.BAD_REQUEST, "paragraph's new index is out of bound").build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -632,13 +632,15 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
// insert to index 0
|
||||
String jsonRequest2 = "{\"index\": 0, \"title\": \"title2\", \"text\": \"text2\"}";
|
||||
PostMethod post2 = httpPost("/notebook/" + note.getId() + "/paragraph", jsonRequest2);
|
||||
LOG.info("testInsertParagraph response2\n" + post.getResponseBodyAsString());
|
||||
assertThat("Test insert method:", post, isCreated());
|
||||
post.releaseConnection();
|
||||
LOG.info("testInsertParagraph response2\n" + post2.getResponseBodyAsString());
|
||||
assertThat("Test insert method:", post2, isCreated());
|
||||
post2.releaseConnection();
|
||||
|
||||
Paragraph paragraphAtIdx0 = note.getParagraphs().get(0);
|
||||
assertEquals("title2", paragraphAtIdx0.getTitle());
|
||||
assertEquals("text2", paragraphAtIdx0.getText());
|
||||
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -666,6 +668,8 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
assertEquals(p.getId(), body.get("id"));
|
||||
assertEquals("hello", body.get("title"));
|
||||
assertEquals("world", body.get("text"));
|
||||
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -677,8 +681,8 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
p.setText("text1");
|
||||
|
||||
Paragraph p2 = note.addParagraph();
|
||||
p.setTitle("title2");
|
||||
p.setText("text2");
|
||||
p2.setTitle("title2");
|
||||
p2.setText("text2");
|
||||
|
||||
note.persist();
|
||||
|
||||
|
|
@ -692,6 +696,12 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
assertEquals(p2.getId(), paragraphAtIdx0.getId());
|
||||
assertEquals(p2.getTitle(), paragraphAtIdx0.getTitle());
|
||||
assertEquals(p2.getText(), paragraphAtIdx0.getText());
|
||||
|
||||
PostMethod post2 = httpPost("/notebook/" + note.getId() + "/paragraph/" + p2.getId() + "/move/" + 10, "");
|
||||
assertThat("Test post method: ", post2, isBadRequest());
|
||||
post.releaseConnection();
|
||||
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -711,6 +721,8 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
Note retrNote = ZeppelinServer.notebook.getNote(note.getId());
|
||||
Paragraph retrParagrah = retrNote.getParagraph(p.getId());
|
||||
assertNull("paragraph should be deleted", retrParagrah);
|
||||
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,10 +49,9 @@ import com.google.gson.Gson;
|
|||
* Binded interpreters for a note
|
||||
*/
|
||||
public class Note implements Serializable, JobListener {
|
||||
transient Logger logger = LoggerFactory.getLogger(Note.class);
|
||||
private static final long serialVersionUID = 7920699076577612429L;
|
||||
|
||||
List<Paragraph> paragraphs = new LinkedList<>();
|
||||
final List<Paragraph> paragraphs = new LinkedList<>();
|
||||
private String name = "";
|
||||
private String id;
|
||||
|
||||
|
|
@ -245,12 +244,29 @@ public class Note implements Serializable, JobListener {
|
|||
* @param index new index
|
||||
*/
|
||||
public void moveParagraph(String paragraphId, int index) {
|
||||
moveParagraph(paragraphId, index, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move paragraph into the new index (order from 0 ~ n-1).
|
||||
*
|
||||
* @param paragraphId
|
||||
* @param index new index
|
||||
* @param throwWhenIndexIsOutOfBound whether throw IndexOutOfBoundException
|
||||
* when index is out of bound
|
||||
*/
|
||||
public void moveParagraph(String paragraphId, int index, boolean throwWhenIndexIsOutOfBound) {
|
||||
synchronized (paragraphs) {
|
||||
int oldIndex = -1;
|
||||
int oldIndex;
|
||||
Paragraph p = null;
|
||||
|
||||
if (index < 0 || index >= paragraphs.size()) {
|
||||
return;
|
||||
if (throwWhenIndexIsOutOfBound) {
|
||||
throw new IndexOutOfBoundsException("paragraph size is " + paragraphs.size() +
|
||||
" , index is " + index);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < paragraphs.size(); i++) {
|
||||
|
|
@ -263,14 +279,8 @@ public class Note implements Serializable, JobListener {
|
|||
}
|
||||
}
|
||||
|
||||
if (p == null) {
|
||||
return;
|
||||
} else {
|
||||
if (oldIndex < index) {
|
||||
paragraphs.add(index, p);
|
||||
} else {
|
||||
paragraphs.add(index, p);
|
||||
}
|
||||
if (p != null) {
|
||||
paragraphs.add(index, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue