public with sharing class Xin_Infrastructure_Project {
    public List<Infrastructure_Project__c> results {get; private set;}
    public Boolean getIsOverLimit() {
        if (results != null && results.size() > 30) {
            return true;
        }
        return false;
    }

    public Xin_Infrastructure_Project() {
        this.results = new List<Infrastructure_Project__c>();
    }

    public void search() {

        // 検索条件を用意
        String query = System.currentPageReference().getParameters().get('q');
        String visitorPlaceId = System.currentPageReference().getParameters().get('r');
        System.debug('visitorPlaceId:' + visitorPlaceId);
        
        if(query == null || query == '' || query.length() < 2) {
            return;
        }
        this.search(query, visitorPlaceId);
    }
    
    public void search(String query, String visitorPlaceId) {
        // Xin_SearchOpportunityと同じにする
        List<String> qwords = new List<String>();
        List<String> qwordstmp = query.split(' ');
        String nameCondition = '';
        for (String qword : qwordstmp) {
            if (String.isBlank(qword) == false) {
                qwords.add('%' + String.escapeSingleQuotes(qword.replaceAll('%', '')) + '%');
                nameCondition += ' Name like \'' + qwords[qwords.size() - 1] + '\' and';
            }
        }
        
        Account a = null;
        try {
            a = [select id,Hospital__r.name from Account where Id=:visitorPlaceId];
        }
        catch ( System.QueryException qe) {
            this.results = new List<Infrastructure_Project__c>();
            return;
        }
        

           // String nameCondition = '%' + String.escapeSingleQuotes(query.replaceAll('%', '')) + '%';
        system.debug('cond=' + nameCondition);
        
        // 検索
        String queryString = 'select Id, Name,HospitalName__c from Infrastructure_Project__c where' + nameCondition + ' OCSM_Hospital__c=\'' + a.Hospital__r.id + '\' limit 31';
        this.results = (List<Infrastructure_Project__c>) Database.query(queryString);
    }
}