/* (non-Javadoc)
   * @see org.grails.orm.hibernate.cfg.GrailsDomainConfiguration#setGrailsApplication(org.codehaus.groovy.grails.commons.GrailsApplication)
   */
  public void setGrailsApplication(GrailsApplication application) {
    grailsApplication = application;
    if (grailsApplication == null) {
      return;
    }

    GrailsClass[] existingDomainClasses =
        grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE);
    for (GrailsClass existingDomainClass : existingDomainClasses) {
      addDomainClass((GrailsDomainClass) existingDomainClass);
    }
  }
  public void afterPropertiesSet() throws Exception {
    if (grailsApplication == null) {
      return;
    }

    String dsName =
        Mapping.DEFAULT_DATA_SOURCE.equals(dataSourceName)
            ? "dataSource"
            : "dataSource_" + dataSourceName;
    getProperties().put(Environment.DATASOURCE, applicationContext.getBean(dsName));
    getProperties()
        .put(Environment.CURRENT_SESSION_CONTEXT_CLASS, GrailsSessionContext.class.getName());
    getProperties().put(AvailableSettings.CLASSLOADERS, grailsApplication.getClassLoader());
    resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(applicationContext);

    configureNamingStrategy();
    GrailsClass[] existingDomainClasses =
        grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE);
    for (GrailsClass existingDomainClass : existingDomainClasses) {
      addDomainClass((GrailsDomainClass) existingDomainClass);
    }

    ArtefactHandler handler = grailsApplication.getArtefactHandler(DomainClassArtefactHandler.TYPE);
    if (!(handler instanceof AnnotationDomainClassArtefactHandler)) {
      return;
    }

    Set<String> jpaDomainNames =
        ((AnnotationDomainClassArtefactHandler) handler).getJpaClassNames();
    if (jpaDomainNames == null) {
      return;
    }

    final ClassLoader loader = grailsApplication.getClassLoader();
    for (String jpaDomainName : jpaDomainNames) {
      try {
        addAnnotatedClass(loader.loadClass(jpaDomainName));
      } catch (ClassNotFoundException e) {
        // impossible condition
      }
    }
  }