Skip to content

Commit

Permalink
Merge pull request #1277 from millmanorama/categorization_fixes
Browse files Browse the repository at this point in the history
Categorization fixes
  • Loading branch information
rcordovano committed May 27, 2015
2 parents e98c04d + b04fa81 commit 5887d2a
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 217 deletions.
Expand Up @@ -95,15 +95,13 @@ protected Object doInBackground() throws Exception {
//remove old category tag if necessary
List<ContentTag> allContentTags = Case.getCurrentCase().getServices().getTagsManager().getContentTagsByContent(file);

boolean hadExistingCategory = false;
for (ContentTag ct : allContentTags) {
//this is bad: treating tags as categories as long as their names start with prefix
//TODO: abandon using tags for categories and instead add a new column to DrawableDB
if (ct.getName().getDisplayName().startsWith(Category.CATEGORY_PREFIX)) {
//LOGGER.log(Level.INFO, "removing old category from {0}", file.getName());
Case.getCurrentCase().getServices().getTagsManager().deleteContentTag(ct);
controller.getDatabase().decrementCategoryCount(Category.fromDisplayName(ct.getName().getDisplayName()));
hadExistingCategory = true;
}
}

Expand Down
Expand Up @@ -25,14 +25,12 @@
import javafx.collections.ObservableList;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.TskCoreException;

/**
* Represents a set of image/video files in a group. The UI listens to changes
* to the group membership and updates itself accordingly.
*/
public class DrawableGroup implements Comparable<DrawableGroup>{
public class DrawableGroup implements Comparable<DrawableGroup> {

private static final Logger LOGGER = Logger.getLogger(DrawableGroup.class.getName());

Expand Down Expand Up @@ -69,18 +67,18 @@ public double getHashHitDensity() {
* Call to indicate that an image has been added or removed from the group,
* so the hash counts may not longer be accurate.
*/
synchronized public void invalidateHashSetHitsCount(){
synchronized public void invalidateHashSetHitsCount() {
filesWithHashSetHitsCount = -1;
}
synchronized public int getFilesWithHashSetHitsCount() {

synchronized public int getFilesWithHashSetHitsCount() {
//TODO: use the drawable db for this ? -jm
if (filesWithHashSetHitsCount < 0) {
filesWithHashSetHitsCount = 0;
for (Long fileID : fileIds()) {

try {
if(ImageGalleryController.getDefault().getDatabase().isInHashSet(fileID)){
if (ImageGalleryController.getDefault().getDatabase().isInHashSet(fileID)) {
filesWithHashSetHitsCount++;
}
} catch (IllegalStateException | NullPointerException ex) {
Expand Down Expand Up @@ -117,20 +115,20 @@ public boolean equals(Object obj) {
}

synchronized public void addFile(Long f) {
invalidateHashSetHitsCount();
if (fileIDs.contains(f) == false) {
fileIDs.add(f);
}
invalidateHashSetHitsCount();
}

synchronized public void removeFile(Long f) {
fileIDs.removeAll(f);
invalidateHashSetHitsCount();
fileIDs.removeAll(f);
}

// By default, sort by group key name
@Override
public int compareTo(DrawableGroup other){
public int compareTo(DrawableGroup other) {
return this.groupKey.getValueDisplayName().compareTo(other.groupKey.getValueDisplayName());
}
}
Expand Up @@ -281,9 +281,11 @@ public synchronized void removeFromGroup(GroupKey<?> groupKey, final Long fileID
final DrawableGroup group = getGroupForKey(groupKey);
if (group != null) {
group.removeFile(fileID);

// If we're grouping by category, we don't want to remove empty groups.
if(! group.groupKey.getValueDisplayName().startsWith("CAT-")){
if (group.groupKey.getValueDisplayName().startsWith("CAT-")) {
return;
} else {
if (group.fileIds().isEmpty()) {
synchronized (groupMap) {
groupMap.remove(groupKey, group);
Expand Down Expand Up @@ -320,13 +322,13 @@ private synchronized <A extends Comparable<A>> void populateAnalyzedGroup(final
* was still running) */
if (task == null || (task.isCancelled() == false)) {
DrawableGroup g = makeGroup(groupKey, filesInGroup);

populateAnalyzedGroup(g, task);
}
}

private synchronized <A extends Comparable<A>> void populateAnalyzedGroup(final DrawableGroup g, ReGroupTask<A> task) {

if (task == null || (task.isCancelled() == false)) {
final boolean groupSeen = db.isGroupSeen(g.groupKey);
Platform.runLater(() -> {
Expand Down Expand Up @@ -430,7 +432,7 @@ private List<Long> getFileIDsWithHashSetName(String hashDbName) {
* @return
*/
@SuppressWarnings({"unchecked"})
public <A extends Comparable<A>> List<A> findValuesForAttribute(DrawableAttribute<A> groupBy) {
public <A extends Comparable<A>> List<A> findValuesForAttribute(DrawableAttribute<A> groupBy) {
List<A> values;
try {
switch (groupBy.attrName) {
Expand All @@ -456,12 +458,12 @@ public <A extends Comparable<A>> List<A> findValuesForAttribute(DrawableAttribut
}

return values;
} catch(TskCoreException ex){
} catch (TskCoreException ex) {
LOGGER.log(Level.WARNING, "TSK error getting list of type " + groupBy.getDisplayName());
return new ArrayList<A>();
}
}

}

public List<Long> getFileIDsInGroup(GroupKey<?> groupKey) throws TskCoreException {
switch (groupKey.getAttribute().attrName) {
Expand Down Expand Up @@ -515,14 +517,17 @@ public List<Long> getFileIDsWithCategory(Category category) throws TskCoreExcept
throw ex;
}
}

/**
* Count the number of files with the given category.
* This is faster than getFileIDsWithCategory and should be used if only the
* counts are needed and not the file IDs.
*
* @param category Category to match against
*
* @return Number of files with the given category
* @throws TskCoreException
*
* @throws TskCoreException
*/
public int countFilesWithCategory(Category category) throws TskCoreException {
return db.getCategoryCount(category);
Expand Down Expand Up @@ -581,10 +586,10 @@ public void setSortOrder(SortOrder sortOrder) {
*/
public <A extends Comparable<A>> void regroup(final DrawableAttribute<A> groupBy, final GroupSortBy sortBy, final SortOrder sortOrder, Boolean force) {

if(! Case.isCaseOpen()){
if (!Case.isCaseOpen()) {
return;
}

//only re-query the db if the group by attribute changed or it is forced
if (groupBy != getGroupBy() || force == true) {
setGroupBy(groupBy);
Expand Down Expand Up @@ -613,15 +618,11 @@ public <A extends Comparable<A>> void regroup(final DrawableAttribute<A> groupBy
}
}



/**
* an executor to submit async ui related background tasks to.
*/
final ExecutorService regroupExecutor = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder().namingPattern("ui task -%d").build());



public ReadOnlyDoubleProperty regroupProgress() {
return regroupProgress.getReadOnlyProperty();
}
Expand All @@ -630,6 +631,8 @@ public ReadOnlyDoubleProperty regroupProgress() {
* handle {@link FileUpdateEvent} sent from Db when files are
* inserted/updated
*
* TODO: why isn't this just two methods!
*
* @param evt
*/
@Override
Expand All @@ -643,19 +646,18 @@ synchronized public void handleFileUpdate(FileUpdateEvent evt) {

for (GroupKey<?> gk : groupsForFile) {
removeFromGroup(gk, fileId);

DrawableGroup g = getGroupForKey(gk);

if (g == null){
if (g == null) {
// It may be that this was the last unanalyzed file in the group, so test
// whether the group is now fully analyzed.
//TODO: use method in groupmanager ?
List<Long> checkAnalyzed = checkAnalyzed(gk);
if (checkAnalyzed != null) { // => the group is analyzed, so add it to the ui
populateAnalyzedGroup(gk, checkAnalyzed);
}
}
else{
} else {
g.invalidateHashSetHitsCount();
}
}
Expand All @@ -676,7 +678,7 @@ synchronized public void handleFileUpdate(FileUpdateEvent evt) {
* innertask? -jm
*/
for (final long fileId : fileIDs) {

db.updateHashSetsForFile(fileId);

//get grouping(s) this file would be in
Expand All @@ -698,13 +700,13 @@ synchronized public void handleFileUpdate(FileUpdateEvent evt) {
}
}
}

Category.fireChange(fileIDs);
if (evt.getChangedAttribute() == DrawableAttribute.CATEGORY) {
Category.fireChange(fileIDs);
}
if (evt.getChangedAttribute() == DrawableAttribute.TAGS) {
TagUtils.fireChange(fileIDs);
}
break;

}
}

Expand Down Expand Up @@ -753,10 +755,10 @@ protected Void call() throws Exception {
synchronized (groupMap) {
groupMap.clear();
}

// Get the list of group keys
final List<A> vals = findValuesForAttribute(groupBy);

// Make a list of each group
final List<DrawableGroup> groups = new ArrayList<>();

Expand All @@ -777,22 +779,22 @@ protected Void call() throws Exception {

List<Long> checkAnalyzed = checkAnalyzed(groupKey);
if (checkAnalyzed != null) { // != null => the group is analyzed, so add it to the ui

// makeGroup will create the group and add it to the map groupMap, but does not
// update anything else
DrawableGroup g = makeGroup(groupKey, checkAnalyzed);
groups.add(g);
}
}

// Sort the group list
Collections.sort(groups, sortBy.getGrpComparator(sortOrder));

// Officially add all groups in order
for(DrawableGroup g:groups){
for (DrawableGroup g : groups) {
populateAnalyzedGroup(g, ReGroupTask.this);
}

updateProgress(1, 1);
return null;
}
Expand Down

0 comments on commit 5887d2a

Please sign in to comment.