001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.lucene;
016    
017    import com.liferay.portal.kernel.search.Query;
018    import com.liferay.portal.kernel.search.StringQueryImpl;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import org.apache.lucene.queryParser.ParseException;
022    import org.apache.lucene.queryParser.QueryParser;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class QueryTranslator {
028    
029            public static org.apache.lucene.search.Query translate(Query query)
030                    throws ParseException {
031    
032                    if (query instanceof BooleanQueryImpl) {
033                            return ((BooleanQueryImpl)query).getBooleanQuery();
034                    }
035                    else if (query instanceof LuceneQueryImpl) {
036                            return ((LuceneQueryImpl)query).getQuery();
037                    }
038                    else if (query instanceof StringQueryImpl) {
039                            QueryParser parser = new QueryParser(
040                                    StringPool.BLANK, LuceneHelperUtil.getAnalyzer());
041    
042                            return parser.parse(query.toString());
043                    }
044                    else if (query instanceof TermQueryImpl) {
045                            return ((TermQueryImpl)query).getTermQuery();
046                    }
047                    else {
048                            return null;
049                    }
050            }
051    
052    }