Skip to content

Commit

Permalink
save Paragraph.text as list of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed Oct 7, 2016
1 parent 2a69495 commit 2bd5fa9
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 6 deletions.
Expand Up @@ -29,6 +29,8 @@
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.slf4j.Logger;
Expand Down Expand Up @@ -123,8 +125,9 @@ private Note getNote(String noteId) throws IOException {

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

Note note = gson.fromJson(json, Note.class);

Expand All @@ -146,6 +149,7 @@ public Note get(String noteId, AuthenticationInfo subject) throws IOException {
public void save(Note note, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);

Expand Down
Expand Up @@ -39,6 +39,8 @@
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job.Status;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.slf4j.Logger;
Expand Down Expand Up @@ -168,8 +170,9 @@ public List<NoteInfo> list(AuthenticationInfo subject) throws IOException {
private Note getNote(String key) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

S3Object s3object;
try {
Expand Down Expand Up @@ -208,6 +211,7 @@ public Note get(String noteId, AuthenticationInfo subject) throws IOException {
public void save(Note note, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);
String key = user + "/" + "notebook" + "/" + note.getId() + "/" + "note.json";
Expand Down
Expand Up @@ -42,6 +42,8 @@
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job.Status;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.slf4j.Logger;
Expand Down Expand Up @@ -160,8 +162,9 @@ private Note getNote(FileObject noteDir) throws IOException {

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

FileContent content = noteJson.getContent();
InputStream ins = content.getInputStream();
Expand Down Expand Up @@ -221,6 +224,7 @@ protected FileObject getRootDir() throws IOException {
public synchronized void save(Note note, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);

Expand Down
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.notebook.typeadapter;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import org.apache.zeppelin.notebook.Paragraph;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.InvocationTargetException;

/**
* Custom deserializer for Paragraph
*/
public class ParagraphDeserializer implements JsonDeserializer<Paragraph> {
private static Paragraph createParagraph() {
try {
Constructor<Paragraph> constructor = Paragraph.class.getDeclaredConstructor();
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance();
} catch (NoSuchMethodException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
return null;
}

private static Field getField(String fieldName) {
Class klass = Paragraph.class;
while (klass != Object.class) {
try {
Field field = klass.getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException e) {
}
klass = klass.getSuperclass();
}
return null;
}

private static void extractFieldValue(JsonDeserializationContext context, JsonObject json,
Paragraph dst, String fieldName) {
Field field = getField(fieldName);
if (field != null) {
Object value = context.deserialize(json.get(fieldName), field.getType());
try {
field.set(dst, value);
} catch (IllegalAccessException e) {
}
}
}

@Override
public Paragraph deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = (JsonObject) json;
Paragraph dst = createParagraph();
if (dst == null) {
return null;
}
extractFieldValue(context, jsonObject, dst, "title");
JsonElement textObject = jsonObject.get("text");
if (textObject != null) {
if (textObject.isJsonArray()) {
String[] value = context.deserialize(textObject, String[].class);
dst.setText(StringUtils.join(value, "\n"));
} else if (textObject.isJsonPrimitive()) {
dst.setText(textObject.getAsJsonPrimitive().getAsString());
}
}
extractFieldValue(context, jsonObject, dst, "user");
extractFieldValue(context, jsonObject, dst, "dateUpdated");
extractFieldValue(context, jsonObject, dst, "config");
extractFieldValue(context, jsonObject, dst, "settings");
extractFieldValue(context, jsonObject, dst, "jobName");
extractFieldValue(context, jsonObject, dst, "id");
extractFieldValue(context, jsonObject, dst, "result");
extractFieldValue(context, jsonObject, dst, "dateCreated");
extractFieldValue(context, jsonObject, dst, "dateStarted");
extractFieldValue(context, jsonObject, dst, "dateFinished");
extractFieldValue(context, jsonObject, dst, "status");
extractFieldValue(context, jsonObject, dst, "errorMessage");
extractFieldValue(context, jsonObject, dst, "progressUpdateIntervalMs");
return dst;
}
}
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.notebook.typeadapter;

import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.apache.zeppelin.notebook.Paragraph;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
* Custom serializer for Paragraph
*/
public class ParagraphSerializer implements JsonSerializer<Paragraph> {
private static Field getField(String fieldName) {
Class klass = Paragraph.class;
while (klass != Object.class) {
try {
Field field = klass.getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException e) {
}
klass = klass.getSuperclass();
}
return null;
}

private static void addFieldValue(JsonSerializationContext context, JsonObject json,
Paragraph src, String fieldName) {
Field field = getField(fieldName);
if (field != null) {
try {
Object value = field.get(src);
if (value != null) {
json.add(fieldName, context.serialize(value));
}
} catch (IllegalAccessException e) {
}
}
}

@Override
public JsonElement serialize(Paragraph src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject json = new JsonObject();
addFieldValue(context, json, src, "title");
String text = src.getText();
if (text != null) {
json.add("text", context.serialize(text.split("\n")));
}
addFieldValue(context, json, src, "user");
addFieldValue(context, json, src, "dateUpdated");
addFieldValue(context, json, src, "config");
addFieldValue(context, json, src, "settings");
addFieldValue(context, json, src, "jobName");
addFieldValue(context, json, src, "id");
addFieldValue(context, json, src, "result");
addFieldValue(context, json, src, "dateCreated");
addFieldValue(context, json, src, "dateStarted");
addFieldValue(context, json, src, "dateFinished");
addFieldValue(context, json, src, "status");
addFieldValue(context, json, src, "errorMessage");
addFieldValue(context, json, src, "progressUpdateIntervalMs");
return json;
}
}

0 comments on commit 2bd5fa9

Please sign in to comment.