use of org.datanucleus.metadata.FieldMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHandler method newFieldObject.
/**
* Utility to create a new field component.
* @param md The parent MetaData
* @param attrs The attributes
* @return The FieldMetaData
*/
protected FieldMetaData newFieldObject(MetaData md, Attributes attrs) {
FieldMetaData fmd = new FieldMetaData(md, getAttr(attrs, "name"));
String modStr = getAttr(attrs, "persistence-modifier");
FieldPersistenceModifier modifier = FieldPersistenceModifier.getFieldPersistenceModifier(modStr);
if (modifier != null) {
fmd.setPersistenceModifier(modifier);
}
fmd.setDeleteAction(getAttr(attrs, "delete-action"));
String pkStr = getAttr(attrs, "primary-key");
if (!StringUtils.isWhitespace(pkStr)) {
fmd.setPrimaryKey(Boolean.valueOf(pkStr));
}
String dfgStr = getAttr(attrs, "default-fetch-group");
if (!StringUtils.isWhitespace(dfgStr)) {
fmd.setDefaultFetchGroup(Boolean.valueOf(dfgStr));
}
String embStr = getAttr(attrs, "embedded");
if (!StringUtils.isWhitespace(embStr)) {
fmd.setEmbedded(Boolean.valueOf(embStr));
}
String serStr = getAttr(attrs, "serialized");
if (!StringUtils.isWhitespace(serStr)) {
fmd.setSerialised(Boolean.valueOf(serStr));
}
String depStr = getAttr(attrs, "dependent");
if (!StringUtils.isWhitespace(depStr)) {
fmd.setDependent(Boolean.valueOf(depStr));
}
fmd.setNullValue(NullValue.getNullValue(getAttr(attrs, "null-value")));
fmd.setMappedBy(getAttr(attrs, "mapped-by"));
fmd.setColumn(getAttr(attrs, "column"));
fmd.setIndexed(IndexedValue.getIndexedValue(getAttr(attrs, "indexed")));
fmd.setUnique(getAttr(attrs, "unique"));
fmd.setTable(getAttr(attrs, "table"));
fmd.setLoadFetchGroup(getAttr(attrs, "load-fetch-group"));
fmd.setRecursionDepth(getAttr(attrs, "recursion-depth"));
fmd.setValueStrategy(getAttr(attrs, "value-strategy"));
fmd.setSequence(getAttr(attrs, "sequence"));
fmd.setFieldTypes(getAttr(attrs, "field-type"));
String cacheableAttr = getAttr(attrs, "cacheable");
if (cacheableAttr != null) {
fmd.setCacheable(cacheableAttr.equalsIgnoreCase("false") ? false : true);
}
String converterAttr = getAttr(attrs, "converter");
String disableConverterAttr = getAttr(attrs, "use-default-conversion");
if (disableConverterAttr != null && Boolean.getBoolean(disableConverterAttr)) {
fmd.setTypeConverterDisabled();
} else if (!StringUtils.isWhitespace(converterAttr)) {
TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
ClassLoaderResolver clr = mmgr.getNucleusContext().getClassLoaderResolver(null);
Class converterCls = clr.classForName(converterAttr);
if (typeMgr.getTypeConverterForName(converterCls.getName()) == null) {
// Not yet cached an instance of this converter so create one
AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), converterCls);
// TODO member type
Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(converterCls, null);
Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(converterCls, attrType, null);
// Register the TypeConverter under the name of the AttributeConverter class
JDOTypeConverter typeConv = new JDOTypeConverter(conv);
typeMgr.registerConverter(converterAttr, typeConv, attrType, dbType, false, null);
}
fmd.setTypeConverterName(converterAttr);
}
return fmd;
}
use of org.datanucleus.metadata.FieldMetaData in project datanucleus-api-jdo by datanucleus.
the class ForeignKeyMetadataImpl method newFieldMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.ForeignKeyMetadata#newFieldMetadata(java.lang.String)
*/
public FieldMetadata newFieldMetadata(String name) {
FieldMetaData internalFmd = new FieldMetaData(getInternal(), name);
FieldMetadataImpl fmd = new FieldMetadataImpl(internalFmd);
fmd.parent = this;
return fmd;
}
use of org.datanucleus.metadata.FieldMetaData in project datanucleus-api-jdo by datanucleus.
the class ClassMetadataImpl method newFieldMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.ClassMetadata#newFieldMetadata(java.lang.reflect.Field)
*/
public FieldMetadata newFieldMetadata(Field fld) {
FieldMetaData internalFmd = getInternal().newFieldMetadata(fld.getName());
FieldMetadataImpl fmd = new FieldMetadataImpl(internalFmd);
fmd.parent = this;
return fmd;
}
use of org.datanucleus.metadata.FieldMetaData in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method getValueForDatastoreMapping.
/**
* Method to return the value to be stored in the specified datastore index given the overall value for this java type.
* @param nucleusCtx Context
* @param index The datastore index
* @param value The overall value for this java type
* @return The value for this datastore index
*/
public Object getValueForDatastoreMapping(NucleusContext nucleusCtx, int index, Object value) {
ExecutionContext ec = nucleusCtx.getApiAdapter().getExecutionContext(value);
if (cmd == null) {
cmd = nucleusCtx.getMetaDataManager().getMetaDataForClass(getType(), ec != null ? ec.getClassLoaderResolver() : nucleusCtx.getClassLoaderResolver(null));
}
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[index]);
ObjectProvider op = null;
if (ec != null) {
op = ec.findObjectProvider(value);
}
if (op == null) {
// Transient or detached maybe, so use reflection to get PK field values
if (mmd instanceof FieldMetaData) {
return ClassUtils.getValueOfFieldByReflection(value, mmd.getName());
}
return ClassUtils.getValueOfMethodByReflection(value, ClassUtils.getJavaBeanGetterName(mmd.getName(), false));
}
if (!mmd.isPrimaryKey()) {
// Make sure the field is loaded
op.isLoaded(mmd.getAbsoluteFieldNumber());
}
FieldManager fm = new SingleValueFieldManager();
op.provideFields(new int[] { mmd.getAbsoluteFieldNumber() }, fm);
return fm.fetchObjectField(mmd.getAbsoluteFieldNumber());
} else if (cmd.getIdentityType() == IdentityType.DATASTORE) {
Object id = nucleusCtx.getApiAdapter().getIdForObject(value);
return id != null ? IdentityUtils.getTargetKeyForDatastoreIdentity(id) : null;
}
return null;
}
use of org.datanucleus.metadata.FieldMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHelper method getXMLForMetaData.
public String getXMLForMetaData(AbstractMemberMetaData mmd, String prefix, String indent) {
if (mmd.isStatic() || mmd.isFinal()) {
// If this field is static or final, don't bother with MetaData since we will ignore it anyway.
return "";
}
boolean field = (mmd instanceof FieldMetaData);
// Field needs outputting so generate metadata
StringBuilder str = new StringBuilder();
if (field) {
str.append(prefix).append("<field name=\"" + mmd.getName() + "\"");
} else {
str.append(prefix).append("<property name=\"" + mmd.getName() + "\"");
}
if (mmd.getPersistenceModifier() != null && !StringUtils.isWhitespace(mmd.getPersistenceModifier().toString())) {
str.append("\n").append(prefix).append(" persistence-modifier=\"" + mmd.getPersistenceModifier() + "\"");
}
if (!StringUtils.isWhitespace(mmd.getTable())) {
str.append("\n").append(prefix).append(" table=\"" + mmd.getTable() + "\"");
}
if (mmd.isPrimaryKey()) {
str.append("\n").append(prefix).append(" primary-key=\"" + mmd.isPrimaryKey() + "\"");
}
str.append("\n").append(prefix).append(" null-value=\"" + mmd.getNullValue() + "\"");
if (mmd.isDefaultFetchGroup()) {
str.append("\n").append(prefix).append(" default-fetch-group=\"" + mmd.isDefaultFetchGroup() + "\"");
}
if (mmd.isEmbedded()) {
str.append("\n").append(prefix).append(" embedded=\"" + mmd.isEmbedded() + "\"");
}
if (mmd.isSerialized()) {
str.append("\n").append(prefix).append(" serialized=\"" + mmd.isSerialized() + "\"");
}
if (mmd.isDependent()) {
str.append("\n").append(prefix).append(" dependent=\"" + mmd.isDependent() + "\"");
}
if (mmd.getMappedBy() != null) {
str.append("\n").append(prefix).append(" mapped-by=\"" + mmd.getMappedBy() + "\"");
}
String[] fieldTypes = mmd.getFieldTypes();
if (fieldTypes != null) {
str.append("\n").append(prefix).append(" field-type=\"");
for (int i = 0; i < fieldTypes.length; i++) {
str.append(fieldTypes[i]);
}
str.append("\"");
}
if (!StringUtils.isWhitespace(mmd.getLoadFetchGroup())) {
str.append("\n").append(prefix).append(" load-fetch-group=\"" + mmd.getLoadFetchGroup() + "\"");
}
if (mmd.getRecursionDepth() != AbstractMemberMetaData.DEFAULT_RECURSION_DEPTH && mmd.getRecursionDepth() != AbstractMemberMetaData.UNDEFINED_RECURSION_DEPTH) {
str.append("\n").append(prefix).append(" recursion-depth=\"" + mmd.getRecursionDepth() + "\"");
}
if (mmd.getValueStrategy() != null) {
str.append("\n").append(prefix).append(" value-strategy=\"" + mmd.getValueStrategy() + "\"");
}
if (mmd.getSequence() != null) {
str.append("\n").append(prefix).append(" sequence=\"" + mmd.getSequence() + "\"");
}
if (!field) {
PropertyMetaData propmd = (PropertyMetaData) mmd;
if (propmd.getFieldName() != null) {
str.append("\n").append(prefix).append(" field-name=\"" + propmd.getFieldName() + "\"");
}
}
if (mmd.getIndexMetaData() == null && mmd.getIndexed() != null) {
str.append("\n").append(prefix).append(" indexed=\"" + mmd.getIndexed() + "\"");
}
if (mmd.getUniqueMetaData() == null) {
str.append("\n").append(prefix).append(" unique=\"" + mmd.isUnique() + "\"");
}
str.append(">\n");
// Add field containers
ContainerMetaData contmd = mmd.getContainer();
if (contmd != null) {
if (contmd instanceof CollectionMetaData) {
CollectionMetaData c = (CollectionMetaData) contmd;
str.append(getXMLForMetaData(c, prefix + indent, indent));
} else if (contmd instanceof ArrayMetaData) {
ArrayMetaData c = (ArrayMetaData) contmd;
str.append(getXMLForMetaData(c, prefix + indent, indent));
} else if (contmd instanceof MapMetaData) {
MapMetaData c = (MapMetaData) contmd;
str.append(getXMLForMetaData(c, prefix + indent, indent));
}
}
// Add columns
ColumnMetaData[] columnMetaData = mmd.getColumnMetaData();
if (columnMetaData != null) {
for (int i = 0; i < columnMetaData.length; i++) {
str.append(getXMLForMetaData(columnMetaData[i], prefix + indent, indent));
}
}
// Add join
if (mmd.getJoinMetaData() != null) {
str.append(getXMLForMetaData(mmd.getJoinMetaData(), prefix + indent, indent));
}
// Add element
if (mmd.getElementMetaData() != null) {
str.append(getXMLForMetaData(mmd.getElementMetaData(), prefix + indent, indent));
}
// Add key
if (mmd.getKeyMetaData() != null) {
str.append(getXMLForMetaData(mmd.getKeyMetaData(), prefix + indent, indent));
}
// Add value
if (mmd.getValueMetaData() != null) {
str.append(getXMLForMetaData(mmd.getValueMetaData(), prefix + indent, indent));
}
// Add order
if (mmd.getOrderMetaData() != null) {
str.append(getXMLForMetaData(mmd.getOrderMetaData(), prefix + indent, indent));
}
// Add embedded
if (mmd.getEmbeddedMetaData() != null) {
str.append(getXMLForMetaData(mmd.getEmbeddedMetaData(), prefix + indent, indent));
}
// Add index
if (mmd.getIndexMetaData() != null) {
str.append(getXMLForMetaData(mmd.getIndexMetaData(), prefix + indent, indent));
}
// Add unique
if (mmd.getUniqueMetaData() != null) {
str.append(getXMLForMetaData(mmd.getUniqueMetaData(), prefix + indent, indent));
}
// Add foreign-key
if (mmd.getForeignKeyMetaData() != null) {
str.append(getXMLForMetaData(mmd.getForeignKeyMetaData(), prefix + indent, indent));
}
// Add extensions
processExtensions(mmd.getExtensions(), str, prefix, indent);
if (field) {
str.append(prefix).append("</field>\n");
} else {
str.append(prefix).append("</property>\n");
}
return str.toString();
}
Aggregations