Пример #1
0
 public void injectPrimaryKeyIntoInstance(CmpEntityBeanContext ctx, Object pk) {
   for (int i = 0; i < primaryKeyFields.length; ++i) {
     JDBCCMPFieldBridge pkField = primaryKeyFields[i];
     Object fieldValue = pkField.getPrimaryKeyValue(pk);
     pkField.setInstanceValue(ctx, fieldValue);
   }
 }
Пример #2
0
 private boolean areCmpFieldsDirty(final CmpEntityBeanContext ctx, final EntityState entityState) {
   for (int i = 0; i < tableFields.length; ++i) {
     final JDBCCMPFieldBridge field = tableFields[i];
     if (entityState.isCheckDirty(i) && field.isDirty(ctx)) {
       return true;
     }
   }
   return false;
 }
Пример #3
0
 /**
  * @param name CMP field name
  * @return JDBCCMPFieldBridge instance or null if no field found.
  */
 public JDBCCMPFieldBridge getCMPFieldByName(String name) {
   for (int i = 0; i < primaryKeyFields.length; ++i) {
     JDBCCMPFieldBridge cmpField = primaryKeyFields[i];
     if (cmpField.getFieldName().equals(name)) return cmpField;
   }
   for (int i = 0; i < cmpFields.length; ++i) {
     JDBCCMPFieldBridge cmpField = cmpFields[i];
     if (cmpField.getFieldName().equals(name)) return cmpField;
   }
   return null;
 }
Пример #4
0
  public FieldIterator getDirtyIterator(CmpEntityBeanContext ctx) {
    int dirtyFields = 0;
    final EntityState entityState = getEntityState(ctx);
    for (int i = 0; i < tableFields.length; ++i) {
      JDBCCMPFieldBridge field = tableFields[i];
      if (entityState.isCheckDirty(i) && field.isDirty(ctx)) {
        entityState.setUpdateRequired(i);
        ++dirtyFields;
      }
    }

    return dirtyFields > 0 ? getEntityState(ctx).getDirtyIterator(ctx) : EMPTY_FIELD_ITERATOR;
  }
Пример #5
0
 public FieldBridge getFieldByName(String name) {
   FieldBridge field = null;
   for (int i = 0; i < primaryKeyFields.length; ++i) {
     JDBCCMPFieldBridge primaryKeyField = primaryKeyFields[i];
     if (primaryKeyField.getFieldName().equals(name)) {
       field = primaryKeyField;
       break;
     }
   }
   if (field == null) {
     field = getCMPFieldByName(name);
   }
   if (field == null) {
     field = getCMRFieldByName(name);
   }
   return field;
 }
Пример #6
0
  public FieldIterator getLoadIterator(
      JDBCCMPFieldBridge requiredField, JDBCReadAheadMetaData readahead, CmpEntityBeanContext ctx) {
    boolean[] loadGroup;
    if (requiredField == null) {
      if (readahead != null && !readahead.isNone()) {
        if (log.isTraceEnabled()) {
          log.trace("Eager-load for entity: readahead=" + readahead);
        }
        loadGroup = getLoadGroupMask(readahead.getEagerLoadGroup());
      } else {
        if (log.isTraceEnabled()) {
          log.trace("Default eager-load for entity: readahead=" + readahead);
        }
        loadGroup = eagerLoadGroupMask;
      }
    } else {
      loadGroup = new boolean[tableFields.length];
      int requiredInd = requiredField.getTableIndex();
      loadGroup[requiredInd] = true;
      for (Iterator groups = lazyLoadGroupMasks.iterator(); groups.hasNext(); ) {
        boolean[] lazyGroup = (boolean[]) groups.next();
        if (lazyGroup[requiredInd]) {
          for (int i = 0; i < loadGroup.length; ++i) loadGroup[i] = loadGroup[i] || lazyGroup[i];
        }
      }
    }

    FieldIterator loadIter;
    if (loadGroup != null) {
      // filter
      int fieldsToLoad = 0;
      EntityState entityState = getEntityState(ctx);
      for (int i = 0; i < tableFields.length; ++i) {
        JDBCCMPFieldBridge field = tableFields[i];
        if (loadGroup[i] && !field.isPrimaryKeyMember() && !field.isLoaded(ctx)) {
          entityState.setLoadRequired(i);
          ++fieldsToLoad;
        }
      }
      loadIter = (fieldsToLoad > 0 ? entityState.getLoadIterator(ctx) : EMPTY_FIELD_ITERATOR);
    } else {
      loadIter = EMPTY_FIELD_ITERATOR;
    }
    return loadIter;
  }
