Search in sources :

Example 11 with INaviGroupNode

use of com.google.security.zynamics.binnavi.disassembly.INaviGroupNode in project binnavi by google.

the class PostgreSQLNodeSaver method saveGroupNodes.

/**
   * Saves the group 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 groupNodeIndices Index into the nodes list that identifies the group nodes.
   *
   * @throws SQLException Thrown if saving the group nodes failed.
   */
protected static void saveGroupNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> groupNodeIndices) throws SQLException {
    Preconditions.checkNotNull(provider, "IE02525: connection argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02526: nodes argument can not be null");
    Preconditions.checkNotNull(groupNodeIndices, "Error: groupNodeIndices argument can not be null");
    if (!groupNodeIndices.isEmpty()) {
        final String query = "INSERT INTO " + CTableNames.GROUP_NODES_TABLE + "(node_id, collapsed, comment_id) VALUES (?, ?, ?)";
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        final List<INaviGroupNode> groupNodesWithUnsavedComments = new ArrayList<INaviGroupNode>();
        try {
            for (final Integer index : groupNodeIndices) {
                final INaviGroupNode node = (INaviGroupNode) nodes.get(index);
                preparedStatement.setInt(1, firstNode + index);
                preparedStatement.setBoolean(2, node.isCollapsed());
                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)) {
                    groupNodesWithUnsavedComments.add(node);
                }
                if (commentId == null) {
                    preparedStatement.setNull(3, Types.INTEGER);
                } else {
                    preparedStatement.setInt(3, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // TODO (timkornau): this can work better.
        for (final INaviGroupNode groupNode : groupNodesWithUnsavedComments) {
            final ArrayList<IComment> groupNodeComments = new ArrayList<IComment>();
            for (final IComment comment : groupNode.getComments()) {
                try {
                    final Integer commentId = provider.appendGroupNodeComment(groupNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    groupNodeComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            groupNode.initializeComment(groupNodeComments);
        }
    }
}
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) PreparedStatement(java.sql.PreparedStatement) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)

Example 12 with INaviGroupNode

use of com.google.security.zynamics.binnavi.disassembly.INaviGroupNode in project binnavi by google.

the class PostgreSQLNodeSaver method writeNodes.

/**
   * Writes the nodes of a view to the database.
   *
   * @param provider The connection to the database.
   * @param newViewId The ID of the view the nodes belong to.
   * @param nodes The nodes to save.
   * @throws SQLException Thrown if saving the nodes failed.
   */
public static void writeNodes(final AbstractSQLProvider provider, final int newViewId, final List<INaviViewNode> nodes) throws SQLException {
    Preconditions.checkNotNull(provider, "IE01992: Provider argument can not be null");
    Preconditions.checkArgument(newViewId > 0, "IE01993: New View ID argument must be greater then zero");
    Preconditions.checkNotNull(nodes, "IE01994: Nodes argument can not be null");
    if (nodes.isEmpty()) {
        return;
    }
    final ArrayList<Integer> functionNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> codeNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> textNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> groupNodeIndices = new ArrayList<Integer>();
    final BiMap<Integer, INaviGroupNode> groupNodeMap = HashBiMap.create();
    final int firstNode = saveNodes(provider, newViewId, nodes, functionNodeIndices, codeNodeIndices, textNodeIndices, groupNodeIndices, groupNodeMap);
    // After this point, the nodes table has been filled
    // After each saving, the node IDs have to be updated
    PostgreSQLNodeSaver.updateNodeIds(nodes, firstNode);
    // Now, the individual node type tables can be saved
    PostgreSQLNodeSaver.saveGroupNodes(provider, nodes, firstNode, PostgreSQLNodeSaver.sortGroupNodes(groupNodeIndices, groupNodeMap));
    PostgreSQLNodeSaver.saveFunctionNodes(provider, nodes, firstNode, functionNodeIndices);
    PostgreSQLNodeSaver.saveCodeNodes(provider, nodes, firstNode, codeNodeIndices);
    PostgreSQLNodeSaver.saveTextNodes(provider, nodes, firstNode, textNodeIndices);
    // Once all nodes are saved, the parent nodes can be saved too
    final CConnection connection = provider.getConnection();
    PostgreSQLNodeSaver.saveParentGroups(connection, nodes, firstNode, groupNodeMap);
    // And finally, we can save the tags associated with the nodes
    PostgreSQLNodeSaver.saveTags(connection, nodes, firstNode);
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ArrayList(java.util.ArrayList) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)

Example 13 with INaviGroupNode

use of com.google.security.zynamics.binnavi.disassembly.INaviGroupNode in project binnavi by google.

the class PostgreSQLGroupNodeCommentTests method appendGroupNodeComment4.

@Test
public void appendGroupNodeComment4() throws CouldntLoadDataException, LoadCancelledException, MaybeNullException, CPartialLoadException, CouldntSaveDataException {
    final INaviGroupNode groupNode = setupGroupNode();
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final String firstCommentString = " APPEND GROUP NODE COMMENT WITHOUT PARENT ID ";
    final Integer firstCommentId = getProvider().appendGroupNodeComment(groupNode, firstCommentString, user.getUserId());
    final IComment firstComment = new CComment(firstCommentId, user, null, firstCommentString);
    final String secondCommentString = " APPEND GROUP NODE COMMENT WITH PARENT ID ";
    final Integer secondCommentId = getProvider().appendGroupNodeComment(groupNode, secondCommentString, user.getUserId());
    final IComment secondComment = new CComment(secondCommentId, user, firstComment, secondCommentString);
    final ArrayList<IComment> commentsFromDatabase = getProvider().loadCommentById(secondCommentId);
    assertNotNull(commentsFromDatabase);
    assertEquals(2, commentsFromDatabase.size());
    assertTrue(commentsFromDatabase.contains(firstComment));
    assertTrue(commentsFromDatabase.contains(secondComment));
// TODO (timkornau): check if the association to the group node did also work and not just the
// appending under the comment id.
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 14 with INaviGroupNode

use of com.google.security.zynamics.binnavi.disassembly.INaviGroupNode in project binnavi by google.

the class PostgreSQLGroupNodeCommentTests method deleteGroupNodeComment3.

@Test(expected = NullPointerException.class)
public void deleteGroupNodeComment3() throws CouldntDeleteException, CouldntLoadDataException, LoadCancelledException, MaybeNullException, CPartialLoadException, CouldntSaveDataException {
    final INaviGroupNode groupNode = setupGroupNode();
    getProvider().deleteGroupNodeComment(groupNode, 1, null);
}
Also used : INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 15 with INaviGroupNode

use of com.google.security.zynamics.binnavi.disassembly.INaviGroupNode in project binnavi by google.

the class PostgreSQLGroupNodeCommentTests method deleteGroupNodeComment8.

/**
   * This test checks if the delete of a comment in a series of comments works if the comment is the
   * first comment.
   *
   * <pre>
   *Comment 1:
   *Comment 2:  ->  Comment 2:
   *Comment 3:      Comment 3:
   *</pre>
   *
   */
@Test
public void deleteGroupNodeComment8() throws CouldntLoadDataException, LoadCancelledException, MaybeNullException, CPartialLoadException, CouldntSaveDataException, CouldntDeleteException {
    final INaviGroupNode groupNode = setupGroupNode();
    final List<IComment> storedComments = groupNode.getComments() == null ? new ArrayList<IComment>() : groupNode.getComments();
    final IComment lastComment = storedComments.size() == 0 ? null : Iterables.getLast(storedComments);
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final String comment1String = " Comment 1: ";
    final int comment1Id = getProvider().appendGroupNodeComment(groupNode, comment1String, user.getUserId());
    final IComment comment1 = new CComment(comment1Id, user, lastComment, comment1String);
    final String comment2String = " Comment 2: ";
    final int comment2Id = getProvider().appendGroupNodeComment(groupNode, comment2String, user.getUserId());
    final IComment comment2 = new CComment(comment2Id, user, comment1, comment2String);
    final String comment3String = " Comment 3: ";
    final int comment3Id = getProvider().appendGroupNodeComment(groupNode, comment3String, user.getUserId());
    final IComment comment3 = new CComment(comment3Id, user, comment2, comment3String);
    final ArrayList<IComment> commentsBeforeDelete = getProvider().loadCommentById(comment3Id);
    assertNotNull(commentsBeforeDelete);
    assertEquals(storedComments.size() + 3, commentsBeforeDelete.size());
    assertTrue(commentsBeforeDelete.contains(comment1));
    assertTrue(commentsBeforeDelete.contains(comment2));
    assertTrue(commentsBeforeDelete.contains(comment3));
    assertEquals(comment3, Iterables.getLast(commentsBeforeDelete));
    assertEquals(comment2, commentsBeforeDelete.get(commentsBeforeDelete.size() - 2));
    assertEquals(comment1, commentsBeforeDelete.get(commentsBeforeDelete.size() - 3));
    getProvider().deleteGroupNodeComment(groupNode, comment1Id, user.getUserId());
    final ArrayList<IComment> commentsAfterDelete1 = getProvider().loadCommentById(comment1Id);
    assertNotNull(commentsAfterDelete1);
    assertTrue(commentsAfterDelete1.isEmpty());
    final ArrayList<IComment> commentsAfterDelete2 = getProvider().loadCommentById(comment3Id);
    final IComment comment2AfterDelete = new CComment(comment2Id, user, lastComment, comment2String);
    final IComment comment3AfterDelete = new CComment(comment3Id, user, comment2AfterDelete, comment3String);
    assertNotNull(commentsAfterDelete2);
    assertEquals(storedComments.size() + 2, commentsAfterDelete2.size());
    assertTrue(commentsAfterDelete2.contains(comment3AfterDelete));
    assertTrue(commentsAfterDelete2.contains(comment2AfterDelete));
    assertEquals(comment3AfterDelete, Iterables.getLast(commentsAfterDelete2));
    assertEquals(comment2AfterDelete, commentsAfterDelete2.get(commentsAfterDelete2.size() - 2));
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Aggregations

INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)40 Test (org.junit.Test)23 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)16 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)15 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)11 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)9 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)9 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)8 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)7 ArrayList (java.util.ArrayList)7 CGroupNode (com.google.security.zynamics.binnavi.disassembly.CGroupNode)6 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)5 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)4 INaviFunctionNode (com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode)4 NaviNode (com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviNode)4 MockDatabase (com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase)3 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)3 GroupNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.GroupNodeCommentNotificationContainer)3 MockTagManager (com.google.security.zynamics.binnavi.Tagging.MockTagManager)3 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)3