Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ public static enum OP {
ANGULAR_OBJECT_UPDATE, // [s-c] add/update angular object
ANGULAR_OBJECT_REMOVE, // [s-c] add angular object del

ANGULAR_OBJECT_UPDATED // [c-s] angular object value updated
ANGULAR_OBJECT_UPDATED, // [c-s] angular object value updated

IMPORT_NOTEBOOK // [c-s] import notebook
// @param object notebook
}

public OP op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

import com.google.gson.JsonElement;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import org.apache.zeppelin.display.AngularObject;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.display.AngularObjectRegistryListener;
import org.apache.zeppelin.display.Input;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.notebook.JobListenerFactory;
Expand Down Expand Up @@ -152,6 +151,9 @@ public void onMessage(NotebookSocket conn, String msg) {
case ANGULAR_OBJECT_UPDATED:
angularObjectUpdated(conn, notebook, messagereceived);
break;
case IMPORT_NOTEBOOK:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have uniform naming? All the other operations are called XYZ_NOTE. Let's follow that here as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @mindprince, that make sense, did that. Thanks.

importNotebook(conn, notebook, messagereceived);
break;
default:
broadcastNoteList();
break;
Expand All @@ -171,7 +173,7 @@ public void onClose(NotebookSocket conn, int code, String reason) {
}
}

private Message deserializeMessage(String msg) {
protected Message deserializeMessage(String msg) {
return gson.fromJson(msg, Message.class);
}

Expand Down Expand Up @@ -467,6 +469,61 @@ private void cloneNote(NotebookSocket conn, Notebook notebook, Message fromMessa
broadcastNoteList();
}

protected Note importNotebook(NotebookSocket conn, Notebook notebook, Message fromMessage)
throws IOException {

Note note = notebook.createNote();
if (fromMessage != null) {
String noteName = (String) ((Map) fromMessage.get("notebook")).get("name");
if (noteName == null || noteName.isEmpty()) {
noteName = "Note " + note.getId();
}
note.setName(noteName);
ArrayList<Map> paragraphs = ((Map<String, ArrayList>) fromMessage.get("notebook"))
.get("paragraphs");
if (paragraphs.size() > 0) {
for (Map paragraph : paragraphs) {
try {
Paragraph p = note.addParagraph();
String text = (String) paragraph.get("text");
p.setText(text);
p.setTitle((String) paragraph.get("title"));
Map<String, Object> params = (Map<String, Object>) ((Map) paragraph
.get("settings")).get("params");
Map<String, Input> forms = (Map<String, Input>) ((Map) paragraph
.get("settings")).get("forms");
if (params != null) {
p.settings.setParams(params);
}
if (forms != null) {
p.settings.setForms(forms);
}
Map<String, Object> result = (Map) paragraph.get("result");
if (result != null) {
InterpreterResult.Code code = InterpreterResult.Code
.valueOf((String) result.get("code"));
InterpreterResult.Type type = InterpreterResult.Type
.valueOf((String) result.get("type"));
String msg = (String) result.get("msg");
p.setReturn(new InterpreterResult(code, type, msg), null);
}

Map<String, Object> config = (Map<String, Object>) paragraph
.get("config");
p.setConfig(config);
} catch (Exception e) {
LOG.error("Exception while setting parameter in paragraph", e);
}
}
}
}

note.persist();
broadcastNote(note);
broadcastNoteList();
return note;
}

private void removeParagraph(NotebookSocket conn, Notebook notebook,
Message fromMessage) throws IOException {
final String paragraphId = (String) fromMessage.get("id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private void createNewNote() {
notebookTitles.add(el.getText());
}

WebElement createNoteLink = driver.findElement(By.xpath("//div[contains(@class, \"col-md-4\")]/div/h5/a"));
WebElement createNoteLink = driver.findElement(By.xpath("//div[contains(@class, \"col-md-4\")]/div/h5/a[contains(.,'Create new note')]"));
createNoteLink.click();

WebDriverWait block = new WebDriverWait(driver, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ public void testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent() thr
notebook.removeNote(note1.getId());
}

@Test
public void testImportNotebook() throws IOException {
String msg = "{\"op\":\"IMPORT_NOTEBOOK\",\"data\":" +
"{\"notebook\":{\"paragraphs\": [{\"text\": \"Test " +
"paragraphs import\",\"config\":{},\"settings\":{}}]," +
"\"name\": \"Test Zeppelin notebook import\",\"config\": " +
"{}}}}";
Message messageReceived = notebookServer.deserializeMessage(msg);
Note note = null;
try {
note = notebookServer.importNotebook(null, notebook, messageReceived);
} catch (NullPointerException e) {
//broadcastNoteList(); failed nothing to worry.
}

assertNotEquals(null, notebook.getNote(note.getId()));
assertEquals("Test Zeppelin notebook import", notebook.getNote(note.getId()).getName());
assertEquals("Test paragraphs import", notebook.getNote(note.getId()).getParagraphs().get(0).getText());
notebook.removeNote(note.getId());
}

private NotebookSocket createWebSocket() {
NotebookSocket sock = mock(NotebookSocket.class);
when(sock.getRequest()).thenReturn(createHttpServletRequest());
Expand Down
Loading