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.Objects;
 
   25 import java.util.Optional;
 
   26 import org.apache.commons.lang3.StringUtils;
 
   40 class KeywordHit 
implements Comparable<KeywordHit> {
 
   42     private static final String GET_CONTENT_ID_FROM_ARTIFACT_ID = 
"SELECT obj_id FROM blackboard_artifacts WHERE artifact_id = ";
 
   44     private final long solrObjectId;
 
   45     private final int chunkId;
 
   46     private final String snippet;
 
   47     private final String hit;
 
   60     KeywordHit(String solrDocumentId, String snippet, String hit) {
 
   61         this.snippet = StringUtils.stripToEmpty(snippet);
 
   73         if(!solrDocumentId.isEmpty()) {
 
   74             String[] split = solrDocumentId.split(Server.CHUNK_ID_SEPARATOR);
 
   75             if (split.length == 1) {
 
   77                 this.solrObjectId = Long.parseLong(solrDocumentId);
 
   80                 this.solrObjectId = Long.parseLong(split[0]);
 
   81                 this.chunkId = Integer.parseInt(split[1]);
 
   84             this.solrObjectId = 0;
 
   89     KeywordHit(
int chunkId, 
long sourceID, String snippet, String hit) {
 
   90         this.snippet = StringUtils.stripToEmpty(snippet);
 
   92         this.chunkId = chunkId;
 
   93         this.solrObjectId = sourceID;
 
  101     String getSolrDocumentId() {
 
  102         return Long.toString(solrObjectId) + Server.CHUNK_ID_SEPARATOR + Long.toString(chunkId);
 
  105     Long getSolrObjectId() {
 
  106         return this.solrObjectId;
 
  109     Integer getChunkId() {
 
  113     boolean hasSnippet() {
 
  114         return StringUtils.isNotBlank(this.snippet);
 
  117     String getSnippet() {
 
  132     long getContentID() throws TskCoreException {
 
  133         if (isArtifactHit()) {
 
  135             SleuthkitCase caseDb;
 
  137                 caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
 
  138             } 
catch (NoCurrentCaseException ex) {
 
  139                 throw new TskCoreException(
"Exception while getting open case.", ex);
 
  141             try (SleuthkitCase.CaseDbQuery executeQuery =
 
  142                     caseDb.executeQuery(GET_CONTENT_ID_FROM_ARTIFACT_ID + 
this.solrObjectId);
 
  143                     ResultSet resultSet = executeQuery.getResultSet();) {
 
  144                 if (resultSet.next()) {
 
  145                     return resultSet.getLong(
"obj_id");
 
  147                     throw new TskCoreException(
"Failed to get obj_id for artifact with artifact_id =" + this.solrObjectId + 
".  No matching artifact was found.");
 
  149             } 
catch (SQLException ex) {
 
  150                 throw new TskCoreException(
"Error getting obj_id for artifact with artifact_id =" + this.solrObjectId, ex);
 
  154             return this.solrObjectId;
 
  163     boolean isArtifactHit() {
 
  165         return this.solrObjectId < 0;
 
  173     Optional<Long> getArtifactID() {
 
  174         if (isArtifactHit()) {
 
  175             return Optional.of(solrObjectId);
 
  177             return Optional.empty();
 
  182     public boolean equals(Object obj) {
 
  186         if (getClass() != obj.getClass()) {
 
  189         final KeywordHit other = (KeywordHit) obj;
 
  190         return compareTo(other) == 0;
 
  194     public int hashCode() {
 
  196         hash = 37 * hash + (int) (this.solrObjectId ^ (this.solrObjectId >>> 32));
 
  197         hash = 37 * hash + this.chunkId;
 
  198         hash = 37 * hash + Objects.hashCode(this.snippet);
 
  199         hash = 37 * hash + Objects.hashCode(this.hit);
 
  204     public int compareTo(KeywordHit other) {
 
  205         return Comparator.comparing(KeywordHit::getSolrObjectId)
 
  206                 .thenComparing(KeywordHit::getChunkId)
 
  207                 .thenComparing(KeywordHit::getHit)
 
  208                 .thenComparing(KeywordHit::getSnippet)
 
  209                 .compare(
this, other);