<commit_before>import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.rmi.* ;
import java.rmi.registry.* ;
import imcode.util.* ;

/**
  Search documents
*/
public class SearchDocuments extends HttpServlet {
	private final static String CVS_REV = "$Revision$" ;
	private final static String CVS_DATE = "$Date$" ;

    /**
       doPost()
    */
    public void doPost( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
	String host 				= req.getHeader("Host") ;
	String imcserver 			= Utility.getDomainPref("adminserver",host) ;
	String start_url        	= Utility.getDomainPref( "start_url",host ) ;
	String servlet_url        	= Utility.getDomainPref( "servlet_url",host ) ;

	imcode.server.User user ;
	String htmlStr = "" ;

	String question_field = "" ;
	String search_type = "" ;        // atc_icd10 or nothing
	String search_area = "" ;
	String string_match = "" ;
	String search_prep = "" ;        // and / or
	String values[] ;
	int start = 0 ;      // The search result counter. Keeps track of hits
	int nbrToShow = 40 ;  // Keeps track of how many results we should show
	// The result = nbrToShow / fieldrecSize
	int meta_id = 0 ;
	int fieldRecSize = 5 ;  // Keeps track of how many fields a record consists of in the res array

	res.setContentType( "text/html" );
	PrintWriter out = res.getWriter( );

	// get question_field
	values = req.getParameterValues( "question_field" ) ;
	if( values != null ) question_field = values[0] ;
	question_field = verifySqlText(question_field) ;

	// get search_type atc_icd10
	search_type = req.getParameter( "search_type" ) ;
	if( search_type == null) search_type = "not codes" ;
	// if( values != null ) search_type = values[0] ;
	//log("search_type = " + search_type) ;

	// Get search_preposition - AND / OR
	search_prep = req.getParameter( "search_prep" ) ;
	if( search_prep == null) search_prep = "and" ;
	//log("search_prep = " + search_prep) ;

	// get search_area
	values = req.getParameterValues( "search_area" ) ;
	if( values != null ) search_area = values[0] ;

	// get string_match
	values = req.getParameterValues( "string_match" ) ;
	if( values != null ) string_match = values[0] ;

	// get startvalue
	String startStr = req.getParameter( "start" ) ;
	if( startStr == null ) startStr = "0" ;
	try {
	    start = Integer.parseInt(startStr) ;
	} catch(NumberFormatException ex) {
	    log(ex.getMessage()) ;
	    start = 0 ;
	}

	// Get the session
	HttpSession session = req.getSession( true );

	// Does the session indicate this user already logged in?
	Object done = session.getValue( "logon.isDone" );  // marker object
	user = (imcode.server.User)done ;

	if( done == null ) {
	    // No logon.isDone means he hasn't logged in.
	    // Save the request URL as the true target and redirect to the login page.
	    session.putValue( "login.target",
			      HttpUtils.getRequestURL( req ).toString( ) );
	    String scheme = req.getScheme( );
	    String serverName = req.getServerName( );
	    int p = req.getServerPort( );
	    String port = (p == 80) ? "" : ":" + p;
	    res.sendRedirect( scheme + "://" + serverName + port + start_url ) ;
	    return ;
	}

	// Lets check the length on the questionfield
	if( question_field.equals("")) {
	    this.doGet(req,res);
	    return ;
	}

	// Lets get the langprefix
	String langPrefix = IMCServiceRMI.sqlQueryStr(imcserver, "select lang_prefix from lang_prefixes where lang_id = "+user.getInt("lang_id")) ;
	// Lets run the question

	String sqlStr = buildSqlStr(imcode.server.HTMLConv.toHTML(question_field), search_type, search_prep, string_match, search_area) ;
	// log("BuildSqlStr: " + sqlStr) ;  
	String[] answer= IMCServiceRMI.sqlQuery(imcserver, sqlStr) ;

	// Lets copy the array into a vector
	Vector docs = new Vector(answer.length) ;
	for(int i=0 ; i<answer.length; i++) {
	    docs.add(answer[i]) ;
	}
    
	// Lets get the number of hits. Docs will have the following 'syntax'
	// meta id, meta_headline, meta_text
	int no_of_docs = docs.size( ) ;
	log("Antal trffar: " + (no_of_docs / fieldRecSize)) ;

	// Lets send the search paramters
	Properties props = this.getSummaryProps(servlet_url) ;
	props.setProperty("#SEARCH_TYPE#", search_type) ;
	props.setProperty("#STRING_MATCH#", string_match) ;
	props.setProperty("#SEARCH_AREA#", search_area) ;
	props.setProperty("#QUEST#", question_field) ;
	props.setProperty("#TOTAL_HITS#", "" + no_of_docs / fieldRecSize) ;
	props.setProperty("#SEARCH_PREP#", search_prep);

<commit_msg>Bug 259: Added a check thet removes the hits that the user isn't allowed to see.<commit_after>import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.rmi.* ;
import java.rmi.registry.* ;
import imcode.util.* ;

/**
  Search documents
*/
public class SearchDocuments extends HttpServlet {
	private final static String CVS_REV = "$Revision$" ;
	private final static String CVS_DATE = "$Date$" ;

    /**
       doPost()
    */
    public void doPost( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
	String host 				= req.getHeader("Host") ;
	String imcserver 			= Utility.getDomainPref("adminserver",host) ;
	String start_url        	= Utility.getDomainPref( "start_url",host ) ;
	String servlet_url        	= Utility.getDomainPref( "servlet_url",host ) ;

	imcode.server.User user ;
	String htmlStr = "" ;

	String question_field = "" ;
	String search_type = "" ;        // atc_icd10 or nothing
	String search_area = "" ;
	String string_match = "" ;
	String search_prep = "" ;        // and / or
	String values[] ;
	int start = 0 ;      // The search result counter. Keeps track of hits
	int nbrToShow = 40 ;  // Keeps track of how many results we should show
	// The result = nbrToShow / fieldrecSize
	int meta_id = 0 ;
	int fieldRecSize = 5 ;  // Keeps track of how many fields a record consists of in the res array

	res.setContentType( "text/html" );
	PrintWriter out = res.getWriter( );

	// get question_field
	values = req.getParameterValues( "question_field" ) ;
	if( values != null ) question_field = values[0] ;
	question_field = verifySqlText(question_field) ;

	// get search_type atc_icd10
	search_type = req.getParameter( "search_type" ) ;
	if( search_type == null) search_type = "not codes" ;
	// if( values != null ) search_type = values[0] ;
	//log("search_type = " + search_type) ;

	// Get search_preposition - AND / OR
	search_prep = req.getParameter( "search_prep" ) ;
	if( search_prep == null) search_prep = "and" ;
	//log("search_prep = " + search_prep) ;

	// get search_area
	values = req.getParameterValues( "search_area" ) ;
	if( values != null ) search_area = values[0] ;

	// get string_match
	values = req.getParameterValues( "string_match" ) ;
	if( values != null ) string_match = values[0] ;

	// get startvalue
	String startStr = req.getParameter( "start" ) ;
	if( startStr == null ) startStr = "0" ;
	try {
	    start = Integer.parseInt(startStr) ;
	} catch(NumberFormatException ex) {
	    log(ex.getMessage()) ;
	    start = 0 ;
	}

	// Get the session
	HttpSession session = req.getSession( true );

	// Does the session indicate this user already logged in?
	Object done = session.getValue( "logon.isDone" );  // marker object
	user = (imcode.server.User)done ;

	if( done == null ) {
	    // No logon.isDone means he hasn't logged in.
	    // Save the request URL as the true target and redirect to the login page.
	    session.putValue( "login.target",
			      HttpUtils.getRequestURL( req ).toString( ) );
	    String scheme = req.getScheme( );
	    String serverName = req.getServerName( );
	    int p = req.getServerPort( );
	    String port = (p == 80) ? "" : ":" + p;
	    res.sendRedirect( scheme + "://" + serverName + port + start_url ) ;
	    return ;
	}

	// Lets check the length on the questionfield
	if( question_field.equals("")) {
	    this.doGet(req,res);
	    return ;
	}

	// Lets get the langprefix
	String langPrefix = IMCServiceRMI.sqlQueryStr(imcserver, "select lang_prefix from lang_prefixes where lang_id = "+user.getInt("lang_id")) ;
	// Lets run the question

	String sqlStr = buildSqlStr(imcode.server.HTMLConv.toHTML(question_field), search_type, search_prep, string_match, search_area) ;
	System.out.println("BuildSqlStr: " + sqlStr) ;  
	String[] answer= IMCServiceRMI.sqlQuery(imcserver, sqlStr) ;
	
	for(int i=0; i<answer.length ; i += 5)
	{	//lets get rid of the pages that the user not allowed to see
		if (!IMCServiceRMI.checkDocRights (imcserver, Integer.parseInt(answer[i]), user ))
		{
			for(int z = i; z<i+5; z++)
				answer[z] = null;			
		}
	}

	// Lets copy the array into a vector
	Vector docs = new Vector() ;
	for(int i=0 ; i<answer.length; i++) {
	    if(answer[i] != null)
			docs.add(answer[i]) ;
	}
    
	// Lets get the number of hits. Docs will have the following 'syntax'
	// meta id, meta_headline, meta_text
	int no_of_docs = docs.size( ) ;
	log("Antal trffar: " + (no_of_docs / fieldRecSize)) ;

	// Lets send the search paramters
	Properties props = this.getSummaryProps(servlet_url) ;
	props.setProperty("#SEARCH_TYPE#", search_type) ;
	props.setProperty("#STRING_MATCH#", string_match) ;
	props.setProperty("#SEARCH_AREA#", search_area) ;
	props.setProperty("#QUEST#", question_field) ;
	props.setProperty("#TOTAL_HITS#", "" + no_of_docs / fieldRecSize) ;
	props.setProperty("#SEARCH_PREP#", search_prep);


{'fraction_non_alphanumeric': 0.13037492056767633, 'fraction_numerical': 0.004448210124973522, 'mean_word_length': 3.5268456375838926, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2109172', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3094, 'n_words': 827}