/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.ogm.dialect.mongodb.query.parsing;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.QueryParser;
import org.hibernate.hql.ast.spi.EntityNamesResolver;
import org.hibernate.ogm.dialect.mongodb.query.MongoDBQueryImpl;
import org.hibernate.ogm.hibernatecore.impl.OgmSession;
import org.hibernate.ogm.logging.mongodb.impl.Log;
import org.hibernate.ogm.logging.mongodb.impl.LoggerFactory;
import org.hibernate.ogm.service.impl.BaseQueryParserService;
import org.hibernate.ogm.service.impl.SessionFactoryEntityNamesResolver;
/**
* {@link org.hibernate.ogm.service.impl.QueryParserService} implementation which creates MongoDB queries in form of
* {@link com.mongodb.DBObject}s.
*
* @author Gunnar Morling
*/
public class MongoDBBasedQueryParserService extends BaseQueryParserService {
private static final Log log = LoggerFactory.getLogger();
private volatile SessionFactoryEntityNamesResolver entityNamesResolver;
@Override
public Query getParsedQueryExecutor(OgmSession session, String queryString, Map<String, Object> namedParameters) {
QueryParser queryParser = new QueryParser();
MongoDBProcessingChain processingChain = createProcessingChain( session, unwrap( namedParameters ) );
MongoDBQueryParsingResult result = queryParser.parseQuery( queryString, processingChain );
log.createdQuery( queryString, result );
return new MongoDBQueryImpl( result.getEntityType(), result.getQuery(), result.getProjection(), session );
}
private MongoDBProcessingChain createProcessingChain(Session session, Map<String, Object> namedParameters) {
EntityNamesResolver entityNamesResolver = getDefinedEntityNames( session.getSessionFactory() );
return new MongoDBProcessingChain(
(SessionFactoryImplementor) session.getSessionFactory(),
entityNamesResolver,
namedParameters );
}
private EntityNamesResolver getDefinedEntityNames(SessionFactory sessionFactory) {
if ( entityNamesResolver == null ) {
entityNamesResolver = new SessionFactoryEntityNamesResolver( sessionFactory );
}
return entityNamesResolver;
}
}