From 356e99e534e11def066dab5cbe289dfeac359ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Berenguel?= Date: Sat, 5 Oct 2024 22:01:11 -0300 Subject: [PATCH] #1631: reuse the code for query build in dockingControl panels for selections --- .../main/java/iped/app/ui/BaseTableModel.java | 56 +++- .../iped/app/ui/DuplicatesTableModel.java | 60 ++-- .../java/iped/app/ui/ParentTableModel.java | 56 ++-- .../iped/app/ui/ReferencedByTableModel.java | 66 ++-- .../iped/app/ui/ReferencingTableModel.java | 103 +++---- .../java/iped/app/ui/ResultTableListener.java | 290 ++---------------- .../java/iped/app/ui/SubitemTableModel.java | 51 ++- 7 files changed, 222 insertions(+), 460 deletions(-) diff --git a/iped-app/src/main/java/iped/app/ui/BaseTableModel.java b/iped-app/src/main/java/iped/app/ui/BaseTableModel.java index 84110a2fdd..e8ea6a71bf 100644 --- a/iped-app/src/main/java/iped/app/ui/BaseTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/BaseTableModel.java @@ -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; @@ -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(); + } + } } diff --git a/iped-app/src/main/java/iped/app/ui/DuplicatesTableModel.java b/iped-app/src/main/java/iped/app/ui/DuplicatesTableModel.java index 994ad1e75d..8fcf5de06b 100644 --- a/iped-app/src/main/java/iped/app/ui/DuplicatesTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/DuplicatesTableModel.java @@ -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) { @@ -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(); } } diff --git a/iped-app/src/main/java/iped/app/ui/ParentTableModel.java b/iped-app/src/main/java/iped/app/ui/ParentTableModel.java index cef3af6dc1..3f850be65b 100644 --- a/iped-app/src/main/java/iped/app/ui/ParentTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/ParentTableModel.java @@ -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()); @@ -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")); } } diff --git a/iped-app/src/main/java/iped/app/ui/ReferencedByTableModel.java b/iped-app/src/main/java/iped/app/ui/ReferencedByTableModel.java index bec56124a0..ff508fe2ff 100644 --- a/iped-app/src/main/java/iped/app/ui/ReferencedByTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/ReferencedByTableModel.java @@ -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; @@ -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]; @@ -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 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()); } } diff --git a/iped-app/src/main/java/iped/app/ui/ReferencingTableModel.java b/iped-app/src/main/java/iped/app/ui/ReferencingTableModel.java index 8ea2e52b58..06af391441 100644 --- a/iped-app/src/main/java/iped/app/ui/ReferencingTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/ReferencingTableModel.java @@ -18,20 +18,24 @@ */ package iped.app.ui; +import java.util.Arrays; +import java.util.Set; +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.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 org.apache.lucene.search.TermInSetQuery; +import org.apache.lucene.util.BytesRef; -import iped.engine.search.IPEDSearcher; -import iped.engine.search.LuceneSearchResult; -import iped.engine.search.MultiSearchResult; +import iped.engine.search.QueryBuilder; import iped.engine.task.HashTask; +import iped.exception.ParseException; +import iped.exception.QueryNodeException; import iped.parsers.ares.AresParser; import iped.parsers.emule.KnownMetParser; import iped.parsers.shareaza.ShareazaLibraryDatParser; @@ -42,6 +46,10 @@ public class ReferencingTableModel extends BaseTableModel { private static final long serialVersionUID = 1L; + public ReferencingTableModel() { + cleanBeforeListItems = true; + } + @Override public void valueChanged(ListSelectionModel lsm) { int id = results.getLuceneIds()[selectedIndex]; @@ -51,66 +59,47 @@ public void valueChanged(ListSelectionModel lsm) { } @Override - public void listItems(Document doc) { - - // clear table, searching for refs can take some time if they are thousands - results = new LuceneSearchResult(0); - fireTableDataChanged(); - - StringBuilder textQuery = null; - Query query = null; + public Query createQuery(Document doc) { + BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); + // linkedItems queries String[] linkedItems = doc.getValues(ExtraProperties.LINKED_ITEMS); - if (linkedItems != null && linkedItems.length > 0) { - textQuery = new StringBuilder(); + if (linkedItems.length > 0) { + QueryBuilder b = new QueryBuilder(App.get().appCase); for (String q : linkedItems) { - textQuery.append("(").append(q).append(") "); - } - } else { - linkedItems = doc.getValues(ExtraProperties.SHARED_HASHES); - if (linkedItems != null && linkedItems.length > 0) { - String term; - String mediaType = doc.get(BasicProps.CONTENTTYPE); - if (KnownMetParser.EMULE_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.EDONKEY.toString(); - } else if (AresParser.ARES_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.SHA1.toString(); - } else if (ShareazaLibraryDatParser.LIBRARY_DAT_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.MD5.toString(); - } else { - term = BasicProps.HASH; + try { + queryBuilder.add(b.getQuery(q), Occur.SHOULD); + } catch (ParseException | QueryNodeException e) { + e.printStackTrace(); } - BooleanQuery.Builder builder = new BooleanQuery.Builder(); - for (String hash : linkedItems) { - builder.add(new TermQuery(new Term(term, hash)), Occur.SHOULD); - } - query = builder.build(); } } - if (textQuery != null || query != null) { - try { - IPEDSearcher task = query != null ? new IPEDSearcher(App.get().appCase, query, BasicProps.NAME) : new IPEDSearcher(App.get().appCase, textQuery.toString(), BasicProps.NAME); - 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() { - App.get().referencesDock.setTitleText(Messages.getString("ReferencesTab.Title") + " " + length); - } - }); - } - - } catch (Exception e) { - results = new LuceneSearchResult(0); - e.printStackTrace(); + // sharedHashes + String[] sharedHashes = doc.getValues(ExtraProperties.SHARED_HASHES); + if (sharedHashes.length > 0) { + String field; + String mediaType = doc.get(BasicProps.CONTENTTYPE); + if (KnownMetParser.EMULE_MIME_TYPE.equals(mediaType)) { + field = HashTask.HASH.EDONKEY.toString(); + } else if (AresParser.ARES_MIME_TYPE.equals(mediaType)) { + field = HashTask.HASH.SHA1.toString(); + } else if (ShareazaLibraryDatParser.LIBRARY_DAT_MIME_TYPE.equals(mediaType)) { + field = HashTask.HASH.MD5.toString(); + } else { + field = BasicProps.HASH; } + + Set hashes = Arrays.asList(sharedHashes).stream().filter(StringUtils::isNotBlank) + .map(h -> new BytesRef(h)).collect(Collectors.toSet()); + queryBuilder.add(new TermInSetQuery(field, hashes), Occur.SHOULD); } - fireTableDataChanged(); + return queryBuilder.build(); + } + + @Override + public void onListItemsResultsComplete() { + App.get().referencesDock.setTitleText(Messages.getString("ReferencesTab.Title") + " " + results.getLength()); } } diff --git a/iped-app/src/main/java/iped/app/ui/ResultTableListener.java b/iped-app/src/main/java/iped/app/ui/ResultTableListener.java index e5ee5664bf..57c08398ae 100644 --- a/iped-app/src/main/java/iped/app/ui/ResultTableListener.java +++ b/iped-app/src/main/java/iped/app/ui/ResultTableListener.java @@ -30,12 +30,9 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.Collator; -import java.util.Arrays; import java.util.List; -import java.util.Objects; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import java.util.stream.Collectors; import javax.swing.JTable; import javax.swing.RowSorter.SortKey; @@ -43,13 +40,8 @@ import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import org.apache.commons.lang3.StringUtils; import org.apache.lucene.document.Document; -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 org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,15 +49,7 @@ import iped.data.IItem; import iped.data.IItemId; import iped.engine.search.IPEDSearcher; -import iped.engine.search.LuceneSearchResult; import iped.engine.search.MultiSearchResult; -import iped.engine.task.HashTask; -import iped.engine.task.index.IndexItem; -import iped.parsers.ares.AresParser; -import iped.parsers.emule.KnownMetParser; -import iped.parsers.shareaza.ShareazaLibraryDatParser; -import iped.properties.BasicProps; -import iped.properties.ExtraProperties; import iped.viewers.ATextViewer; import iped.viewers.components.HitsTableModel; import iped.viewers.util.ProgressDialog; @@ -327,8 +311,7 @@ public void itemSelection(boolean value) { App.get().subItemTable.repaint(); } - public void itemSelectionAndSubItems(boolean value) { - + public void itemSelectionAndResultsByQuery(boolean value, BaseTableModel tableModel) { ProgressDialog dialog = createProgressDialog(); executor.execute(() -> { try { @@ -338,118 +321,38 @@ public void itemSelectionAndSubItems(boolean value) { int[] selectedRows = App.get().resultsTable.getSelectedRows(); dialog.setMaximum(selectedRows.length); for (int i = 0; i < selectedRows.length; i++) { - if (i == selectedRows.length - 1) { - BookmarksController.get().setMultiSetting(false); - App.get().resultsTable.setUpdateSelectionOnSort(true); - } - App.get().resultsTable.setValueAt(value, selectedRows[i], col); - - int modelIndex = App.get().resultsTable.convertRowIndexToModel(selectedRows[i]); - selectAllSubitems(value, App.get().ipedResult.getItem(modelIndex)); - dialog.setProgress(i); - if (dialog.isCanceled()) { - return; - } - } - } finally { - SwingUtilities.invokeLater(() -> { - dialog.close(); - BookmarksController.get().updateUI(); - App.get().subItemTable.repaint(); - }); - } - }); - } - public void itemSelectionAndParent(boolean value) { - ProgressDialog dialog = createProgressDialog(); - executor.execute(() -> { - try { - int col = App.get().resultsTable.convertColumnIndexToView(1); - BookmarksController.get().setMultiSetting(true); - App.get().resultsTable.setUpdateSelectionOnSort(false); - int[] selectedRows = App.get().resultsTable.getSelectedRows(); - dialog.setMaximum(selectedRows.length); - for (int i = 0; i < selectedRows.length; i++) { + // item selection if (i == selectedRows.length - 1) { BookmarksController.get().setMultiSetting(false); App.get().resultsTable.setUpdateSelectionOnSort(true); } App.get().resultsTable.setValueAt(value, selectedRows[i], col); - int modelIndex = App.get().resultsTable.convertRowIndexToModel(selectedRows[i]); - selectReferencedParent(value, App.get().ipedResult.getItem(modelIndex)); - dialog.setProgress(i); - if (dialog.isCanceled()) { - return; - } - } - } finally { - SwingUtilities.invokeLater(() -> { - dialog.close(); - BookmarksController.get().updateUI(); - App.get().subItemTable.repaint(); - }); - } - }); - } - - public void itemSelectionAndReferences(boolean value) { - ProgressDialog dialog = createProgressDialog(); - executor.execute(() -> { - try { - int col = App.get().resultsTable.convertColumnIndexToView(1); - BookmarksController.get().setMultiSetting(true); - App.get().resultsTable.setUpdateSelectionOnSort(false); - int[] selectedRows = App.get().resultsTable.getSelectedRows(); - dialog.setMaximum(selectedRows.length); - for (int i = 0; i < selectedRows.length; i++) { - if (i == selectedRows.length - 1) { - BookmarksController.get().setMultiSetting(false); - App.get().resultsTable.setUpdateSelectionOnSort(true); - } - App.get().resultsTable.setValueAt(value, selectedRows[i], col); - - int modelIndex = App.get().resultsTable.convertRowIndexToModel(selectedRows[i]); - selectReferencetems(value, App.get().ipedResult.getItem(modelIndex)); - dialog.setProgress(i); - if (dialog.isCanceled()) { - return; - } - } - } finally { - SwingUtilities.invokeLater(() -> { - dialog.close(); - BookmarksController.get().updateUI(); - App.get().subItemTable.repaint(); - }); - } - }); - } - - public void itemSelectionAndReferencedBy(boolean value) { - ProgressDialog dialog = createProgressDialog(); - executor.execute(() -> { - try { - int col = App.get().resultsTable.convertColumnIndexToView(1); - BookmarksController.get().setMultiSetting(true); - App.get().resultsTable.setUpdateSelectionOnSort(false); - int[] selectedRows = App.get().resultsTable.getSelectedRows(); - dialog.setMaximum(selectedRows.length); - for (int i = 0; i < selectedRows.length; i++) { - if (i == selectedRows.length - 1) { - BookmarksController.get().setMultiSetting(false); - App.get().resultsTable.setUpdateSelectionOnSort(true); + // query results selection + int selectedIndex = App.get().resultsTable.convertRowIndexToModel(selectedRows[i]); + IItemId selectedItemId = App.get().ipedResult.getItem(selectedIndex); + final int selectedDocId = App.get().appCase.getLuceneId(selectedItemId); + Document selectedDoc = App.get().appCase.getSearcher().doc(selectedDocId); + Query query = tableModel.createQuery(selectedDoc); + if (query != null) { + IPEDSearcher task = new IPEDSearcher(App.get().appCase, query); + MultiSearchResult result = task.multiSearch(); + if (result.getLength() > 0) { + logger.debug("Found {} results of sourceId {} id {}", result.getLength(), selectedItemId.getSourceId(), selectedItemId.getId()); + for (IItemId subItem : result.getIterator()) { + App.get().appCase.getMultiBookmarks().setChecked((Boolean) value, subItem); + } + } } - App.get().resultsTable.setValueAt(value, selectedRows[i], col); - int modelIndex = App.get().resultsTable.convertRowIndexToModel(selectedRows[i]); - selectReferencedByItems(value, App.get().ipedResult.getItem(modelIndex)); dialog.setProgress(i); if (dialog.isCanceled()) { return; } } + } catch (Exception e) { + logger.error("Error selecting item and its query results", e); } finally { SwingUtilities.invokeLater(() -> { dialog.close(); @@ -460,159 +363,20 @@ public void itemSelectionAndReferencedBy(boolean value) { }); } - /** - * Perform selection of all subitems - * - * @param state - * - which state to set true or false - * @param rootID - * - parent of the selection - */ - private void selectAllSubitems(boolean state, IItemId rootID) { - try { - IItem item = App.get().appCase.getItemByItemId(rootID); - if (item.hasChildren() || item.isDir()) { // Filter subItems which have children or are directories. - logger.debug("Searching items with evidenceUUID {} id {}", item.getDataSource().getUUID(), item.getId()); - String query = BasicProps.EVIDENCE_UUID + ":" + item.getDataSource().getUUID() + " AND " + BasicProps.PARENTIDs + ":" + rootID.getId(); - IPEDSearcher task = new IPEDSearcher(App.get().appCase, query); - MultiSearchResult result = task.multiSearch(); - if (result.getLength() > 0) { - logger.debug("Found {} subitems of sourceId {} id {}", result.getLength(), rootID.getSourceId(), rootID.getId()); - for (IItemId subItem : result.getIterator()) { - App.get().appCase.getMultiBookmarks().setChecked((Boolean) state, subItem); - } - } - } - - } catch (Exception e) { - logger.debug("Error :" + e.getLocalizedMessage()); - e.printStackTrace(); - } + public void itemSelectionAndSubItems(boolean value) { + itemSelectionAndResultsByQuery(value, App.get().subItemModel); } - private void selectReferencedParent(boolean state, IItemId rootId) { - - try { - final int docId = App.get().appCase.getLuceneId(rootId); - Document doc = App.get().appCase.getSearcher().doc(docId); - - String parentId = doc.get(IndexItem.PARENTID); - if (parentId == null) { - return; - } - String sourceUUID = doc.get(IndexItem.EVIDENCE_UUID); - - String textQuery = IndexItem.ID + ":" + parentId + " && " + IndexItem.EVIDENCE_UUID + ":" + sourceUUID; - - IPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery, BasicProps.NAME); - LuceneSearchResult results = MultiSearchResult.get(task.multiSearch(), App.get().appCase); - - if (results.getLength() > 0) { - for (int resDocId : results.getLuceneIds()) { - App.get().appCase.getMultiBookmarks().setChecked((Boolean) state, App.get().appCase.getItemId(resDocId)); - } - } - - } catch (Exception e) { - logger.error("Error :" + e.getLocalizedMessage()); - e.printStackTrace(); - } + public void itemSelectionAndParent(boolean value) { + itemSelectionAndResultsByQuery(value, App.get().parentItemModel); } - private void selectReferencedByItems(boolean state, IItemId rootId) { - - try { - final int docId = App.get().appCase.getLuceneId(rootId); - Document doc = App.get().appCase.getSearcher().doc(docId); - - 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 + "\""; - } - String hashes = Arrays.asList(md5, sha1, sha256, edonkey, ufedId).stream().filter(Objects::nonNull).collect(Collectors.joining(" ")); - - if (hashes.isEmpty()) { - return; - } - - String textQuery = ExtraProperties.LINKED_ITEMS + ":(" + hashes + ") "; - textQuery += ExtraProperties.SHARED_HASHES + ":(" + hashes + ")"; - - IPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery, BasicProps.NAME); - LuceneSearchResult results = MultiSearchResult.get(task.multiSearch(), App.get().appCase); - - if (results.getLength() > 0) { - for (int resDocId : results.getLuceneIds()) { - App.get().appCase.getMultiBookmarks().setChecked((Boolean) state, - App.get().appCase.getItemId(resDocId)); - } - } - - } catch (Exception e) { - logger.error("Error :" + e.getLocalizedMessage()); - e.printStackTrace(); - } + public void itemSelectionAndReferences(boolean value) { + itemSelectionAndResultsByQuery(value, App.get().referencesModel); } - private void selectReferencetems(boolean state, IItemId rootId) { - try { - - final int docId = App.get().appCase.getLuceneId(rootId); - Document doc = App.get().appCase.getSearcher().doc(docId); - - StringBuilder textQuery = null; - Query query = null; - - String[] linkedItems = doc.getValues(ExtraProperties.LINKED_ITEMS); - if (linkedItems != null && linkedItems.length > 0) { - textQuery = new StringBuilder(); - for (String q : linkedItems) { - textQuery.append("(").append(q).append(") "); - } - } else { - linkedItems = doc.getValues(ExtraProperties.SHARED_HASHES); - if (linkedItems != null && linkedItems.length > 0) { - String term; - String mediaType = doc.get(BasicProps.CONTENTTYPE); - if (KnownMetParser.EMULE_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.EDONKEY.toString(); - } else if (AresParser.ARES_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.SHA1.toString(); - } else if (ShareazaLibraryDatParser.LIBRARY_DAT_MIME_TYPE.equals(mediaType)) { - term = HashTask.HASH.MD5.toString(); - } else { - term = BasicProps.HASH; - } - BooleanQuery.Builder builder = new BooleanQuery.Builder(); - for (String hash : linkedItems) { - builder.add(new TermQuery(new Term(term, hash)), Occur.SHOULD); - } - query = builder.build(); - } - } - - if (textQuery != null || query != null) { - - IPEDSearcher task = query != null ? new IPEDSearcher(App.get().appCase, query, BasicProps.NAME) - : new IPEDSearcher(App.get().appCase, textQuery.toString(), BasicProps.NAME); - task.setRewritequery(false); - LuceneSearchResult results = MultiSearchResult.get(task.multiSearch(), App.get().appCase); - - if (results.getLength() > 0) { - for (int resDocId : results.getLuceneIds()) { - App.get().appCase.getMultiBookmarks().setChecked((Boolean) state, - App.get().appCase.getItemId(resDocId)); - } - } - } - } catch (Exception e) { - logger.error("Error :" + e.getLocalizedMessage()); - e.printStackTrace(); - } + public void itemSelectionAndReferencedBy(boolean value) { + itemSelectionAndResultsByQuery(value, App.get().referencedByModel); } /** diff --git a/iped-app/src/main/java/iped/app/ui/SubitemTableModel.java b/iped-app/src/main/java/iped/app/ui/SubitemTableModel.java index 3f47efb7bc..47c63bb1a0 100644 --- a/iped-app/src/main/java/iped/app/ui/SubitemTableModel.java +++ b/iped-app/src/main/java/iped/app/ui/SubitemTableModel.java @@ -21,14 +21,15 @@ 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.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.utils.LocalizedFormat; @@ -36,6 +37,9 @@ public class SubitemTableModel extends BaseTableModel { private static final long serialVersionUID = 1L; + public SubitemTableModel() { + } + @Override public void valueChanged(ListSelectionModel lsm) { App.get().getTextViewer().textTable.scrollRectToVisible(new Rectangle()); @@ -47,35 +51,20 @@ public void valueChanged(ListSelectionModel lsm) { } @Override - public void listItems(Document doc) { - - String parentId = doc.get(IndexItem.ID); - - String textQuery = IndexItem.PARENTID + ":" + parentId; + public Query createQuery(Document doc) { - String sourceUUID = doc.get(IndexItem.EVIDENCE_UUID); - textQuery += " && " + IndexItem.EVIDENCE_UUID + ":" + sourceUUID; + String id = doc.get(BasicProps.ID); + String sourceUUID = doc.get(BasicProps.EVIDENCE_UUID); - try { - IPEDSearcher task = new IPEDSearcher(App.get().appCase, textQuery, BasicProps.NAME); - results = MultiSearchResult.get(task.multiSearch(), App.get().appCase); + BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); + queryBuilder.add(IntPoint.newExactQuery(BasicProps.PARENTID, Integer.parseInt(id)), Occur.MUST); + queryBuilder.add(new TermQuery(new Term(BasicProps.EVIDENCE_UUID, sourceUUID)), Occur.MUST); - final int sumSubitens = results.getLength(); - - if (sumSubitens > 0) { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - App.get().subitemDock.setTitleText(LocalizedFormat.format(sumSubitens) + Messages.getString("SubitemTableModel.Subitens")); - } - }); - } - - } catch (Exception e) { - results = new LuceneSearchResult(0); - e.printStackTrace(); - } + return queryBuilder.build(); + } - fireTableDataChanged(); + @Override + public void onListItemsResultsComplete() { + App.get().subitemDock.setTitleText(LocalizedFormat.format(results.getLength()) + Messages.getString("SubitemTableModel.Subitens")); } }