class DatabaseConsistencyCheckHelper {

  @Transactional
  static void fixConfigsSelectedMoreThanOnce() {
    LOG.info("Fix configs selected more than once");
    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);

    Clusters clusters = injector.getInstance(Clusters.class);
    Map<String, Cluster> clusterMap = clusters.getClusters();


    Multimap<String, String> clusterConfigTypeMap = HashMultimap.create();
    ResultSet rs = null;
    Statement statement = null;

    ensureConnection();

    try {
      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = statement.executeQuery(GET_CONFIGS_SELECTED_MORE_THAN_ONCE_QUERY);
      if (rs != null) {
        while (rs.next()) {
          clusterConfigTypeMap.put(rs.getString("cluster_name"), rs.getString("type_name"));
        }
      }

    } catch (SQLException e) {
      warning("Exception occurred during check for config selected more than once procedure: ", e);
    } finally {
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {
          LOG.error("Exception occurred during result set closing procedure: ", e);
        }
      }

      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
          LOG.error("Exception occurred during statement closing procedure: ", e);
        }
      }
    }

    for (String clusterName : clusterConfigTypeMap.keySet()) {
      Cluster cluster = null;
      try {
        cluster = clusters.getCluster(clusterName);

        Collection<String> typesWithMultipleSelectedConfigs = clusterConfigTypeMap.get(clusterName);

        for (String type: typesWithMultipleSelectedConfigs) {
          List<ClusterConfigEntity> enabledConfigsByType = getEnabledConfigsByType(cluster.getClusterId(), type);
          ClusterConfigEntity latestConfig = enabledConfigsByType.get(0);
          for (ClusterConfigEntity entity : enabledConfigsByType){
            entity.setSelected(false);
            if (latestConfig.getSelectedTimestamp() < entity.getSelectedTimestamp()){
              latestConfig = entity;
            }
            clusterDAO.merge(entity, true);
          }
          latestConfig.setSelected(true);
          clusterDAO.merge(latestConfig, true);
        }
      } catch (AmbariException e) {
        warning("Exception occurred during fix for config selected more than once procedure: ", e);
      }
    }
  }

}