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.Objects;
28 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();
139 String insertClause =
" INTO persona_accounts (persona_id, account_id, justification, confidence_id, date_added, examiner_id ) "
141 + persona.
getId() +
", "
142 + account.
getId() +
", "
143 +
"'" + ((StringUtils.isBlank(justification) ?
"" : SleuthkitCase.escapeSingleQuotes(justification))) +
"', "
145 + timeStampMillis.toString() +
", "
146 + currentExaminer.
getId()
152 +
"WHERE persona_id = " + persona.
getId()
154 +
" AND account_unique_identifier = '" + account.
getIdentifier() +
"'";
155 PersonaAccountsQueryCallback queryCallback =
new PersonaAccountsQueryCallback();
158 Collection<PersonaAccount> accounts = queryCallback.getPersonaAccountsList();
159 if (accounts.size() != 1) {
160 throw new CentralRepoException(
"Account add query failed");
163 return accounts.iterator().next();
171 Collection<PersonaAccount> personaAccountsList =
new ArrayList<>();
174 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
179 rs.getInt(
"pa_examiner_id"),
180 rs.getString(
"pa_examiner_login_name"));
184 rs.getInt(
"persona_examiner_id"),
185 rs.getString(
"persona_examiner_login_name"));
190 rs.getInt(
"persona_id"),
191 rs.getString(
"uuid"),
192 rs.getString(
"name"),
193 rs.getString(
"comment"),
194 Long.parseLong(rs.getString(
"created_date")),
195 Long.parseLong(rs.getString(
"modified_date")),
203 rs.getInt(
"account_id"),
205 rs.getString(
"account_unique_identifier"));
209 rs.getString(
"justification"),
211 Long.parseLong(rs.getString(
"date_added")),
214 personaAccountsList.add(personaAccount);
218 Collection<PersonaAccount> getPersonaAccountsList() {
219 return Collections.unmodifiableCollection(personaAccountsList);
224 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,"
225 +
" personas.id as persona_id, personas.uuid, personas.name, personas.comment, personas.created_date, personas.modified_date, personas.status_id, "
226 +
" 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, "
227 +
" accounts.id as account_id, account_type_id, account_unique_identifier,"
228 +
" account_types.type_name as type_name "
229 +
" FROM persona_accounts as persona_accounts "
230 +
" JOIN personas as personas on persona_accounts.persona_id = personas.id "
231 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
232 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
233 +
" JOIN examiners as pa_examiner ON pa_examiner.id = persona_accounts.examiner_id "
234 +
" JOIN examiners as persona_examiner ON persona_examiner.id = personas.examiner_id ";
246 static Collection<PersonaAccount> getPersonaAccountsForPersona(
long personaId)
throws CentralRepoException {
247 String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
248 +
" WHERE persona_accounts.persona_id = " + personaId;
253 return queryCallback.getPersonaAccountsList();
267 String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
268 +
" WHERE persona_accounts.account_id = " + accountId
273 return queryCallback.getPersonaAccountsList();
289 String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
290 +
" WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + accountIdentifierSubstring +
"%')"
295 return queryCallback.getPersonaAccountsList();
310 String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
311 +
" WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + account.getTypeSpecificID() +
"%')"
312 +
" AND type_name = '" + account.
getAccountType().getTypeName() +
"' "
317 return queryCallback.getPersonaAccountsList();
328 static void removePersonaAccount(
long id)
throws CentralRepoException {
329 String deleteClause =
" DELETE FROM persona_accounts WHERE id = " +
id;
341 static void modifyPersonaAccount(
long id, Persona.Confidence
confidence, String justification)
throws CentralRepoException {
342 String updateClause =
"UPDATE persona_accounts SET confidence_id = " +
confidence.getLevelId() +
", justification = '" + justification +
"' WHERE id = " +
id;
352 Collection<CentralRepoAccount> accountsList =
new ArrayList<>();
355 public void process(ResultSet rs)
throws CentralRepoException, SQLException {
362 rs.getInt(
"account_id"),
364 rs.getString(
"account_unique_identifier"));
366 accountsList.add(account);
370 Collection<CentralRepoAccount> getAccountsList() {
371 return Collections.unmodifiableCollection(accountsList);
386 static Collection<CentralRepoAccount> getAccountsForPersona(
long personaId)
throws CentralRepoException {
387 String queryClause =
"SELECT account_id, "
388 +
" accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
389 +
" account_types.type_name as type_name "
390 +
" FROM persona_accounts "
391 +
" JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
392 +
" JOIN account_types as account_types on accounts.account_type_id = account_types.id "
393 +
" WHERE persona_accounts.persona_id = " + personaId;
395 AccountsForPersonaQueryCallback queryCallback =
new AccountsForPersonaQueryCallback();
398 return queryCallback.getAccountsList();
412 if (instance == null) {
413 throw new CentralRepoException(
"Failed to get instance of CentralRespository, CR was null");
final CentralRepoAccount account
void executeSelectSQL(String sql, CentralRepositoryDbQueryCallback queryCallback)
static CentralRepository getCRInstance()
static Collection< PersonaAccount > getPersonaAccountsForAccount(long accountId)
static Collection< PersonaAccount > getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring)
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)
void executeUpdateSQL(String sql)
static Collection< PersonaAccount > getPersonaAccountsForAccount(Account account)
void executeDeleteSQL(String sql)
final CentralRepoExaminer examiner
boolean equals(Object obj)
static PersonaStatus fromId(int value)
static Confidence fromId(int value)
final Persona.Confidence confidence
void executeInsertSQL(String sql)
CentralRepoAccountType getAccountType()
CentralRepoAccount getAccount()
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()