Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.query.remote.search;
  
  import  com.google.protobuf.Descriptors;
  import  org.antlr.runtime.tree.Tree;
  import  org.hibernate.hql.ast.common.JoinType;
  import  org.hibernate.hql.ast.origin.hql.resolve.path.PathedPropertyReference;
  import  org.hibernate.hql.ast.origin.hql.resolve.path.PathedPropertyReferenceSource;
  import  org.hibernate.hql.ast.origin.hql.resolve.path.PropertyPath;
  import  org.hibernate.hql.ast.spi.QueryResolverDelegate;
 import  org.hibernate.hql.lucene.internal.ast.HSearchEmbeddedEntityTypeDescriptor;
 import  org.hibernate.hql.lucene.internal.ast.HSearchPropertyTypeDescriptor;
 import  org.hibernate.hql.lucene.internal.ast.HSearchTypeDescriptor;
 import  org.hibernate.hql.lucene.internal.logging.Log;
 import  org.hibernate.hql.lucene.internal.logging.LoggerFactory;
 import  org.infinispan.protostream.SerializationContext;
 
 import java.util.List;
 import java.util.Map;

This extends the ANTLR generated AST walker to transform a parsed tree into a Lucene Query and collect the target entity types of the query.
TODO:
  • It is currently human-written but should evolve into another ANTLR generated tree walker, not extending GeneratedHQLResolver but using its output as a generic normalization AST transformer.
  • We are assembling the Lucene Query directly, but this doesn't take into account parameter types which might need some transformation; the Hibernate Search provided QueryBuilder could do this.
  • Implement more predicates
  • Support multiple types being targeted by the Query
  • Support positional parameters (currently only consumed named parameters)
  • Author(s):
    Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
    Gunnar Morling
    anistor@redhat.com
  •  
     class IspnLuceneQueryResolverDelegate implements QueryResolverDelegate {
     
     	private enum Status {
     		DEFINING_SELECT, DEFINING_FROM;
     	}
     
     	private static final Log log = LoggerFactory.make();
    Persister space: keep track of aliases and entity names.
     
     	private final Map<StringStringaliasToEntityType = new HashMap<StringString>();
     
     	private Status status;
     
        private Descriptors.Descriptor targetType;
     
        private final SerializationContext serializationContext;
     
        public IspnLuceneQueryResolverDelegate(SerializationContext serializationContext) {
           this. = serializationContext;
        }
     
        Descriptors.Descriptor getTargetType() {
           return targetType;
        }

    See rule entityName
     
     	public void registerPersisterSpace(Tree entityName, Tree alias) {
     		String put = .putalias.getText(), entityName.getText() );
     		if ( put != null && !put.equalsIgnoreCaseentityName.getText() ) ) {
     					"Alias reuse currently not supported: alias " + alias.getText()
     					+ " already assigned to type " + put );
     		}
           Descriptors.Descriptor targetedType = .getMessageDescriptor(entityName.getText());
           if ( targetType != null ) {
     			throw new IllegalStateException"Can't target multiple types: " + targetType + " already selected before " + targetedType );
     		}
     		targetType = targetedType;
     	}
     
     	public boolean isUnqualifiedPropertyReference() {
     		return true// TODO - very likely always true for our supported use cases
     	}
     
     	public PathedPropertyReferenceSource normalizeUnqualifiedPropertyReference(Tree property) {
     		if ( .containsKeyproperty.getText() ) ) {
     			return normalizeQualifiedRootproperty );
     		}
     
                 new ProtobufValueWrapperTypeDescriptor(targetType),
     				Collections.<String>emptyList(),
     				property.getText()
    		);
    	}
    	public boolean isPersisterReferenceAlias() {
    		if ( .size() == 1 ) {
    			return true// should be safe
    		}
    		else {
    			throw new UnsupportedOperationException"Unexpected use case: not implemented yet?" );
    		}
    	}
    	public PathedPropertyReferenceSource normalizeUnqualifiedRoot(Tree identifier382) {
    		throw new UnsupportedOperationException"Not yet implemented" );
    	}
    	public PathedPropertyReferenceSource normalizeQualifiedRoot(Tree root) {
    		String entityNameForAlias = .getroot.getText() );
    		if ( entityNameForAlias == null ) {
    			throw .getUnknownAliasException( root.getText() );
    		}
          Descriptors.Descriptor descriptor = .getMessageDescriptor(entityNameForAlias);
    		return new PathedPropertyReference(
    				root.getText(),
                new ProtobufValueWrapperTypeDescriptor(descriptor),
    				true
    		);
    	}
    	public PathedPropertyReferenceSource normalizePropertyPathIntermediary(
    			PropertyPath path, Tree propertyName) {
          ProtobufValueWrapperTypeDescriptor sourceType = (ProtobufValueWrapperTypeDescriptorpath.getLastNode().getType();
    		if ( !sourceType.hasPropertypropertyName.getText() ) ) {
    			throw .getNoSuchPropertyException( sourceType.toString(), propertyName.getText() );
    		}
          Descriptors.Descriptor descriptor = sourceType.getMessageDescriptor().findFieldByName(propertyName.getText()).getMessageType();
          PathedPropertyReference property = new PathedPropertyReference(propertyName.getText(), new ProtobufValueWrapperTypeDescriptor(descriptor), false);
    		return property;
    	}
    	public PathedPropertyReferenceSource normalizeIntermediateIndexOperation(
    			PathedPropertyReferenceSource propertyReferenceSource, Tree collectionProperty, Tree selector) {
    		throw new UnsupportedOperationException"Not yet implemented" );
    	}
    			PathedPropertyReferenceSource propertyReferenceSource, Tree collectionProperty, Tree selector) {
    		throw new UnsupportedOperationException"Not yet implemented" );
    	}
    	public PathedPropertyReferenceSource normalizeUnqualifiedPropertyReferenceSource(Tree identifier394) {
    		throw new UnsupportedOperationException"Not yet implemented" );
    	}
    	public PathedPropertyReferenceSource normalizePropertyPathTerminus(PropertyPath path, Tree propertyNameNode) {
    		// receives the property name on a specific entity reference _source_
    		return normalizeProperty( (HSearchTypeDescriptor) path.getLastNode().getType(), path.getNodeNamesWithoutAlias(), propertyNameNode.getText() );
    	}
    	private PathedPropertyReferenceSource normalizeProperty(HSearchTypeDescriptor typeList<StringpathString propertyName) {
    		if ( !type.hasProperty( propertyName ) ) {
    			throw .getNoSuchPropertyException( type.toString(), propertyName );
    		}
    		if (  != . && !type.isEmbedded( propertyName ) && type.isAnalyzed( propertyName ) ) {
    			throw .getQueryOnAnalyzedPropertyNotSupportedException( type.getIndexedEntityType().getCanonicalName(), propertyName );
    		}
    		if ( type.isEmbedded( propertyName ) ) {
             Descriptors.Descriptor descriptor = sourceType.getMessageDescriptor().findFieldByName(propertyName).getMessageType();
             return new PathedPropertyReference(
                   propertyName,
                   new ProtobufValueWrapperTypeDescriptor(descriptor),
                   false);
    		}
    		else {
    			return new PathedPropertyReference(
    					propertyName,
    					new HSearchPropertyTypeDescriptor(),
    					false
    			);
    		}
    	}
    	public void pushFromStrategy(
    			JoinType joinType,
    			Tree assosiationFetchTree,
    			Tree propertyFetchTree,
    			Tree alias) {
    		throw new UnsupportedOperationException"Not yet implemented" );
    	}
    	public void pushSelectStrategy() {
    	}
    	public void popStrategy() {
    		 = null;
    	}
    	public void propertyPathCompleted(PropertyPath path) {
    		if (  == . && path.getLastNode().getType() instanceof HSearchEmbeddedEntityTypeDescriptor ) {
    			HSearchEmbeddedEntityTypeDescriptor type = (HSearchEmbeddedEntityTypeDescriptor) path.getLastNode().getType();
    			throw .getProjectionOfCompleteEmbeddedEntitiesNotSupportedException(
    					type.getIndexedEntityType().getCanonicalName(),
    					path.asStringPathWithoutAlias()
    			);
    		}
    	}
    New to GrepCode? Check out our FAQ X