4650 newDataSource should get the existing data source if there is one

This commit is contained in:
William Schaefer 2019-01-17 15:32:01 -05:00
parent c93b05540f
commit c2f3bbfb2e

View File

@ -1,7 +1,7 @@
/* /*
* Central Repository * Central Repository
* *
* Copyright 2015-2018 Basis Technology Corp. * Copyright 2015-2019 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org * Contact: carrier <at> sleuthkit <dot> org
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -245,7 +245,7 @@ abstract class AbstractSqlEamDb implements EamDb {
if (eamCase.getCaseUUID() == null) { if (eamCase.getCaseUUID() == null) {
throw new EamDbException("Case UUID is null"); throw new EamDbException("Case UUID is null");
} }
// check if there is already an existing CorrelationCase for this Case // check if there is already an existing CorrelationCase for this Case
CorrelationCase cRCase = getCaseByUUID(eamCase.getCaseUUID()); CorrelationCase cRCase = getCaseByUUID(eamCase.getCaseUUID());
if (cRCase != null) { if (cRCase != null) {
@ -636,13 +636,22 @@ abstract class AbstractSqlEamDb implements EamDb {
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys(); resultSet = preparedStatement.getGeneratedKeys();
if (!resultSet.next()) { if (!resultSet.next()) {
throw new EamDbException(String.format("Failed to INSERT data source %s in central repo", eamDataSource.getName())); //if nothing was inserted then return the DataSource that exists in the central repository
try {
return dataSource = dataSourceCacheByDsObjectId.get(getDataSourceByDSObjectIdCacheKey(
eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()),
() -> getDataSourceFromCr(eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()));
} catch (CacheLoader.InvalidCacheLoadException ex) {
throw new EamDbException(String.format("Unable to to INSERT or get data source %s in central repo", eamDataSource.getName()), ex);
}
} else {
//if a new data source was added to the central repository update the caches to include it and return it
int dataSourceId = resultSet.getInt(1); //last_insert_rowid()
CorrelationDataSource dataSource = new CorrelationDataSource(eamDataSource.getCaseID(), dataSourceId, eamDataSource.getDeviceID(), eamDataSource.getName(), eamDataSource.getDataSourceObjectID());
dataSourceCacheByDsObjectId.put(getDataSourceByDSObjectIdCacheKey(dataSource.getCaseID(), dataSource.getDataSourceObjectID()), dataSource);
dataSourceCacheById.put(getDataSourceByIdCacheKey(dataSource.getCaseID(), dataSource.getID()), dataSource);
return dataSource;
} }
int dataSourceId = resultSet.getInt(1); //last_insert_rowid()
CorrelationDataSource dataSource = new CorrelationDataSource(eamDataSource.getCaseID(), dataSourceId, eamDataSource.getDeviceID(), eamDataSource.getName(), eamDataSource.getDataSourceObjectID());
dataSourceCacheByDsObjectId.put(getDataSourceByDSObjectIdCacheKey(dataSource.getCaseID(), dataSource.getDataSourceObjectID()), dataSource);
dataSourceCacheById.put(getDataSourceByIdCacheKey(dataSource.getCaseID(), dataSource.getID()), dataSource);
return dataSource;
} catch (SQLException ex) { } catch (SQLException ex) {
throw new EamDbException("Error inserting new data source.", ex); // NON-NLS throw new EamDbException("Error inserting new data source.", ex); // NON-NLS
} finally { } finally {
@ -670,7 +679,7 @@ abstract class AbstractSqlEamDb implements EamDb {
throw new EamDbException("Correlation case is null"); throw new EamDbException("Correlation case is null");
} }
try { try {
return dataSourceCacheByDsObjectId.get(getDataSourceByDSObjectIdCacheKey(correlationCase.getID(), dataSourceObjectId), () -> getDataSourceFromCr(correlationCase, dataSourceObjectId)); return dataSourceCacheByDsObjectId.get(getDataSourceByDSObjectIdCacheKey(correlationCase.getID(), dataSourceObjectId), () -> getDataSourceFromCr(correlationCase.getID(), dataSourceObjectId));
} catch (CacheLoader.InvalidCacheLoadException ignored) { } catch (CacheLoader.InvalidCacheLoadException ignored) {
//lambda valueloader returned a null value and cache can not store null values this is normal if the dataSource does not exist in the central repo yet //lambda valueloader returned a null value and cache can not store null values this is normal if the dataSource does not exist in the central repo yet
return null; return null;
@ -683,15 +692,15 @@ abstract class AbstractSqlEamDb implements EamDb {
* Gets the Data Source details based on data source device ID from the * Gets the Data Source details based on data source device ID from the
* central repository. * central repository.
* *
* @param correlationCase the current CorrelationCase used for ensuring * @param correlationCaseId the current CorrelationCase id used for
* uniqueness of DataSource * ensuring uniqueness of DataSource
* @param dataSourceDeviceId the object id of the data source * @param dataSourceObjectId the object id of the data source
* *
* @return The data source * @return The data source
* *
* @throws EamDbException * @throws EamDbException
*/ */
private CorrelationDataSource getDataSourceFromCr(CorrelationCase correlationCase, Long dataSourceObjectId) throws EamDbException { private CorrelationDataSource getDataSourceFromCr(int correlationCaseId, Long dataSourceObjectId) throws EamDbException {
Connection conn = connect(); Connection conn = connect();
CorrelationDataSource eamDataSourceResult = null; CorrelationDataSource eamDataSourceResult = null;
@ -703,13 +712,13 @@ abstract class AbstractSqlEamDb implements EamDb {
try { try {
preparedStatement = conn.prepareStatement(sql); preparedStatement = conn.prepareStatement(sql);
preparedStatement.setLong(1, dataSourceObjectId); preparedStatement.setLong(1, dataSourceObjectId);
preparedStatement.setInt(2, correlationCase.getID()); preparedStatement.setInt(2, correlationCaseId);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
eamDataSourceResult = getEamDataSourceFromResultSet(resultSet); eamDataSourceResult = getEamDataSourceFromResultSet(resultSet);
} }
if (eamDataSourceResult != null) { if (eamDataSourceResult != null) {
dataSourceCacheById.put(getDataSourceByIdCacheKey(correlationCase.getID(), eamDataSourceResult.getID()), eamDataSourceResult); dataSourceCacheById.put(getDataSourceByIdCacheKey(correlationCaseId, eamDataSourceResult.getID()), eamDataSourceResult);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
throw new EamDbException("Error getting data source.", ex); // NON-NLS throw new EamDbException("Error getting data source.", ex); // NON-NLS