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

org.datanucleus.metadata.MetaDataMerger Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2007 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:
    ...
**********************************************************************/
package org.datanucleus.metadata;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;

/**
 * Convenience class to handle the merging of MetaData.
 * This is used in the following situations
 * 
    *
  • JDO : Merging ORM MetaData into JDO MetaData
  • *
  • JPA : Merging Annotations information into JPA MetaData
  • *
  • JDO : Merging Annotations information into JDO MetaData
  • *
*/ public class MetaDataMerger { /** * Method to take a file JDO MetaData definition and merge in the ORM MetaData definition. * If something is specified in the JDO MetaData and also in the ORM MetaData then the ORM MetaData takes precedence. * @param primaryFmd The JDO Field definition (to be updated) * @param ormFmd The ORM Field definition (to be merged into the JDO Field definition) * @throws NucleusException if an error occurs while merging the ORM info */ public static void mergeFileORMData(FileMetaData primaryFmd, FileMetaData ormFmd) { if (ormFmd == null || primaryFmd == null) { return; } if (primaryFmd.isInitialised() || primaryFmd.isPopulated()) { throw new NucleusException(Localiser.msg("MetaData.File.AlreadyPopulatedError", primaryFmd.getFilename())).setFatal(); } if (NucleusLogger.METADATA.isDebugEnabled()) { NucleusLogger.METADATA.debug(Localiser.msg("044056", primaryFmd.getFilename())); } if (ormFmd.getCatalog() != null) { primaryFmd.setCatalog(ormFmd.getCatalog()); } if (ormFmd.getSchema() != null) { primaryFmd.setSchema(ormFmd.getSchema()); } } /** * Method to take a class JDO MetaData definition and merge in the ORM MetaData definition. * If something is specified in the JDO MetaData and also in the ORM MetaData then the ORM MetaData takes precedence. * This is tied pretty intrinsically to the AbstractClassMetaData class and so could have been included there. * @param primaryCmd The JDO Class definition (to be updated) * @param ormCmd The ORM Class definition (to be merged into the JDO Class definition) * @param mmgr MetaData manager * @throws NucleusException if an error occurs while merging the ORM info */ public static void mergeClassORMData(AbstractClassMetaData primaryCmd, AbstractClassMetaData ormCmd, MetaDataManager mmgr) { if (ormCmd == null || primaryCmd == null) { return; } if (primaryCmd.isInitialised() || primaryCmd.isPopulated()) { throw new NucleusException(Localiser.msg("044068", primaryCmd.name)).setFatal(); } if (NucleusLogger.METADATA.isDebugEnabled()) { NucleusLogger.METADATA.debug(Localiser.msg("044096", primaryCmd.getFullClassName())); } // Merge the attributes where they are set in the ORM // A). Simple data if (ormCmd.getCatalog() != null) { primaryCmd.catalog = ormCmd.getCatalog(); } if (ormCmd.getSchema() != null) { primaryCmd.schema = ormCmd.getSchema(); } if (ormCmd.getTable() != null) { primaryCmd.table = ormCmd.getTable(); } if (ormCmd.detachable) { primaryCmd.detachable = true; } if (!ormCmd.requiresExtent) { primaryCmd.requiresExtent = false; } if (ormCmd.embeddedOnly) { primaryCmd.embeddedOnly = true; } // B). Object data. Assume that if it exists at all we copy it all if (ormCmd.getPrimaryKeyMetaData() != null) { primaryCmd.setPrimaryKeyMetaData(ormCmd.getPrimaryKeyMetaData()); } if (ormCmd.getInheritanceMetaData() != null) { primaryCmd.setInheritanceMetaData(ormCmd.getInheritanceMetaData()); } if (ormCmd.getIdentityMetaData() != null) { primaryCmd.setIdentityMetaData(ormCmd.getIdentityMetaData()); } if (ormCmd.getVersionMetaData() != null) { primaryCmd.setVersionMetaData(ormCmd.getVersionMetaData()); } if (ormCmd.listeners != null) { if (primaryCmd.listeners == null) { primaryCmd.listeners = new ArrayList(); } primaryCmd.listeners.addAll(ormCmd.listeners); } if (ormCmd.queries != null) { if (primaryCmd.queries == null) { primaryCmd.queries = new ArrayList(); } else { primaryCmd.queries.clear(); } primaryCmd.queries.addAll(ormCmd.queries); } if (!ormCmd.joins.isEmpty()) { primaryCmd.joins.clear(); Iterator iter = ormCmd.joins.iterator(); while (iter.hasNext()) { primaryCmd.addJoin((JoinMetaData)iter.next()); } } if (!ormCmd.indexes.isEmpty()) { primaryCmd.indexes.clear(); Iterator iter = ormCmd.indexes.iterator(); while (iter.hasNext()) { primaryCmd.addIndex((IndexMetaData)iter.next()); } } if (!ormCmd.foreignKeys.isEmpty()) { primaryCmd.foreignKeys.clear(); Iterator iter = ormCmd.foreignKeys.iterator(); while (iter.hasNext()) { primaryCmd.addForeignKey((ForeignKeyMetaData)iter.next()); } } if (!ormCmd.uniqueConstraints.isEmpty()) { primaryCmd.uniqueConstraints.clear(); Iterator iter = ormCmd.uniqueConstraints.iterator(); while (iter.hasNext()) { primaryCmd.addUniqueConstraint((UniqueMetaData)iter.next()); } } if (!ormCmd.fetchGroups.isEmpty()) { primaryCmd.fetchGroups.clear(); Iterator iter = ormCmd.fetchGroups.iterator(); while (iter.hasNext()) { primaryCmd.addFetchGroup((FetchGroupMetaData)iter.next()); } } if (ormCmd.unmappedColumns != null) { primaryCmd.unmappedColumns = null; Iterator iter = ormCmd.unmappedColumns.iterator(); while (iter.hasNext()) { primaryCmd.addUnmappedColumn(iter.next()); } } // C). Add on any fields that weren't defined previously for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy