19 package org.sleuthkit.autopsy.filequery;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import java.util.stream.Collectors;
35 private final FileGroup.GroupSortingAlgorithm groupSortingType;
36 private final FileSearch.AttributeType attrType;
37 private final FileSorter fileSorter;
39 private final Map<FileSearch.GroupKey, FileGroup> groupMap =
new HashMap<>();
40 private List<FileGroup> groupList =
new ArrayList<>();
42 private static final long MAX_OUTPUT_FILES = 2000;
53 SearchResults(FileGroup.GroupSortingAlgorithm groupSortingType, FileSearch.AttributeType attrType,
54 FileSorter.SortingMethod fileSortingMethod) {
55 this.groupSortingType = groupSortingType;
56 this.attrType = attrType;
57 this.fileSorter =
new FileSorter(fileSortingMethod);
65 this.groupSortingType = FileGroup.GroupSortingAlgorithm.BY_GROUP_NAME;
66 this.attrType =
new FileSearch.FileSizeAttribute();
67 this.fileSorter =
new FileSorter(FileSorter.SortingMethod.BY_FILE_NAME);
75 void add(List<ResultFile> files) {
76 for (ResultFile file : files) {
78 FileSearch.GroupKey groupKey = attrType.getGroupKey(file);
80 if (!groupMap.containsKey(groupKey)) {
81 groupMap.put(groupKey,
new FileGroup(groupSortingType, groupKey));
83 groupMap.get(groupKey).addFile(file);
91 void sortGroupsAndFiles() {
94 for (FileGroup group : groupMap.values()) {
95 group.sortFiles(fileSorter);
99 groupList =
new ArrayList<>(groupMap.values());
100 Collections.sort(groupList);
104 public String toString() {
106 if (groupList == null) {
111 for (FileGroup group : groupList) {
112 result += group.getDisplayName() +
"\n";
114 for (ResultFile file : group.getFiles()) {
115 result +=
" " + file.toString() +
"\n";
117 if (count > MAX_OUTPUT_FILES) {
118 result +=
"(truncated)";
131 List<String> getGroupNamesWithCounts() {
132 return groupList.stream().map(p -> p.getDisplayName() +
" (" + p.getFiles().size() +
")").collect(Collectors.toList());
142 List<ResultFile> getResultFilesInGroup(String groupName) {
143 if (groupName != null) {
144 final String modifiedGroupName = groupName.replaceAll(
" \\([0-9]+\\)$",
"");
146 java.util.Optional<FileGroup> fileGroup = groupList.stream().filter(p -> p.getDisplayName().equals(modifiedGroupName)).findFirst();
147 if (fileGroup.isPresent()) {
148 return fileGroup.get().getFiles();
151 return new ArrayList<>();
159 Map<GroupKey, List<ResultFile>> toLinkedHashMap() throws FileSearchException {
160 Map<GroupKey, List<ResultFile>> map =
new LinkedHashMap<>();
163 sortGroupsAndFiles();
166 for (FileGroup group : groupList) {
167 map.put(group.getGroupKey(), group.getFiles());