Search in sources :

Example 86 with IComment

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

the class PostgreSQLCommentNotificationParser method processCommentNotification.

/**
   * Parses a {@link PGNotification} notification from the database back end for comment table
   * changes. These changes can not directly be mapped to any of the commentable objects as the
   * relation to which commentable object they belong is not in the notification message. This
   * notification is generated for the following situations for any commentable object:
   *
   * <pre>
   *
   *Delete a comment:
   *
   *[1] Comment 1    [1] Comment 1
   *[2] Comment 2 ->
   *[3] Comment 3    [3] Comment 3
   *
   *Edit a comment:
   *
   *[1] Comment 1    [1] Comment 1
   *[2] Comment 2 -> [2] Edited Comment 2
   *[3] Comment 3    [3] Comment 3
   *
   *</pre>
   *
   * @param notification The {@link PGNotification} from the PostgreSQL database server.
   * @param provider The {@link SQLProvider} which is used to communicate with the database.
   */
static CommentNotification processCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = COMMENTS_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    Integer commentId = null;
    try {
        commentId = Integer.parseInt(matcher.group(3));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    final IComment comment = CommentManager.get(provider).getCommentById(commentId);
    if (comment == null) {
        return null;
    }
    final String databaseOperation = matcher.group(2);
    Integer parentId = null;
    try {
        parentId = matcher.group(4).equalsIgnoreCase("null") ? null : Integer.parseInt(matcher.group(4));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    // parent.
    if (databaseOperation.equals("DELETE")) {
        if (((parentId == null) && (comment.getParent() != null)) || ((parentId != null) && (comment.getParent() != null) && (!parentId.equals(comment.getParent().getId())))) {
            final Integer localCommentParentId = parentId;
            final Integer notificationCommentParentId = comment.getParent() != null ? comment.getParent().getId() : null;
            throw new IllegalStateException("IE02521: The parent comment of the localy stored comment: " + localCommentParentId + " is not equal to the " + "notification comments parent comment: " + notificationCommentParentId);
        }
    }
    final String commentContent = matcher.group(9);
    if (!commentContent.equals(comment.getComment()) && databaseOperation.equals("DELETE")) {
        throw new IllegalStateException("IE02522: The local comments comment: " + comment.getComment() + "is not equal to the notification comments content: " + commentContent);
    }
    Integer commentUserId = null;
    try {
        commentUserId = Integer.parseInt(matcher.group(7));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    if (!commentUserId.equals(comment.getUser().getUserId())) {
        throw new IllegalStateException("IE02523: The user of the localy stored comment: " + commentUserId + " is not equal to the " + "notifications comments user: " + comment.getUser().getUserId());
    }
    final IComment parentComment = CommentManager.get(provider).getCommentById(parentId);
    final IComment newComment = new CComment(comment.getId(), comment.getUser(), parentComment, commentContent);
    final CommentOperation operation = databaseOperation.equalsIgnoreCase("UPDATE") ? CommentOperation.EDIT : CommentOperation.DELETE;
    return new CommentNotificationContainer(comment, newComment, operation);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) Matcher(java.util.regex.Matcher) TypeInstanceCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TypeInstanceCommentNotificationContainer) GroupNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.GroupNodeCommentNotificationContainer) TextNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TextNodeCommentNotificationContainer) FunctionNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionNodeCommentNotificationContainer) CodeNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CodeNodeCommentNotificationContainer) CommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer) FunctionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionCommentNotificationContainer) InstructionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer) EdgeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer)

Example 87 with IComment

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

the class PostgreSQLEdgeLoader method loadEdges.

/**
   * Loads the edges of a view.
   *
   * @param provider The connection to the database.
   * @param view The view whose edges are loaded.
   * @param nodeLookup Maps between node IDs and their corresponding node objects.
   * @param edgeToGlobalCommentMap Maps between edge IDs and their associated comments.
   *
   * @return The loaded edges.
   *
   * @throws CouldntLoadDataException
   */
