mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
Moved export/import methods to Notebook.java
This commit is contained in:
parent
6c19668c53
commit
7351f31963
2 changed files with 57 additions and 29 deletions
|
|
@ -157,13 +157,9 @@ public class NotebookRestApi {
|
|||
*/
|
||||
@GET
|
||||
@Path("export/{id}")
|
||||
public Response exportNoteBook(@PathParam("id") String noteId) {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
Note note = notebook.getNote(noteId);
|
||||
String json = gson.toJson(note);
|
||||
return new JsonResponse(Status.OK, "", json).build();
|
||||
public Response exportNoteBook(@PathParam("id") String noteId) throws IOException {
|
||||
String exportJson = notebook.exportNote(noteId);
|
||||
return new JsonResponse(Status.OK, "", exportJson).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,28 +171,8 @@ public class NotebookRestApi {
|
|||
*/
|
||||
@POST
|
||||
@Path("import")
|
||||
public Response importNotebook(String req) {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
JsonReader reader = new JsonReader(new StringReader(req));
|
||||
reader.setLenient(true);
|
||||
Note newNote;
|
||||
try {
|
||||
Note oldNote = gson.fromJson(reader, Note.class);
|
||||
newNote = notebook.createNote();
|
||||
newNote.setName(oldNote.getName());
|
||||
List<Paragraph> paragraphs = oldNote.getParagraphs();
|
||||
for (Paragraph p : paragraphs) {
|
||||
newNote.addCloneParagraph(p);
|
||||
}
|
||||
|
||||
newNote.persist();
|
||||
} catch (IOException e) {
|
||||
return new JsonResponse(Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
notebookServer.broadcastNote(newNote);
|
||||
notebookServer.broadcastNoteList();
|
||||
public Response importNotebook(String req) throws IOException {
|
||||
Note newNote = notebook.importNote(req, null);
|
||||
return new JsonResponse<>(Status.CREATED, "", newNote.getId()).build();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
package org.apache.zeppelin.notebook;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
|
@ -54,6 +55,9 @@ import org.quartz.impl.StdSchedulerFactory;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
/**
|
||||
* Collection of Notes.
|
||||
*/
|
||||
|
|
@ -150,6 +154,54 @@ public class Notebook {
|
|||
note.persist();
|
||||
return note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export existing note.
|
||||
* @param noteId - the note ID to clone
|
||||
* @return Note JSON
|
||||
* @throws IOException
|
||||
*/
|
||||
public String exportNote(String noteId) throws IOException {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
Note note = getNote(noteId);
|
||||
return gson.toJson(note);
|
||||
}
|
||||
|
||||
/**
|
||||
* import JSON as a new note.
|
||||
* @param sourceJSON - the note JSON to import
|
||||
* @param noteName - the name of the new note
|
||||
* @return notebook ID
|
||||
* @throws IOException
|
||||
*/
|
||||
public Note importNote(String sourceJson, String noteName) throws IOException {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
JsonReader reader = new JsonReader(new StringReader(sourceJson));
|
||||
reader.setLenient(true);
|
||||
Note newNote;
|
||||
try {
|
||||
Note oldNote = gson.fromJson(reader, Note.class);
|
||||
newNote = createNote();
|
||||
if (noteName != null)
|
||||
newNote.setName(noteName);
|
||||
else
|
||||
newNote.setName(oldNote.getName());
|
||||
List<Paragraph> paragraphs = oldNote.getParagraphs();
|
||||
for (Paragraph p : paragraphs) {
|
||||
newNote.addCloneParagraph(p);
|
||||
}
|
||||
|
||||
newNote.persist();
|
||||
} catch (IOException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
|
||||
return newNote;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone existing note.
|
||||
|
|
|
|||
Loading…
Reference in a new issue