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 org.apache.commons.lang3.StringUtils;
41 private final long id;
90 hash = 83 * hash + Objects.hashCode(this.persona);
91 hash = 83 * hash + Objects.hashCode(this.account);
92 hash = 83 * hash + (int) (this.dateAdded ^ (this.dateAdded >>> 32));
93 hash = 83 * hash + Objects.hashCode(this.examiner);
105 if (getClass() != obj.getClass()) {
112 if (!Objects.equals(
this.persona, other.
getPersona())) {
115 if (!Objects.equals(
this.account, other.
getAccount())) {
137 Instant instant = Instant.now();
138 Long timeStampMillis = instant.toEpochMilli();
140 String insertSQL =
"INSERT INTO persona_accounts (persona_id, account_id, justification, confidence_id, date_added, examiner_id ) "
141 +
" VALUES ( ?, ?, ?, ?, ?, ?)";
143 List<Object> params =
new ArrayList<>();
144 params.add(persona.
getId());
145 params.add(account.
getId());
146 params.add(StringUtils.isBlank(justification) ?
"" :
justification);
148 params.add(timeStampMillis);
149 params.add(currentExaminer.
getId());
154 +
"WHERE persona_id = ? "
155 +
" AND account_type_id = ?"
156 +
" AND account_unique_identifier = ?";
158 List<Object> queryParams =
new ArrayList<>();
159 queryParams.add(persona.
getId());
163 PersonaAccountsQueryCallback queryCallback =
new PersonaAccountsQueryCallback();
166 Collection<PersonaAccount> accounts = queryCallback.getPersonaAccountsList();
167 if (accounts.size() != 1) {
168 throw new CentralRepoException(
"Account add query failed");
171 return accounts.iterator().next();
179 Collection<PersonaAccount> personaAccountsList =
new ArrayList<>();
182 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
187 rs.getInt(
"pa_examiner_id"),
188 rs.getString(
"pa_examiner_login_name"));
192 rs.getInt(
"persona_examiner_id"),
193 rs.getString(
"persona_examiner_login_name"));
198 rs.getInt(
"persona_id"),
199 rs.getString(
"uuid"),
200 rs.getString(
"name"),
201 rs.getString(
"comment"),
202 Long.parseLong(rs.getString(
"created_date")),
203 Long.parseLong(rs.getString(
"modified_date")),
211 rs.getInt(
"account_id"),
213 rs.getString(
"account_unique_identifier"));
217 rs.getString(
"justification"),
219 Long.parseLong(rs.getString(
"date_added")),
222 personaAccountsList.add(personaAccount);
226 Collection<PersonaAccount> getPersonaAccountsList() {
227 return Collections.unmodifiableCollection(personaAccountsList);
232 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,"
233 +
" personas.id as persona_id, personas.uuid, personas.name, personas.comment, personas.created_date, personas.modified_date, personas.status_id, "
234 +
" 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, "
235 +
" accounts.id as account_id, account_type_id, account_unique_identifier,"
236 +
" account_types.type_name as type_name "
237 +
" FROM persona_accounts as persona_accounts "
238 +
" JOIN personas as personas on persona_accounts.persona_id = personas.id "
239 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
240 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
241 +
" JOIN examiners as pa_examiner ON pa_examiner.id = persona_accounts.examiner_id "
242 +
" JOIN examiners as persona_examiner ON persona_examiner.id = personas.examiner_id ";
254 static Collection<PersonaAccount> getPersonaAccountsForPersona(
long personaId)
throws CentralRepoException {
255 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
256 +
" WHERE persona_accounts.persona_id = ?";
258 List<Object> queryParams =
new ArrayList<>();
259 queryParams.add(personaId);
264 return queryCallback.getPersonaAccountsList();
278 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
279 +
" WHERE persona_accounts.account_id = ?"
280 +
" AND personas.status_id != ?";
282 List<Object> queryParams =
new ArrayList<>();
283 queryParams.add(accountId);
288 return queryCallback.getPersonaAccountsList();
304 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
305 +
" WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER(?)"
306 +
" AND personas.status_id != ?";
308 List<Object> queryParams =
new ArrayList<>();
309 queryParams.add(
"%" + accountIdentifierSubstring +
"%");
314 return queryCallback.getPersonaAccountsList();
328 String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
329 +
" WHERE LOWER(accounts.account_unique_identifier) = LOWER(?)"
330 +
" AND type_name = ?"
331 +
" AND personas.status_id != ?";
333 List<Object> queryParams =
new ArrayList<>();
334 queryParams.add(account.getTypeSpecificID());
340 return queryCallback.getPersonaAccountsList();
351 static void removePersonaAccount(
long id)
throws CentralRepoException {
352 String deleteSQL =
" DELETE FROM persona_accounts WHERE id = ?";
353 List<Object> params =
new ArrayList<>();
367 static void modifyPersonaAccount(
long id, Persona.Confidence
confidence, String justification)
throws CentralRepoException {
368 String updateSQL =
"UPDATE persona_accounts SET confidence_id = ?, justification = ? WHERE id = ?";
370 List<Object> params =
new ArrayList<>();
372 params.add(StringUtils.isBlank(justification) ?
"" :
justification);
384 Collection<CentralRepoAccount> accountsList =
new ArrayList<>();
387 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
394 rs.getInt(
"account_id"),
396 rs.getString(
"account_unique_identifier"));
398 accountsList.add(account);
402 Collection<CentralRepoAccount> getAccountsList() {
403 return Collections.unmodifiableCollection(accountsList);
418 static Collection<CentralRepoAccount> getAccountsForPersona(
long personaId)
throws CentralRepoException {
419 String queryClause =
"SELECT account_id, "
420 +
" accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
421 +
" account_types.type_name as type_name "
422 +
" FROM persona_accounts "
423 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
424 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
425 +
" WHERE persona_accounts.persona_id = ?";
427 List<Object> queryParams =
new ArrayList<>();
428 queryParams.add(personaId);
430 AccountsForPersonaQueryCallback queryCallback =
new AccountsForPersonaQueryCallback();
433 return queryCallback.getAccountsList();
447 if (instance == null) {
448 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()
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)
CentralRepoAccountType getAccountTypeByName(String accountTypeName)
PersonaAccount(long id, Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)
static CentralRepository getInstance()
CentralRepoExaminer getExaminer()