Skip to content

Commit 7351f31

Browse files
committed
Moved export/import methods to Notebook.java
1 parent 6c19668 commit 7351f31

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,9 @@ public Response getNotebook(@PathParam("notebookId") String notebookId) throws I
157157
*/
158158
@GET
159159
@Path("export/{id}")
160-
public Response exportNoteBook(@PathParam("id") String noteId) {
161-
GsonBuilder gsonBuilder = new GsonBuilder();
162-
gsonBuilder.setPrettyPrinting();
163-
Gson gson = gsonBuilder.create();
164-
Note note = notebook.getNote(noteId);
165-
String json = gson.toJson(note);
166-
return new JsonResponse(Status.OK, "", json).build();
160+
public Response exportNoteBook(@PathParam("id") String noteId) throws IOException {
161+
String exportJson = notebook.exportNote(noteId);
162+
return new JsonResponse(Status.OK, "", exportJson).build();
167163
}
168164

169165
/**
@@ -175,28 +171,8 @@ public Response exportNoteBook(@PathParam("id") String noteId) {
175171
*/
176172
@POST
177173
@Path("import")
178-
public Response importNotebook(String req) {
179-
GsonBuilder gsonBuilder = new GsonBuilder();
180-
gsonBuilder.setPrettyPrinting();
181-
Gson gson = gsonBuilder.create();
182-
JsonReader reader = new JsonReader(new StringReader(req));
183-
reader.setLenient(true);
184-
Note newNote;
185-
try {
186-
Note oldNote = gson.fromJson(reader, Note.class);
187-
newNote = notebook.createNote();
188-
newNote.setName(oldNote.getName());
189-
List<Paragraph> paragraphs = oldNote.getParagraphs();
190-
for (Paragraph p : paragraphs) {
191-
newNote.addCloneParagraph(p);
192-
}
193-
194-
newNote.persist();
195-
} catch (IOException e) {
196-
return new JsonResponse(Status.INTERNAL_SERVER_ERROR).build();
197-
}
198-
notebookServer.broadcastNote(newNote);
199-
notebookServer.broadcastNoteList();
174+
public Response importNotebook(String req) throws IOException {
175+
Note newNote = notebook.importNote(req, null);
200176
return new JsonResponse<>(Status.CREATED, "", newNote.getId()).build();
201177
}
202178

zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.zeppelin.notebook;
1919

2020
import java.io.IOException;
21+
import java.io.StringReader;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.Comparator;
@@ -54,6 +55,9 @@
5455
import org.slf4j.Logger;
5556
import org.slf4j.LoggerFactory;
5657

58+
import com.google.gson.Gson;
59+
import com.google.gson.GsonBuilder;
60+
import com.google.gson.stream.JsonReader;
5761
/**
5862
* Collection of Notes.
5963
*/
@@ -150,6 +154,54 @@ public Note createNote(List<String> interpreterIds) throws IOException {
150154
note.persist();
151155
return note;
152156
}
157+
158+
/**
159+
* Export existing note.
160+
* @param noteId - the note ID to clone
161+
* @return Note JSON
162+
* @throws IOException
163+
*/
164+
public String exportNote(String noteId) throws IOException {
165+
GsonBuilder gsonBuilder = new GsonBuilder();
166+
gsonBuilder.setPrettyPrinting();
167+
Gson gson = gsonBuilder.create();
168+
Note note = getNote(noteId);
169+
return gson.toJson(note);
170+
}
171+
172+
/**
173+
* import JSON as a new note.
174+
* @param sourceJSON - the note JSON to import
175+
* @param noteName - the name of the new note
176+
* @return notebook ID
177+
* @throws IOException
178+
*/
179+
public Note importNote(String sourceJson, String noteName) throws IOException {
180+
GsonBuilder gsonBuilder = new GsonBuilder();
181+
gsonBuilder.setPrettyPrinting();
182+
Gson gson = gsonBuilder.create();
183+
JsonReader reader = new JsonReader(new StringReader(sourceJson));
184+
reader.setLenient(true);
185+
Note newNote;
186+
try {
187+
Note oldNote = gson.fromJson(reader, Note.class);
188+
newNote = createNote();
189+
if (noteName != null)
190+
newNote.setName(noteName);
191+
else
192+
newNote.setName(oldNote.getName());
193+
List<Paragraph> paragraphs = oldNote.getParagraphs();
194+
for (Paragraph p : paragraphs) {
195+
newNote.addCloneParagraph(p);
196+
}
197+
198+
newNote.persist();
199+
} catch (IOException e) {
200+
throw new IOException(e);
201+
}
202+
203+
return newNote;
204+
}
153205

154206
/**
155207
* Clone existing note.

0 commit comments

Comments
 (0)