Пример #7
0
  public Object extractPrimaryKeyFromInstance(CmpEntityBeanContext ctx) {
    try {
      Object pk = null;
      for (int i = 0; i < primaryKeyFields.length; ++i) {
        JDBCCMPFieldBridge pkField = primaryKeyFields[i];
        Object fieldValue = pkField.getInstanceValue(ctx);

        // updated pk object with return form set primary key value to
        // handle single valued non-composite pks and more complicated behivors.
        pk = pkField.setPrimaryKeyValue(pk, fieldValue);
      }
      return pk;
    } catch (EJBException e) {
      // to avoid double wrap of EJBExceptions
      throw e;
    } catch (Exception e) {
      // Non recoverable internal exception
      throw MESSAGES.errorExtractingPk(e);
    }
  }
Пример #8
0
  private void loadLoadGroups(JDBCEntityMetaData metadata) {
    loadGroupMasks = new HashMap();

    // load optimistic locking mask and add it to all the load group masks
    JDBCOptimisticLockingMetaData olMD = metadata.getOptimisticLocking();
    if (olMD != null) {
      if (versionField != null) {
        defaultLockGroupMask = new boolean[tableFields.length];
        defaultLockGroupMask[versionField.getTableIndex()] = true;
        versionField.setLockingStrategy(LockingStrategy.VERSION);
      } else if (olMD.getGroupName() != null) {
        defaultLockGroupMask = loadGroupMask(olMD.getGroupName(), null);
        for (int i = 0; i < tableFields.length; ++i) {
          if (defaultLockGroupMask[i]) {
            JDBCCMPFieldBridge tableField = tableFields[i];
            tableField.setLockingStrategy(LockingStrategy.GROUP);
            tableField.addDefaultFlag(ADD_TO_WHERE_ON_UPDATE);
          }
        }
      } else { // read or modified strategy
        LockingStrategy strategy =
            (olMD.getLockingStrategy()
                    == JDBCOptimisticLockingMetaData.LockingStrategy.READ_STRATEGY
                ? LockingStrategy.READ
                : LockingStrategy.MODIFIED);
        for (int i = 0; i < tableFields.length; ++i) {
          JDBCCMPFieldBridge field = tableFields[i];
          if (!field.isPrimaryKeyMember()) field.setLockingStrategy(strategy);
        }
      }
    }

    // add the * load group
    boolean[] defaultLoadGroup = new boolean[tableFields.length];
    Arrays.fill(defaultLoadGroup, true);
    for (int i = 0; i < primaryKeyFields.length; ++i) {
      int tableIndex = primaryKeyFields[i].getTableIndex();
      defaultLoadGroup[tableIndex] = false;
    }
    loadGroupMasks.put(DEFAULT_LOADGROUP_NAME, defaultLoadGroup);

    // put each group in the load groups map by group name
    Iterator groupNames = metadata.getLoadGroups().keySet().iterator();
    while (groupNames.hasNext()) {
      // get the group name
      String groupName = (String) groupNames.next();
      boolean[] loadGroup = loadGroupMask(groupName, defaultLockGroupMask);
      loadGroupMasks.put(groupName, loadGroup);
    }
    loadGroupMasks = Collections.unmodifiableMap(loadGroupMasks);
  }
