Ejemplo n.º 1
0
  @Override
  public void complete() {
    AmberPersistenceUnit persistenceUnit = _sourceType.getPersistenceUnit();

    Class targetClass = getTargetClass();

    if (targetClass == null || void.class.equals(targetClass))
      throw error(
          _field,
          L.l(
              "Can't determine targetEntity for {0}.  @OneToMany properties must target @Entity beans.",
              _fieldName));

    AmberType targetType = persistenceUnit.createType(targetClass);

    if (targetType == null) {
      throw error(
          _field,
          L.l(
              "targetClass '{0}' is not a known element collection class for {1}.  The targetClass of a @ElementCollection must be a basic class.",
              targetClass.getName(), _fieldName));
    }

    /*
    if (_orderBy != null)
      calculateOrderBy(_orderBy);
    */

    addCollection(targetType);
  }
  private EntityType introspectParent(Class parentClass) throws SQLException {
    if (parentClass == null) return null;

    getInternalEntityConfig(parentClass, _annotationCfg);

    if (!_annotationCfg.isNull()) return (EntityType) _configManager.introspect(parentClass);
    else if (parentClass.isAnnotationPresent(javax.persistence.Entity.class))
      return (EntityType) _configManager.introspect(parentClass);
    else if (parentClass.isAnnotationPresent(javax.persistence.MappedSuperclass.class))
      return (EntityType) _configManager.introspect(parentClass);
    else return null;
  }
  private EntityType introspectEntityType(Class type, EntityType parentType) throws SQLException {
    getInternalEntityConfig(type, _annotationCfg);

    Entity entityAnn = (Entity) _annotationCfg.getAnnotation();
    EntityConfig entityConfig = _annotationCfg.getEntityConfig();

    boolean isEntity = !_annotationCfg.isNull();

    if (!isEntity) return null;

    EntityType entityType;
    String typeName;

    if (entityConfig != null) typeName = entityConfig.getClassName();
    else typeName = entityAnn.name();

    // Validates the type
    String entityName;
    Inheritance inheritanceAnn = null;
    InheritanceConfig inheritanceConfig = null;
    Class rootClass = type;
    Entity rootEntityAnn = null;
    EntityConfig rootEntityConfig = null;

    validateType(type, true);

    // jpa/0ge2
    // if (hasInheritance) {

    if (entityConfig == null) entityName = entityAnn.name();
    else {
      entityName = entityConfig.getClassName();

      int p = entityName.lastIndexOf('.');

      if (p > 0) entityName = entityName.substring(p + 1);
    }

    if ((entityName == null) || "".equals(entityName)) {
      entityName = type.getSimpleName();
    }

    entityType = _persistenceUnit.createEntity(entityName, type);

    _configManager.addType(type, new EntityConfig(type.getName(), this, entityType));

    boolean isField = isField(type, entityConfig, false);

    if (isField) entityType.setFieldAccess(true);

    return entityType;
  }
  /** Introspects. */
  private EmbeddableType introspectEmbeddableType(Class type) throws ConfigException, SQLException {
    getInternalEmbeddableConfig(type, _annotationCfg);
    Embeddable embeddableAnn = (Embeddable) _annotationCfg.getAnnotation();
    EmbeddableConfig embeddableConfig = _annotationCfg.getEmbeddableConfig();

    /*
    if (_annotationCfg.isNull())
      return null;
    */

    String typeName = type.getName();
    EmbeddableType embeddableType = _persistenceUnit.createEmbeddable(typeName, type);

    _configManager.addType(type, new EmbeddableConfig(type.getName(), this, embeddableType));

    try {
      boolean isField = isField(type, embeddableConfig);

      if (isField) embeddableType.setFieldAccess(true);

      // XXX: jpa/0u21
      Embeddable ann = (Embeddable) type.getAnnotation(javax.persistence.Embeddable.class);

      if (ann == null) {
        isField = true;
        embeddableType.setIdClass(true);

        _persistenceUnit.getAmberContainer().addEmbeddable(typeName, embeddableType);
      }

      embeddableType.setInstanceClassName(type.getName() + "__ResinExt");
      embeddableType.setEnhanced(true);

      if (isField)
        introspectFields(_persistenceUnit, embeddableType, null, type, embeddableConfig, true);
      else introspectMethods(_persistenceUnit, embeddableType, null, type, embeddableConfig);

    } catch (ConfigException e) {
      if (embeddableType != null) embeddableType.setConfigException(e);

      throw e;
    } catch (RuntimeException e) {
      if (embeddableType != null) embeddableType.setConfigException(e);

      throw e;
    }

    return embeddableType;
  }
  private void introspectTableCache(EntityType entityType, Class type) {
    AmberTableCache tableCache = (AmberTableCache) type.getAnnotation(AmberTableCache.class);

    if (tableCache != null) {
      entityType.getTable().setReadOnly(tableCache.readOnly());

      long cacheTimeout = Period.toPeriod(tableCache.timeout());
      entityType.getTable().setCacheTimeout(cacheTimeout);
    }
  }
  boolean isField(Class type, AbstractEnhancedConfig typeConfig) throws ConfigException {
    for (Method method : type.getDeclaredMethods()) {
      Annotation ann[] = method.getDeclaredAnnotations();

      for (int i = 0; ann != null && i < ann.length; i++) {
        if (ann[i] instanceof Basic || ann[i] instanceof Column) return false;
      }
    }

    return true;
  }
  private void introspectDiscriminatorValue(Class type, EntityType entityType) {
    DiscriminatorValue discValueAnn =
        (DiscriminatorValue) type.getAnnotation(DiscriminatorValue.class);

    String discriminatorValue = null;

    if (discValueAnn != null) discriminatorValue = discValueAnn.value();

    if (discriminatorValue == null || discriminatorValue.equals("")) {
      String name = entityType.getBeanClass().getSimpleName();

      discriminatorValue = name;
    }

    entityType.setDiscriminatorValue(discriminatorValue);
  }
  /** Introspects. */
  public BeanType introspect(Class type) throws ConfigException, SQLException {
    BeanType beanType = null;

    try {
      EntityType entityType = null;

      EntityType parentType = introspectParent(type.getSuperclass());

      entityType = introspectEntityType(type, parentType);

      if (entityType == null) entityType = introspectMappedType(type, parentType);

      if (entityType == null) return introspectEmbeddableType(type);

      beanType = entityType;

      // jpa/0ge2
      entityType.setInstanceClassName(type.getName() + "__ResinExt");
      entityType.setEnhanced(true);

      MappedSuperclassConfig mappedSuperOrEntityConfig = introspectEntityConfig(type);

      entityType.setParentType(parentType);

      introspectTable(type, entityType, parentType);

      // inheritance must be after table since it adds columns
      introspectInheritance(type, entityType, parentType);

      introspectTableCache(entityType, type);

      getInternalIdClassConfig(type, _annotationCfg);
      IdClass idClassAnn = (IdClass) _annotationCfg.getAnnotation();
      IdClassConfig idClassConfig = _annotationCfg.getIdClassConfig();

      Class idClass = null;
      if (!_annotationCfg.isNull()) {
        if (idClassAnn != null) idClass = idClassAnn.value();
        else {
          String s = idClassConfig.getClassName();
          idClass = _persistenceUnit.loadTempClass(s);
        }

        // XXX: temp. introspects idClass as an embeddable type.
        _persistenceUnit.addEntityClass(idClass.getName(), idClass);

        // jpa/0i49 vs jpa/0i40
        // embeddable.setFieldAccess(isField);
      }

      if (entityType.getId() != null) {
      } else if (entityType.isFieldAccess())
        introspectIdField(
            _persistenceUnit, entityType, parentType, type, idClass, mappedSuperOrEntityConfig);
      else {
        introspectIdMethod(
            _persistenceUnit, entityType, parentType, type, idClass, mappedSuperOrEntityConfig);
      }

      HashMap<String, IdConfig> idMap = null;

      AttributesConfig attributes = null;

      if (mappedSuperOrEntityConfig != null) {
        attributes = mappedSuperOrEntityConfig.getAttributes();

        if (attributes != null) idMap = attributes.getIdMap();
      }

      // if ((idMap == null) || (idMap.size() == 0)) {
      //   idMap = entityType.getSuperClass();
      // }

      if (entityType.isEntity()
          && (entityType.getId() == null)
          && ((idMap == null) || (idMap.size() == 0)))
        throw new ConfigException(
            L.l(
                "{0} does not have any primary keys.  Entities must have at least one @Id or exactly one @EmbeddedId field.",
                entityType.getName()));

      // Introspect overridden attributes. (jpa/0ge2)
      introspectAttributeOverrides(entityType, type);

      introspectSecondaryTable(entityType, type);

      if (entityType.isFieldAccess())
        introspectFields(
            _persistenceUnit, entityType, parentType, type, mappedSuperOrEntityConfig, false);
      else
        introspectMethods(
            _persistenceUnit, entityType, parentType, type, mappedSuperOrEntityConfig);

      introspectCallbacks(type, entityType);

      // Adds entity listeners, if any.
      introspectEntityListeners(type, entityType, _persistenceUnit);

      // Adds sql result set mappings, if any.
      introspectSqlResultSetMappings(type, entityType, entityType.getName());

      // Adds named queries, if any.
      introspectNamedQueries(type, entityType.getName());
      introspectNamedNativeQueries(type, entityType.getName());
    } catch (ConfigException e) {
      if (beanType != null) beanType.setConfigException(e);

      throw e;
    } catch (SQLException e) {
      if (beanType != null) beanType.setConfigException(e);

      throw e;
    } catch (RuntimeException e) {
      if (beanType != null) beanType.setConfigException(e);

      throw e;
    }

    return beanType;
  }
  private void introspectAttributeOverrides(EntityType entityType, Class type) {
    EntityType parent = entityType.getParentType();

    if (parent == null) return;

    boolean isAbstract = Modifier.isAbstract(parent.getBeanClass().getModifiers());

    if (parent.isEntity() && !isAbstract) return;

    HashMap<String, ColumnConfig> overrideMap = new HashMap<String, ColumnConfig>();

    getInternalAttributeOverrideConfig(type, _annotationCfg);
    AttributeOverride attributeOverrideAnn = (AttributeOverride) _annotationCfg.getAnnotation();

    boolean hasAttributeOverride = (attributeOverrideAnn != null);

    AttributeOverrides attributeOverridesAnn =
        (AttributeOverrides) type.getAnnotation(AttributeOverrides.class);

    ArrayList<AttributeOverrideConfig> attributeOverrideList = null;

    EntityConfig entityConfig = getEntityConfig(type.getName());

    if (entityConfig != null) attributeOverrideList = entityConfig.getAttributeOverrideList();

    boolean hasAttributeOverrides = false;

    if ((attributeOverrideList != null) && (attributeOverrideList.size() > 0)) {
      hasAttributeOverrides = true;
    } else if (attributeOverridesAnn != null) hasAttributeOverrides = true;

    if (hasAttributeOverride && hasAttributeOverrides)
      throw new ConfigException(
          L.l("{0} may not have both @AttributeOverride and @AttributeOverrides", type));

    if (attributeOverrideList == null)
      attributeOverrideList = new ArrayList<AttributeOverrideConfig>();

    if (hasAttributeOverride) {
      // Convert annotation to configuration.

      AttributeOverrideConfig attOverrideConfig =
          convertAttributeOverrideAnnotationToConfig(attributeOverrideAnn);

      attributeOverrideList.add(attOverrideConfig);
    } else if (hasAttributeOverrides) {
      if (attributeOverrideList.size() > 0) {
        // OK: attributeOverrideList came from orm.xml
      } else {
        // Convert annotations to configurations.

        AttributeOverride attOverridesAnn[] = attributeOverridesAnn.value();

        AttributeOverrideConfig attOverrideConfig;

        /* XXX:
        for (int i = 0; i < attOverridesAnn.length; i++) {
          attOverrideConfig
            = convertAttributeOverrideAnnotationToConfig((JAnnotation) attOverridesAnn[i]);

          attributeOverrideList.add(attOverrideConfig);
        }
                * */
      }
    }

    for (AttributeOverrideConfig override : attributeOverrideList) {
      overrideMap.put(override.getName(), override.getColumn());
    }

    _depCompletions.add(new AttributeOverrideCompletion(this, entityType, type, overrideMap));
  }
  private EntityType introspectMappedType(Class type, EntityType parentType) throws SQLException {
    EntityType entityType;

    boolean isMappedSuperclass = false;
    MappedSuperclass mappedSuperAnn = null;
    MappedSuperclassConfig mappedSuperConfig = null;

    String typeName;

    MappedSuperclassConfig mappedSuperOrEntityConfig = null;

    getInternalMappedSuperclassConfig(type, _annotationCfg);
    mappedSuperAnn = (MappedSuperclass) _annotationCfg.getAnnotation();
    mappedSuperConfig = _annotationCfg.getMappedSuperclassConfig();

    isMappedSuperclass = !_annotationCfg.isNull();

    if (!isMappedSuperclass) return null;

    mappedSuperOrEntityConfig = mappedSuperConfig;

    if (mappedSuperConfig != null) typeName = mappedSuperConfig.getClassName();
    /*    else
          typeName = mappedSuperAnn.name();
    */
    // Validates the type
    String entityName;
    Inheritance inheritanceAnn = null;
    InheritanceConfig inheritanceConfig = null;
    Class rootClass = type;
    Entity rootEntityAnn = null;
    EntityConfig rootEntityConfig = null;

    validateType(type, false);

    // jpa/0ge2
    // if (hasInheritance) {

    if (mappedSuperConfig == null) entityName = null; // mappedSuperAnn.name();
    else {
      entityName = mappedSuperConfig.getSimpleClassName();
    }

    if ((entityName == null) || "".equals(entityName)) {
      entityName = type.getSimpleName();
    }

    entityType = _persistenceUnit.createMappedSuperclass(entityName, type);

    _configManager.addType(type, new EntityConfig(type.getName(), this, entityType));

    boolean isField = isField(type, mappedSuperOrEntityConfig, false);

    if (isField) entityType.setFieldAccess(true);

    // jpa/0ge2
    entityType.setInstanceClassName(type.getName() + "__ResinExt");
    entityType.setEnhanced(true);

    return entityType;
  }