-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rest Apis to export/import with common code base #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
3645354
0e94dce
9b64a66
ecb8f1e
6c19668
7351f31
db8b016
b080d7d
630664c
ec14034
db6a580
4be62f6
e928c14
e282958
4cb69be
270e17b
f8a992c
f8bf1f3
869b48f
f5b0805
042d9af
83e7a6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.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,57 @@ public Note createNote(List<String> interpreterIds) throws IOException { | |
| note.persist(); | ||
| return note; | ||
| } | ||
|
|
||
| /** | ||
| * Export existing note. | ||
| * @param noteId - the note ID to clone | ||
| * @return Note JSON | ||
| * @throws IOException, IllegalArgumentException | ||
| */ | ||
| public String exportNote(String noteId) throws IOException, IllegalArgumentException { | ||
| GsonBuilder gsonBuilder = new GsonBuilder(); | ||
| gsonBuilder.setPrettyPrinting(); | ||
| Gson gson = gsonBuilder.create(); | ||
| Note note = getNote(noteId); | ||
| if (note == null) { | ||
| throw new IllegalArgumentException(noteId + "not found"); | ||
| } | ||
| return gson.toJson(note); | ||
| } | ||
|
|
||
| /** | ||
| * import JSON as a new note. | ||
| * @param sourceJSON - the note JSON to import | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sourceJSON -> sourceJson |
||
| * @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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry coming in late - could we log here alternatively, let's remove the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @felixcheung , we need the catch to log the exception right? so i will just add logger?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, this is great |
||
| } | ||
|
|
||
| return newNote; | ||
| } | ||
|
|
||
| /** | ||
| * Clone existing note. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest
noteId + " not found"for extra space