use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.
the class PostgreSQLGroupNodeLoader method load.
/**
* Loads the group nodes of a view.
*
* @param provider The connection to the database.
* @param view The view whose group nodes are loaded.
* @param nodes The loaded nodes are stored here.
*
* @throws SQLException Thrown of loading the nodes failed.
* @throws CouldntLoadDataException
*/
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws SQLException, CouldntLoadDataException {
Preconditions.checkNotNull(provider, "IE02513: provider argument can not be null");
Preconditions.checkNotNull(view, "IE02514: view argument can not be null");
Preconditions.checkNotNull(nodes, "IE02515: nodes argument can not be null");
final Map<Integer, INaviGroupNode> commentIdToGroupNode = new HashMap<Integer, INaviGroupNode>();
final String query = "SELECT id, comment_id , collapsed, x, y, width, height, color, selected, visible " + " FROM " + CTableNames.NODES_TABLE + " JOIN " + CTableNames.GROUP_NODES_TABLE + " ON id = node_id WHERE view_id = " + view.getConfiguration().getId();
try (ResultSet resultSet = provider.getConnection().executeQuery(query, true)) {
while (resultSet.next()) {
final int nodeId = resultSet.getInt("id");
Integer commentId = resultSet.getInt("comment_id");
if (resultSet.wasNull()) {
commentId = null;
}
final boolean collapsed = resultSet.getBoolean("collapsed");
final double posX = resultSet.getDouble("x");
final double posY = 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 INaviGroupNode groupNode = new CGroupNode(nodeId, posX, posY, width, height, color, selected, visible, new HashSet<CTag>(), null, collapsed, provider);
if (commentId != null) {
commentIdToGroupNode.put(commentId, groupNode);
}
nodes.add(groupNode);
}
if (!commentIdToGroupNode.isEmpty()) {
final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToGroupNode.keySet());
for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
commentIdToGroupNode.get(commentIdToComment.getKey()).initializeComment(commentIdToComment.getValue());
}
}
}
}
use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.
the class PostgreSQLNodeLoader method loadNodeTags.
/**
* Loads the node tags of a list of nodes.
*
* @param connection The connection to the database.
* @param nodes The nodes whose tags are loaded.
* @param nodeTagManager Manages all node tags of the database.
*
* @throws SQLException Thrown if loading the node tags failed.
*/
private static void loadNodeTags(final CConnection connection, final List<INaviViewNode> nodes, final CTagManager nodeTagManager) throws SQLException {
final HashMap<Integer, INaviViewNode> idNodeMap = new HashMap<Integer, INaviViewNode>();
final HashMap<Integer, CTag> idTagMap = new HashMap<Integer, CTag>();
final StringBuffer range = new StringBuffer();
boolean isFirst = true;
for (final INaviViewNode node : nodes) {
range.append(isFirst ? "" : ",");
range.append(node.getId());
isFirst = false;
idNodeMap.put(node.getId(), node);
}
if (isFirst) {
return;
}
final String query = String.format("SELECT node_id, tag_id FROM %s WHERE node_id IN (%s)", CTableNames.TAGGED_NODES_TABLE, range.toString());
final ResultSet resultSet = connection.executeQuery(query, true);
try {
final Set<Integer> tagIds = new HashSet<Integer>();
while (resultSet.next()) {
tagIds.add(resultSet.getInt("tag_id"));
}
final Collection<CTag> tags = CTagHelpers.findTags(nodeTagManager.getRootTag(), tagIds);
for (final CTag tag : tags) {
idTagMap.put(tag.getId(), tag);
}
resultSet.beforeFirst();
while (resultSet.next()) {
final INaviViewNode node = idNodeMap.get(resultSet.getInt("node_id"));
final CTag tag = idTagMap.get(resultSet.getInt("tag_id"));
((CNaviViewNode) node).tagNodeSilent(tag);
}
} finally {
resultSet.close();
}
}
use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.
the class PostgreSQLTagFunctions method createTag.
/**
* Creates a new tag in the database.
*
* @param provider The connection to the database.
* @param parent The parent tag of the tag.
* @param name The name of the new tag.
* @param description The description of the new tag.
* @param type The type of the new tag.
*
* @return The new tag.
*
* @throws CouldntSaveDataException Thrown if creating the tag failed.
*/
public static CTag createTag(final AbstractSQLProvider provider, final CTag parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
checkArguments(provider, parent, type);
Preconditions.checkNotNull(name, "IE00556: Name argument can not be null");
Preconditions.checkNotNull(description, "IE00557: Description argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "insert into " + CTableNames.TAGS_TABLE + "(parent_id, name, description, type) values(?, ?, ?, ?::tag_type) returning id";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
if (parent.getId() == 0) {
statement.setNull(1, Types.INTEGER);
} else {
statement.setInt(1, parent.getId());
}
statement.setString(2, name);
statement.setString(3, description);
statement.setString(4, tagToString(type));
Integer id = null;
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
if (resultSet.isFirst()) {
id = resultSet.getInt(1);
}
}
}
if (id != null) {
return new CTag(id, name, description, type, provider);
} else {
throw new IllegalStateException("IE02141: Error id can not be null");
}
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.
the class PostgreSQLTagFunctions method deleteTag.
/**
* Deletes a tag from the database.
*
* @param provider The connection to the database.
* @param tag The tag to delete.
*
* @throws CouldntDeleteException Thrown if the tag could not be deleted.
*/
public static void deleteTag(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
checkArguments(provider, tag);
Preconditions.checkNotNull(tag.getParent(), "IE00558: Can not delete the root tag");
final CConnection connection = provider.getConnection();
try {
final ITreeNode<CTag> parent = tag.getParent();
final String parentId = parent.getObject().getId() == 0 ? "null" : String.valueOf(parent.getObject().getId());
final String query_1 = String.format("UPDATE %s SET parent_id = %s WHERE parent_id = ?", CTableNames.TAGS_TABLE, parentId);
try (PreparedStatement statement_1 = connection.getConnection().prepareStatement(query_1)) {
statement_1.setInt(1, tag.getObject().getId());
statement_1.executeUpdate();
}
final String query_2 = String.format("DELETE FROM %s WHERE id = ?", CTableNames.TAGS_TABLE);
try (PreparedStatement statement_2 = connection.getConnection().prepareStatement(query_2)) {
statement_2.setInt(1, tag.getObject().getId());
statement_2.executeUpdate();
}
} catch (final SQLException e) {
throw new CouldntDeleteException(e);
}
}
use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.
the class PostgreSQLTagFunctions method moveTag.
/**
* Moves a tag.
*
* @param provider Connection to the database.
* @param newParentNode The new parent node of the tag.
* @param movedNode The tag to move.
*
* @throws CouldntSaveDataException Thrown if the tag could not be moved.
*/
public static void moveTag(final AbstractSQLProvider provider, final ITreeNode<CTag> newParentNode, final ITreeNode<CTag> movedNode, final TagType type) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE02083: Provider argument can not be null");
Preconditions.checkNotNull(newParentNode, "IE02190: Parent argument can not be null");
Preconditions.checkNotNull(movedNode, "IE02191: Child argument can not be null");
final List<Integer> childIds = new ArrayList<>();
for (final ITreeNode<CTag> childChild : movedNode.getChildren()) {
childIds.add(childChild.getObject().getId());
}
try {
final String childParentId = movedNode.getParent().getObject().getId() == 0 ? "null" : String.valueOf(movedNode.getParent().getObject().getId());
if (!childIds.isEmpty()) {
// Connect the parent of the child tag with the children of the child tag
provider.getConnection().executeUpdate("update " + CTableNames.TAGS_TABLE + " set parent_id = " + childParentId + " where id in (" + Commafier.commafy(childIds) + ") and type = '" + tagToString(type) + "'", true);
}
// Connect the parent tag with the child tag
provider.getConnection().executeUpdate("update " + CTableNames.TAGS_TABLE + " set parent_id = " + newParentNode.getObject().getId() + " where id = " + movedNode.getObject().getId() + " and type = '" + tagToString(type) + "'", true);
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
Aggregations