001: package org.sakaiproject.citation.util.impl;
002:
003: import org.sakaiproject.citation.util.api.SearchQuery;
004:
005: public class CQLSearchQuery implements
006: org.sakaiproject.citation.util.api.CQLSearchQuery {
007:
008: private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
009: .getLog("org.sakaibrary.common.search.impl.CQLSearchQuery");
010:
011: private org.sakaiproject.citation.util.api.SearchQuery searchQuery;
012: private String cqlQuery;
013:
014: /**
015: * Gets a CQL-formatted search query by converting searchQuery.
016: *
017: * @param searchQuery SearchQuery object to convert.
018: * @return the search query in CQL format or null if searchQuery is null.
019: */
020: public String getCQLSearchQueryString(
021: org.sakaiproject.citation.util.api.SearchQuery searchQuery) {
022:
023: if (searchQuery == null) {
024: return null;
025: }
026:
027: this .searchQuery = searchQuery;
028:
029: // get criteria strings
030: String keywordString = getKeywordString();
031: String titleString = getTitleString();
032: String authorString = getAuthorString();
033: String subjectString = getSubjectString();
034: String yearString = getYearString();
035:
036: // debugging
037: LOG.debug("going to parse - keyword: " + keywordString
038: + "; title: " + titleString + "; author: "
039: + authorString + "; subject: " + subjectString
040: + "; year: " + yearString);
041:
042: // concatenate criteria strings
043: StringBuffer searchStringBuffer = new StringBuffer();
044:
045: if (keywordString != null) {
046: searchStringBuffer.append(keywordString + " ");
047: }
048: if (titleString != null) {
049: searchStringBuffer.append(titleString + " ");
050: }
051: if (authorString != null) {
052: searchStringBuffer.append(authorString + " ");
053: }
054: if (subjectString != null) {
055: searchStringBuffer.append(subjectString + " ");
056: }
057: if (yearString != null) {
058: searchStringBuffer.append(yearString + " ");
059: }
060:
061: // manipulate the buffer
062: String searchString = searchStringBuffer.toString();
063: searchString = searchString.substring(0,
064: searchString.length() - 1);
065: searchString = searchString.replaceAll("\\s", " and ");
066:
067: // debugging
068: LOG.debug("full search string to parse: " + searchString);
069:
070: // convert the searchString to CQL
071: org.z3950.zing.cql.CQLParser parser = new org.z3950.zing.cql.CQLParser();
072: org.z3950.zing.cql.CQLNode root = null;
073:
074: try {
075: // parse the criteria
076: root = parser.parse(searchString);
077: } catch (java.io.IOException ioe) {
078: LOG.warn("CQLSearchQuery.getCQLSearchQueryString() IO "
079: + "exception while parsing: " + ioe.getMessage());
080: } catch (org.z3950.zing.cql.CQLParseException e) {
081: LOG.warn("CQLSearchQuery.getCQLSearchQueryString() CQL "
082: + "parsing exception while parsing: "
083: + e.getMessage());
084: }
085:
086: cqlQuery = (root == null) ? null : root.toCQL();
087:
088: return cqlQuery;
089: }
090:
091: private String getKeywordString() {
092: java.util.Set<String> keywords = searchQuery.getKeywords();
093: String keywordString = null;
094:
095: if (keywords != null) {
096: keywordString = criteriaCleanup(keywords, "keyword");
097: }
098:
099: return keywordString;
100: }
101:
102: private String getTitleString() {
103: java.util.Set<String> titles = searchQuery.getTitles();
104: String titleString = null;
105:
106: if (titles != null) {
107: titleString = criteriaCleanup(titles, "title");
108: }
109:
110: return titleString;
111: }
112:
113: private String getAuthorString() {
114: String authorString = null;
115: java.util.Set<String> authors = searchQuery.getAuthors();
116:
117: if (authors != null) {
118: authorString = criteriaCleanup(authors, "author");
119: }
120:
121: return authorString;
122: }
123:
124: private String getSubjectString() {
125: String subjectString = null;
126: java.util.Set<String> subjects = searchQuery.getSubjects();
127:
128: if (subjects != null) {
129: subjectString = criteriaCleanup(subjects, "subject");
130: }
131:
132: return subjectString;
133: }
134:
135: private String getYearString() {
136: java.util.Set<String> years = searchQuery.getYears();
137: String yearString = null;
138:
139: if (years != null) {
140: yearString = criteriaCleanup(years, "year");
141: }
142:
143: return yearString;
144: }
145:
146: private String criteriaCleanup(java.util.Set<String> criteriaSet,
147: String criteriaName) {
148: java.util.Iterator criteria = criteriaSet.iterator();
149:
150: StringBuffer result = new StringBuffer();
151: while (criteria.hasNext()) {
152: result.append(" " + criteriaName + "=");
153: String criterion = (String) criteria.next();
154:
155: // remove any punctuation
156: criterion = criterion.replaceAll("\\p{Punct}", " ");
157: criterion = criterion.trim();
158:
159: // take care of any adjacent spaces
160: criterion = criterion.replaceAll("\\s+", " ");
161:
162: // replace spaces with +
163: criterion = criterion.replaceAll("\\s", "+");
164:
165: // append this keyword
166: result.append(criterion);
167: }
168:
169: return (result.toString().trim().equals("")) ? null : result
170: .toString().trim();
171: }
172:
173: }
|