Пример #1
0
 /**
  * Convenience logging method to output the mapping information for an element, key, value field
  *
  * @param mapping The mapping
  */
 protected void debugMapping(JavaTypeMapping mapping) {
   if (NucleusLogger.DATASTORE.isDebugEnabled()) {
     // Provide field->column mapping debug message
     StringBuffer columnsStr = new StringBuffer();
     for (int i = 0; i < mapping.getNumberOfDatastoreMappings(); i++) {
       if (i > 0) {
         columnsStr.append(",");
       }
       columnsStr.append(mapping.getDatastoreMapping(i).getDatastoreField());
     }
     if (mapping.getNumberOfDatastoreMappings() == 0) {
       columnsStr.append("[none]");
     }
     StringBuffer datastoreMappingTypes = new StringBuffer();
     for (int i = 0; i < mapping.getNumberOfDatastoreMappings(); i++) {
       if (i > 0) {
         datastoreMappingTypes.append(',');
       }
       datastoreMappingTypes.append(mapping.getDatastoreMapping(i).getClass().getName());
     }
     NucleusLogger.DATASTORE.debug(
         LOCALISER.msg(
             "057010",
             mmd.getFullFieldName(),
             columnsStr.toString(),
             mapping.getClass().getName(),
             datastoreMappingTypes.toString()));
   }
 }
  /**
   * Utility to add the mapping for a field/property to the managed list.
   *
   * @param fieldMapping The mapping for the field/property
   */
  protected void addMemberMapping(JavaTypeMapping fieldMapping) {
    AbstractMemberMetaData mmd = fieldMapping.getMemberMetaData();
    logMapping(mmd.getFullFieldName(), fieldMapping);
    memberMappingsMap.put(mmd, fieldMapping);

    // Update highest field number if this is higher
    int absoluteFieldNumber = mmd.getAbsoluteFieldNumber();
    if (absoluteFieldNumber > highestMemberNumber) {
      highestMemberNumber = absoluteFieldNumber;
    }
  }
 /**
  * Accessor for the JavaTypeMapping that is handling the member of the specified name. Returns the
  * first one that matches.
  *
  * @param memberName Name of the field/property
  * @return The java type mapping
  */
 protected JavaTypeMapping getMappingForMemberName(String memberName) {
   Iterator<Map.Entry<AbstractMemberMetaData, JavaTypeMapping>> memberMapEntryIter =
       memberMappingsMap.entrySet().iterator();
   while (memberMapEntryIter.hasNext()) {
     Map.Entry<AbstractMemberMetaData, JavaTypeMapping> memberMapEntry = memberMapEntryIter.next();
     AbstractMemberMetaData mmd = memberMapEntry.getKey();
     if (mmd.getFullFieldName().equals(memberName)) {
       return memberMapEntry.getValue();
     }
   }
   return null;
 }
