use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.
the class OutputContainerSerializerTest method setUp.
@Before
public void setUp() throws Exception {
KernelManager.register(new KernelTest());
outputContainer = new OutputContainer();
}
use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.
the class OutputContainerDeserializerTest method deserialize_resultObjectHasItems.
@Test
public void deserialize_resultObjectHasItems() throws Exception {
// given
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(json);
OutputContainerDeserializer deserializer = new OutputContainerDeserializer(new BasicObjectSerializer());
// when
OutputContainer outputContainer = (OutputContainer) deserializer.deserialize(actualObj, mapper);
// then
Assertions.assertThat(outputContainer).isNotNull();
Assertions.assertThat(outputContainer.getItems()).isNotEmpty();
}
use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.
the class QueryExecutor method executeQuery.
public synchronized Object executeQuery(String script, NamespaceClient namespaceClient, ConnectionStringHolder defaultConnectionString, Map<String, ConnectionStringHolder> namedConnectionString) throws SQLException, IOException, ReadVariableException {
BeakerParser beakerParser = new BeakerParser(script, namespaceClient, defaultConnectionString, namedConnectionString, jdbcClient);
BasicDataSource ds = jdbcClient.getDataSource(beakerParser.getDbURI().getActualConnectionString());
Properties info = null;
if (beakerParser.getDbURI().getUser() != null && !beakerParser.getDbURI().getUser().isEmpty()) {
if (info == null) {
info = new Properties();
}
info.put("user", beakerParser.getDbURI().getUser());
}
if (beakerParser.getDbURI().getPassword() != null && !beakerParser.getDbURI().getPassword().isEmpty()) {
if (info == null) {
info = new Properties();
}
info.put("password", beakerParser.getDbURI().getPassword());
}
boolean isConnectionExeption = true;
// Workaround for "h2database" : do not work correctly with empty or null "Properties"
try (Connection connection = info != null ? ds.getDriver().connect(beakerParser.getDbURI().getActualConnectionString(), info) : ds.getConnection()) {
this.connection = connection;
connection.setAutoCommit(false);
List<Object> resultsForOutputCell = new ArrayList<>();
Map<String, List<Object>> resultsForNamspace = new HashMap<>();
for (BeakerParseResult queryLine : beakerParser.getResults()) {
BeakerInputVar basicIterationArray = null;
for (BeakerInputVar parameter : queryLine.getInputVars()) {
if (parameter.isAll()) {
basicIterationArray = parameter;
if (parameter.getErrorMessage() != null)
throw new ReadVariableException(parameter.getErrorMessage());
// ToDo make recursively iteration over several arrays
break;
}
}
try {
if (basicIterationArray != null) {
int l;
Object obj;
try {
obj = namespaceClient.get(basicIterationArray.objectName);
} catch (Exception e) {
throw new ReadVariableException(basicIterationArray.objectName, e);
}
if (obj instanceof List) {
l = ((List) obj).size();
} else if (obj.getClass().isArray()) {
l = Array.getLength(obj);
} else
break;
for (int i = 0; i < l; i++) {
QueryResult queryResult = executeQuery(i, queryLine, connection, namespaceClient);
adoptResult(queryLine, queryResult, resultsForOutputCell, resultsForNamspace);
}
} else {
QueryResult queryResult = executeQuery(-1, queryLine, connection, namespaceClient);
adoptResult(queryLine, queryResult, resultsForOutputCell, resultsForNamspace);
}
} catch (Exception e) {
isConnectionExeption = false;
throw e;
}
}
connection.commit();
for (String output : resultsForNamspace.keySet()) {
if (resultsForNamspace.get(output).size() > 1) {
OutputContainer outputContainer = new OutputContainer(resultsForNamspace.get(output));
namespaceClient.set(output, outputContainer);
} else if (!resultsForNamspace.get(output).isEmpty()) {
namespaceClient.set(output, resultsForNamspace.get(output).get(0));
} else {
namespaceClient.set(output, null);
}
}
if (resultsForOutputCell.size() > 1) {
OutputContainer outputContainer = new OutputContainer(resultsForOutputCell);
return outputContainer;
} else if (!resultsForOutputCell.isEmpty()) {
return resultsForOutputCell.get(0);
} else {
return OutputCell.HIDDEN;
}
} catch (Exception e) {
if (beakerParser.getDbURI() != null && isConnectionExeption) {
beakerParser.getDbURI().setShowDialog(true);
}
throw e;
} finally {
statement = null;
}
}
use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.
the class OutputContainerDeserializer method deserialize.
@Override
public Object deserialize(JsonNode n, ObjectMapper mapper) {
Object o = null;
try {
List<Object> items = null;
List<String> labels = null;
if (n.has("items")) {
JsonNode nn = n.get("items");
if (nn.isArray()) {
items = new ArrayList<>();
for (JsonNode no : nn) {
items.add(parent.deserialize(no, mapper));
}
}
}
if (n.has("labels")) {
JsonNode nn = n.get("labels");
if (nn.isArray()) {
labels = new ArrayList<>();
for (JsonNode no : nn) {
labels.add(no.asText());
}
}
}
o = createOutputContainer(items, labels);
if (n.has("layout")) {
Object layout = parent.deserialize(n.get("layout"), mapper);
((OutputContainer) o).setLayoutManager((OutputContainerLayoutManager) layout);
}
} catch (Exception e) {
OutputContainer.LOGGER.error("exception deserializing OutputContainer ", e);
}
return o;
}
Aggregations