package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.api.SearchFields;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@ViewScoped
@Named("AdvancedSearchPage")
public class AdvancedSearchPage {

    private static final Logger logger = Logger.getLogger(AdvancedSearchPage.class.getCanonicalName());

    @EJB
    DataverseServiceBean dataverseServiceBean;

    @EJB
    DatasetFieldServiceBean datasetFieldService;

    private Dataverse dataverse;
    private String query;
    private String title;
    private String author;
    private List<DatasetField> metadataFieldList;


    public void init() {
        /**
         * @todo: support advanced search at any depth in the dataverse
         * hierarchy
         */
        this.dataverse = dataverseServiceBean.findRootDataverse();
        this.metadataFieldList = datasetFieldService.findAll();          
    }

    public String find() throws IOException {
        /*
         logger.info("clicked find. author: " + author + ". title: " + title);
         List<String> queryStrings = new ArrayList();
         if (title != null && !title.isEmpty()) {
         queryStrings.add(SearchFields.TITLE + ":" + title);
         }

         if (author != null && !author.isEmpty()) {
         queryStrings.add(SearchFields.AUTHOR_STRING + ":" + author);
         }
         query = new String();
         for (String string : queryStrings) {
         query += string + " ";
         }
         logger.info("query: " + query); */
        List<String> queryStrings = new ArrayList();
        for (DatasetField datasetField : metadataFieldList) {
            if (!"".equals(datasetField.getSearchValue())) {
                queryStrings.add(datasetField.getName() + ":" + datasetField.getSearchValue());
            }
        }
        query = new String();
        for (String string : queryStrings) {
            query += string + " ";
        }
        return "/search.xhtml?q=" + query + "faces-redirect=true";
    }

    public Dataverse getDataverse() {
        return dataverse;
    }

    public void setDataverse(Dataverse dataverse) {
        this.dataverse = dataverse;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public List getMetadataFieldList() {
        List metadataBlock = new ArrayList();
        for (int i = 1; i < 6; i++) {
            List metadata = new ArrayList();
            for (DatasetField datasetField : metadataFieldList) {
                if (datasetField.getMetadataBlock().getId() == i) {
                    metadata.add(datasetField);
                }
            }
            metadataBlock.add(metadata);
        }
        return metadataBlock;
    }
}

@@ -153,24 +153,58 @@
 .util.List;%0A
+import java.util.StringTokenizer;%0A
 import java.
@@ -1882,24 +1882,164 @@
 Value())) %7B%0A
+                StringTokenizer st = new StringTokenizer(datasetField.getSearchValue());%0A                while (st.hasMoreElements()) %7B%0A    
             
@@ -2094,39 +2094,44 @@
 %22 + 
-datasetField.getSearchValue());
+st.nextElement());%0A                %7D
 %0A   

Split a String into tokens by space delimiter for Advanced Search. So if user enters for Title: A space B, app will search for Title: A space Title: B