All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper Maven / Gradle / Ivy

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 

Contributors:
2004 Erik Bengtson - original version in ClassTable
2005 Andy Jefferson - changed to work from ColumnMetaData and create missing entries
    ...
**********************************************************************/
package org.datanucleus.store.rdbms.mapping;

import java.util.HashMap;
import java.util.Map;

import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.ColumnMetaData;
import org.datanucleus.metadata.ColumnMetaDataContainer;
import org.datanucleus.store.rdbms.identifier.DatastoreIdentifier;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.MultiMapping;
import org.datanucleus.store.rdbms.table.Table;
import org.datanucleus.util.Localiser;

/**
 * Class to make correspondence between columns in one side of an association to the mapping at the other side. 
 * The 2 sides of the association are referred to as "sideA" and "sideB". The JDO metadata allows definition
 * of the correspondence using the <column name="{column-name}" target="{target-name}"/> syntax.
 * 

* This means that the column specified on sideA will be mapped to the specified "target" column on sideB. * If no target is provided then the first available sideB column is used for the mapping. * Where no columns are defined on sideA, then they will be created to match those on sideB. * Checks are made for consistency of the sideA data. * When there is insufficient ColumnMetaData on sideA then a new ColumnMetaData is added to the column container. */ public class CorrespondentColumnsMapper { /** Map of the ColumnMetaData for each column keyed by the sideB identifier name. */ private final Map columnMetaDataBySideBIdentifier = new HashMap(); private final String columnsName; /** * Constructor. * Takes the sideB mapping and the side A definition of column metadata and matches them up as defined by the user, * and if not defined by the user matches them as best it can. This constructor allows specification of the column metadata array * directly, rather than taking what the container has - is used by ColumnCreator where the user has specified multiple columns * but only some of them are for this field being mapped. * @param columnContainer Container of the columns for side A * @param tableA Table on side A * @param colmds MetaData for the columns to be used * @param mappingSideB the mapping in the side B * @param updateContainer Whether to add any missing ColumnMetaData objects to the container */ public CorrespondentColumnsMapper(ColumnMetaDataContainer columnContainer, Table tableA, ColumnMetaData[] colmds, JavaTypeMapping mappingSideB, boolean updateContainer) { // Go through the user-defined columns and allocate them as required if (columnContainer != null && colmds != null) { int noOfUserColumns = colmds.length; // Generate string of user-specified columns for use in diagnostics StringBuilder str = new StringBuilder("Columns ["); for (int i=0;i mappingSideB.getNumberOfDatastoreMappings()) { throw new NucleusUserException(Localiser.msg("020003", columnsName, "" + noOfUserColumns, "" + mappingSideB.getNumberOfDatastoreMappings())).setFatal(); } // Retrieve sideB column names DatastoreIdentifier[] sideBidentifiers = new DatastoreIdentifier[mappingSideB.getNumberOfDatastoreMappings()]; boolean[] sideButilised = new boolean[mappingSideB.getNumberOfDatastoreMappings()]; for (int i = 0; i < mappingSideB.getNumberOfDatastoreMappings(); i++) { sideBidentifiers[i] = mappingSideB.getDatastoreMapping(i).getColumn().getIdentifier(); sideButilised[i] = false; } JavaTypeMapping[] sideBidMappings = ((MultiMapping)mappingSideB).getJavaTypeMapping(); // Allocate the user-defined columns using the sideB list where target column has been defined for (int i = 0; i < noOfUserColumns; i++) { String targetColumnName = colmds[i].getTarget(); if (targetColumnName == null) { // No target column, so try the field String targetFieldName = colmds[i].getTargetMember(); if (targetFieldName != null) { for (int j=0;j mappingSideB.getNumberOfDatastoreMappings()) { throw new NucleusUserException(Localiser.msg("020003", columnsName, "" + noOfUserColumns, "" + mappingSideB.getNumberOfDatastoreMappings())).setFatal(); } // Retrieve sideB column names DatastoreIdentifier[] sideBidentifiers = new DatastoreIdentifier[mappingSideB.getNumberOfDatastoreMappings()]; boolean[] sideButilised = new boolean[mappingSideB.getNumberOfDatastoreMappings()]; for (int i = 0; i < mappingSideB.getNumberOfDatastoreMappings(); i++) { sideBidentifiers[i] = mappingSideB.getDatastoreMapping(i).getColumn().getIdentifier(); sideButilised[i] = false; } JavaTypeMapping[] sideBidMappings = ((MultiMapping)mappingSideB).getJavaTypeMapping(); // Allocate the user-defined columns using the sideB list where target column has been defined for (int i = 0; i < noOfUserColumns; i++) { String targetColumnName = colmds[i].getTarget(); if (targetColumnName == null) { // No target column, so try the field String targetFieldName = colmds[i].getTargetMember(); if (targetFieldName != null) { for (int j=0;j





© 2015 - 2024 Weber Informatics LLC | Privacy Policy