19 package org.sleuthkit.autopsy.centralrepository.datamodel;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.Optional;
30 import org.apache.commons.lang3.StringUtils;
43 private final long id;
92 hash = 83 * hash + Objects.hashCode(this.persona);
93 hash = 83 * hash + Objects.hashCode(this.account);
94 hash = 83 * hash + (int) (this.dateAdded ^ (this.dateAdded >>> 32));
95 hash = 83 * hash + Objects.hashCode(this.examiner);
107 if (getClass() != obj.getClass()) {
114 if (!Objects.equals(
this.persona, other.
getPersona())) {
117 if (!Objects.equals(
this.account, other.
getAccount())) {
139 Instant instant = Instant.now();
140 Long timeStampMillis = instant.toEpochMilli();
142 String insertSQL =
"INSERT INTO persona_accounts (persona_id, account_id, justification, confidence_id, date_added, examiner_id ) "
143 +
" VALUES ( ?, ?, ?, ?, ?, ?)";
145 List<Object> params =
new ArrayList<>();
146 params.add(persona.
getId());
147 params.add(account.
getId());
148 params.add(StringUtils.isBlank(justification) ?
"" :
justification);
150 params.add(timeStampMillis);
151 params.add(currentExaminer.
getId());
156 +
"WHERE persona_id = ? "
157 +
" AND account_type_id = ?"
158 +
" AND account_unique_identifier = ?";
160 List<Object> queryParams =
new ArrayList<>();
161 queryParams.add(persona.
getId());
165 PersonaAccountsQueryCallback queryCallback =
new PersonaAccountsQueryCallback();
168 Collection<PersonaAccount> accounts = queryCallback.getPersonaAccountsList();
169 if (accounts.size() != 1) {
170 throw new CentralRepoException(
"Account add query failed");
173 return accounts.iterator().next();
181 Collection<PersonaAccount> personaAccountsList =
new ArrayList<>();
184 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
189 rs.getInt(
"pa_examiner_id"),
190 rs.getString(
"pa_examiner_login_name"));
194 rs.getInt(
"persona_examiner_id"),
195 rs.getString(
"persona_examiner_login_name"));
200 rs.getInt(
"persona_id"),
201 rs.getString(
"uuid"),
202 rs.getString(
"name"),
203 rs.getString(
"comment"),
204 Long.parseLong(rs.getString(
"created_date")),
205 Long.parseLong(rs.getString(
"modified_date")),
211 String accountTypeName = rs.getString(
"type_name");
213 if (! optCrAccountType.isPresent()) {
215 throw new CentralRepoException(
"Account type with name '" + accountTypeName +
"' not found in Central Repository");
218 rs.getInt(
"account_id"),
219 optCrAccountType.get(),
220 rs.getString(
"account_unique_identifier"));
224 rs.getString(
"justification"),
226 Long.parseLong(rs.getString(
"date_added")),
229 personaAccountsList.add(personaAccount);
233 Collection<PersonaAccount> getPersonaAccountsList() {
234 return Collections.unmodifiableCollection(personaAccountsList);
239 private static final String
PERSONA_ACCOUNTS_QUERY_CLAUSE =
"SELECT persona_accounts.id as persona_accounts_id, justification, confidence_id, date_added, persona_accounts.examiner_id as pa_examiner_id, pa_examiner.login_name as pa_examiner_login_name, pa_examiner.display_name as pa_examiner_display_name,"
240 +
" personas.id as persona_id, personas.uuid, personas.name, personas.comment, personas.created_date, personas.modified_date, personas.status_id, "
241 +
" personas.examiner_id as persona_examiner_id, persona_examiner.login_name as persona_examiner_login_name, persona_examiner.display_name as persona_examiner_display_name, "
242 +
" accounts.id as account_id, account_type_id, account_unique_identifier,"
243 +
" account_types.type_name as type_name "
244 +
" FROM persona_accounts as persona_accounts "
245 +
" JOIN personas as personas on persona_accounts.persona_id = personas.id "
246 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
247 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
248 +
" JOIN examiners as pa_examiner ON pa_examiner.id = persona_accounts.examiner_id "
249 +
" JOIN examiners as persona_examiner ON persona_examiner.id = personas.examiner_id ";
261 static Collection<PersonaAccount> getPersonaAccountsForPersona(
long personaId)
throws CentralRepoException {
262 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
263 +
" WHERE persona_accounts.persona_id = ?";
265 List<Object> queryParams =
new ArrayList<>();
266 queryParams.add(personaId);
271 return queryCallback.getPersonaAccountsList();
285 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
286 +
" WHERE persona_accounts.account_id = ?"
287 +
" AND personas.status_id != ?";
289 List<Object> queryParams =
new ArrayList<>();
290 queryParams.add(accountId);
295 return queryCallback.getPersonaAccountsList();
311 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
312 +
" WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER(?)"
313 +
" AND personas.status_id != ?";
315 List<Object> queryParams =
new ArrayList<>();
316 queryParams.add(
"%" + accountIdentifierSubstring +
"%");
321 return queryCallback.getPersonaAccountsList();
335 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
336 +
" WHERE LOWER(accounts.account_unique_identifier) = LOWER(?)"
337 +
" AND type_name = ?"
338 +
" AND personas.status_id != ?";
340 List<Object> queryParams =
new ArrayList<>();
341 queryParams.add(account.getTypeSpecificID());
347 return queryCallback.getPersonaAccountsList();
358 static void removePersonaAccount(
long id)
throws CentralRepoException {
359 String deleteSQL =
" DELETE FROM persona_accounts WHERE id = ?";
360 List<Object> params =
new ArrayList<>();
374 static void modifyPersonaAccount(
long id, Persona.Confidence
confidence, String justification)
throws CentralRepoException {
375 String updateSQL =
"UPDATE persona_accounts SET confidence_id = ?, justification = ? WHERE id = ?";
377 List<Object> params =
new ArrayList<>();
379 params.add(StringUtils.isBlank(justification) ?
"" :
justification);
391 Collection<CentralRepoAccount> accountsList =
new ArrayList<>();
394 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
399 String accountTypeName = rs.getString(
"type_name");
401 if (! optCrAccountType.isPresent()) {
403 throw new CentralRepoException(
"Account type with name '" + accountTypeName +
"' not found in Central Repository");
406 rs.getInt(
"account_id"),
407 optCrAccountType.get(),
408 rs.getString(
"account_unique_identifier"));
410 accountsList.add(account);
414 Collection<CentralRepoAccount> getAccountsList() {
415 return Collections.unmodifiableCollection(accountsList);
430 static Collection<CentralRepoAccount> getAccountsForPersona(
long personaId)
throws CentralRepoException {
431 String queryClause =
"SELECT account_id, "
432 +
" accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
433 +
" account_types.type_name as type_name "
434 +
" FROM persona_accounts "
435 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
436 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
437 +
" WHERE persona_accounts.persona_id = ?";
439 List<Object> queryParams =
new ArrayList<>();
440 queryParams.add(personaId);
442 AccountsForPersonaQueryCallback queryCallback =
new AccountsForPersonaQueryCallback();
445 return queryCallback.getAccountsList();
459 if (instance == null) {
460 throw new CentralRepoException(
"Failed to get instance of CentralRespository, CR was null");
final CentralRepoAccount account
static CentralRepository getCRInstance()
static Collection< PersonaAccount > getPersonaAccountsForAccount(long accountId)
static Collection< PersonaAccount > getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
void process(ResultSet rs)
Persona.Confidence getConfidence()
final String justification
CentralRepoExaminer getOrInsertExaminer(String examinerLoginName)
String getJustification()
Optional< CentralRepoAccountType > getAccountTypeByName(String accountTypeName)
static final String PERSONA_ACCOUNTS_QUERY_CLAUSE
void process(ResultSet rs)
static Collection< PersonaAccount > getPersonaAccountsForAccount(Account account)
final CentralRepoExaminer examiner
boolean equals(Object obj)
static PersonaStatus fromId(int value)
static Confidence fromId(int value)
final Persona.Confidence confidence
CentralRepoAccountType getAccountType()
CentralRepoAccount getAccount()
void executeCommand(String sql, List< Object > params)
PersonaAccount(long id, Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)
static CentralRepository getInstance()
CentralRepoExaminer getExaminer()