use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHelper method getXMLForMetaData.
public String getXMLForMetaData(PackageMetaData pmd, String prefix, String indent) {
StringBuilder str = new StringBuilder();
str.append(prefix).append("<package name=\"" + pmd.getName() + "\"");
if (pmd.getCatalog() != null) {
str.append(" catalog=\"" + pmd.getCatalog() + "\"");
}
if (pmd.getSchema() != null) {
str.append(" schema=\"" + pmd.getSchema() + "\"");
}
str.append(">\n");
// Add interfaces
if (pmd.getNoOfInterfaces() > 0) {
for (int i = 0; i < pmd.getNoOfInterfaces(); i++) {
InterfaceMetaData imd = pmd.getInterface(i);
str.append(getXMLForMetaData(imd, prefix + indent, indent));
}
}
// Add classes
if (pmd.getNoOfClasses() > 0) {
for (int i = 0; i < pmd.getNoOfClasses(); i++) {
ClassMetaData cmd = pmd.getClass(i);
str.append(getXMLForMetaData(cmd, prefix + indent, indent));
}
}
// Add sequences
SequenceMetaData[] seqmds = pmd.getSequences();
if (seqmds != null) {
for (SequenceMetaData seqmd : seqmds) {
str.append(getXMLForMetaData(seqmd, prefix + indent, indent));
}
}
// Add extensions
processExtensions(pmd.getExtensions(), str, prefix, indent);
str.append(prefix).append("</package>\n");
return str.toString();
}
use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetadataImpl method newInterfaceMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.JDOMetadata#newInterfaceMetadata(java.lang.Class)
*/
public InterfaceMetadata newInterfaceMetadata(Class cls) {
String packageName = ClassUtils.getPackageNameForClass(cls);
// Adds if necessary
PackageMetaData internalPmd = getInternal().newPackageMetadata(packageName);
PackageMetadataImpl pmd = new PackageMetadataImpl(internalPmd);
pmd.parent = this;
String className = ClassUtils.getClassNameForClass(cls);
InterfaceMetaData internalImd = internalPmd.newInterfaceMetadata(className);
InterfaceMetadataImpl imd = new InterfaceMetadataImpl(internalImd);
imd.parent = pmd;
return imd;
}
use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method registerMetadata.
/**
* Method to register the supplied metadata with the persistence process managed by this
* <code>PersistenceManagerFactory</code>.
* Metadata can be created using the method {@link #newMetadata}.
* @param metadata The Metadata to register.
*/
public void registerMetadata(javax.jdo.metadata.JDOMetadata metadata) {
checkJDOPermission(JDOPermission.GET_METADATA);
MetaDataManager mmgr = nucleusContext.getMetaDataManager();
FileMetaData filemd = ((JDOMetadataImpl) metadata).getInternal();
// Check if already defined
for (int i = 0; i < filemd.getNoOfPackages(); i++) {
PackageMetaData pmd = filemd.getPackage(i);
for (int j = 0; j < pmd.getNoOfClasses(); j++) {
ClassMetaData cmd = pmd.getClass(j);
if (mmgr.hasMetaDataForClass(cmd.getFullClassName())) {
throw new JDOUserException("Cannot redefine metadata for " + cmd.getFullClassName());
}
}
for (int j = 0; j < pmd.getNoOfInterfaces(); j++) {
InterfaceMetaData imd = pmd.getInterface(j);
if (mmgr.hasMetaDataForClass(imd.getFullClassName())) {
throw new JDOUserException("Cannot redefine metadata for " + imd.getFullClassName());
}
}
}
mmgr.loadUserMetaData(filemd, null);
}
use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getMemberMapping.
/**
* Accessor for the field mapping for the specified field.
* The field can be managed by a supertable of this table.
* @param mmd MetaData for this field/property
* @return the Mapping for the field/property
*/
public JavaTypeMapping getMemberMapping(AbstractMemberMetaData mmd) {
if (mmd == null) {
return null;
}
if (mmd instanceof PropertyMetaData && mmd.getAbstractClassMetaData() instanceof InterfaceMetaData) {
// TODO Fix Persistent Interfaces lookup
return getMemberMapping(mmd.getName());
}
if (mmd.isPrimaryKey()) {
assertIsPKInitialized();
} else {
assertIsInitialized();
}
// Check if we manage this field
JavaTypeMapping m = memberMappingsMap.get(mmd);
if (m != null) {
return m;
}
if (mmd.isPrimaryKey() && pkMappings != null) {
// pkMappings aren't in memberMappingsMap when in subclasses
for (int i = 0; i < pkMappings.length; i++) {
if (pkMappings[i].getMemberMetaData().equals(mmd)) {
return pkMappings[i];
}
}
}
Iterator<Map.Entry<AbstractMemberMetaData, JavaTypeMapping>> memberMapIter = memberMappingsMap.entrySet().iterator();
while (memberMapIter.hasNext()) {
// If we have overridden this member then it may have a different AbstractMemberMetaData passed in (i.e the override)
Map.Entry<AbstractMemberMetaData, JavaTypeMapping> entry = memberMapIter.next();
if (entry.getKey().getFullFieldName().equals(mmd.getFullFieldName())) {
return entry.getValue();
}
}
// Check supertable
int ifc = cmd.getNoOfInheritedManagedMembers();
if (mmd.getAbsoluteFieldNumber() < ifc) {
if (supertable != null) {
m = supertable.getMemberMapping(mmd);
if (m != null) {
return m;
}
}
}
// Check secondary tables
if (secondaryTables != null) {
Collection secTables = secondaryTables.values();
Iterator iter = secTables.iterator();
while (iter.hasNext()) {
SecondaryTable secTable = (SecondaryTable) iter.next();
m = secTable.getMemberMapping(mmd);
if (m != null) {
return m;
}
}
}
return null;
}
use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-rdbms by datanucleus.
the class DeleteRequest method updateOneToOneBidirectionalOwnerObjectForField.
/**
* Method to update any 1-1 bidir non-owner fields where the foreign-key is stored in the other object.
* @param op ObjectProvider of this object
* @param mmd MetaData for field that has related (owner) objects
*/
private void updateOneToOneBidirectionalOwnerObjectForField(ObjectProvider op, AbstractMemberMetaData fmd) {
if (softDeleteStmt != null) {
// If we are soft deleting the owner then no need to null the linkage BUT we need to check the soft delete status on retrieval of the owner from related
return;
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052217", op.getObjectAsPrintable(), fmd.getFullFieldName()));
}
RDBMSStoreManager storeMgr = table.getStoreManager();
ExecutionContext ec = op.getExecutionContext();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
// Check if we should null here, or leave to the datastore FK handler
boolean checkFK = true;
if (ec.getStringProperty(PropertyNames.PROPERTY_DELETION_POLICY).equals("JDO2")) {
// JDO2 doesn't currently (2.0 spec) take note of foreign-key
checkFK = false;
}
if (checkFK) {
for (int i = 0; i < relatedMmds.length; i++) {
ForeignKeyMetaData relFkmd = relatedMmds[i].getForeignKeyMetaData();
if (relFkmd != null && relFkmd.getDeleteAction() != null) {
// Field has a FK with a delete-action so leave to the datastore to process the delete
return;
}
}
}
// TODO Cater for more than 1 related field
String fullClassName = ((AbstractClassMetaData) relatedMmds[0].getParent()).getFullClassName();
// TODO I'm not sure that we need to loop all implementations. will we have the fk set to all tables, if many?
String[] classes;
if (((AbstractClassMetaData) relatedMmds[0].getParent()) instanceof InterfaceMetaData) {
classes = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(fullClassName, clr);
} else {
classes = new String[] { fullClassName };
}
Set<DatastoreClass> datastoreClasses = new HashSet();
for (int i = 0; i < classes.length; i++) {
// just remove duplicates
datastoreClasses.add(storeMgr.getDatastoreClass(classes[i], clr));
}
Iterator<DatastoreClass> it = datastoreClasses.iterator();
while (it.hasNext()) {
DatastoreClass refTable = it.next();
JavaTypeMapping refMapping = refTable.getMemberMapping(fmd.getMappedBy());
if (// Only clear the references that can be cleared
refMapping.isNullable()) {
// Create a statement to clear the link from the previous related object
StringBuilder clearLinkStmt = new StringBuilder("UPDATE " + refTable.toString() + " SET ");
for (int j = 0; j < refMapping.getNumberOfDatastoreMappings(); j++) {
if (j > 0) {
clearLinkStmt.append(",");
}
clearLinkStmt.append(refMapping.getDatastoreMapping(j).getColumn().getIdentifier());
clearLinkStmt.append("=NULL");
}
clearLinkStmt.append(" WHERE ");
for (int j = 0; j < refMapping.getNumberOfDatastoreMappings(); j++) {
if (j > 0) {
clearLinkStmt.append(" AND ");
}
clearLinkStmt.append(refMapping.getDatastoreMapping(j).getColumn().getIdentifier());
clearLinkStmt.append("=?");
}
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Null out the relationship to the object being deleted.
PreparedStatement ps = null;
try {
ps = sqlControl.getStatementForUpdate(mconn, clearLinkStmt.toString(), false);
refMapping.setObject(ec, ps, MappingHelper.getMappingIndices(1, refMapping), op.getObject());
sqlControl.executeStatementUpdate(ec, mconn, clearLinkStmt.toString(), ps, true);
} finally {
if (ps != null) {
sqlControl.closeStatement(mconn, ps);
}
}
} finally {
mconn.release();
}
} catch (Exception e) {
throw new NucleusDataStoreException("Update request failed", e);
}
}
}
}
Aggregations