Exemple #1
0
 public JDBCEntityBridge(JDBCEntityMetaData metadata, JDBCStoreManager manager) throws Exception {
   this.metadata = metadata;
   this.manager = manager;
   primaryKeyFieldName = metadata.getPrimaryKeyFieldName();
   primaryKeyClass = metadata.getPrimaryKeyClass();
   log = Logger.getLogger(this.getClass().getName() + "." + metadata.getName());
 }
Exemple #2
0
 private void loadCMRFields(JDBCEntityMetaData metadata) {
   cmrFields = new JDBCCMRFieldBridge[metadata.getRelationshipRoles().size()];
   // create each field
   int cmrFieldIndex = 0;
   for (Iterator iter = metadata.getRelationshipRoles().iterator(); iter.hasNext(); ) {
     JDBCRelationshipRoleMetaData relationshipRole = (JDBCRelationshipRoleMetaData) iter.next();
     JDBCCMRFieldBridge cmrField = new JDBCCMRFieldBridge(this, manager, relationshipRole);
     cmrFields[cmrFieldIndex++] = cmrField;
   }
 }
Exemple #3
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);
  }
Exemple #4
0
 private JDBCCMPFieldBridge createCMPField(
     JDBCEntityMetaData metadata, JDBCCMPFieldMetaData cmpFieldMetaData) {
   JDBCCMPFieldBridge cmpField;
   if (metadata.isCMP1x()) cmpField = new JDBCCMP1xFieldBridge(manager, cmpFieldMetaData);
   else cmpField = new JDBCCMP2xFieldBridge(manager, cmpFieldMetaData);
   return cmpField;
 }
Exemple #5
0
 private void loadEagerLoadGroup(JDBCEntityMetaData metadata) {
   String eagerLoadGroupName = metadata.getEagerLoadGroup();
   if (eagerLoadGroupName == null) {
     // can be null in case of <eager-load-group/>, meaning empty load group
     eagerLoadGroupMask = defaultLockGroupMask;
   } else eagerLoadGroupMask = (boolean[]) loadGroupMasks.get(eagerLoadGroupName);
 }
Exemple #6
0
  private void loadCMPFields(JDBCEntityMetaData metadata) {
    // only non pk fields are stored here at first and then later
    // the pk fields are added to the front (makes sql easier to read)
    List<JDBCCMPFieldMetaData> cmpFieldsMD = metadata.getCMPFields();
    List<JDBCCMPFieldBridge> cmpFieldsList = new ArrayList<JDBCCMPFieldBridge>();
    // primary key cmp fields
    List<JDBCCMPFieldBridge> pkFieldsList = new ArrayList<JDBCCMPFieldBridge>();

    // create pk fields
    for (JDBCCMPFieldMetaData fieldMD : cmpFieldsMD) {
      if (fieldMD.isPrimaryKeyMember()) {
        pkFieldsList.add(createCMPField(metadata, fieldMD));
      }
    }
    for (JDBCCMPFieldMetaData fieldMD : cmpFieldsMD) {
      if (!fieldMD.isPrimaryKeyMember()) {
        cmpFieldsList.add(createCMPField(metadata, fieldMD));
      }
    }

    // save the pk fields in the pk field array
    primaryKeyFields = pkFieldsList.toArray(new JDBCCMPFieldBridge[pkFieldsList.size()]);
    // add the pk fields to the front of the cmp list, per guarantee above
    cmpFields = cmpFieldsList.toArray(new JDBCCMPFieldBridge[cmpFieldsList.size()]);
  }
Exemple #7
0
 private void loadLazyLoadGroups(JDBCEntityMetaData metadata) {
   List lazyGroupNames = metadata.getLazyLoadGroups();
   lazyLoadGroupMasks = new ArrayList(lazyGroupNames.size());
   for (Iterator lazyLoadGroupNames = lazyGroupNames.iterator(); lazyLoadGroupNames.hasNext(); ) {
     String lazyLoadGroupName = (String) lazyLoadGroupNames.next();
     lazyLoadGroupMasks.add(loadGroupMasks.get(lazyLoadGroupName));
   }
   lazyLoadGroupMasks = Collections.unmodifiableList(lazyLoadGroupMasks);
 }
Exemple #8
0
 private void loadSelectors(JDBCEntityMetaData metadata) {
   // Don't know if this is the best way to do this.  Another way would be
   // to delegate selectors to the JDBCFindEntitiesCommand, but this is
   // easier now.
   selectorsByMethod = new HashMap(metadata.getQueries().size());
   Iterator definedFinders = manager.getMetaData().getQueries().iterator();
   while (definedFinders.hasNext()) {
     JDBCQueryMetaData q = (JDBCQueryMetaData) definedFinders.next();
     if (q.getMethod().getName().startsWith("ejbSelect"))
       selectorsByMethod.put(q.getMethod(), new JDBCSelectorBridge(manager, q));
   }
   selectorsByMethod = Collections.unmodifiableMap(selectorsByMethod);
 }
Exemple #9
0
 private boolean[] loadGroupMask(String groupName, boolean[] defaultGroup) {
   List<String> fieldNames = metadata.getLoadGroup(groupName);
   boolean[] group = new boolean[tableFields.length];
   if (defaultGroup != null) System.arraycopy(defaultGroup, 0, group, 0, group.length);
   for (String fieldName : fieldNames) {
     JDBCFieldBridge field = (JDBCFieldBridge) getFieldByName(fieldName);
     if (field == null) throw MESSAGES.fieldNotFound(fieldName, getEntityName());
     if (field instanceof JDBCCMRFieldBridge) {
       JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge) field;
       if (cmrField.hasForeignKey()) {
         JDBCCMPFieldBridge[] fkFields = (JDBCCMPFieldBridge[]) cmrField.getForeignKeyFields();
         for (int i = 0; i < fkFields.length; ++i) {
           group[fkFields[i].getTableIndex()] = true;
         }
       } else {
         throw MESSAGES.onlyCmrFieldsWithFkInLoadGroup(fieldName);
       }
     } else {
       group[((JDBCCMPFieldBridge) field).getTableIndex()] = true;
     }
   }
   return group;
 }
Exemple #10
0
 public int getFetchSize() {
   return metadata.getFetchSize();
 }
Exemple #11
0
 public int getListCacheMax() {
   return metadata.getListCacheMax();
 }
Exemple #12
0
 public Class getLocalInterface() {
   return metadata.getLocalClass();
 }
Exemple #13
0
 public Class getRemoteInterface() {
   return metadata.getRemoteClass();
 }
Exemple #14
0
 public String getAbstractSchemaName() {
   return metadata.getAbstractSchemaName();
 }
Exemple #15
0
 public String getEntityName() {
   return metadata.getName();
 }
Exemple #16
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);
  }