-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#117 Implemented mapping ODML objects into ElasticSearch. Finally.
- Loading branch information
rinkesj
committed
Jul 2, 2015
1 parent
b898269
commit e0d65f0
Showing
11 changed files
with
248 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/CustomEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata; | ||
|
||
import java.io.IOException; | ||
|
||
import odml.core.Section; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.codehaus.jackson.Version; | ||
import org.codehaus.jackson.map.DeserializationConfig.Feature; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.codehaus.jackson.map.module.SimpleModule; | ||
import org.springframework.data.elasticsearch.core.EntityMapper; | ||
|
||
public class CustomEntityMapper implements EntityMapper { | ||
|
||
protected Log log = LogFactory.getLog(getClass()); | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
public CustomEntityMapper() { | ||
|
||
objectMapper = new ObjectMapper(); | ||
objectMapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
// objectMapper.configure(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); need it ??? don't know now. | ||
|
||
SimpleModule module = new SimpleModule("section", Version.unknownVersion()); | ||
module.addSerializer(Section.class, new ODMLSectionSerializer()); | ||
module.addDeserializer(Section.class, new ODMLSectionDeserializer()); | ||
|
||
objectMapper.registerModule(module); | ||
} | ||
|
||
@Override | ||
public String mapToString(Object object) throws IOException { | ||
return objectMapper.writeValueAsString(object); | ||
} | ||
|
||
@Override | ||
public <T> T mapToObject(String source, Class<T> clazz) throws IOException { | ||
return objectMapper.readValue(source, clazz); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...in/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
|
||
import odml.core.Reader; | ||
import odml.core.Section; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.wicket.ajax.json.JSONException; | ||
import org.apache.wicket.ajax.json.JSONObject; | ||
import org.apache.wicket.ajax.json.XML; | ||
import org.codehaus.jackson.JsonParser; | ||
import org.codehaus.jackson.JsonProcessingException; | ||
import org.codehaus.jackson.map.DeserializationContext; | ||
import org.codehaus.jackson.map.JsonDeserializer; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
||
public class ODMLSectionDeserializer extends JsonDeserializer<Section> { | ||
|
||
protected Log log = LogFactory.getLog(getClass()); | ||
|
||
@Override | ||
public Section deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
long start = System.currentTimeMillis(); | ||
|
||
try { | ||
|
||
String jsonString = jp.getCodec().readTree(jp).toString(); | ||
JSONObject jsonObject = new JSONObject(jsonString); | ||
String xmlString = XML.toString(jsonObject); | ||
Reader reader = new Reader(); | ||
|
||
ByteArrayInputStream stream = new ByteArrayInputStream(xmlString.getBytes()); | ||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder dbuilder = dbf.newDocumentBuilder(); | ||
Document dom = dbuilder.parse(stream); | ||
Element rootElement = dom.getDocumentElement(); | ||
rootElement.setAttribute("version", "1.0"); | ||
|
||
stream = new ByteArrayInputStream(getStringFromDocument(dom).getBytes()); | ||
return reader.load(stream); | ||
} catch (JSONException e) { | ||
log.warn(e.getMessage(), e); | ||
} catch (Exception e) { | ||
log.warn(e.getMessage(), e); | ||
} finally { | ||
long end = System.currentTimeMillis(); | ||
log.warn("Deserialize time - " + (end - start) + " ms."); | ||
} | ||
return null; | ||
} | ||
|
||
public String getStringFromDocument(Document doc) { | ||
try { | ||
DOMSource domSource = new DOMSource(doc); | ||
StringWriter writer = new StringWriter(); | ||
StreamResult result = new StreamResult(writer); | ||
TransformerFactory tf = TransformerFactory.newInstance(); | ||
Transformer transformer = tf.newTransformer(); | ||
transformer.transform(domSource, result); | ||
return writer.toString(); | ||
} catch (TransformerException e) { | ||
log.warn(e.getMessage(), e); | ||
return ""; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import odml.core.Section; | ||
import odml.core.Writer; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.wicket.ajax.json.JSONException; | ||
import org.apache.wicket.ajax.json.JSONObject; | ||
import org.apache.wicket.ajax.json.XML; | ||
import org.codehaus.jackson.JsonGenerator; | ||
import org.codehaus.jackson.JsonProcessingException; | ||
import org.codehaus.jackson.map.JsonSerializer; | ||
import org.codehaus.jackson.map.SerializerProvider; | ||
|
||
public class ODMLSectionSerializer extends JsonSerializer<Section> { | ||
|
||
protected Log log = LogFactory.getLog(getClass()); | ||
|
||
@Override | ||
public void serialize(Section value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { | ||
long start = System.currentTimeMillis(); | ||
try { | ||
|
||
Writer wr = new Writer(value, true, false); | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
wr.write(stream, false, false); | ||
|
||
String xmlString = stream.toString("UTF-8"); | ||
JSONObject jsonObject = XML.toJSONObject(xmlString); | ||
String jsonString = jsonObject.toString(); | ||
|
||
jgen.writeRawValue(jsonString); | ||
|
||
} catch (JSONException e) { | ||
log.error(e.getMessage(), e); | ||
} finally { | ||
long end = System.currentTimeMillis(); | ||
log.error("Serialize time - " + (end - start) + " ms."); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.