diff --git a/bindings/java/src/org/sleuthkit/datamodel/HostManager.java b/bindings/java/src/org/sleuthkit/datamodel/HostManager.java index 5d8136b50f44b8da6b4a53bdc962df893bb9b5b4..0a89578fb2fa5d4e49eb01e9d619017c59459c6a 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/HostManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/HostManager.java @@ -469,70 +469,6 @@ public Host getHost(DataSource dataSource) throws TskCoreException { db.releaseSingleUserCaseReadLock(); } } - - /** - * Get person for the given host or empty if no associated person. - * - * @param host The host. - * - * @return The parent person or empty if no parent person. - * - * @throws TskCoreException if error occurs. - */ - public Optional<Person> getPerson(Host host) throws TskCoreException { - - String queryString = "SELECT p.id AS personId, p.name AS name FROM \n" - + "tsk_persons p INNER JOIN tsk_hosts h\n" - + "ON p.id = h.person_id \n" - + "WHERE h.id = " + host.getHostId(); - - db.acquireSingleUserCaseReadLock(); - try (CaseDbConnection connection = this.db.getConnection(); - Statement s = connection.createStatement(); - ResultSet rs = connection.executeQuery(s, queryString)) { - - if (rs.next()) { - return Optional.of(new Person(rs.getLong("personId"), rs.getString("name"))); - } else { - return Optional.empty(); - } - } catch (SQLException ex) { - throw new TskCoreException(String.format("Error getting person for host with ID = %d", host.getHostId()), ex); - } finally { - db.releaseSingleUserCaseReadLock(); - } - } - - /** - * Set host's parent person. - * - * @param host The host whose parent will be set. - * @param person The person to be a parent or null to remove any parent - * person reference from this host. - * - * @throws TskCoreException - */ - public void setPerson(Host host, Person person) throws TskCoreException { - if (host == null) { - throw new TskCoreException("Illegal argument passed to setPerson: host must be non-null."); - } - - String queryString = (person == null) - ? String.format("UPDATE tsk_hosts SET person_id = NULL WHERE id = %d", host.getHostId()) - : String.format("UPDATE tsk_hosts SET person_id = %d WHERE id = %d", person.getPersonId(), host.getHostId()); - - db.acquireSingleUserCaseWriteLock(); - try (CaseDbConnection connection = this.db.getConnection(); - Statement s = connection.createStatement();) { - s.executeUpdate(queryString); - } catch (SQLException ex) { - throw new TskCoreException(String.format("Error getting persons"), ex); - } finally { - db.releaseSingleUserCaseWriteLock(); - } - - db.getPersonManager().fireChangeEvent(person); - } /** * Merge source host into destination host. diff --git a/bindings/java/src/org/sleuthkit/datamodel/PersonManager.java b/bindings/java/src/org/sleuthkit/datamodel/PersonManager.java index 1293ac2668a17052d69e39c4b39ef4bc27eefb1b..1956b4fdf2cf6e377b149151108ee08fb1fd48c3 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/PersonManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/PersonManager.java @@ -322,6 +322,70 @@ private Optional<Person> getPerson(String name, CaseDbConnection connection) thr throw new TskCoreException(String.format("Error getting person with name = %s", name), ex); } } + + /** + * Get person for the given host or empty if no associated person. + * + * @param host The host. + * + * @return The parent person or empty if no parent person. + * + * @throws TskCoreException if error occurs. + */ + public Optional<Person> getPerson(Host host) throws TskCoreException { + + String queryString = "SELECT p.id AS personId, p.name AS name FROM \n" + + "tsk_persons p INNER JOIN tsk_hosts h\n" + + "ON p.id = h.person_id \n" + + "WHERE h.id = " + host.getHostId(); + + db.acquireSingleUserCaseReadLock(); + try (CaseDbConnection connection = this.db.getConnection(); + Statement s = connection.createStatement(); + ResultSet rs = connection.executeQuery(s, queryString)) { + + if (rs.next()) { + return Optional.of(new Person(rs.getLong("personId"), rs.getString("name"))); + } else { + return Optional.empty(); + } + } catch (SQLException ex) { + throw new TskCoreException(String.format("Error getting person for host with ID = %d", host.getHostId()), ex); + } finally { + db.releaseSingleUserCaseReadLock(); + } + } + + /** + * Set host's parent person. + * + * @param host The host whose parent will be set. + * @param person The person to be a parent or null to remove any parent + * person reference from this host. + * + * @throws TskCoreException + */ + public void setPerson(Host host, Person person) throws TskCoreException { + if (host == null) { + throw new TskCoreException("Illegal argument passed to setPerson: host must be non-null."); + } + + String queryString = (person == null) + ? String.format("UPDATE tsk_hosts SET person_id = NULL WHERE id = %d", host.getHostId()) + : String.format("UPDATE tsk_hosts SET person_id = %d WHERE id = %d", person.getPersonId(), host.getHostId()); + + db.acquireSingleUserCaseWriteLock(); + try (CaseDbConnection connection = this.db.getConnection(); + Statement s = connection.createStatement();) { + s.executeUpdate(queryString); + } catch (SQLException ex) { + throw new TskCoreException(String.format("Error getting persons"), ex); + } finally { + db.releaseSingleUserCaseWriteLock(); + } + + db.getPersonManager().fireChangeEvent(person); + } /** * Fires an event when a person is created. diff --git a/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java b/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java index 7feb33197fd50042361966b0390ac8797ea40fa5..c81df651f9efbcad4ee554966a6098a2faadf152 100644 --- a/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java +++ b/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java @@ -800,8 +800,7 @@ public void osAccountInstanceTests() throws TskCoreException, OsAccountManager.N accountAttributes.add(attrib2); // add attributes to account. - osAccount1.addAttributes(accountAttributes); - + caseDB.getOsAccountManager().addOsAccountAttributes(osAccount1, accountAttributes); // now get the account with same sid, and get its attribuites and verify. Optional<OsAccount> existingAccount1 = caseDB.getOsAccountManager().getOsAccountByAddr(osAccount1.getAddr().get(), caseDB.getOsAccountRealmManager().getRealmById(osAccount1.getRealmId()));