Open Source Repository

Home /hibernate/hibernate-3.2.6.ga | Repository Home



org/hibernate/cfg/NamedSQLQuerySecondPass.java
//$Id: NamedSQLQuerySecondPass.java 10194 2006-08-03 07:53:09Z [email protected] $
package org.hibernate.cfg;

import java.util.Map;
import java.util.ArrayList;
import java.util.Iterator;

import org.hibernate.MappingException;
import org.hibernate.util.StringHelper;
import org.hibernate.engine.NamedSQLQueryDefinition;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 @author Emmanuel Bernard
 */
public class NamedSQLQuerySecondPass extends ResultSetMappingBinder implements QuerySecondPass {
  private static Log log = LogFactory.getLogNamedSQLQuerySecondPass.class);
  private Element queryElem;
  private String path;
  private Mappings mappings;

  public NamedSQLQuerySecondPass(Element queryElem, String path, Mappings mappings) {
    this.queryElem = queryElem;
    this.path = path;
    this.mappings = mappings;
  }

  public void doSecondPass(Map persistentClassesthrows MappingException {
    String queryName = queryElem.attribute"name" ).getValue();
    if (path!=nullqueryName = path + '.' + queryName;

    boolean cacheable = "true".equalsqueryElem.attributeValue"cacheable" ) );
    String region = queryElem.attributeValue"cache-region" );
    Attribute tAtt = queryElem.attribute"timeout" );
    Integer timeout = tAtt == null null new IntegertAtt.getValue() );
    Attribute fsAtt = queryElem.attribute"fetch-size" );
    Integer fetchSize = fsAtt == null null new IntegerfsAtt.getValue() );
    Attribute roAttr = queryElem.attribute"read-only" );
    boolean readOnly = roAttr != null && "true".equalsroAttr.getValue() );
    Attribute cacheModeAtt = queryElem.attribute"cache-mode" );
    String cacheMode = cacheModeAtt == null null : cacheModeAtt.getValue();
    Attribute cmAtt = queryElem.attribute"comment" );
    String comment = cmAtt == null null : cmAtt.getValue();

    java.util.List synchronizedTables = new ArrayList();
    Iterator tables = queryElem.elementIterator"synchronize" );
    while tables.hasNext() ) {
      synchronizedTables.add( ( (Elementtables.next() ).attributeValue"table" ) );
    }
    boolean callable = "true".equalsqueryElem.attributeValue"callable" ) );

    NamedSQLQueryDefinition namedQuery;
    Attribute ref = queryElem.attribute"resultset-ref" );
    String resultSetRef = ref == null null : ref.getValue();
    if StringHelper.isNotEmptyresultSetRef ) ) {
      namedQuery = new NamedSQLQueryDefinition(
          queryElem.getText(),
          resultSetRef,
          synchronizedTables,
          cacheable,
          region,
          timeout,
          fetchSize,
          HbmBinder.getFlushModequeryElem.attributeValue"flush-mode" ) ),
          HbmBinder.getCacheModecacheMode ),
          readOnly,
          comment,
          HbmBinder.getParameterTypesqueryElem ),
          callable
      );
      //TODO check there is no actual definition elemnents when a ref is defined
    }
    else {
      ResultSetMappingDefinition definition = buildResultSetMappingDefinitionqueryElem, path, mappings );
      namedQuery = new NamedSQLQueryDefinition(
          queryElem.getText(),
          definition.getQueryReturns(),
          synchronizedTables,
          cacheable,
          region,
          timeout,
          fetchSize,
          HbmBinder.getFlushModequeryElem.attributeValue"flush-mode" ) ),
          HbmBinder.getCacheModecacheMode ),
          readOnly,
          comment,
          HbmBinder.getParameterTypesqueryElem ),
          callable
      );
    }

    log.debug"Named SQL query: " + queryName + " -> " + namedQuery.getQueryString() );
    mappings.addSQLQueryqueryName, namedQuery );
  }
}