19 package org.sleuthkit.datamodel;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
51 private final Map<
Account.
Type, Integer> accountTypeToTypeIdMap
52 =
new ConcurrentHashMap<>();
53 private final Map<String,
Account.
Type> typeNameToAccountTypeMap
54 =
new ConcurrentHashMap<>();
57 private static final Set<Integer> RELATIONSHIP_ARTIFACT_TYPE_IDS =
new HashSet<Integer>(Arrays.asList(
63 private static final String RELATIONSHIP_ARTIFACT_TYPE_IDS_CSV_STR = StringUtils.buildCSVString(RELATIONSHIP_ARTIFACT_TYPE_IDS);
85 CaseDbConnection connection = db.getConnection();
87 Statement statement = null;
88 ResultSet resultSet = null;
91 statement = connection.createStatement();
93 int count = readAccountTypes();
98 statement.execute(
"INSERT INTO account_types (type_name, display_name) VALUES ( '" + type.getTypeName() +
"', '" + type.getDisplayName() +
"')");
99 }
catch (SQLException ex) {
100 resultSet = connection.executeQuery(statement,
"SELECT COUNT(*) AS count FROM account_types WHERE type_name = '" + type.getTypeName() +
"'");
102 if (resultSet.getLong(
"count") == 0) {
108 ResultSet rs2 = connection.executeQuery(statement,
"SELECT account_type_id FROM account_types WHERE type_name = '" + type.getTypeName() +
"'");
110 int typeID = rs2.getInt(
"account_type_id");
114 this.accountTypeToTypeIdMap.put(accountType, typeID);
115 this.typeNameToAccountTypeMap.put(type.getTypeName(), accountType);
118 }
catch (SQLException ex) {
119 LOGGER.log(Level.SEVERE,
"Failed to add row to account_types", ex);
121 closeResultSet(resultSet);
122 closeStatement(statement);
137 CaseDbConnection connection = db.getConnection();
139 Statement statement = null;
140 ResultSet resultSet = null;
144 statement = connection.createStatement();
147 resultSet = connection.executeQuery(statement,
"SELECT COUNT(*) AS count FROM account_types");
149 if (resultSet.getLong(
"count") > 0) {
152 resultSet = connection.executeQuery(statement,
"SELECT * FROM account_types");
153 while (resultSet.next()) {
154 Account.
Type accountType =
new Account.
Type(resultSet.getString(
"type_name"), resultSet.getString(
"display_name"));
155 this.accountTypeToTypeIdMap.put(accountType, resultSet.getInt(
"account_type_id"));
156 this.typeNameToAccountTypeMap.put(accountType.getTypeName(), accountType);
158 count = this.typeNameToAccountTypeMap.size();
161 }
catch (SQLException ex) {
164 closeResultSet(resultSet);
165 closeStatement(statement);
199 if (this.accountTypeToTypeIdMap.containsKey(accountType)) {
203 CaseDbConnection connection = db.getConnection();
208 connection.beginTransaction();
209 s = connection.createStatement();
210 rs = connection.executeQuery(s,
"SELECT * FROM account_types WHERE type_name = '" + accountTypeName +
"'");
214 s.execute(
"INSERT INTO account_types (type_name, display_name) VALUES ( '" + accountTypeName +
"', '" + displayName +
"')");
217 rs = connection.executeQuery(s,
"SELECT * FROM account_types WHERE type_name = '" + accountTypeName +
"'");
220 int typeID = rs.getInt(
"account_type_id");
221 accountType =
new Account.
Type(rs.getString(
"type_name"), rs.getString(
"display_name"));
223 this.accountTypeToTypeIdMap.put(accountType, typeID);
224 this.typeNameToAccountTypeMap.put(accountTypeName, accountType);
226 connection.commitTransaction();
230 int typeID = rs.getInt(
"account_type_id");
232 accountType =
new Account.
Type(rs.getString(
"type_name"), rs.getString(
"display_name"));
233 this.accountTypeToTypeIdMap.put(accountType, typeID);
237 }
catch (SQLException ex) {
238 connection.rollbackTransaction();
269 Account account = getOrCreateAccount(accountType, normalizeAccountID(accountType, accountUniqueID));
277 BlackboardArtifact accountArtifact = getOrCreateAccountFileInstanceArtifact(accountType, normalizeAccountID(accountType, accountUniqueID), moduleName, sourceFile);
303 CaseDbConnection connection = db.getConnection();
308 s = connection.createStatement();
309 rs = connection.executeQuery(s,
"SELECT * FROM accounts WHERE account_type_id = " + getAccountTypeId(accountType)
310 +
" AND account_unique_identifier = '" + normalizeAccountID(accountType, accountUniqueID) +
"'");
313 account =
new Account(rs.getInt(
"account_id"), accountType,
314 rs.getString(
"account_unique_identifier"));
316 }
catch (SQLException ex) {
354 if (relationshipType.isCreatableFrom(sourceArtifact) ==
false) {
355 throw new TskDataException(
"Can not make a " + relationshipType.getDisplayName()
356 +
" relationship from a" + sourceArtifact.getDisplayName());
365 List<Long> accountIDs =
new ArrayList<Long>();
367 if (null != sender) {
368 accountIDs.add(sender.getAccount().getAccountID());
369 if (sender.getDataSourceObjectID() != sourceArtifact.getDataSourceObjectID()) {
370 throw new TskDataException(
"Sender and relationship are from different data sources :"
371 +
"Sender source ID" + sender.getDataSourceObjectID() +
" != relationship source ID" + sourceArtifact.getDataSourceObjectID());
376 accountIDs.add(recipient.getAccount().getAccountID());
377 if (recipient.getDataSourceObjectID() != sourceArtifact.getDataSourceObjectID()) {
378 throw new TskDataException(
"Recipient and relationship are from different data sources :"
379 +
"Recipient source ID" + recipient.getDataSourceObjectID() +
" != relationship source ID" + sourceArtifact.getDataSourceObjectID());
383 for (
int i = 0; i < accountIDs.size(); i++) {
384 for (
int j = i + 1; j < accountIDs.size(); j++) {
386 addAccountsRelationship(accountIDs.get(i), accountIDs.get(j),
387 sourceArtifact, relationshipType, dateTime);
388 }
catch (TskCoreException ex) {
390 LOGGER.log(Level.WARNING,
"Error adding relationship", ex);
408 private Account getOrCreateAccount(
Account.
Type accountType, String accountUniqueID)
throws TskCoreException {
410 if (null == account) {
411 String query =
" INTO accounts (account_type_id, account_unique_identifier) "
412 +
"VALUES ( " + getAccountTypeId(accountType) +
", '"
413 + normalizeAccountID(accountType, accountUniqueID) +
"'" +
")";
416 query =
"INSERT " + query +
" ON CONFLICT DO NOTHING";
419 query =
"INSERT OR IGNORE " + query;
422 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
425 CaseDbConnection connection = db.getConnection();
430 connection.beginTransaction();
431 s = connection.createStatement();
435 connection.commitTransaction();
436 account =
getAccount(accountType, accountUniqueID);
437 }
catch (SQLException ex) {
438 connection.rollbackTransaction();
439 throw new TskCoreException(
"Error adding an account", ex);
468 private BlackboardArtifact getOrCreateAccountFileInstanceArtifact(Account.Type accountType, String accountUniqueID, String moduleName, Content sourceFile)
throws TskCoreException {
469 BlackboardArtifact accountArtifact = getAccountFileInstanceArtifact(accountType, accountUniqueID, sourceFile);
470 if (accountArtifact == null) {
471 accountArtifact = db.
newBlackboardArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_ACCOUNT, sourceFile.getId());
472 Collection<BlackboardAttribute> attributes =
new ArrayList<>();
473 attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE, moduleName, accountType.getTypeName()));
474 attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID, moduleName, accountUniqueID));
478 }
catch (BlackboardException ex) {
479 LOGGER.log(Level.SEVERE, String.format(
"Error posting new account artifact to the blackboard (object ID = %d)", accountArtifact.getId()), ex);
482 return accountArtifact;
498 private BlackboardArtifact getAccountFileInstanceArtifact(Account.Type accountType, String accountUniqueID, Content sourceFile)
throws TskCoreException {
499 BlackboardArtifact accountArtifact = null;
500 CaseDbConnection connection = db.getConnection();
506 s = connection.createStatement();
507 String queryStr =
"SELECT artifacts.artifact_id AS artifact_id,"
508 +
" artifacts.obj_id AS obj_id,"
509 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
510 +
" artifacts.data_source_obj_id AS data_source_obj_id,"
511 +
" artifacts.artifact_type_id AS artifact_type_id,"
512 +
" artifacts.review_status_id AS review_status_id"
513 +
" FROM blackboard_artifacts AS artifacts"
514 +
" JOIN blackboard_attributes AS attr_account_type"
515 +
" ON artifacts.artifact_id = attr_account_type.artifact_id"
516 +
" JOIN blackboard_attributes AS attr_account_id"
517 +
" ON artifacts.artifact_id = attr_account_id.artifact_id"
518 +
" AND attr_account_id.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID.getTypeID()
519 +
" AND attr_account_id.value_text = '" + accountUniqueID +
"'"
520 +
" WHERE artifacts.artifact_type_id = " + BlackboardArtifact.ARTIFACT_TYPE.TSK_ACCOUNT.getTypeID()
521 +
" AND attr_account_type.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE.getTypeID()
522 +
" AND attr_account_type.value_text = '" + accountType.getTypeName() +
"'"
523 +
" AND artifacts.obj_id = " + sourceFile.getId();
525 rs = connection.executeQuery(s, queryStr);
527 BlackboardArtifact.Type bbartType = db.
getArtifactType(rs.getInt(
"artifact_type_id"));
529 accountArtifact =
new BlackboardArtifact(db, rs.getLong(
"artifact_id"), rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"), rs.getLong(
"data_source_obj_id"),
530 bbartType.getTypeID(), bbartType.getTypeName(), bbartType.getDisplayName(),
531 BlackboardArtifact.ReviewStatus.withID(rs.getInt(
"review_status_id")));
533 }
catch (SQLException ex) {
534 throw new TskCoreException(
"Error getting account", ex);
542 return accountArtifact;
556 if (this.typeNameToAccountTypeMap.containsKey(accountTypeName)) {
557 return this.typeNameToAccountTypeMap.get(accountTypeName);
560 CaseDbConnection connection = db.getConnection();
566 s = connection.createStatement();
567 rs = connection.executeQuery(s,
"SELECT account_type_id, type_name, display_name FROM account_types WHERE type_name = '" + accountTypeName +
"'");
570 accountType =
new Account.
Type(accountTypeName, rs.getString(
"display_name"));
571 this.accountTypeToTypeIdMap.put(accountType, rs.getInt(
"account_type_id"));
572 this.typeNameToAccountTypeMap.put(accountTypeName, accountType);
575 }
catch (SQLException ex) {
576 throw new TskCoreException(
"Error getting account type id", ex);
598 private void addAccountsRelationship(
long account1_id,
long account2_id,
BlackboardArtifact relationshipaArtifact,
Relationship.
Type relationshipType,
long dateTime)
throws TskCoreException {
599 CaseDbConnection connection = db.getConnection();
605 String dateTimeValStr = (dateTime > 0) ? Long.toString(dateTime) :
"NULL";
607 connection.beginTransaction();
608 s = connection.createStatement();
609 String query =
"INTO account_relationships (account1_id, account2_id, relationship_source_obj_id, date_time, relationship_type, data_source_obj_id ) "
610 +
"VALUES ( " + account1_id +
", " + account2_id +
", " + relationshipaArtifact.getId() +
", " + dateTimeValStr +
", " + relationshipType.getTypeID() +
", " + relationshipaArtifact.getDataSourceObjectID() +
")";
613 query =
"INSERT " + query +
" ON CONFLICT DO NOTHING";
616 query =
"INSERT OR IGNORE " + query;
619 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
622 connection.commitTransaction();
623 }
catch (SQLException ex) {
624 connection.rollbackTransaction();
625 throw new TskCoreException(
"Error adding accounts relationship", ex);
649 CaseDbConnection connection = db.getConnection();
655 s = connection.createStatement();
658 Set<String> applicableInnerQueryFilters =
new HashSet<String>(Arrays.asList(
663 String relationshipFilterSQL = getCommunicationsFilterSQL(filter, applicableInnerQueryFilters);
665 String relationshipLimitSQL = getMostRecentFilterLimitSQL(filter);
667 String relTblfilterQuery
669 +
"FROM account_relationships as relationships"
670 + (relationshipFilterSQL.isEmpty() ?
"" :
" WHERE " + relationshipFilterSQL)
671 + (relationshipLimitSQL.isEmpty() ?
"" : relationshipLimitSQL);
673 String uniqueAccountQueryTemplate
674 =
" SELECT %1$1s as account_id,"
675 +
" data_source_obj_id"
676 +
" FROM ( " + relTblfilterQuery +
")AS %2$s";
678 String relationshipTableFilterQuery1 = String.format(uniqueAccountQueryTemplate,
"account1_id",
"union_query_1");
679 String relationshipTableFilterQuery2 = String.format(uniqueAccountQueryTemplate,
"account2_id",
"union_query_2");
682 String uniqueAccountQuery
683 =
"SELECT DISTINCT account_id, data_source_obj_id"
684 +
" FROM ( " + relationshipTableFilterQuery1 +
" UNION " + relationshipTableFilterQuery2 +
" ) AS inner_union"
685 +
" GROUP BY account_id, data_source_obj_id";
688 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
692 String accountTypeFilterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
696 " accounts.account_id AS account_id,"
697 +
" accounts.account_unique_identifier AS account_unique_identifier,"
699 +
" account_types.type_name AS type_name,"
701 +
" data_source_info.device_id AS device_id"
702 +
" FROM ( " + uniqueAccountQuery +
" ) AS account_device_instances"
703 +
" JOIN accounts AS accounts"
704 +
" ON accounts.account_id = account_device_instances.account_id"
705 +
" JOIN account_types AS account_types"
706 +
" ON accounts.account_type_id = account_types.account_type_id"
707 +
" JOIN data_source_info AS data_source_info"
708 +
" ON account_device_instances.data_source_obj_id = data_source_info.obj_id"
709 + (accountTypeFilterSQL.isEmpty() ?
"" :
" WHERE " + accountTypeFilterSQL);
713 queryStr =
"SELECT DISTINCT ON ( accounts.account_id, data_source_info.device_id) " + queryStr;
716 queryStr =
"SELECT " + queryStr +
" GROUP BY accounts.account_id, data_source_info.device_id";
719 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
722 rs = connection.executeQuery(s, queryStr);
723 ArrayList<AccountDeviceInstance> accountDeviceInstances =
new ArrayList<AccountDeviceInstance>();
725 long account_id = rs.getLong(
"account_id");
726 String deviceID = rs.getString(
"device_id");
727 final String type_name = rs.getString(
"type_name");
728 final String account_unique_identifier = rs.getString(
"account_unique_identifier");
730 Account.
Type accountType = typeNameToAccountTypeMap.get(type_name);
731 Account account =
new Account(account_id, accountType, account_unique_identifier);
735 return accountDeviceInstances;
736 }
catch (SQLException ex) {
737 throw new TskCoreException(
"Error getting account device instances. " + ex.getMessage(), ex);
767 Set<Long> accountIDs =
new HashSet<Long>();
768 Set<String> accountDeviceIDs =
new HashSet<String>();
770 accountIDs.add(adi.getAccount().getAccountID());
771 accountDeviceIDs.add(
"'" + adi.getDeviceId() +
"'");
774 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
780 String accountIDsCSL = StringUtils.buildCSVString(accountIDs);
781 String accountDeviceIDsCSL = StringUtils.buildCSVString(accountDeviceIDs);
782 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
784 final String queryString
785 =
" SELECT count(DISTINCT relationships.relationship_source_obj_id) AS count,"
786 +
" data_source_info.device_id AS device_id,"
788 +
" accounts1.account_id AS account1_id,"
789 +
" accounts1.account_unique_identifier AS account1_unique_identifier,"
790 +
" account_types1.type_name AS type_name1,"
791 +
" account_types1.display_name AS display_name1,"
793 +
" accounts2.account_id AS account2_id,"
794 +
" accounts2.account_unique_identifier AS account2_unique_identifier,"
795 +
" account_types2.type_name AS type_name2,"
796 +
" account_types2.display_name AS display_name2"
797 +
" FROM account_relationships AS relationships"
798 +
" JOIN data_source_info AS data_source_info"
799 +
" ON relationships.data_source_obj_id = data_source_info.obj_id "
801 +
" JOIN accounts AS accounts1 "
802 +
" ON accounts1.account_id = relationships.account1_id"
803 +
" JOIN account_types AS account_types1"
804 +
" ON accounts1.account_type_id = account_types1.account_type_id"
806 +
" JOIN accounts AS accounts2 "
807 +
" ON accounts2.account_id = relationships.account2_id"
808 +
" JOIN account_types AS account_types2"
809 +
" ON accounts2.account_type_id = account_types2.account_type_id"
810 +
" WHERE (( relationships.account1_id IN (" + accountIDsCSL +
")) "
811 +
" AND ( relationships.account2_id IN ( " + accountIDsCSL +
" ))"
812 +
" AND ( data_source_info.device_id IN (" + accountDeviceIDsCSL +
"))) "
813 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL)
814 +
" GROUP BY data_source_info.device_id, "
815 +
" accounts1.account_id, "
816 +
" account_types1.type_name, "
817 +
" account_types1.display_name, "
818 +
" accounts2.account_id, "
819 +
" account_types2.type_name, "
820 +
" account_types2.display_name";
821 CaseDbConnection connection = db.getConnection();
826 Map<AccountPair, Long> results =
new HashMap<AccountPair, Long>();
829 s = connection.createStatement();
830 rs = connection.executeQuery(s, queryString);
836 rs.getString(
"account1_unique_identifier")),
837 rs.getString(
"device_id"));
842 rs.getString(
"account2_unique_identifier")),
843 rs.getString(
"device_id"));
846 long count = rs.getLong(
"count");
849 Long oldCount = results.get(relationshipKey);
850 if (oldCount != null) {
853 results.put(relationshipKey, count);
856 }
catch (SQLException ex) {
857 throw new TskCoreException(
"Error getting relationships between accounts. " + ex.getMessage(), ex);
883 long account_id = accountDeviceInstance.getAccount().getAccountID();
886 String datasourceObjIdsCSV = StringUtils.buildCSVString(
887 db.getDataSourceObjIds(accountDeviceInstance.getDeviceId()));
890 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
894 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
896 CaseDbConnection connection = db.getConnection();
902 s = connection.createStatement();
904 String innerQuery =
" account_relationships AS relationships";
905 String limitStr = getMostRecentFilterLimitSQL(filter);
907 if (!limitStr.isEmpty()) {
908 innerQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
912 =
"SELECT count(DISTINCT relationships.relationship_source_obj_id) as count "
913 +
" FROM" + innerQuery
914 +
" WHERE relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV +
" )"
915 +
" AND ( relationships.account1_id = " + account_id
916 +
" OR relationships.account2_id = " + account_id +
" )"
917 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL);
919 rs = connection.executeQuery(s, queryStr);
921 return (rs.getLong(
"count"));
922 }
catch (SQLException ex) {
923 throw new TskCoreException(
"Error getting relationships count for account device instance. " + ex.getMessage(), ex);
950 if (accountDeviceInstanceList.isEmpty()) {
952 return Collections.emptySet();
955 Map<Long, Set<Long>> accountIdToDatasourceObjIdMap =
new HashMap<Long, Set<Long>>();
957 long accountID = accountDeviceInstance.getAccount().getAccountID();
958 List<Long> dataSourceObjIds = db.getDataSourceObjIds(accountDeviceInstance.getDeviceId());
960 if (accountIdToDatasourceObjIdMap.containsKey(accountID)) {
961 accountIdToDatasourceObjIdMap.get(accountID).addAll(dataSourceObjIds);
963 accountIdToDatasourceObjIdMap.put(accountID,
new HashSet<Long>(dataSourceObjIds));
967 List<String> adiSQLClauses =
new ArrayList<String>();
968 for (Map.Entry<Long, Set<Long>> entry : accountIdToDatasourceObjIdMap.entrySet()) {
969 final Long accountID = entry.getKey();
970 String datasourceObjIdsCSV = StringUtils.buildCSVString(entry.getValue());
973 "( ( relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV +
" ) )"
974 +
" AND ( relationships.account1_id = " + accountID
975 +
" OR relationships.account2_id = " + accountID +
" ) )"
978 String adiSQLClause = StringUtils.joinAsStrings(adiSQLClauses,
" OR ");
981 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
987 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
989 String limitQuery =
" account_relationships AS relationships";
990 String limitStr = getMostRecentFilterLimitSQL(filter);
991 if (!limitStr.isEmpty()) {
992 limitQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
995 CaseDbConnection connection = db.getConnection();
1001 s = connection.createStatement();
1003 =
"SELECT DISTINCT artifacts.artifact_id AS artifact_id,"
1004 +
" artifacts.obj_id AS obj_id,"
1005 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
1006 +
" artifacts.data_source_obj_id AS data_source_obj_id, "
1007 +
" artifacts.artifact_type_id AS artifact_type_id, "
1008 +
" artifacts.review_status_id AS review_status_id "
1009 +
" FROM blackboard_artifacts as artifacts"
1010 +
" JOIN " + limitQuery
1011 +
" ON artifacts.artifact_obj_id = relationships.relationship_source_obj_id"
1013 +
" WHERE (" + adiSQLClause +
" )"
1015 + (filterSQL.isEmpty() ?
"" :
" AND (" + filterSQL +
" )");
1017 rs = connection.executeQuery(s, queryStr);
1018 Set<Content> relationshipSources =
new HashSet<Content>();
1022 rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"),
1023 rs.getLong(
"data_source_obj_id"), bbartType.getTypeID(),
1024 bbartType.getTypeName(), bbartType.getDisplayName(),
1028 return relationshipSources;
1029 }
catch (SQLException ex) {
1030 throw new TskCoreException(
"Error getting relationships for account. " + ex.getMessage(), ex);
1055 final List<Long> dataSourceObjIds
1056 = getSleuthkitCase().getDataSourceObjIds(accountDeviceInstance.getDeviceId());
1059 Set<String> applicableInnerQueryFilters =
new HashSet<String>(Arrays.asList(
1065 String innerQueryfilterSQL = getCommunicationsFilterSQL(filter, applicableInnerQueryFilters);
1067 String innerQueryTemplate
1068 =
" SELECT %1$1s as account_id,"
1069 +
" data_source_obj_id"
1070 +
" FROM account_relationships as relationships"
1071 +
" WHERE %2$1s = " + accountDeviceInstance.getAccount().getAccountID() +
""
1072 +
" AND data_source_obj_id IN (" + StringUtils.buildCSVString(dataSourceObjIds) +
")"
1073 + (innerQueryfilterSQL.isEmpty() ?
"" :
" AND " + innerQueryfilterSQL);
1075 String innerQuery1 = String.format(innerQueryTemplate,
"account1_id",
"account2_id");
1076 String innerQuery2 = String.format(innerQueryTemplate,
"account2_id",
"account1_id");
1079 String combinedInnerQuery
1080 =
"SELECT account_id, data_source_obj_id "
1081 +
" FROM ( " + innerQuery1 +
" UNION " + innerQuery2 +
" ) AS inner_union"
1082 +
" GROUP BY account_id, data_source_obj_id";
1085 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
1089 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
1093 " accounts.account_id AS account_id,"
1094 +
" accounts.account_unique_identifier AS account_unique_identifier,"
1096 +
" account_types.type_name AS type_name,"
1098 +
" data_source_info.device_id AS device_id"
1099 +
" FROM ( " + combinedInnerQuery +
" ) AS account_device_instances"
1100 +
" JOIN accounts AS accounts"
1101 +
" ON accounts.account_id = account_device_instances.account_id"
1102 +
" JOIN account_types AS account_types"
1103 +
" ON accounts.account_type_id = account_types.account_type_id"
1104 +
" JOIN data_source_info AS data_source_info"
1105 +
" ON account_device_instances.data_source_obj_id = data_source_info.obj_id"
1106 + (filterSQL.isEmpty() ?
"" :
" WHERE " + filterSQL);
1110 queryStr =
"SELECT DISTINCT ON ( accounts.account_id, data_source_info.device_id) " + queryStr;
1113 queryStr =
"SELECT " + queryStr +
" GROUP BY accounts.account_id, data_source_info.device_id";
1116 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
1119 CaseDbConnection connection = db.getConnection();
1122 ResultSet rs = null;
1125 s = connection.createStatement();
1127 rs = connection.executeQuery(s, queryStr);
1128 ArrayList<AccountDeviceInstance> accountDeviceInstances =
new ArrayList<AccountDeviceInstance>();
1130 long account_id = rs.getLong(
"account_id");
1131 String deviceID = rs.getString(
"device_id");
1132 final String type_name = rs.getString(
"type_name");
1133 final String account_unique_identifier = rs.getString(
"account_unique_identifier");
1135 Account.
Type accountType = typeNameToAccountTypeMap.get(type_name);
1136 Account account =
new Account(account_id, accountType, account_unique_identifier);
1140 return accountDeviceInstances;
1141 }
catch (SQLException ex) {
1142 throw new TskCoreException(
"Error getting account device instances. " + ex.getMessage(), ex);
1170 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
1176 String limitQuery =
" account_relationships AS relationships";
1177 String limitStr = getMostRecentFilterLimitSQL(filter);
1178 if (!limitStr.isEmpty()) {
1179 limitQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
1182 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
1183 final String queryString =
"SELECT artifacts.artifact_id AS artifact_id,"
1184 +
" artifacts.obj_id AS obj_id,"
1185 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
1186 +
" artifacts.data_source_obj_id AS data_source_obj_id,"
1187 +
" artifacts.artifact_type_id AS artifact_type_id,"
1188 +
" artifacts.review_status_id AS review_status_id"
1189 +
" FROM blackboard_artifacts AS artifacts"
1190 +
" JOIN " + limitQuery
1191 +
" ON artifacts.artifact_obj_id = relationships.relationship_source_obj_id"
1192 +
" WHERE (( relationships.account1_id = " + account1.getAccount().getAccountID()
1193 +
" AND relationships.account2_id = " + account2.getAccount().getAccountID()
1194 +
" ) OR ( relationships.account2_id = " + account1.getAccount().getAccountID()
1195 +
" AND relationships.account1_id =" + account2.getAccount().getAccountID() +
" ))"
1196 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL);
1197 CaseDbConnection connection = db.getConnection();
1200 ResultSet rs = null;
1202 s = connection.createStatement();
1203 rs = connection.executeQuery(s, queryString);
1205 ArrayList<Content> artifacts =
new ArrayList<Content>();
1208 artifacts.add(
new BlackboardArtifact(db, rs.getLong(
"artifact_id"), rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"), rs.getLong(
"data_source_obj_id"),
1209 bbartType.getTypeID(), bbartType.getTypeName(), bbartType.getDisplayName(),
1214 }
catch (SQLException ex) {
1215 throw new TskCoreException(
"Error getting relationships between accounts. " + ex.getMessage(), ex);
1235 List<AccountFileInstance> accountFileInstanceList =
new ArrayList<>();
1239 if (artifactList != null && !artifactList.isEmpty()) {
1245 if (!accountFileInstanceList.isEmpty()) {
1246 return accountFileInstanceList;
1261 CaseDbConnection connection = db.getConnection();
1264 ResultSet rs = null;
1265 List<
Account.
Type> inUseAccounts =
new ArrayList<>();
1268 String query =
"SELECT DISTINCT accounts.account_type_id, type_name, display_name FROM accounts JOIN account_types ON accounts.account_type_id = account_types.account_type_id";
1269 s = connection.createStatement();
1270 rs = connection.executeQuery(s, query);
1273 String accountTypeName = rs.getString(
"type_name");
1274 accountType = this.typeNameToAccountTypeMap.get(accountTypeName);
1276 if (accountType == null) {
1277 accountType =
new Account.
Type(accountTypeName, rs.getString(
"display_name"));
1278 this.accountTypeToTypeIdMap.put(accountType, rs.getInt(
"account_type_id"));
1281 inUseAccounts.add(accountType);
1283 return inUseAccounts;
1284 }
catch (SQLException ex) {
1285 throw new TskCoreException(
"Error getting account type id", ex);
1302 if (accountTypeToTypeIdMap.containsKey(accountType)) {
1303 return accountTypeToTypeIdMap.get(accountType);
1318 private String normalizeAccountID(Account.Type accountType, String accountUniqueID)
throws TskCoreException {
1319 String normailzeAccountID = accountUniqueID;
1321 if (accountType.equals(Account.Type.PHONE)) {
1322 normailzeAccountID = CommunicationsUtils.normalizePhoneNum(accountUniqueID);
1323 }
else if (accountType.equals(Account.Type.EMAIL)) {
1324 normailzeAccountID = CommunicationsUtils.normalizeEmailAddress(accountUniqueID);
1327 return normailzeAccountID;
1344 private String getCommunicationsFilterSQL(CommunicationsFilter commFilter, Set<String> applicableFilters) {
1345 if (null == commFilter || commFilter.getAndFilters().isEmpty()) {
1350 StringBuilder sqlSB =
new StringBuilder();
1351 boolean first =
true;
1352 for (CommunicationsFilter.SubFilter subFilter : commFilter.getAndFilters()) {
1355 if (applicableFilters.contains(subFilter.getClass().getName())) {
1356 String subfilterSQL = subFilter.getSQL(
this);
1357 if (!subfilterSQL.isEmpty()) {
1361 sqlSB.append(
" AND ");
1364 sqlSB.append(subfilterSQL);
1370 if (!sqlSB.toString().isEmpty()) {
1371 sqlStr =
"( " + sqlSB.toString() +
" )";
1384 private String getMostRecentFilterLimitSQL(CommunicationsFilter filter) {
1385 String limitStr =
"";
1387 if (filter != null && !filter.getAndFilters().isEmpty()) {
1389 for (CommunicationsFilter.SubFilter subFilter : filter.getAndFilters()) {
1390 if (subFilter.getClass().getName().equals(CommunicationsFilter.MostRecentFilter.class.getName())) {
1391 limitStr = subFilter.getSQL(
this);
Set< Content > getRelationshipSources(Set< AccountDeviceInstance > accountDeviceInstanceList, CommunicationsFilter filter)
void postArtifact(BlackboardArtifact artifact, String moduleName)
ArrayList< BlackboardArtifact > getBlackboardArtifacts(int artifactTypeID)
Blackboard getBlackboard()
void addAttributes(Collection< BlackboardAttribute > attributes)
List< AccountDeviceInstance > getRelatedAccountDeviceInstances(AccountDeviceInstance accountDeviceInstance, CommunicationsFilter filter)
org.sleuthkit.datamodel.Account.Type getAccountType(String accountTypeName)
AccountFileInstance createAccountFileInstance(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID, String moduleName, Content sourceFile)
org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeName, String displayName)
String getTypeSpecificID()
Map< AccountPair, Long > getRelationshipCountsPairwise(Set< AccountDeviceInstance > accounts, CommunicationsFilter filter)
void releaseSingleUserCaseReadLock()
List< Account.Type > getAccountTypesInUse()
BlackboardArtifact newBlackboardArtifact(int artifactTypeID, long obj_id)
static final List< Account.Type > PREDEFINED_ACCOUNT_TYPES
void acquireSingleUserCaseWriteLock()
BlackboardArtifact.Type getArtifactType(String artTypeName)
void releaseSingleUserCaseWriteLock()
List< Content > getRelationshipSources(AccountDeviceInstance account1, AccountDeviceInstance account2, CommunicationsFilter filter)
long getRelationshipSourcesCount(AccountDeviceInstance accountDeviceInstance, CommunicationsFilter filter)
void acquireSingleUserCaseReadLock()
List< AccountDeviceInstance > getAccountDeviceInstancesWithRelationships(CommunicationsFilter filter)
static ReviewStatus withID(int id)
void addRelationships(AccountFileInstance sender, List< AccountFileInstance > recipients, BlackboardArtifact sourceArtifact, org.sleuthkit.datamodel.Relationship.Type relationshipType, long dateTime)
Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID)
List< AccountFileInstance > getAccountFileInstances(Account account)