Search in sources :

Example 16 with INaviTextNode

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);
        }
    }
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) PreparedStatement(java.sql.PreparedStatement)

Example 17 with INaviTextNode

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);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Color(java.awt.Color) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) CTextNode(com.google.security.zynamics.binnavi.disassembly.CTextNode)

Example 18 with INaviTextNode

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);
}
Also used : CView(com.google.security.zynamics.binnavi.disassembly.views.CView) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Test(org.junit.Test)

Example 19 with INaviTextNode

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);
}
Also used : CView(com.google.security.zynamics.binnavi.disassembly.views.CView) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Test(org.junit.Test)

Example 20 with INaviTextNode

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());
}
Also used : CView(com.google.security.zynamics.binnavi.disassembly.views.CView) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Test(org.junit.Test)

Aggregations

INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)28 Test (org.junit.Test)20 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)15 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)13 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)9 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)8 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)8 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)7 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)5 TextNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TextNodeCommentNotificationContainer)3 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)3 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)3 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)3 ArrayList (java.util.ArrayList)3 CommentNotification (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.interfaces.CommentNotification)2 INaviFunctionNode (com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode)2 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)2 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)2 PreparedStatement (java.sql.PreparedStatement)2 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)1