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.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.z3950.zing.cql.CQLNode;
12 import org.z3950.zing.cql.CQLParseException;
13 import org.z3950.zing.cql.CQLParser;
14
15 import java.io.IOException;
16
17
18
19
20
21
22
23
24 public class OleCQLQueryParserServiceImpl implements OleCQLQueryParserService {
25
26 private static final Logger LOG = LoggerFactory.getLogger(OleCQLQueryParserServiceImpl.class);
27 private StringBuffer query = new StringBuffer("");
28 private boolean solrQueryFlag = true;
29 private SRUCQLQueryService srucqlQueryService = null;
30
31 public OleCQLQueryParserServiceImpl() {
32 srucqlQueryService = SRUCQLQueryServiceImpl.getInstance();
33 }
34
35
36
37
38
39
40
41 public String parseCQLQuery(String query) {
42
43 try {
44 CQLParser cql = new CQLParser();
45 CQLNode node = cql.parse(query);
46 LOG.info(node.toCQL());
47 LOG.info(node.toXCQL());
48 return node.toXCQL();
49
50 } catch (CQLParseException e) {
51 e.printStackTrace();
52 } catch (IOException e) {
53 e.printStackTrace();
54 }
55 return null;
56 }
57
58
59
60
61
62
63
64 public CQLResponseBO getCQLResponseObject(String cqlParseXml) {
65 CQLResponseBO cQLResponseBO = null;
66 OleCQLResponseHandler oleCQLResponseHandler = new OleCQLResponseHandler();
67 try {
68 try {
69 cQLResponseBO = oleCQLResponseHandler.fromXML(cqlParseXml);
70 } catch (Exception e) {
71 }
72 if (cQLResponseBO == null) {
73 CQLSearchClauseTag cqlSearchClauseTag = oleCQLResponseHandler.fromCQLXML(cqlParseXml);
74 cQLResponseBO = new CQLResponseBO();
75 cQLResponseBO.setSearchClauseTag(cqlSearchClauseTag);
76 }
77 } catch (Exception ex) {
78 }
79 return cQLResponseBO;
80 }
81
82
83
84
85
86
87
88 public String getSolrQueryFromCQLBO(CQLResponseBO cqlResponseBO) {
89 String solrQuery = "";
90 try {
91 solrQuery = getSolrQueryFromCQLBO(cqlResponseBO, true);
92 } catch (Exception e) {
93 LOG.error(e.getMessage() , e );
94 }
95 return solrQuery;
96 }
97
98
99
100
101
102
103
104
105
106 public String getSolrQueryFromCQLBO(CQLResponseBO cqlResponseBO, boolean flag) throws Exception {
107
108 String term = "";
109 String relation = "";
110 String index = "";
111 if (cqlResponseBO.getSearchClauseTag() != null) {
112 term = cqlResponseBO.getSearchClauseTag().getTerm();
113 relation = cqlResponseBO.getSearchClauseTag().getRelationTag().getValue();
114 index = cqlResponseBO.getSearchClauseTag().getIndex();
115 }
116 if (!solrQueryFlag)
117 return null;
118
119 if (cqlResponseBO.getLeftOperand() != null) {
120 query.append("(");
121 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(), false));
122 query.append(")");
123 if (cqlResponseBO.getTriple() != null)
124 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(), false));
125 else if (cqlResponseBO.getSearchClauseTag() != null)
126 query.append(srucqlQueryService.getQuery(term, relation, index, solrQueryFlag));
127 }
128 if (cqlResponseBO.getTriple() != null) {
129 query.append("(");
130 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getTriple(), false));
131 query.append(")");
132 if (cqlResponseBO.getSearchClauseTag() != null)
133 query.append(srucqlQueryService.getQuery(term, relation, index, solrQueryFlag));
134 }
135 if (cqlResponseBO.getRightOperand() != null) {
136 query.append(" " + cqlResponseBO.getBooleanTagValue().getValue() + " " );
137 query.append("(");
138 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getRightOperand(), false));
139 query.append(")");
140 if (cqlResponseBO.getTriple() != null)
141 query.append(getSolrQueryFromCQLBO(cqlResponseBO.getLeftOperand(), false));
142 else if (cqlResponseBO.getSearchClauseTag() != null)
143 query.append(srucqlQueryService.getQuery(term, relation, index, solrQueryFlag));
144 }
145 if (cqlResponseBO.getSearchClauseTag() != null) {
146 String query = srucqlQueryService.getQuery(term, relation, index, solrQueryFlag);
147 if (query != null)
148 return query.toString();
149 return null;
150 }
151 if (flag)
152 return query.toString();
153 return "";
154 }
155
156
157 }