/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2011 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.datamodel;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.ContentVisitor;
import org.sleuthkit.datamodel.DerivedFile;
import org.sleuthkit.datamodel.File;
import org.sleuthkit.datamodel.LocalFile;
import org.sleuthkit.datamodel.LayoutFile;
import org.sleuthkit.datamodel.SleuthkitCase;
import org.sleuthkit.datamodel.TskCoreException;

/**
 * Children factory for the file by type view in dir tree
 */
class FileSearchFilterChildren extends ChildFactory<Content> {

    private SleuthkitCase skCase;
    private SearchFilters.SearchFilterInterface filter;
    private static final Logger logger = Logger.getLogger(FileSearchFilterChildren.class.getName());
    //private final static int MAX_OBJECTS = 2000;

    public FileSearchFilterChildren(SearchFilters.SearchFilterInterface filter, SleuthkitCase skCase) {
        this.filter = filter;
        this.skCase = skCase;
    }

    @Override
    protected boolean createKeys(List<Content> list) {
        list.addAll(runQuery());
        return true;
    }

    
    private String createQuery(){
        String query = "(known IS NULL OR known != 1) AND (0";
        for(String s : filter.getFilter()){
            query += " OR name LIKE '%" + s + "'";
        }
        query += ')';
        //query += " LIMIT " + MAX_OBJECTS;
        return query;
    }

    
    private List<AbstractFile> runQuery(){
        List<AbstractFile> list = new ArrayList<AbstractFile>();
        try {
            List<AbstractFile> res = skCase.findAllFilesWhere(createQuery());
            for(AbstractFile c : res){
                if(c.isFile()){
                    list.add(c);
                }
            }
        } catch (TskCoreException ex) {
            logger.log(Level.SEVERE, "Couldn't get search results", ex);
        }

        return list;
        
    }
    
    /**
     * Get children count without actually loading all nodes
     * @return 
     */
    long calculateItems() {
        try {
            return skCase.countFilesWhere(createQuery());
        } catch (TskCoreException ex) {
            logger.log(Level.SEVERE, "Error getting file search view count", ex);
            return 0;
        }
    }

    @Override
    protected Node createNodeForKey(Content key) {
        return key.accept(new ContentVisitor.Default<AbstractNode>() {
            @Override
            public FileNode visit(File f) {
                return new FileNode(f, false);
            }
            
            @Override
            public LayoutFileNode visit(LayoutFile lf) {
                return new LayoutFileNode(lf);
            }
                      
            @Override
            public LocalFileNode visit(DerivedFile df) {
                return new LocalFileNode(df);
            }
            
            @Override
            public LocalFileNode visit(LocalFile lf) {
                return new LocalFileNode(lf);
            }

            @Override
            protected AbstractNode defaultVisit(Content di) {
                throw new UnsupportedOperationException("Not supported for this type of Displayable Item: " + di.toString());
            }
        });
    }
}

@@ -964,44 +964,8 @@
 de;%0A
-import org.openide.util.Exceptions;%0A
 impo
@@ -1001,24 +1001,24 @@
 stractFile;%0A
+
 import org.s
@@ -1132,24 +1132,66 @@
 erivedFile;%0A
+import org.sleuthkit.datamodel.Directory;%0A
 import org.s
@@ -1394,16 +1394,56 @@
 ception;
+%0Aimport org.sleuthkit.datamodel.TskData;
 %0A%0A/**%0A *
@@ -2173,16 +2173,107 @@
 uery = %22
+(dir_type = %22 + TskData.TSK_FS_NAME_TYPE_ENUM.REG.getValue() + %22)%22%0A                + %22 AND 
 (known I
@@ -2751,40 +2751,8 @@
 s)%7B%0A
-                if(c.isFile())%7B%0A
     
@@ -2784,26 +2784,8 @@
 c);%0A
-                %7D%0A
     
@@ -3511,24 +3511,24 @@
 t(File f) %7B%0A
-
             
@@ -3603,32 +3603,180 @@
       @Override%0A
+            public DirectoryNode visit(Directory d) %7B%0A                return new DirectoryNode(d);%0A            %7D%0A            %0A            @Override%0A
             publ

fix showing some deleted contents in file view