use of com.google.security.zynamics.binnavi.disassembly.INaviTextNode in project binnavi by google.
the class PostgreSQLNodeSaver method saveTextNodes.
/**
* Saves the text nodes to the database.
*
* @param provider The connection to the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param textNodeIndices Index into the nodes list that identifies the text nodes.
*
* @throws SQLException Thrown if saving the text nodes failed.
*/
protected static void saveTextNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> textNodeIndices) throws SQLException {
Preconditions.checkNotNull(provider, "IE02527: provider argument can not be null");
Preconditions.checkNotNull(nodes, "IE02528: nodes argument can not be null");
Preconditions.checkNotNull(textNodeIndices, "IE02529: textNodeIndices argument can not be null");
if (!textNodeIndices.isEmpty()) {
final String query = "INSERT INTO " + CTableNames.TEXT_NODES_TABLE + "(node_id, comment_id) VALUES (?, ?)";
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
final List<INaviTextNode> textNodesWithUnsavedComments = new ArrayList<INaviTextNode>();
try {
for (final Integer index : textNodeIndices) {
final INaviTextNode node = (INaviTextNode) nodes.get(index);
final List<IComment> comment = node.getComments();
final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
textNodesWithUnsavedComments.add(node);
}
preparedStatement.setInt(1, firstNode + index);
if (commentId == null) {
preparedStatement.setNull(2, Types.INTEGER);
} else {
preparedStatement.setInt(2, commentId);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
// unsaved comments to be stored. Possibly one can handle all of those in one query.
for (final INaviTextNode textNode : textNodesWithUnsavedComments) {
final ArrayList<IComment> textNodeComments = new ArrayList<IComment>();
for (final IComment comment : textNode.getComments()) {
try {
final Integer commentId = provider.appendTextNodeComment(textNode, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
textNodeComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
textNode.initializeComment(textNodeComments);
}
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviTextNode in project binnavi by google.
the class PostgreSQLTextNodeLoader method load.
/**
* Loads the text nodes of a view.
*
* @param provider The connection to the database.
* @param view The view whose text nodes are loaded.
* @param nodes The loaded nodes are stored here.
*
* @throws CouldntLoadDataException
*/
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "IE02516: provider argument can not be null");
Preconditions.checkNotNull(view, "IE02517: view argument can not be null");
Preconditions.checkNotNull(nodes, "IE02518: nodes argument can not be null");
final Map<Integer, INaviTextNode> commentIdToTextNode = new HashMap<Integer, INaviTextNode>();
final String query = "SELECT id, comment_id, x, y, width, height, color, selected, visible " + " FROM " + CTableNames.NODES_TABLE + " JOIN " + CTableNames.TEXT_NODES_TABLE + " ON id = node_id " + " WHERE view_id = " + view.getConfiguration().getId();
try {
final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
final int nodeId = resultSet.getInt("id");
Integer commentId = resultSet.getInt("comment_id");
if (resultSet.wasNull()) {
commentId = null;
}
final double xPos = resultSet.getDouble("x");
final double yPos = resultSet.getDouble("y");
final double width = resultSet.getDouble("width");
final double height = resultSet.getDouble("height");
final Color color = new Color(resultSet.getInt("color"));
final boolean selected = resultSet.getBoolean("selected");
final boolean visible = resultSet.getBoolean("visible");
final INaviTextNode textNode = new CTextNode(nodeId, xPos, yPos, width, height, color, selected, visible, new HashSet<CTag>(), null, provider);
if (commentId != null) {
commentIdToTextNode.put(commentId, textNode);
}
nodes.add(textNode);
}
} finally {
resultSet.close();
}
if (!commentIdToTextNode.isEmpty()) {
final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToTextNode.keySet());
for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
commentIdToTextNode.get(commentIdToComment.getKey()).initializeComment(commentIdToComment.getValue());
}
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviTextNode in project binnavi by google.
the class PostgreSQLNotificationProviderTest method testDeleteTextNodeCommentSync.
@Test
public void testDeleteTextNodeCommentSync() throws CouldntSaveDataException, CouldntLoadDataException, LoadCancelledException, CPartialLoadException, InterruptedException, CouldntDeleteException {
final CView databaseOneTextNodeView = databaseOneModuleTwo.getContent().getViewContainer().createView(" TEXT NODE TESTING VIEW ", "");
CViewInserter.insertView(databaseOneView, databaseOneTextNodeView);
final INaviTextNode databaseOneTextNode = databaseOneTextNodeView.getContent().createTextNode(new ArrayList<IComment>());
databaseOneTextNodeView.save();
databaseTwoModuleTwo.close();
databaseTwoModuleTwo.load();
databaseTwoView.load();
final INaviView databaseTwoTextNodeView = Iterables.getLast(databaseTwoModuleTwo.getContent().getViewContainer().getUserViews());
INaviTextNode databaseTwoTextNode = null;
assertEquals(databaseOneTextNodeView.getName(), databaseTwoTextNodeView.getName());
databaseTwoTextNodeView.load();
for (final INaviViewNode node : databaseTwoTextNodeView.getContent().getGraph().getNodes()) {
if (node instanceof INaviTextNode) {
databaseTwoTextNode = (INaviTextNode) node;
}
}
assertNotNull(databaseTwoTextNode);
assertEquals(databaseTwoTextNode.getId(), databaseOneTextNode.getId());
final List<IComment> comments = databaseOneTextNode.appendComment(" TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) ");
synchronized (lock) {
lock.await(1000, TimeUnit.MILLISECONDS);
}
final List<IComment> oneAfter = databaseOneTextNode.getComments();
final List<IComment> twoAfter = databaseTwoTextNode.getComments();
assertNotNull(oneAfter);
assertNotNull(twoAfter);
assertEquals(1, oneAfter.size());
assertEquals(1, twoAfter.size());
assertEquals(oneAfter, twoAfter);
final int oneTwoSize = oneAfter.size();
final int twoTwoSize = twoAfter.size();
databaseOneTextNode.deleteComment(Iterables.getLast(comments));
// database one to two over the PostgreSQL back end.
synchronized (lock) {
lock.await(1000, TimeUnit.MILLISECONDS);
}
final List<IComment> oneThree = databaseOneTextNode.getComments();
final List<IComment> twoThree = databaseTwoTextNode.getComments();
assertEquals(oneTwoSize - 1, oneThree.size());
assertEquals(twoTwoSize - 1, twoThree.size());
assertEquals(oneThree, twoThree);
}
use of com.google.security.zynamics.binnavi.disassembly.INaviTextNode in project binnavi by google.
the class PostgreSQLNotificationProviderTest method testAppendTextNodeCommentSync.
@Test
public void testAppendTextNodeCommentSync() throws CouldntSaveDataException, CouldntLoadDataException, CPartialLoadException, LoadCancelledException, InterruptedException {
final CView databaseOneTextNodeView = databaseOneModuleTwo.getContent().getViewContainer().createView(" TEXT NODE TESTING VIEW ", "");
CViewInserter.insertView(databaseOneView, databaseOneTextNodeView);
final INaviTextNode databaseOneTextNode = databaseOneTextNodeView.getContent().createTextNode(new ArrayList<IComment>());
databaseOneTextNodeView.save();
databaseTwoModuleTwo.close();
databaseTwoModuleTwo.load();
databaseTwoView.load();
final INaviView databaseTwoTextNodeView = Iterables.getLast(databaseTwoModuleTwo.getContent().getViewContainer().getUserViews());
INaviTextNode databaseTwoTextNode = null;
assertEquals(databaseOneTextNodeView.getName(), databaseTwoTextNodeView.getName());
databaseTwoTextNodeView.load();
for (final INaviViewNode node : databaseTwoTextNodeView.getContent().getGraph().getNodes()) {
if (node instanceof INaviTextNode) {
databaseTwoTextNode = (INaviTextNode) node;
}
}
assertNotNull(databaseTwoTextNode);
assertEquals(databaseTwoTextNode.getId(), databaseOneTextNode.getId());
databaseOneTextNode.appendComment(" TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) ");
synchronized (lock) {
lock.await(1000, TimeUnit.MILLISECONDS);
}
final List<IComment> oneAfter = databaseOneTextNode.getComments();
final List<IComment> twoAfter = databaseTwoTextNode.getComments();
assertNotNull(oneAfter);
assertNotNull(twoAfter);
assertEquals(1, oneAfter.size());
assertEquals(1, twoAfter.size());
assertEquals(oneAfter, twoAfter);
}
use of com.google.security.zynamics.binnavi.disassembly.INaviTextNode in project binnavi by google.
the class PostgreSQLNotificationProviderTest method testEditTextNodeCommentSync.
@Test
public void testEditTextNodeCommentSync() throws CouldntSaveDataException, CouldntLoadDataException, CPartialLoadException, LoadCancelledException, InterruptedException {
final CView databaseOneTextNodeView = databaseOneModuleTwo.getContent().getViewContainer().createView(" TEXT NODE TESTING VIEW ", "");
CViewInserter.insertView(databaseOneView, databaseOneTextNodeView);
final INaviTextNode databaseOneTextNode = databaseOneTextNodeView.getContent().createTextNode(new ArrayList<IComment>());
databaseOneTextNodeView.save();
databaseTwoModuleTwo.close();
databaseTwoModuleTwo.load();
databaseTwoView.load();
final INaviView databaseTwoTextNodeView = Iterables.getLast(databaseTwoModuleTwo.getContent().getViewContainer().getUserViews());
INaviTextNode databaseTwoTextNode = null;
assertEquals(databaseOneTextNodeView.getName(), databaseTwoTextNodeView.getName());
databaseTwoTextNodeView.load();
for (final INaviViewNode node : databaseTwoTextNodeView.getContent().getGraph().getNodes()) {
if (node instanceof INaviTextNode) {
databaseTwoTextNode = (INaviTextNode) node;
}
}
assertNotNull(databaseTwoTextNode);
assertEquals(databaseTwoTextNode.getId(), databaseOneTextNode.getId());
final List<IComment> comments = databaseOneTextNode.appendComment(" TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) BEFORE ");
synchronized (lock) {
lock.await(1000, TimeUnit.MILLISECONDS);
}
final List<IComment> oneAfter = databaseOneTextNode.getComments();
final List<IComment> twoAfter = databaseTwoTextNode.getComments();
assertNotNull(oneAfter);
assertNotNull(twoAfter);
assertEquals(1, oneAfter.size());
assertEquals(1, twoAfter.size());
assertEquals(oneAfter, twoAfter);
final int oneTwoSize = oneAfter.size();
final int twoTwoSize = twoAfter.size();
databaseOneTextNode.editComment(Iterables.getLast(comments), " TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) AFTER ");
// database one to two over the PostgreSQL back end.
synchronized (lock) {
lock.await(1000, TimeUnit.MILLISECONDS);
}
final List<IComment> oneThree = databaseOneTextNode.getComments();
final List<IComment> twoThree = databaseTwoTextNode.getComments();
assertEquals(oneTwoSize, oneThree.size());
assertEquals(twoTwoSize, twoThree.size());
assertEquals(oneThree, twoThree);
assertEquals(" TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) AFTER ", Iterables.getLast(oneThree).getComment());
assertEquals(" TEST NOTIFICATION PROVIDER TESTS (TEXT NODE COMMENT) AFTER ", Iterables.getLast(twoThree).getComment());
}
Aggregations