View Javadoc

1   package org.kuali.ole.serviceimpl;
2   
3   import org.kuali.ole.bo.cql.CQLResponseBO;
4   import org.kuali.ole.bo.cql.CQLSearchClauseTag;
5   import org.kuali.ole.docstore.discovery.service.SRUCQLQueryService;
6   import org.kuali.ole.docstore.discovery.service.SRUCQLQueryServiceImpl;
7   import org.kuali.ole.handler.OleCQLResponseHandler;
8   import org.kuali.ole.service.OleCQLQueryParserService;
9   import org.z3950.zing.cql.CQLNode;
10  import org.z3950.zing.cql.CQLParseException;
11  import org.z3950.zing.cql.CQLParser;
12  
13  import java.io.IOException;
14  
15  /**
16   * Created with IntelliJ IDEA.
17   * User: ?
18   * Date: 7/9/12
19   * Time: 6:26 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class OleCQLQueryParserServiceImpl implements OleCQLQueryParserService {
23      private StringBuffer query=new StringBuffer("");
24      private boolean solrQueryFlag=true;
25      private SRUCQLQueryService srucqlQueryService=null;
26  
27      public OleCQLQueryParserServiceImpl(){
28          srucqlQueryService =SRUCQLQueryServiceImpl.getInstance();
29      }
30  
31      /**
32       * this method is use to parse the query to get the response for the query as an xml cql or proper cql query
33       * @param query
34       * @return xml as a string
35       */
36      public String parseCQLQuery(String query){
37  
38          try {
39              CQLParser cql=new CQLParser();
40              CQLNode node=cql.parse(query);
41              System.out.println(node.toCQL());
42              System.out.println(node.toXCQL(0));
43              return node.toXCQL(0);
44  
45          } catch (CQLParseException e) {
46             e.printStackTrace();
47          } catch (IOException e) {
48             e.printStackTrace();
49          }
50          return null;
51      }
52  
53      /**
54       * this method is used to call the oleCQLResponseHandler which converts an xml to an object
55       * @param cqlParseXml
56       * @return  cQLResponseBO object
57       */
58      public CQLResponseBO getCQLResponseObject(String cqlParseXml){
59          CQLResponseBO cQLResponseBO=null;
60          OleCQLResponseHandler oleCQLResponseHandler=new OleCQLResponseHandler();
61          try{
62              try{
63                 cQLResponseBO=oleCQLResponseHandler.fromXML(cqlParseXml);
64                }
65              catch(Exception e){}
66              if(cQLResponseBO==null) {
67              CQLSearchClauseTag cqlSearchClauseTag=oleCQLResponseHandler.fromCQLXML(cqlParseXml);
68              cQLResponseBO=new CQLResponseBO();
69              cQLResponseBO.setSearchClauseTag(cqlSearchClauseTag);
70             }
71          }
72          catch(Exception ex){}
73          return cQLResponseBO;
74      }
75  
76      /**
77       * this will generate the proper query
78       * @param cqlResponseBO
79       * @return query as a string
80       */
81      public String getSolrQueryFromCQLBO(CQLResponseBO cqlResponseBO){
82          String solrQuery="";
83          try{
84              solrQuery= getSolrQueryFromCQLBO(cqlResponseBO,true);
85          }
86          catch(Exception e){
87              System.out.println(e);
88          }
89          return solrQuery;
90      }
91  
92      /**
93       * this method will generate a proper query as like solr, from the CQLResponseBO object
94       * @param cqlResponseBO object
95       * @param flag
96       * @return  proper query as a string
97       * @throws Exception
98       */
99      public String getSolrQueryFromCQLBO(CQLResponseBO cqlResponseBO,boolean flag)throws Exception{
100 
101         String term="";
102         String relation="";
103         String index="";
104         if(cqlResponseBO.getSearchClauseTag()!=null){
105             term=cqlResponseBO.getSearchClauseTag().getTerm();
106             relation= cqlResponseBO.getSearchClauseTag().getRelationTag().getValue();
107             index=cqlResponseBO.getSearchClauseTag().getIndex();
108         }
109         if(!solrQueryFlag)
110             return null;
111 
112         if(cqlResponseBO.getLeftOperand()!=null){
113             query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(), false));
114             if(cqlResponseBO.getTriple()!=null)
115                 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(),false));
116             else if(cqlResponseBO.getSearchClauseTag()!=null)
117                 query.append(srucqlQueryService.getQuery(term,relation,index,solrQueryFlag));
118         }
119         if(cqlResponseBO.getTriple()!=null){
120             query.append(getSolrQueryFromCQLBO(cqlResponseBO.getTriple(), false));
121             if(cqlResponseBO.getSearchClauseTag()!=null)
122                 query.append(srucqlQueryService.getQuery(term,relation,index,solrQueryFlag));
123         }
124         if(cqlResponseBO.getRightOperand()!=null){
125             query.append(" " + cqlResponseBO.getBooleanTagValue().getValue() + " " + getSolrQueryFromCQLBO(cqlResponseBO.getRightOperand(), false));
126             if(cqlResponseBO.getTriple()!=null)
127                 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(),false));
128             else if(cqlResponseBO.getSearchClauseTag()!=null)
129                 query.append(srucqlQueryService.getQuery(term,relation,index,solrQueryFlag));
130         }
131         if(cqlResponseBO.getSearchClauseTag()!=null){
132             String query=srucqlQueryService.getQuery(term,relation,index,solrQueryFlag);
133             if(query!=null)
134                 return query.toString();
135             return null;
136         }
137         if(flag)
138             return query.toString();
139         return "";
140     }
141 
142 
143 }