private static List<INaviEdge> loadEdges(final AbstractSQLProvider provider, final INaviView view, final Map<Integer, INaviViewNode> nodeLookup, final Map<Integer, ArrayList<IComment>> edgeToGlobalCommentMap) throws CouldntLoadDataException {
    final String query = "SELECT * FROM load_view_edges(" + view.getConfiguration().getId() + ")";
    List<CBend> currentPaths = new ArrayList<CBend>();
    final Map<Integer, INaviEdge> commentIdToEdge = new HashMap<Integer, INaviEdge>();
    final Map<Integer, INaviEdge> edgeIdToEdge = new HashMap<Integer, INaviEdge>();
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int edgeId = resultSet.getInt("id");
                if (edgeIdToEdge.containsKey(edgeId)) {
                    final INaviEdge edge = edgeIdToEdge.get(edgeId);
                    final double pathX = resultSet.getDouble("x");
                    final double pathY = resultSet.getDouble("y");
                    if (!resultSet.wasNull()) {
                        edge.addBend(pathX, pathY);
                    }
                    continue;
                }
                final int sourceNode = resultSet.getInt("source_node_id");
                final int targetNode = resultSet.getInt("target_node_id");
                Integer localCommentId = resultSet.getInt("comment_id");
                if (resultSet.wasNull()) {
                    localCommentId = null;
                }
                final double x1 = resultSet.getDouble("x1");
                final double y1 = resultSet.getDouble("y1");
                final double x2 = resultSet.getDouble("x2");
                final double y2 = resultSet.getDouble("y2");
                final EdgeType type = EdgeType.valueOf(resultSet.getString("type").toUpperCase());
                final Color color = new Color(resultSet.getInt("color"));
                final boolean visible = resultSet.getBoolean("visible");
                final boolean selected = resultSet.getBoolean("selected");
                final INaviViewNode source = nodeLookup.get(sourceNode);
                final INaviViewNode target = nodeLookup.get(targetNode);
                final double pathX = resultSet.getDouble("x");
                final double pathY = resultSet.getDouble("y");
                if (!resultSet.wasNull()) {
                    currentPaths.add(new CBend(pathX, pathY));
                }
                final CNaviViewEdge edge = new CNaviViewEdge(edgeId, source, target, type, x1, y1, x2, y2, color, selected, visible, null, currentPaths, provider);
                if (localCommentId != null) {
                    commentIdToEdge.put(localCommentId, edge);
                }
                final ArrayList<IComment> globalComments = edgeToGlobalCommentMap.containsKey(edgeId) ? edgeToGlobalCommentMap.get(edgeId) : null;
                if ((globalComments != null) && (globalComments.size() != 0)) {
                    initializeGlobalComment(edge, globalComments, provider);
                }
                source.addOutgoingEdge(edge);
                target.addIncomingEdge(edge);
                edgeIdToEdge.put(edge.getId(), edge);
                currentPaths = new ArrayList<CBend>();
            }
            if (!commentIdToEdge.isEmpty()) {
                final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToEdge.keySet());
                for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
                    commentIdToEdge.get(commentIdToComment.getKey()).initializeLocalComment(commentIdToComment.getValue());
                }
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException("Error: Loading of view edges failed");
    }
    return Lists.newArrayList(edgeIdToEdge.values());
}
Also used : 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) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) CBend(com.google.security.zynamics.zylib.gui.zygraph.edges.CBend) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Color(java.awt.Color) PreparedStatement(java.sql.PreparedStatement) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)

Example 88 with IComment

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

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

the class InstructionCommentsDataModel method getCommentCharacterBuffer.

private FormattedCharacterBuffer getCommentCharacterBuffer(int rowIndex, int columnIndex, int lineIndex) {
    // Return the actual comment.
    CodeDisplayCoordinate coordinate = new CodeDisplayCoordinate(rowIndex, lineIndex, columnIndex, 0);
    Pair<IComment, Pair<Integer, Integer>> commentAndIndex = getCommentAndIndexAtCoordinate(coordinate);
    String finalComment = "";
    if (commentAndIndex.first().getNumberOfCommentLines() > 0) {
        String commentString = commentAndIndex.first().getComment();
        Pair<Integer, Integer> indices = commentAndIndex.second();
        finalComment = commentString.substring(indices.first(), indices.second());
    }
    finalComment = CodeDisplay.padRight(finalComment, getColumnWidthInCharacters(columnIndex));
    return new FormattedCharacterBuffer(finalComment, STANDARD_FONT, columns[COMMENT_INDEX].getDefaultFontColor(), columns[COMMENT_INDEX].getDefaultBackgroundColor());
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CodeDisplayCoordinate(com.google.security.zynamics.zylib.gui.CodeDisplay.CodeDisplayCoordinate) Pair(com.google.security.zynamics.zylib.general.Pair) FormattedCharacterBuffer(com.google.security.zynamics.zylib.gui.CodeDisplay.FormattedCharacterBuffer)

Example 90 with IComment

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

the class InstructionCommentsDataModel method keyPressedOrTyped.

@Override
public /**
   * Please read the comment in the base class regarding this method - it is somewhat counter-
   * intuitive for people used to the Swing keyboard handling model.
   */
void keyPressedOrTyped(CodeDisplayCoordinate coordinate, KeyEvent event) {
    if (!isEditable(coordinate)) {
        return;
    }
    // Are there any existing comments?
    INaviInstruction instruction = internalData.get(coordinate.getRow()).first();
    Pair<IComment, Pair<Integer, Integer>> commentAndIndex = getCommentAndIndexAtCoordinate(coordinate);
    IComment comment = commentAndIndex.first();
    Pair<Integer, Integer> indices = commentAndIndex.second();
    switch(event.getKeyCode()) {
        // VK_UNDEFINED implies that this was a KEY_TYPED event.
        case KeyEvent.VK_UNDEFINED:
            switch(event.getKeyChar()) {
                case java.awt.Event.ENTER:
                    handleRegularKeyInComment(instruction, comment, indices, coordinate, event);
                    coordinate.setLine(coordinate.getLine() + 1);
                    coordinate.setFieldIndex(0);
                    break;
                case java.awt.Event.BACK_SPACE:
                    handleBackspaceKeyInComment(instruction, comment, indices, coordinate);
                    break;
                case java.awt.Event.DELETE:
                    handleDeleteKeyInComment(instruction, comment, indices, coordinate);
                    break;
                default:
                    handleRegularKeyInComment(instruction, comment, indices, coordinate, event);
                    coordinate.setFieldIndex(coordinate.getFieldIndex() + 1);
                    break;
            }
            break;
        case KeyEvent.VK_HOME:
            coordinate.setFieldIndex(0);
            break;
        case KeyEvent.VK_END:
            coordinate.setFieldIndex(indices.second() - indices.first());
            break;
        default:
            Logger.warning("Default case in keyTyped hit, investigate why.");
            break;
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) Pair(com.google.security.zynamics.zylib.general.Pair)

Aggregations

IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)167 Test (org.junit.Test)129 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)114 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)97 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)89 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)89 ArrayList (java.util.ArrayList)20 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)18 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)16 INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)13 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)12 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)11 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)10 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)10 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)9 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)9 PreparedStatement (java.sql.PreparedStatement)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)7 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)7 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)7