Skip to content

Commit

Permalink
sepinf-inc#1631: reuse the code for query build in dockingControl pan…
Browse files Browse the repository at this point in the history
…els for selections
  • Loading branch information
aberenguel committed Oct 6, 2024
1 parent d13a1bc commit 356e99e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 460 deletions.
56 changes: 55 additions & 1 deletion iped-app/src/main/java/iped/app/ui/BaseTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@
import java.awt.event.MouseListener;

import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Query;

import iped.app.ui.bookmarks.BookmarkIcon;
import iped.engine.search.IPEDSearcher;
import iped.engine.search.LuceneSearchResult;
import iped.engine.search.MultiSearchResult;
import iped.engine.task.index.IndexItem;
import iped.engine.util.Util;
import iped.properties.BasicProps;
import iped.search.IMultiSearchResult;

public abstract class BaseTableModel extends AbstractTableModel implements MouseListener, ListSelectionListener, SearchResultTableModel {

private static final long serialVersionUID = 1L;

protected String sortResultsBy = BasicProps.NAME;
protected boolean cleanBeforeListItems;

protected LuceneSearchResult results = new LuceneSearchResult(0);
protected int selectedIndex = -1;
protected Document refDoc;
Expand Down Expand Up @@ -138,5 +145,52 @@ public final void valueChanged(ListSelectionEvent evt) {

public abstract void valueChanged(ListSelectionModel lsm);

public abstract void listItems(Document doc);
public abstract Query createQuery(Document doc);

protected abstract void onListItemsResultsComplete();

public final void listItems(Document doc) {

if (cleanBeforeListItems) {
results = new LuceneSearchResult(0);
fireTableDataChanged();
}

Query query = createQuery(doc);

if (query != null) {

try {
IPEDSearcher task = new IPEDSearcher(App.get().appCase, query, sortResultsBy);
task.setRewritequery(false);
results = MultiSearchResult.get(task.multiSearch(), App.get().appCase);

final int length = results.getLength();

if (length > 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
onListItemsResultsComplete();
}
});
refDoc = doc;
} else {
refDoc = null;
}

} catch (Exception e) {
results = new LuceneSearchResult(0);
refDoc = null;
e.printStackTrace();
}
if (cleanBeforeListItems) {
fireTableDataChanged();
}
}

if (!cleanBeforeListItems) {
fireTableDataChanged();
}
}
}
60 changes: 27 additions & 33 deletions iped-app/src/main/java/iped/app/ui/DuplicatesTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@
import java.awt.Rectangle;

import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

import iped.engine.search.IPEDSearcher;
import iped.engine.search.LuceneSearchResult;
import iped.engine.search.MultiSearchResult;
import iped.engine.task.index.IndexItem;
import iped.properties.BasicProps;
import iped.search.IIPEDSearcher;

public class DuplicatesTableModel extends BaseTableModel {

private static final long serialVersionUID = 1L;

public DuplicatesTableModel() {
sortResultsBy = BasicProps.PATH;
}

@Override
public Object getValueAt(int row, int col) {
if (col == 3) {
Expand All @@ -60,40 +67,27 @@ public void valueChanged(ListSelectionModel lsm) {
}

@Override
public void listItems(Document doc) {
public void onListItemsResultsComplete() {
App.get().duplicateDock.setTitleText(results.getLength() + Messages.getString("DuplicatesTableModel.Duplicates"));
}

@Override
public Query createQuery(Document doc) {
String hash = doc.get(IndexItem.HASH);
if (hash == null || hash.trim().isEmpty())
return;

String textQuery = IndexItem.HASH + ":" + hash;
if (StringUtils.isBlank(hash)) {
return null;
}

String id = doc.get(IndexItem.ID);
String sourceUUID = doc.get(IndexItem.EVIDENCE_UUID);

textQuery += " && NOT (" + IndexItem.ID + ":" + id;
textQuery += " && " + IndexItem.EVIDENCE_UUID + ":" + sourceUUID + ")";

try {
IIPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery, BasicProps.PATH);
results = MultiSearchResult.get(task.multiSearch(), App.get().appCase);

final int duplicates = results.getLength();

if (duplicates > 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
App.get().duplicateDock.setTitleText(duplicates + Messages.getString("DuplicatesTableModel.Duplicates"));
}
});
}

} catch (Exception e) {
results = new LuceneSearchResult(0);
e.printStackTrace();
}
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
queryBuilder.add(new TermQuery(new Term(IndexItem.HASH, hash.toLowerCase())), Occur.MUST);
queryBuilder.add(new BooleanClause(new BooleanQuery.Builder()
.add(IntPoint.newExactQuery(IndexItem.ID, Integer.parseInt(id)), Occur.MUST)
.add(new TermQuery(new Term(IndexItem.EVIDENCE_UUID, sourceUUID)), Occur.MUST)
.build(), Occur.MUST_NOT));

fireTableDataChanged();
return queryBuilder.build();
}
}
56 changes: 24 additions & 32 deletions iped-app/src/main/java/iped/app/ui/ParentTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@
import java.awt.Rectangle;

import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

import iped.properties.BasicProps;

import iped.engine.search.IPEDSearcher;
import iped.engine.search.LuceneSearchResult;
import iped.engine.search.MultiSearchResult;
import iped.engine.task.index.IndexItem;

