Reduce NPath complexity

This commit is contained in:
Andrew Ziehl 2018-08-23 09:37:17 -07:00
parent e405c3ab98
commit 7f0ba58eab
2 changed files with 109 additions and 42 deletions

View File

@ -628,31 +628,7 @@ abstract class AbstractSqlEamDb implements EamDb {
*/ */
@Override @Override
public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws EamDbException { public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws EamDbException {
if (eamArtifact == null) { checkAddArtifactInstanceNulls(eamArtifact);
throw new EamDbException("CorrelationAttribute is null");
}
if (eamArtifact.getCorrelationType() == null) {
throw new EamDbException("Correlation type is null");
}
if (eamArtifact.getCorrelationValue() == null) {
throw new EamDbException("Correlation value is null");
}
if (eamArtifact.getCorrelationValue().length() >= MAX_VALUE_LENGTH) {
throw new EamDbException("Artifact value too long for central repository."
+ "\nCorrelationArtifact ID: " + eamArtifact.getID()
+ "\nCorrelationArtifact Type: " + eamArtifact.getCorrelationType().getDisplayName()
+ "\nCorrelationArtifact Value: " + eamArtifact.getCorrelationValue());
}
if (eamArtifact.getCorrelationCase() == null) {
throw new EamDbException("CorrelationAttributeInstance case is null");
}
if (eamArtifact.getCorrelationDataSource() == null) {
throw new EamDbException("CorrelationAttributeInstance data source is null");
}
if (eamArtifact.getKnownStatus() == null) {
throw new EamDbException("CorrelationAttributeInstance known status is null");
}
Connection conn = connect(); Connection conn = connect();
@ -696,6 +672,34 @@ abstract class AbstractSqlEamDb implements EamDb {
} }
} }
private void checkAddArtifactInstanceNulls(CorrelationAttributeInstance eamArtifact) throws EamDbException {
if (eamArtifact == null) {
throw new EamDbException("CorrelationAttribute is null");
}
if (eamArtifact.getCorrelationType() == null) {
throw new EamDbException("Correlation type is null");
}
if (eamArtifact.getCorrelationValue() == null) {
throw new EamDbException("Correlation value is null");
}
if (eamArtifact.getCorrelationValue().length() >= MAX_VALUE_LENGTH) {
throw new EamDbException("Artifact value too long for central repository."
+ "\nCorrelationArtifact ID: " + eamArtifact.getID()
+ "\nCorrelationArtifact Type: " + eamArtifact.getCorrelationType().getDisplayName()
+ "\nCorrelationArtifact Value: " + eamArtifact.getCorrelationValue());
}
if (eamArtifact.getCorrelationCase() == null) {
throw new EamDbException("CorrelationAttributeInstance case is null");
}
if (eamArtifact.getCorrelationDataSource() == null) {
throw new EamDbException("CorrelationAttributeInstance data source is null");
}
if (eamArtifact.getKnownStatus() == null) {
throw new EamDbException("CorrelationAttributeInstance known status is null");
}
}
/** /**
* Retrieves eamArtifact instances from the database that are associated * Retrieves eamArtifact instances from the database that are associated
* with the eamArtifactType and eamArtifactValue of the given eamArtifact. * with the eamArtifactType and eamArtifactValue of the given eamArtifact.
@ -2460,7 +2464,27 @@ abstract class AbstractSqlEamDb implements EamDb {
if (newType == null) { if (newType == null) {
throw new EamDbException("Correlation type is null"); throw new EamDbException("Correlation type is null");
} }
int typeId;
if (-1 == newType.getId()) {
typeId = newCorrelationTypeNotKnownId(newType);
} else {
typeId = newCorrelationTypeKnownId(newType);
}
return typeId;
}
/**
* Helper function which adds a new EamArtifact.Type to the db without an
* id.
*
* @param newType New type to add.
*
* @return ID of this new Correlation Type
*
* @throws EamDbException
*/
public int newCorrelationTypeNotKnownId(CorrelationAttributeInstance.Type newType) throws EamDbException {
Connection conn = connect(); Connection conn = connect();
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -2470,28 +2494,71 @@ abstract class AbstractSqlEamDb implements EamDb {
String insertSql; String insertSql;
String querySql; String querySql;
// if we have a known ID, use it, if not (is -1) let the db assign it. // if we have a known ID, use it, if not (is -1) let the db assign it.
if (-1 == newType.getId()) { insertSql = "INSERT INTO correlation_types(display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?) " + getConflictClause();
insertSql = "INSERT INTO correlation_types(display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?) " + getConflictClause();
} else {
insertSql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?) " + getConflictClause();
}
querySql = "SELECT * FROM correlation_types WHERE display_name=? AND db_table_name=?"; querySql = "SELECT * FROM correlation_types WHERE display_name=? AND db_table_name=?";
try { try {
preparedStatement = conn.prepareStatement(insertSql); preparedStatement = conn.prepareStatement(insertSql);
if (-1 == newType.getId()) { preparedStatement.setString(1, newType.getDisplayName());
preparedStatement.setString(1, newType.getDisplayName()); preparedStatement.setString(2, newType.getDbTableName());
preparedStatement.setString(2, newType.getDbTableName()); preparedStatement.setInt(3, newType.isSupported() ? 1 : 0);
preparedStatement.setInt(3, newType.isSupported() ? 1 : 0); preparedStatement.setInt(4, newType.isEnabled() ? 1 : 0);
preparedStatement.setInt(4, newType.isEnabled() ? 1 : 0);
} else { preparedStatement.executeUpdate();
preparedStatement.setInt(1, newType.getId());
preparedStatement.setString(2, newType.getDisplayName()); preparedStatementQuery = conn.prepareStatement(querySql);
preparedStatement.setString(3, newType.getDbTableName()); preparedStatementQuery.setString(1, newType.getDisplayName());
preparedStatement.setInt(4, newType.isSupported() ? 1 : 0); preparedStatementQuery.setString(2, newType.getDbTableName());
preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
resultSet = preparedStatementQuery.executeQuery();
if (resultSet.next()) {
CorrelationAttributeInstance.Type correlationType = getCorrelationTypeFromResultSet(resultSet);
typeId = correlationType.getId();
} }
} catch (SQLException ex) {
throw new EamDbException("Error inserting new correlation type.", ex); // NON-NLS
} finally {
EamDbUtil.closeStatement(preparedStatement);
EamDbUtil.closeStatement(preparedStatementQuery);
EamDbUtil.closeResultSet(resultSet);
EamDbUtil.closeConnection(conn);
}
return typeId;
}
/**
* Helper function which adds a new EamArtifact.Type to the db.
*
* @param newType New type to add.
*
* @return ID of this new Correlation Type
*
* @throws EamDbException
*/
private int newCorrelationTypeKnownId(CorrelationAttributeInstance.Type newType) throws EamDbException {
Connection conn = connect();
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementQuery = null;
ResultSet resultSet = null;
int typeId = 0;
String insertSql;
String querySql;
// if we have a known ID, use it, if not (is -1) let the db assign it.
insertSql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?) " + getConflictClause();
querySql = "SELECT * FROM correlation_types WHERE display_name=? AND db_table_name=?";
try {
preparedStatement = conn.prepareStatement(insertSql);
preparedStatement.setInt(1, newType.getId());
preparedStatement.setString(2, newType.getDisplayName());
preparedStatement.setString(3, newType.getDbTableName());
preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();

View File

@ -371,7 +371,7 @@ public class CorrelationAttributeInstance implements Serializable {
.append(isSupported().toString()) .append(isSupported().toString())
.append(", enabled=") .append(", enabled=")
.append(isEnabled().toString()) .append(isEnabled().toString())
.append(")"); .append(')');
return str.toString(); return str.toString();
} }