diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java index 18674d05f939e62699ce427c18fc975c6c842610..dfffd13e5124089668e5dcf280a21f17ffcfabb2 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java @@ -640,12 +640,13 @@ public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) resultSet = preparedStatement.getGeneratedKeys(); if (!resultSet.next()) { //if nothing was inserted then return the DataSource that exists in the central repository + //expected to occur in regards to PostgreSQL central repository databases try { return dataSourceCacheByDsObjectId.get(getDataSourceByDSObjectIdCacheKey( eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()), () -> getDataSourceFromCr(eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID())); - } catch (CacheLoader.InvalidCacheLoadException | ExecutionException ex) { - throw new EamDbException(String.format("Unable to to INSERT or get data source %s in central repo", eamDataSource.getName()), ex); + } catch (CacheLoader.InvalidCacheLoadException | ExecutionException getException) { + throw new EamDbException(String.format("Unable to to INSERT or get data source %s in central repo:", eamDataSource.getName()), getException); } } else { //if a new data source was added to the central repository update the caches to include it and return it @@ -656,8 +657,16 @@ public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) return dataSource; } - } catch (SQLException ex) { - throw new EamDbException("Error inserting new data source.", ex); // NON-NLS + } catch (SQLException insertException) { + //if an exception was thrown causing us to not return the Datasource attempt to get the datasource to return + //is expected to occur in regards to SQLite central repository databases + try { + return dataSourceCacheByDsObjectId.get(getDataSourceByDSObjectIdCacheKey( + eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()), + () -> getDataSourceFromCr(eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID())); + } catch (CacheLoader.InvalidCacheLoadException | ExecutionException getException) { + throw new EamDbException(String.format("Unable to to INSERT or get data source %s in central repo, insert failed due to Exception: %s", eamDataSource.getName(), insertException.getMessage()), getException); + } } finally { EamDbUtil.closeResultSet(resultSet); EamDbUtil.closeStatement(preparedStatement); @@ -832,7 +841,7 @@ public List<CorrelationDataSource> getDataSources() throws EamDbException { return dataSources; } - + /** * Updates the MD5 hash value in an existing data source in the database. * @@ -842,7 +851,7 @@ public List<CorrelationDataSource> getDataSources() throws EamDbException { public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws EamDbException { updateDataSourceStringValue(eamDataSource, "md5", eamDataSource.getMd5()); } - + /** * Updates the SHA-1 hash value in an existing data source in the database. * @@ -852,9 +861,10 @@ public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws EamDbException { updateDataSourceStringValue(eamDataSource, "sha1", eamDataSource.getSha1()); } - + /** - * Updates the SHA-256 hash value in an existing data source in the database. + * Updates the SHA-256 hash value in an existing data source in the + * database. * * @param eamDataSource The data source to update */ @@ -862,13 +872,13 @@ public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws EamDbException { updateDataSourceStringValue(eamDataSource, "sha256", eamDataSource.getSha256()); } - + /** * Updates the specified value in an existing data source in the database. * * @param eamDataSource The data source to update - * @param column The name of the column to be updated - * @param value The value to assign to the specified column + * @param column The name of the column to be updated + * @param value The value to assign to the specified column */ private void updateDataSourceStringValue(CorrelationDataSource eamDataSource, String column, String value) throws EamDbException { if (eamDataSource == null) { @@ -884,10 +894,10 @@ private void updateDataSourceStringValue(CorrelationDataSource eamDataSource, St try { preparedStatement = conn.prepareStatement(sql); - + preparedStatement.setString(1, value); preparedStatement.setInt(2, eamDataSource.getID()); - + preparedStatement.executeUpdate(); //update the case in the cache dataSourceCacheByDsObjectId.put(getDataSourceByDSObjectIdCacheKey(eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()), eamDataSource); @@ -902,15 +912,15 @@ private void updateDataSourceStringValue(CorrelationDataSource eamDataSource, St /** * Changes the name of a data source in the DB - * - * @param eamDataSource The data source - * @param newName The new name - * - * @throws EamDbException + * + * @param eamDataSource The data source + * @param newName The new name + * + * @throws EamDbException */ @Override public void updateDataSourceName(CorrelationDataSource eamDataSource, String newName) throws EamDbException { - + Connection conn = connect(); PreparedStatement preparedStatement = null; @@ -922,7 +932,7 @@ public void updateDataSourceName(CorrelationDataSource eamDataSource, String new preparedStatement.setString(1, newName); preparedStatement.setInt(2, eamDataSource.getID()); preparedStatement.executeUpdate(); - + CorrelationDataSource updatedDataSource = new CorrelationDataSource( eamDataSource.getCaseID(), eamDataSource.getID(), @@ -932,7 +942,7 @@ public void updateDataSourceName(CorrelationDataSource eamDataSource, String new eamDataSource.getMd5(), eamDataSource.getSha1(), eamDataSource.getSha256()); - + dataSourceCacheByDsObjectId.put(getDataSourceByDSObjectIdCacheKey(updatedDataSource.getCaseID(), updatedDataSource.getDataSourceObjectID()), updatedDataSource); dataSourceCacheById.put(getDataSourceByIdCacheKey(updatedDataSource.getCaseID(), updatedDataSource.getID()), updatedDataSource); } catch (SQLException ex) { @@ -941,9 +951,9 @@ public void updateDataSourceName(CorrelationDataSource eamDataSource, String new } finally { EamDbUtil.closeStatement(preparedStatement); EamDbUtil.closeConnection(conn); - } - } - + } + } + /** * Inserts new Artifact(s) into the database. Should add associated Case and * Data Source first.