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.StringTokenizer;
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.findAllAdvancedSearchFields();          
    }

    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())) {
                StringTokenizer st = new StringTokenizer(datasetField.getSearchValue(), "\"");
                while (st.hasMoreElements()) {
                    String token = st.nextToken().trim();
                    if (!token.equals(" ") && !token.isEmpty()) {
                        queryStrings.add(datasetField.getName() + ":" + token); 
                    }
                }    
            }
        }
        query = new String();
        for (String string : queryStrings) {
            query += string + " ";
        }
        //return "/search.xhtml?q=" + query + "faces-redirect=true";
        return "/dataverse.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;
    }
}

@@ -1775,32 +1775,68 @@
 ew ArrayList();%0A
+        String delimiter = %22%5B%5C%22%5D+%22;%0A
         for (Dat
@@ -1968,43 +1968,20 @@
 ring
-Tokenizer st = new StringTokenizer(
+ myString = 
 data
@@ -2009,15 +2009,8 @@
 ue()
-, %22%5C%22%22)
 ;%0A  
@@ -2027,34 +2027,34 @@
     
-while (st.hasMoreElements(
+if (myString.contains(%22%5C%22%22
 )) %7B
@@ -2085,69 +2085,180 @@
 ing 
-token = st.nextToken().trim();%0A                    if (!token
+%5B%5D tempString = datasetField.getSearchValue().split(delimiter);%0A                    for (int i = 1; i %3C tempString.length; i++) %7B%0A                        if (!tempString%5Bi%5D
 .equ
@@ -2271,20 +2271,28 @@
 %22) && !t
-oken
+empString%5Bi%5D
 .isEmpty
@@ -2289,32 +2289,36 @@
 i%5D.isEmpty()) %7B%0A
+    
                 
@@ -2362,74 +2362,417 @@
 .get
-Name() + %22:%22 + token); %0A                    %7D%0A                %7D   
+SolrField() + %22:%22 + %22%5C%22%22 + tempString%5Bi%5D.trim() + %22%5C%22%22);%0A                        %7D%0A                    %7D%0A                %7D else %7B%0A                    StringTokenizer st = new StringTokenizer(datasetField.getSearchValue());%0A                    while (st.hasMoreElements()) %7B%0A                        queryStrings.add(datasetField.getSolrField() + %22:%22 + st.nextElement());%0A                    %7D%0A                %7D
  %0A  
@@ -2917,77 +2917,8 @@
   %7D%0A
-        //return %22/search.xhtml?q=%22 + query + %22faces-redirect=true%22;%0A
     

Split a String into tokens by both double quote and space delimiters for Advanced Search. Still need more work to do to handle all the cases.