19 package org.sleuthkit.autopsy.datasourcesummary.datamodel;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.DecimalFormat;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.SortedMap;
29 import java.util.TreeMap;
32 import org.apache.commons.lang.StringUtils;
37 import org.
sleuthkit.datamodel.TskData.TSK_DB_FILES_TYPE_ENUM;
38 import org.
sleuthkit.datamodel.TskData.TSK_FS_META_FLAG_ENUM;
39 import org.
sleuthkit.datamodel.TskData.TSK_FS_META_TYPE_ENUM;
48 public static final DecimalFormat
COMMA_FORMATTER =
new DecimalFormat(COMMA_FORMAT_STR);
62 static Long getCountOfTskFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
63 throws TskCoreException, SQLException {
64 if (currentDataSource != null) {
65 return skCase.countFilesWhere(
66 "data_source_obj_id=" + currentDataSource.getId()
67 + (StringUtils.isBlank(additionalWhere) ?
"" : (
" AND " + additionalWhere)));
84 static Long getCountOfRegularFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
85 throws TskCoreException, SQLException {
86 String whereClause =
"meta_type=" + TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_REG.getValue();
88 if (StringUtils.isNotBlank(additionalWhere)) {
89 whereClause +=
" AND " + additionalWhere;
92 return getCountOfTskFiles(skCase, currentDataSource, whereClause);
108 throws TskCoreException, SQLException {
109 String whereClause =
"meta_type=" + TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_REG.getValue()
110 +
" AND type<>" + TSK_DB_FILES_TYPE_ENUM.SLACK.getFileType();
112 if (StringUtils.isNotBlank(additionalWhere)) {
113 whereClause +=
" AND " + additionalWhere;
116 return getCountOfTskFiles(skCase, currentDataSource, whereClause);
124 T
process(ResultSet resultset)
throws SQLException;
140 static <T> T getBaseQueryResult(SleuthkitCase skCase, String query,
ResultSetHandler<T> processor)
141 throws TskCoreException, SQLException {
142 try (SleuthkitCase.CaseDbQuery dbQuery = skCase.executeQuery(query)) {
143 ResultSet resultSet = dbQuery.getResultSet();
144 return processor.process(resultSet);
157 return "meta_flags & " + flag.getValue() +
" > 0";
188 public static List<BlackboardArtifact>
getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType,
SortOrder sortOrder)
throws TskCoreException {
189 return getArtifacts(skCase, artifactType, dataSource, attributeType, sortOrder, 0);
214 public static List<BlackboardArtifact>
getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType,
SortOrder sortOrder,
int maxCount)
throws TskCoreException {
216 throw new IllegalArgumentException(
"Invalid maxCount passed to getArtifacts, value must be equal to or greater than 0");
244 static private SortedMap<BlackboardAttribute, List<BlackboardArtifact>>
getArtifactMap(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType,
SortOrder sortOrder)
throws TskCoreException {
245 SortedMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap =
new TreeMap<>(
new AttributeComparator(sortOrder));
246 List<BlackboardArtifact> artifactList = skCase.getBlackboard().getArtifacts(artifactType.getTypeID(), dataSource.getId());
248 for (BlackboardArtifact artifact : artifactList) {
249 BlackboardAttribute attribute = artifact.getAttribute(attributeType);
250 if (attribute == null) {
254 List<BlackboardArtifact> mapArtifactList = sortedMap.get(attribute);
255 if (mapArtifactList == null) {
256 mapArtifactList =
new ArrayList<>();
257 sortedMap.put(attribute, mapArtifactList);
260 mapArtifactList.add(artifact);
274 static private List<BlackboardArtifact>
createListFromMap(SortedMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap,
int maxCount) {
275 List<BlackboardArtifact> artifactList =
new ArrayList<>();
277 for (List<BlackboardArtifact> mapArtifactList : sortedMap.values()) {
279 if (maxCount == 0 || (artifactList.size() + mapArtifactList.size()) <= maxCount) {
280 artifactList.addAll(mapArtifactList);
284 if (maxCount == artifactList.size()) {
288 for (BlackboardArtifact artifact : mapArtifactList) {
289 if (artifactList.size() < maxCount) {
290 artifactList.add(artifact);
317 public int compare(BlackboardAttribute attribute1, BlackboardAttribute attribute2) {
318 if (!attribute1.getAttributeType().equals(attribute2.getAttributeType())) {
319 throw new IllegalArgumentException(
"Unable to compare attributes of different types");
322 int result =
compare(attribute1.getAttributeType(), attribute1, attribute2);
342 private int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2) {
343 switch (type.getValueType()) {
345 return attribute1.getValueString().compareToIgnoreCase(attribute2.getValueString());
347 return Integer.compare(attribute1.getValueInt(), attribute2.getValueInt());
350 return Long.compare(attribute1.getValueLong(), attribute2.getValueLong());
352 return Double.compare(attribute1.getValueDouble(), attribute2.getValueDouble());
356 throw new IllegalArgumentException(
"Unable to compare attributes of type " + attribute1.getAttributeType().getTypeName());
370 private static BlackboardAttribute
getAttributeOrNull(BlackboardArtifact artifact, Type attributeType) {
372 return artifact.getAttribute(attributeType);
373 }
catch (TskCoreException ex) {
387 public static String
getStringOrNull(BlackboardArtifact artifact, Type attributeType) {
389 return (attr == null) ? null : attr.getValueString();
401 public static Long
getLongOrNull(BlackboardArtifact artifact, Type attributeType) {
403 return (attr == null) ? null : attr.getValueLong();
415 public static Integer
getIntOrNull(BlackboardArtifact artifact, Type attributeType) {
417 return (attr == null) ? null : attr.getValueInt();
430 public static Date
getDateOrNull(BlackboardArtifact artifact, Type attributeType) {
432 return (longVal == null || longVal == 0) ? null :
new Date(longVal * 1000);
443 return longVal == null ? 0 : longVal;
455 return longVal == null ?
"0" : COMMA_FORMATTER.format(longVal);
static List< BlackboardArtifact > getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder, int maxCount)
static Long getCountOfRegNonSlackFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
static List< BlackboardArtifact > createListFromMap(SortedMap< BlackboardAttribute, List< BlackboardArtifact >> sortedMap, int maxCount)
static SortedMap< BlackboardAttribute, List< BlackboardArtifact > > getArtifactMap(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder)
static String getMetaFlagsContainsStatement(TSK_FS_META_FLAG_ENUM flag)
static long getLongOrZero(Long longVal)
static List< BlackboardArtifact > getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder)
int compare(BlackboardAttribute attribute1, BlackboardAttribute attribute2)
T process(ResultSet resultset)
static Long getLongOrNull(BlackboardArtifact artifact, Type attributeType)
DataSourceInfoUtilities()
static String getStringOrZero(Long longVal)
static Integer getIntOrNull(BlackboardArtifact artifact, Type attributeType)
final SortOrder direction
static BlackboardAttribute getAttributeOrNull(BlackboardArtifact artifact, Type attributeType)
static Date getDateOrNull(BlackboardArtifact artifact, Type attributeType)
static String getStringOrNull(BlackboardArtifact artifact, Type attributeType)
static final DecimalFormat COMMA_FORMATTER
int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2)
static final String COMMA_FORMAT_STR