19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.Comparator;
24 import java.util.Optional;
25 import org.apache.commons.lang3.StringUtils;
37 class KeywordHit
implements Comparable<KeywordHit> {
39 private static final String GET_CONTENT_ID_FROM_ARTIFACT_ID =
"SELECT obj_id FROM blackboard_artifacts WHERE artifact_id = ";
41 private final long solrObjectId;
42 private final int chunkId;
43 private final String snippet;
44 private final String hit;
57 KeywordHit(String solrDocumentId, String snippet, String hit) {
58 this.snippet = StringUtils.stripToEmpty(snippet);
70 String[] split = solrDocumentId.split(Server.CHUNK_ID_SEPARATOR);
71 if (split.length == 1) {
73 this.solrObjectId = Long.parseLong(solrDocumentId);
76 this.solrObjectId = Long.parseLong(split[0]);
77 this.chunkId = Integer.parseInt(split[1]);
85 String getSolrDocumentId() {
86 return Long.toString(solrObjectId) + Server.CHUNK_ID_SEPARATOR + Long.toString(chunkId);
89 long getSolrObjectId() {
90 return this.solrObjectId;
97 boolean hasSnippet() {
98 return StringUtils.isNotBlank(this.snippet);
101 String getSnippet() {
116 long getContentID() throws TskCoreException {
117 if (isArtifactHit()) {
119 SleuthkitCase caseDb = Case.getCurrentCase().getSleuthkitCase();
120 try (SleuthkitCase.CaseDbQuery executeQuery =
121 caseDb.executeQuery(GET_CONTENT_ID_FROM_ARTIFACT_ID +
this.solrObjectId);
122 ResultSet resultSet = executeQuery.getResultSet();) {
123 if (resultSet.next()) {
124 return resultSet.getLong(
"obj_id");
126 throw new TskCoreException(
"Failed to get obj_id for artifact with artifact_id =" + this.solrObjectId +
". No matching artifact was found.");
128 }
catch (SQLException ex) {
129 throw new TskCoreException(
"Error getting obj_id for artifact with artifact_id =" + this.solrObjectId, ex);
133 return this.solrObjectId;
142 boolean isArtifactHit() {
144 return this.solrObjectId < 0;
152 Optional<Long> getArtifactID() {
153 if (isArtifactHit()) {
154 return Optional.of(solrObjectId);
156 return Optional.empty();
161 public boolean equals(Object obj) {
165 if (getClass() != obj.getClass()) {
168 final KeywordHit other = (KeywordHit) obj;
169 return this.compareTo(other) == 0;
173 public int hashCode() {
175 hash = 41 * hash + (int) this.solrObjectId + this.chunkId;
180 public int compareTo(KeywordHit o) {
181 return Comparator.comparing(KeywordHit::getSolrObjectId)
182 .thenComparing(KeywordHit::getChunkId)