Search in sources :

Example 6 with CComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment in project binnavi by google.

the class PostgreSQLNodeSaver method saveCodeNodes.

/**
   * Saves the code 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 codeNodeIndices Index into the nodes list that identifies the code nodes.
   *
   * @throws SQLException Thrown if saving the code node instructions failed.
   */
protected static void saveCodeNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
    if (!codeNodeIndices.isEmpty()) {
        final List<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = PostgreSQLNodeSaver.saveCodeNodeInstructions(provider, nodes, firstNode, codeNodeIndices);
        final String query = "INSERT INTO " + CTableNames.CODE_NODES_TABLE + "(module_id, node_id, parent_function, comment_id) VALUES (?, ?, ?, ?)";
        final ArrayList<INaviCodeNode> codeNodesWithUnsavedComments = new ArrayList<INaviCodeNode>();
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        try {
            for (final int index : codeNodeIndices) {
                final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
                codeNode.setId(firstNode + index);
                INaviFunction function = null;
                try {
                    function = codeNode.getParentFunction();
                } catch (final MaybeNullException e) {
                }
                final int moduleId = Iterables.getLast(codeNode.getInstructions()).getModule().getConfiguration().getId();
                final List<IComment> comment = codeNode.getComments().getLocalCodeNodeComment();
                final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
                if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
                    codeNodesWithUnsavedComments.add(codeNode);
                }
                preparedStatement.setInt(1, moduleId);
                preparedStatement.setInt(2, firstNode + index);
                if (function == null) {
                    preparedStatement.setNull(3, Types.BIGINT);
                } else {
                    preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
                }
                if (commentId == null) {
                    preparedStatement.setNull(4, Types.INTEGER);
                } else {
                    preparedStatement.setInt(4, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // implementation.
        for (final INaviCodeNode codeNode : codeNodesWithUnsavedComments) {
            final ArrayList<IComment> codeNodecomments = new ArrayList<IComment>();
            for (final IComment comment : codeNode.getComments().getLocalCodeNodeComment()) {
                try {
                    final Integer commentId = PostgreSQLNodeFunctions.appendLocalCodeNodeComment(provider, codeNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    codeNodecomments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            codeNode.getComments().initializeLocalCodeNodeComment(codeNodecomments);
        }
        // implementation.
        for (final Pair<INaviCodeNode, INaviInstruction> pair : instructionsWithUnsavedLocalComments) {
            final ArrayList<IComment> localInstructionComments = new ArrayList<IComment>();
            for (final IComment comment : pair.first().getComments().getLocalInstructionComment(pair.second())) {
                try {
                    final int commentId = PostgreSQLInstructionFunctions.appendLocalInstructionComment(provider, pair.first(), pair.second(), comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    localInstructionComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            pair.first().getComments().initializeLocalInstructionComment(pair.second(), localInstructionComments);
        }
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Pair(com.google.security.zynamics.zylib.general.Pair) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 7 with CComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment 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 8 with CComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment in project binnavi by google.

the class PostgreSQLCommentFunctions method loadCommentByCommentId.

/**
   * Loads comments by a comment root id it returns all ancestors of the comment.
   *
   * @param provider The provider used to access the database.
   * @param commentRootId The comment root id where to start the recursive query.
   * @return An array of comments from and including the comment referenced by the comment root id.
   *
   * @throws CouldntLoadDataException if the data could not be loaded from the database.
   */
public static ArrayList<IComment> loadCommentByCommentId(final SQLProvider provider, final int commentRootId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE00440: connection argument can not be null");
    final HashMap<Integer, IComment> commentIdToComment = new HashMap<>();
    final ArrayList<IComment> comments = new ArrayList<>();
    final String commentQuery = "SELECT * FROM get_all_comment_ancestors(" + commentRootId + ");";
    try (ResultSet resultSet = provider.getConnection().executeQuery(commentQuery, true)) {
        while (resultSet.next()) {
            resultSet.getInt("level");
            final int commentId = resultSet.getInt("id");
            Integer parentId = resultSet.getInt("parent_id");
            if (resultSet.wasNull()) {
                parentId = null;
            }
            final int userId = resultSet.getInt("user_id");
            final String commentText = resultSet.getString("comment");
            final CComment comment = new CComment(commentId, CUserManager.get(provider).getUserById(userId), commentIdToComment.get(parentId), commentText);
            commentIdToComment.put(commentId, comment);
            comments.add(comment);
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return comments;
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Example 9 with CComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment 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 10 with CComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment 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

CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)121 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)114 Test (org.junit.Test)104 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)96 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)89 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)89 ArrayList (java.util.ArrayList)13 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)11 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)10 INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)9 Date (java.util.Date)9 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)8 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)8 CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)8 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)7 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)6 CTextNode (com.google.security.zynamics.binnavi.disassembly.CTextNode)6 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)5 CFunction (com.google.security.zynamics.binnavi.disassembly.CFunction)5