use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.
the class DatabaseGenerator method createMappingClass.
/**
* Create mapping class and associated table and PC class
* @param pcClass PC class that mapping class associated
* @param table table element that mapping class associated
* @return MappingClassElement associated with table and PC class
* @throws ModelException
*/
private MappingClassElement createMappingClass(PersistenceClassElement pcClass, TableElement table) throws ModelException {
MappingClassElement mappingClass = new MappingClassElementImpl(pcClass);
mappingClass.setDatabaseRoot(schema);
mappingClass.addTable(table);
return mappingClass;
}
use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.
the class DatabaseGenerator method addRelationships.
/**
* Generate relationships for schema and mapping model from
* mappingClasses which already have all mapping fields populated.
* @throws DBException
* @throws ModelExpception
*/
private void addRelationships() throws DBException, ModelException {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("add relationship");
}
Map relationFKey = new HashMap();
// This is a list of 1-1 relationships that are deferred for
// processing until all other relationships are processed. Deferral
// allows us to concentrate foreign keys on one side of the
// relationship.
List deferredRelationships = new ArrayList();
for (Iterator i = mappingClasses.values().iterator(); i.hasNext(); ) {
MappingClassElement mappingClass = (MappingClassElement) i.next();
String pcClassName = mappingClass.getName();
PersistenceClassElement pcClass = model.getPersistenceClass(pcClassName);
// NOI18N
validateModel(pcClass, "pcClass", pcClassName);
TableElement sourceTable = getPrimaryTable(mappingClass);
// NOI18N
validateModel(sourceTable, "sourceTable", pcClassName);
// Create a string that can keep names unique
String uniqueId = getShortClassName(pcClassName);
// Ideally, take this many chars from end
int want = 8;
int end = uniqueId.length();
int start = want > end ? 0 : end - want;
uniqueId = uniqueId.substring(start, end);
RelationshipElement[] rels = pcClass.getRelationships();
if (rels != null) {
for (int j = 0; j < rels.length; j++) {
// relationship
RelationshipElement relation = rels[j];
String relationName = relation.getName();
int upperBound = relation.getUpperBound();
// inverseRelationship
String inverseRelName = relation.getInverseRelationshipName();
validateModel(inverseRelName, "inverseRelName", // NOI18N
relationName);
String relClassName = model.getRelatedClass(relation);
validateModel(relClassName, "relClassName", // NOI18N
relationName);
// get related MappingClass and PersistenceClass
MappingClassElement relMappingClass = (MappingClassElement) mappingClasses.get(relClassName);
validateModel(relMappingClass, "relMappingClass", // NOI18N
relClassName);
PersistenceClassElement relClass = model.getPersistenceClass(relClassName);
validateModel(relClass, "relClass", // NOI18N
relClassName);
RelationshipElement inverseRelation = relClass.getRelationship(inverseRelName);
validateModel(inverseRelation, "inverseRelation", // NOI18N
inverseRelName);
TableElement relTable = getPrimaryTable(relMappingClass);
validateModel(relTable, "relTable", // NOI18N
relClassName);
int relUpperBound = inverseRelation.getUpperBound();
if (logger.isLoggable(Logger.FINE)) {
logger.fine(// NOI18N
"Before adding relationship:" + // NOI18N
getTblInfo("sourceTable", sourceTable, relationName) + // NOI18N
getTblInfo("relTable", relTable, inverseRelName));
}
if ((upperBound > 1) && (relUpperBound > 1)) {
// M-N relationship, create new table
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("M-N relationship");
}
ForeignKeyElement fKey = getMappedForeignKey(relation, inverseRelation, relationFKey);
if (fKey == null) {
TableElement joinTable = DBElementFactory.createAndAttachTable(schema, mappingPolicy.getJoinTableName(sourceTable.getName().getName(), relTable.getName().getName()));
fKey = createRelationship(joinTable, sourceTable, relationName, inverseRelName, mappingClass, relMappingClass, uniqueId, true);
relationFKey.put(relation, fKey);
ForeignKeyElement fKey2 = createRelationship(joinTable, relTable, inverseRelName, relationName, relMappingClass, mappingClass, uniqueId, true);
relationFKey.put(inverseRelation, fKey2);
}
} else if ((upperBound > 1) && (relUpperBound == 1)) {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("M-1 relationship: skip");
}
} else if ((upperBound == 1) && (relUpperBound > 1)) {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("1-M relationship");
}
ForeignKeyElement fKey = getMappedForeignKey(relation, inverseRelation, relationFKey);
if (fKey == null) {
fKey = createRelationship(sourceTable, relTable, relationName, inverseRelName, mappingClass, relMappingClass, uniqueId, false);
relationFKey.put(relation, fKey);
}
} else if ((upperBound == 1) && (relUpperBound == 1)) {
// 1-1 relationship, add foreign key at either side.
// Check existence of foreign key at the other side
// before adding one to here. If there is cascade
// delete in this side, add FK. Otherwise, defer
// adding it until all other relationships are added.
ForeignKeyElement fKey = getMappedForeignKey(relation, inverseRelation, relationFKey);
if (fKey == null) {
if (relation.getDeleteAction() == RelationshipElement.CASCADE_ACTION) {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("1-1 relationship: cascade(this)");
}
fKey = createRelationship(sourceTable, relTable, relationName, inverseRelName, mappingClass, relMappingClass, uniqueId, false);
relationFKey.put(relation, fKey);
} else if (inverseRelation.getDeleteAction() == RelationshipElement.CASCADE_ACTION) {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("1-1 relationship: cascade(inverse)");
}
fKey = createRelationship(relTable, sourceTable, inverseRelName, relationName, relMappingClass, mappingClass, uniqueId, false);
relationFKey.put(inverseRelation, fKey);
} else {
if (logger.isLoggable(Logger.FINE)) {
// NOI18N
logger.fine("1-1 relationship: defer");
}
deferredRelationships.add(new DeferredRelationship(relation, inverseRelation, sourceTable, relTable, relationName, inverseRelName, mappingClass, relMappingClass, uniqueId));
}
}
}
if (logger.isLoggable(Logger.FINE)) {
logger.fine(// NOI18N
"After adding relationship:" + // NOI18N
getTblInfo("sourceTable", sourceTable, relationName) + // NOI18N
getTblInfo("relTable", relTable, inverseRelName));
}
}
}
}
if (deferredRelationships.size() > 0) {
addDeferredRelationships(deferredRelationships, relationFKey);
}
}
use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.
the class DatabaseGenerator method addDeferredRelationships.
/**
* Generate foreign keys for relationships that were deferred; see {@link
* addRelationships}.
* @param deferredRelationships List of 1-1 relationships which are not
* yet mapped by foreign keys
* @param relationFKey Map from RelationshipElement to ForeignKeyElement,
* indicating which relationships have already been mapped.
*/
private void addDeferredRelationships(List deferredRelationships, Map relationFKey) throws DBException, ModelException {
for (Iterator i = deferredRelationships.iterator(); i.hasNext(); ) {
DeferredRelationship dr = (DeferredRelationship) i.next();
RelationshipElement relation = dr.getRelation();
RelationshipElement inverseRelation = dr.getInverseRelation();
ForeignKeyElement fKey = getMappedForeignKey(relation, inverseRelation, relationFKey);
// Only map if not already mapped
if (fKey == null) {
TableElement sourceTable = dr.getSourceTable();
TableElement relTable = dr.getRelTable();
String relationName = dr.getRelationName();
String inverseRelName = dr.getInverseRelName();
MappingClassElement mappingClass = dr.getMappingClass();
MappingClassElement relMappingClass = dr.getRelMappingClass();
String uniqueId = dr.getUniqueId();
// If this side already has any foreign keys, map it on this
// side, else on the other side.
ForeignKeyElement[] keys = sourceTable.getForeignKeys();
if (null != keys && keys.length > 0) {
fKey = createRelationship(sourceTable, relTable, relationName, inverseRelName, mappingClass, relMappingClass, uniqueId, false);
if (logger.isLoggable(Logger.FINE)) {
logger.fine(// NOI18N
"1-1 deferred relationship (this)" + // NOI18N
getTblInfo("sourceTable", sourceTable, relationName) + // NOI18N
getTblInfo("relTable", relTable, inverseRelName));
}
} else {
fKey = createRelationship(relTable, sourceTable, inverseRelName, relationName, relMappingClass, mappingClass, uniqueId, false);
if (logger.isLoggable(Logger.FINE)) {
logger.fine(// NOI18N
"1-1 deferred relationship (inverse)" + // NOI18N
getTblInfo("sourceTable", sourceTable, relationName) + // NOI18N
getTblInfo("relTable", relTable, inverseRelName));
}
}
relationFKey.put(relation, fKey);
}
}
}
use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.
the class Model method removeResourcesFromCaches.
/**
* Removes the specified classes from all caches. The specified
* collection includes the fully qualified class names of the classes
* to be removed. The method removes each class from the cache of
* MappingClassElements and the set of classes known to be non
* PC. Furthermore it removes the SchemaElement associated with this
* class from the SchemaElement cache. The next call getMappingClass
* will determine the status of the classes.
* @param classNames a collection of fully qualified class names
*/
protected void removeResourcesFromCaches(Collection classNames) {
if (classNames == null)
return;
synchronized (this._classes) {
for (Iterator i = classNames.iterator(); i.hasNext(); ) {
String className = (String) i.next();
MappingClassElement mapping = (MappingClassElement) _classes.get(className);
// SchemaElement from the SchemaElement cache.
if (mapping != null)
SchemaElement.removeFromCache(mapping.getDatabaseRoot());
// remove the corresponding MappingClassElement from cache
_classes.remove(className);
// remove the class from the set of classes known to be non PC
_nonPCClasses.remove(className);
}
}
}
use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.
the class Model method getMappingClass.
/**
* Returns the MappingClassElement created for the specified class name.
* This method looks up the class in the internal cache. If not present
* it loads the corresponding xml file containing the mapping information.
* @param className the fully qualified name of the mapping class
* @param classLoader the class loader used to find mapping information
* @return the MappingClassElement for className,
* <code>null</code> if an error occurs or none exists
* @see MappingClassElementImpl#forName
*/
public MappingClassElement getMappingClass(String className, ClassLoader classLoader) {
// using the same variable _classes (e.g. updateKeyForClass).
synchronized (this._classes) {
MappingClassElement mappingClass = (MappingClassElement) _classes.get(className);
if (mappingClass == null) {
// check whether the class is known to be non PC
if (_nonPCClasses.contains(className))
return null;
try {
InputStream stream = getInputStreamForResource(className, classLoader, getResourceNameWithExtension(className));
if (stream != null) {
// return null without updating either cache
if (stream.available() > 0) {
XMLInputStream xmlInput = new XMLInputStream(stream, getClass().getClassLoader());
mappingClass = (MappingClassElement) xmlInput.readObject();
xmlInput.close();
// postUnarchive performs version number checking
// and possible format conversions
mappingClass.postUnarchive();
// can't call updateKeyForClass here there are cases
// when the mapping class name doesn't match the
// classname (such as copy/paste, move etc.)
_classes.put(className, mappingClass);
// update the modified flags for the mapping and
// persistence classes since the xml archiver uses
// all the set methods
mappingClass.setModified(false);
getPersistenceClass(mappingClass).setModified(false);
}
} else {
// stream is null, mapping file does not exist =>
// class is not PC, so store the class name in the
// set of classes known to be non PC
_nonPCClasses.add(className);
}
} catch (ModelException e) {
// MBO: print reason to logger
LogHelperModel.getLogger().log(Logger.WARNING, e.getMessage());
return null;
} catch (Exception e) {
// MBO: print reason to logger
LogHelperModel.getLogger().log(Logger.WARNING, I18NHelper.getMessage(getMessages(), "file.cannot_read", className, // NOI18N
e.toString()));
}
// will return null
}
return mappingClass;
}
}
Aggregations