Skip to content

Commit

Permalink
When adding tags, ignore duplicates. This protects against issues adding
Browse files Browse the repository at this point in the history
multiple conditions against the same dataId, and is simply more tolerant in
general.
  • Loading branch information
jshaughn committed Mar 18, 2015
1 parent 680ecf2 commit b605877
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ Collection<Condition> setConditions(String triggerId, Trigger.Mode triggerMode,
CRUD interface for Tag
*/

/**
* Add Tag with the specified name to the specified Trigger. Category is optional. If the Tag exists the
* call returns successfully but has no effect.
* @param tag
* @throws Exception
*/
void addTag(Tag tag) throws Exception;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,12 @@ public void addTag(Tag tag) throws Exception {

private void insertTag(Connection c, Statement s, String triggerId, String category, String name, boolean visible)
throws Exception {

// If the desired Tag already exists just return
if (!getTags(c, s, triggerId, category, name).isEmpty()) {
return;
}

StringBuilder sql = new StringBuilder("INSERT INTO HWK_ALERTS_TAGS VALUES (");
sql.append("'").append(triggerId).append("', ");
if (isEmpty(category)) {
Expand Down Expand Up @@ -1849,32 +1855,53 @@ public List<Tag> getTriggerTags(String triggerId, String category) throws Except
throw new Exception("DataSource is null");
}

List<Tag> tags = new ArrayList<>();
List<Tag> tags = null;
Connection c = null;
Statement s = null;
ResultSet rs = null;
try {
c = ds.getConnection();
s = c.createStatement();

StringBuilder sql = new StringBuilder(
"SELECT triggerId, category, name, visible FROM HWK_ALERTS_TAGS WHERE ");
sql.append("triggerId = '").append(triggerId).append("' ");
if (!isEmpty(category)) {
sql.append("AND category = '").append(category).append("' ");
}
sql.append("ORDER BY category, name");
log.debugf("SQL: " + sql);
tags = getTags(c, s, triggerId, category, null);

} catch (SQLException e) {
msgLog.errorDatabaseException(e.getMessage());
throw e;
} finally {
close(c, s);
}

return tags;
}

private List<Tag> getTags(Connection c, Statement s, String triggerId, String category, String name)
throws Exception {

StringBuilder sql = new StringBuilder(
"SELECT triggerId, category, name, visible FROM HWK_ALERTS_TAGS WHERE ");
sql.append("triggerId = '").append(triggerId).append("' ");
if (!isEmpty(category)) {
sql.append("AND category = '").append(category).append("' ");
}
if (!isEmpty(name)) {
sql.append("AND name = '").append(name).append("' ");
}

sql.append("ORDER BY category, name");
log.debugf("SQL: " + sql);

List<Tag> tags = new ArrayList<>();
ResultSet rs = null;
try {
rs = s.executeQuery(sql.toString());
while (rs.next()) {
Tag tag = new Tag(rs.getString(1), rs.getString(2), rs.getString(3), rs.getBoolean(4));
tags.add(tag);
}
} catch (SQLException e) {
msgLog.errorDatabaseException(e.getMessage());
throw e;
} finally {
close(c, s, rs);
if (null != rs) {
rs.close();
}
}

return tags;
Expand Down

0 comments on commit b605877

Please sign in to comment.