/*
 * 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.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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.openide.util.lookup.Lookups;
import org.sleuthkit.autopsy.datamodel.SearchFilters.FileSearchFilter;
import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.ContentVisitor;
import org.sleuthkit.datamodel.Directory;
import org.sleuthkit.datamodel.File;
import org.sleuthkit.datamodel.FsContent;
import org.sleuthkit.datamodel.SleuthkitCase;

/**
 *
 * @author dfickling
 */
class FileSearchFilterChildren extends ChildFactory<Content> {
    
    SleuthkitCase skCase;
    SearchFilters.SearchFilterInterface filter;
    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 = "select * from tsk_files where known <> 1 and (0";
        for(String s : filter.getFilter()){
            query += " or name like '%" + s + "'";
        }
        query += ')';
        //query += " limit " + MAX_OBJECTS;
        return query;
    }
    
    @SuppressWarnings("deprecation")
    private List<FsContent> runQuery(){
        ResultSet rs = null;
        List<FsContent> list = new ArrayList<FsContent>();
        try {
            rs = skCase.runQuery(createQuery());
            for(FsContent c : skCase.resultSetToFsContents(rs)){
                if(c.isFile()){
                    list.add(c);
                }
            }
        } catch (SQLException ex) {
            logger.log(Level.WARNING, "Couldn't get search results", ex);
        }
        finally {
            if (rs != null) {
                try {
                    skCase.closeRunQuery(rs);
                } catch (SQLException ex) {
                    logger.log(Level.SEVERE, "Error closing result set after executing fscontents query for file search results", ex);
                }
            }
        }
        return list;
        
    }
    
    @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
            protected AbstractNode defaultVisit(Content di) {
                throw new UnsupportedOperationException("Not supported for this type of Displayable Item: " + di.toString());
            }
            
        });
    }
    
}

@@ -782,35 +782,8 @@
 on;%0A
-import java.sql.Statement;%0A
 impo
@@ -1021,155 +1021,8 @@
 de;%0A
-import org.openide.util.Exceptions;%0Aimport org.openide.util.lookup.Lookups;%0Aimport org.sleuthkit.autopsy.datamodel.SearchFilters.FileSearchFilter;%0A
 impo
@@ -1108,50 +1108,8 @@
 or;%0A
-import org.sleuthkit.datamodel.Directory;%0A
 impo
@@ -1228,16 +1228,65 @@
 kitCase;
+%0Aimport org.sleuthkit.datamodel.TskCoreException;
 %0A%0A/**%0A *
@@ -1952,38 +1952,8 @@
  = %22
-select * from tsk_files where 
 know
@@ -1959,19 +1959,19 @@
 wn %3C%3E 1 
-and
+AND
  (0%22;%0A  
@@ -2039,20 +2039,20 @@
 = %22 
-or
+OR
  name 
-like
+LIKE
  '%25%22
@@ -2116,21 +2116,21 @@
 ry += %22 
-limit
+LIMIT
  %22 + MAX
@@ -2176,45 +2176,8 @@
    %0A
-    @SuppressWarnings(%22deprecation%22)%0A
     
@@ -2330,28 +2330,51 @@
     
-rs = skCase.runQuery
+List%3CFsContent%3E res = skCase.findFilesWhere
 (cre
@@ -2420,40 +2420,11 @@
 c : 
-skCase.resultSetToFsContents(rs)
+res
 )%7B%0A 
@@ -2528,35 +2528,39 @@
        %7D catch (
-SQL
+TskCore
 Exception ex) %7B%0A
@@ -2647,344 +2647,8 @@
   %7D%0A
-        finally %7B%0A            if (rs != null) %7B%0A                try %7B%0A                    skCase.closeRunQuery(rs);%0A                %7D catch (SQLException ex) %7B%0A                    logger.log(Level.SEVERE, %22Error closing result set after executing fscontents query for file search results%22, ex);%0A                %7D%0A            %7D%0A        %7D
 %0A   

remove use of deprecated method for file search, use thread-safe method