Пример #4
0
  /**
   * Constructor, taking the meta data for the field, and the table it is mapped to.
   *
   * @param mmd MetaData for the field.
   * @param table Table definition
   */
  public RDBMSStoreData(AbstractMemberMetaData mmd, Table table) {
    super(mmd.getFullFieldName(), mmd, SCO_TYPE, null);

    if (table == null) {
      throw new NullPointerException("table should not be null");
    }
    this.table = table;
    this.tableName = table.toString();
    this.tableOwner = true;
    this.tableIdentifier = table.getIdentifier();

    String interfaceName =
        (table.getStoreManager().getMetaDataManager().isPersistentInterface(mmd.getType().getName())
            ? mmd.getType().getName()
            : null);
    if (interfaceName != null) {
      this.interfaceName = interfaceName;
    }
  }
  /**
   * Utility to create the application identity columns and mapping. Uses the id mapping of the
   * specified class table and copies the mappings and columns, whilst retaining the passed
   * preferences for column namings. This is used to copy the PK mappings of a superclass table so
   * we have the same PK.
   *
   * @param columnContainer The container of column MetaData with any namings
   * @param refTable The table that we use as reference
   * @param clr The ClassLoaderResolver
   * @param cmd The ClassMetaData
   */
  final void addApplicationIdUsingClassTableId(
      ColumnMetaDataContainer columnContainer,
      DatastoreClass refTable,
      ClassLoaderResolver clr,
      AbstractClassMetaData cmd) {
    ColumnMetaData[] userdefinedCols = null;
    int nextUserdefinedCol = 0;
    if (columnContainer != null) {
      userdefinedCols = columnContainer.getColumnMetaData();
    }

    pkMappings = new JavaTypeMapping[cmd.getPKMemberPositions().length];
    for (int i = 0; i < cmd.getPKMemberPositions().length; i++) {
      AbstractMemberMetaData mmd =
          cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
      JavaTypeMapping mapping = refTable.getMemberMapping(mmd);
      if (mapping == null) {
        // probably due to invalid metadata defined by the user
        throw new NucleusUserException(
            "Cannot find mapping for field "
                + mmd.getFullFieldName()
                + " in table "
                + refTable.toString()
                + " "
                + StringUtils.collectionToString(refTable.getColumns()));
      }

      JavaTypeMapping masterMapping =
          storeMgr.getMappingManager().getMapping(clr.classForName(mapping.getType()));
      masterMapping.setMemberMetaData(mmd); // Update field info in mapping
      masterMapping.setTable(this);
      pkMappings[i] = masterMapping;

      // Loop through each id column in the reference table and add the same here
      // applying the required names from the columnContainer
      for (int j = 0; j < mapping.getNumberOfDatastoreMappings(); j++) {
        JavaTypeMapping m = masterMapping;
        Column refColumn = mapping.getDatastoreMapping(j).getColumn();
        if (mapping instanceof PersistableMapping) {
          m =
              storeMgr
                  .getMappingManager()
                  .getMapping(clr.classForName(refColumn.getJavaTypeMapping().getType()));
          ((PersistableMapping) masterMapping).addJavaTypeMapping(m);
        }

        ColumnMetaData userdefinedColumn = null;
        if (userdefinedCols != null) {
          for (int k = 0; k < userdefinedCols.length; k++) {
            if (refColumn.getIdentifier().toString().equals(userdefinedCols[k].getTarget())) {
              userdefinedColumn = userdefinedCols[k];
              break;
            }
          }
          if (userdefinedColumn == null && nextUserdefinedCol < userdefinedCols.length) {
            userdefinedColumn = userdefinedCols[nextUserdefinedCol++];
          }
        }

        // Add this application identity column
        Column idColumn = null;
        if (userdefinedColumn != null) {
          // User has provided a name for this column
          // Currently we only use the column namings from the users definition but we could easily
          // take more of their details.
          idColumn =
              addColumn(
                  refColumn.getStoredJavaType(),
                  storeMgr
                      .getIdentifierFactory()
                      .newIdentifier(IdentifierType.COLUMN, userdefinedColumn.getName()),
                  m,
                  refColumn.getColumnMetaData());
        } else {
          // No name provided so take same as superclass
          idColumn =
              addColumn(
                  refColumn.getStoredJavaType(),
                  refColumn.getIdentifier(),
                  m,
                  refColumn.getColumnMetaData());
        }
        if (mapping.getDatastoreMapping(j).getColumn().getColumnMetaData() != null) {
          refColumn.copyConfigurationTo(idColumn);
        }
        idColumn.setPrimaryKey();

        // Set the column type based on the field.getType()
        getStoreManager()
            .getMappingManager()
            .createDatastoreMapping(m, idColumn, refColumn.getJavaTypeMapping().getType());
      }

      // Update highest field number if this is higher
      int absoluteFieldNumber = mmd.getAbsoluteFieldNumber();
      if (absoluteFieldNumber > highestMemberNumber) {
        highestMemberNumber = absoluteFieldNumber;
      }
    }
  }