public class ParentTableModel extends BaseTableModel {

private static final long serialVersionUID = 1L;

public ParentTableModel() {
}

@Override
public void valueChanged(ListSelectionModel lsm) {
App.get().getTextViewer().textTable.scrollRectToVisible(new Rectangle());
Expand All @@ -45,38 +51,24 @@ public void valueChanged(ListSelectionModel lsm) {
}

@Override
public void listItems(Document doc) {
public Query createQuery(Document doc) {

String textQuery = null;
String parentId = doc.get(IndexItem.PARENTID);
if (parentId != null) {
textQuery = IndexItem.ID + ":" + parentId;
String parentId = doc.get(BasicProps.PARENTID);
if (parentId == null) {
return null;
}

String sourceUUID = doc.get(IndexItem.EVIDENCE_UUID);
textQuery += " && " + IndexItem.EVIDENCE_UUID + ":" + sourceUUID;
String sourceUUID = doc.get(BasicProps.EVIDENCE_UUID);

results = new LuceneSearchResult(0);
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
queryBuilder.add(IntPoint.newExactQuery(BasicProps.ID, Integer.parseInt(parentId)), Occur.MUST);
queryBuilder.add(new TermQuery(new Term(BasicProps.EVIDENCE_UUID, sourceUUID)), Occur.MUST);

if (textQuery != null) {
try {
IPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery);
results = MultiSearchResult.get(task.multiSearch(), App.get().appCase);

} catch (Exception e) {
e.printStackTrace();
}
}

if (results.getLength() > 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
App.get().parentDock.setTitleText(Messages.getString("ParentTableModel.ParentCount"));
}
});
}
return queryBuilder.build();
}

fireTableDataChanged();
@Override
public void onListItemsResultsComplete() {
App.get().parentDock.setTitleText(Messages.getString("ParentTableModel.ParentCount"));
}
}
66 changes: 23 additions & 43 deletions iped-app/src/main/java/iped/app/ui/ReferencedByTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
package iped.app.ui;

import java.util.Arrays;
import java.util.Objects;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.util.BytesRef;

import iped.data.IItem;
import iped.engine.search.IPEDSearcher;
import iped.engine.search.LuceneSearchResult;
import iped.engine.search.MultiSearchResult;
import iped.engine.task.HashTask;
import iped.parsers.ares.AresParser;
import iped.parsers.emule.KnownMetParser;
Expand All @@ -43,6 +44,10 @@ public class ReferencedByTableModel extends BaseTableModel {

private static final long serialVersionUID = 1L;

public ReferencedByTableModel() {
cleanBeforeListItems = true;
}

@Override
public void valueChanged(ListSelectionModel lsm) {
int id = results.getLuceneIds()[selectedIndex];
Expand Down Expand Up @@ -70,51 +75,26 @@ public void valueChanged(ListSelectionModel lsm) {
}

@Override
public void listItems(Document doc) {
public Query createQuery(Document doc) {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();

// hashes
String md5 = doc.get(HashTask.HASH.MD5.toString());
String sha1 = doc.get(HashTask.HASH.SHA1.toString());
String sha256 = doc.get(HashTask.HASH.SHA256.toString());
String edonkey = doc.get(HashTask.HASH.EDONKEY.toString());
String ufedId = doc.get(ExtraProperties.UFED_META_PREFIX + "id");
if (StringUtils.isNotBlank(ufedId)) {
ufedId = "\"" + ufedId + "\"";
List<BytesRef> hashes = Arrays.asList(md5, sha1, sha256, edonkey).stream().filter(StringUtils::isNotBlank)
.map(h -> new BytesRef(h)).collect(Collectors.toList());
if (!hashes.isEmpty()) {
queryBuilder.add(new TermInSetQuery(ExtraProperties.LINKED_ITEMS, hashes), Occur.SHOULD);
queryBuilder.add(new TermInSetQuery(ExtraProperties.SHARED_HASHES, hashes), Occur.SHOULD);
}

String hashes = Arrays.asList(md5, sha1, sha256, edonkey, ufedId).stream().filter(Objects::nonNull).collect(Collectors.joining(" "));

if (hashes.isEmpty()) {
results = new LuceneSearchResult(0);
refDoc = null;
} else {
String textQuery = ExtraProperties.LINKED_ITEMS + ":(" + hashes + ") ";
textQuery += ExtraProperties.SHARED_HASHES + ":(" + hashes + ")";

try {
IPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery, BasicProps.NAME);
results = MultiSearchResult.get(task.multiSearch(), App.get().appCase);

final int length = results.getLength();

if (length > 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
App.get().referencedByDock.setTitleText(Messages.getString("ReferencedByTab.Title") + " " + length);
}
});
refDoc = doc;
} else {
refDoc = null;
}

} catch (Exception e) {
results = new LuceneSearchResult(0);
refDoc = null;
e.printStackTrace();
}
}
return queryBuilder.build();
}

fireTableDataChanged();
@Override
public void onListItemsResultsComplete() {
App.get().referencedByDock.setTitleText(Messages.getString("ReferencedByTab.Title") + " " + results.getLength());
}
}
Loading

0 comments on commit 356e99e

Please sign in to comment.