Пример #9
0
  public void init() throws Exception {
    dataSource = manager.getDataSource(metadata.getDataSourceName());
    qualifiedTableName = SQLUtil.fixTableName(metadata.getDefaultTableName(), dataSource);
    int dotIndex = qualifiedTableName.indexOf('.');
    tableName = dotIndex == -1 ? qualifiedTableName : qualifiedTableName.substring(dotIndex + 1);

    // CMP fields
    loadCMPFields(metadata);

    // CMR fields
    loadCMRFields(metadata);

    // create locking field
    JDBCOptimisticLockingMetaData lockMetaData = metadata.getOptimisticLocking();
    if (lockMetaData != null && lockMetaData.getLockingField() != null) {
      JDBCOptimisticLockingMetaData.LockingStrategy strategy = lockMetaData.getLockingStrategy();
      JDBCCMPFieldMetaData versionMD = lockMetaData.getLockingField();

      versionField = getCMPFieldByName(versionMD.getFieldName());
      boolean hidden = versionField == null;
      switch (strategy) {
        case VERSION_COLUMN_STRATEGY:
          {
            if (hidden) versionField = new JDBCLongVersionFieldBridge(manager, versionMD);
            else versionField = new JDBCLongVersionFieldBridge((JDBCCMP2xFieldBridge) versionField);
            break;
          }
        case TIMESTAMP_COLUMN_STRATEGY:
          {
            if (hidden) versionField = new JDBCTimestampVersionFieldBridge(manager, versionMD);
            else
              versionField =
                  new JDBCTimestampVersionFieldBridge((JDBCCMP2xFieldBridge) versionField);
            break;
          }
        case KEYGENERATOR_COLUMN_STRATEGY:
          {
            if (hidden)
              versionField =
                  new JDBCKeyGenVersionFieldBridge(
                      manager, versionMD, lockMetaData.getKeyGeneratorFactory());
            else
              versionField =
                  new JDBCKeyGenVersionFieldBridge(
                      (JDBCCMP2xFieldBridge) versionField, lockMetaData.getKeyGeneratorFactory());
            break;
          }
      }
      if (hidden) addCMPField(versionField);
      else tableFields[versionField.getTableIndex()] = versionField;
    }

    // audit fields
    JDBCAuditMetaData auditMetaData = metadata.getAudit();
    if (auditMetaData != null) {
      JDBCCMPFieldMetaData auditField = auditMetaData.getCreatedPrincipalField();
      if (auditField != null) {
        createdPrincipalField = getCMPFieldByName(auditField.getFieldName());
        if (createdPrincipalField == null) {
          createdPrincipalField = new JDBCCMP2xFieldBridge(manager, auditField);
          addCMPField(createdPrincipalField);
        }
      } else {
        createdPrincipalField = null;
      }

      auditField = auditMetaData.getCreatedTimeField();
      if (auditField != null) {
        createdTimeField = getCMPFieldByName(auditField.getFieldName());
        if (createdTimeField == null) {
          createdTimeField =
              new JDBCCMP2xFieldBridge(manager, auditField, JDBCTypeFactory.EQUALS, false);
          addCMPField(createdTimeField);
        } else {
          // just to override state factory and check-dirty-after-get
          createdTimeField =
              new JDBCCMP2xFieldBridge(
                  (JDBCCMP2xFieldBridge) createdTimeField, JDBCTypeFactory.EQUALS, false);
          tableFields[createdTimeField.getTableIndex()] = createdTimeField;
        }
      } else {
        createdTimeField = null;
      }

      auditField = auditMetaData.getUpdatedPrincipalField();
      if (auditField != null) {
        updatedPrincipalField = getCMPFieldByName(auditField.getFieldName());
        if (updatedPrincipalField == null) {
          updatedPrincipalField = new JDBCCMP2xUpdatedPrincipalFieldBridge(manager, auditField);
          addCMPField(updatedPrincipalField);
        } else {
          updatedPrincipalField =
              new JDBCCMP2xUpdatedPrincipalFieldBridge(
                  (JDBCCMP2xFieldBridge) updatedPrincipalField);
          tableFields[updatedPrincipalField.getTableIndex()] = updatedPrincipalField;
        }
      } else {
        updatedPrincipalField = null;
      }

      auditField = auditMetaData.getUpdatedTimeField();
      if (auditField != null) {
        updatedTimeField = getCMPFieldByName(auditField.getFieldName());
        if (updatedTimeField == null) {
          updatedTimeField = new JDBCCMP2xUpdatedTimeFieldBridge(manager, auditField);
          addCMPField(updatedTimeField);
        } else {
          updatedTimeField =
              new JDBCCMP2xUpdatedTimeFieldBridge((JDBCCMP2xFieldBridge) updatedTimeField);
          tableFields[updatedTimeField.getTableIndex()] = updatedTimeField;
        }
      } else {
        updatedTimeField = null;
      }
    }

    // ejbSelect methods
    loadSelectors(metadata);
  }