View Javadoc
1   package org.kuali.ole.externalds;
2   
3   import org.kuali.ole.docstore.OleException;
4   import org.kuali.ole.docstore.common.search.SearchCondition;
5   import org.kuali.ole.docstore.common.search.SearchParams;
6   
7   import java.util.*;
8   
9   /**
10   * Created by IntelliJ IDEA.
11   * User: ND6967
12   * Date: 2/20/13
13   * Time: 9:03 PM
14   * To change this template use File | Settings | File Templates.
15   */
16  public class Z3950QueryBuilder {
17      static Map<String, Integer> fieldsMap = new HashMap<String, Integer>();
18  
19  
20      static {
21  
22          fieldsMap.put("Title", 4);
23          fieldsMap.put("Author", 1003);
24      }
25  
26      public String buildQuery(SearchParams searchParams) throws OleException {
27  
28          StringBuffer queryStringBuffer = new StringBuffer();
29          String query = "";
30          String[] fieldsToIncludeInQuery = {"Title", "Author"};
31          List listOfFieldsToIncludeInQuery = Arrays.asList(fieldsToIncludeInQuery);
32          List<SearchCondition> finalFieldsToIncludeInQuery = new ArrayList<SearchCondition>();
33          List<SearchCondition> searchConditionList = searchParams.getSearchConditions();
34          //
35          for (SearchCondition searchCondition : searchConditionList) {
36              System.out.println("In for 22" + searchCondition.getSearchField().getFieldName());
37              if (searchCondition.getSearchField().getFieldName() != null && !searchCondition.getSearchField().getFieldName().equalsIgnoreCase("")) {
38                  System.out.println("In if one");
39                  if (listOfFieldsToIncludeInQuery.contains(searchCondition.getSearchField().getFieldName())) {
40                      System.out.println("In if two");
41                      if (searchCondition.getSearchField().getFieldValue() != null && !searchCondition.getSearchField().getFieldValue()
42                              .equalsIgnoreCase("")) {
43                          System.out.println("In if three");
44                          finalFieldsToIncludeInQuery.add(searchCondition);
45                      }
46                  }
47              }
48          }
49          System.out.println("finalFieldsToIncludeInQuery size " + finalFieldsToIncludeInQuery.size());
50          for (SearchCondition searchCondition : finalFieldsToIncludeInQuery) {
51              System.out.println("getSearchText " + searchCondition.getSearchField().getFieldValue());
52              System.out.println("getDocField " + searchCondition.getSearchField().getFieldName());
53              System.out.println("getOperator " + searchCondition.getOperator());
54          }
55          if (finalFieldsToIncludeInQuery.size() == 0) {
56              SearchCondition searchCondition = searchParams.getSearchConditions().get(0);
57              if (searchCondition.getSearchField().getFieldValue() != null && !searchCondition.getSearchField().getFieldValue()
58                      .equalsIgnoreCase("")) {
59                  query = searchCondition.getSearchField().getFieldValue();
60              } else {
61                  System.out.println("No Field Selected Or Data entered");
62                  //query = "failure";
63                  throw new OleException("No Field Selected Or Data entered");
64              }
65          } else {
66              query = prepareQuery(finalFieldsToIncludeInQuery);
67              System.out.println("buildQuery " + query);
68          }
69          return query;
70      }
71  
72      //@attrset bib-1 @attr 1=4 "XML" @attr 1=1003 "Sanderson"
73      //@attrset bib-1 @or @attr 1=4 xml @attr 1=1003 Sanderson
74      public String prepareQuery(List<SearchCondition> searchConditionList) {
75          String partOfQuery = "@attrset bib-1";
76          String query = "";
77          StringBuffer sb = new StringBuffer(partOfQuery);
78          if (searchConditionList.size() == 1) {
79              SearchCondition searchCondition = searchConditionList.get(0);
80              query = partOfQuery + " @attr 1=" + fieldsMap.get(searchCondition.getSearchField().getFieldName()) + " " + searchCondition
81                      .getSearchField().getFieldValue();
82          } else {
83              for (int i = 0; i < searchConditionList.size(); i++) {
84                  SearchCondition searchCondition = searchConditionList.get(i);
85                  if (i == 0) {
86                      sb.append(" ");
87                      sb.append("@");
88                      sb.append(searchCondition.getOperator());
89                  }
90                  sb.append(" @attr 1=" + fieldsMap.get(searchCondition.getSearchField().getFieldName()) + " " + searchCondition.getSearchField().getFieldValue());
91              }
92              query = sb.toString();
93          }
94  
95          return query;
96      }
97  
98  }