final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final boolean hasIdClass = !(identifierType.isCoreType() || identifierType
.equals(GAE_DATASTORE_KEY));
final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
annotations.add(new AnnotationMetadataBuilder(annotationType));
// Encode keys as strings on GAE to support entity group hierarchies
if (isGaeEnabled && identifierType.equals(JavaType.STRING)) {
AnnotationMetadataBuilder extensionBuilder = new AnnotationMetadataBuilder(
DATANUCLEUS_JPA_EXTENSION);
extensionBuilder.addStringAttribute("vendorName", "datanucleus");
extensionBuilder.addStringAttribute("key", "gae.encoded-pk");
extensionBuilder.addStringAttribute("value", "true");
annotations.add(extensionBuilder);
}
// Compute the column name, as required
if (!hasIdClass) {
if (!"".equals(annotationValues.getSequenceName())) {
String generationType = isGaeEnabled || isDatabaseDotComEnabled ? "IDENTITY"
: "AUTO";
// ROO-746: Use @GeneratedValue(strategy = GenerationType.TABLE)
// If the root of the governor declares @Inheritance(strategy =
// InheritanceType.TABLE_PER_CLASS)
if ("AUTO".equals(generationType)) {
AnnotationMetadata inheritance = governorTypeDetails
.getAnnotation(INHERITANCE);
if (inheritance == null) {
inheritance = getInheritanceAnnotation();
}
if (inheritance != null) {
final AnnotationAttributeValue<?> value = inheritance
.getAttribute(new JavaSymbolName("strategy"));
if (value instanceof EnumAttributeValue) {
final EnumAttributeValue enumAttributeValue = (EnumAttributeValue) value;
final EnumDetails details = enumAttributeValue
.getValue();
if (details != null
&& details.getType().equals(
INHERITANCE_TYPE)) {
if ("TABLE_PER_CLASS".equals(details.getField()
.getSymbolName())) {
generationType = "TABLE";
}
}
}
}
}
final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(
GENERATED_VALUE);
generatedValueBuilder.addEnumAttribute("strategy",
new EnumDetails(GENERATION_TYPE, new JavaSymbolName(
generationType)));
if (StringUtils.isNotBlank(annotationValues.getSequenceName())
&& !(isGaeEnabled || isDatabaseDotComEnabled)) {
final String sequenceKey = StringUtils
.uncapitalize(destination.getSimpleTypeName())
+ "Gen";
generatedValueBuilder.addStringAttribute("generator",
sequenceKey);
final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(
SEQUENCE_GENERATOR);
sequenceGeneratorBuilder.addStringAttribute("name",
sequenceKey);
sequenceGeneratorBuilder.addStringAttribute("sequenceName",
annotationValues.getSequenceName());
annotations.add(sequenceGeneratorBuilder);
}
annotations.add(generatedValueBuilder);
}
final String identifierColumn = StringUtils
.stripToEmpty(getIdentifierColumn());
String columnName = idField.getSymbolName();
if (StringUtils.isNotBlank(identifierColumn)) {
// User has specified an alternate column name
columnName = identifierColumn;
}
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(
COLUMN);
columnBuilder.addStringAttribute("name", columnName);
if (identifier != null
&& StringUtils.isNotBlank(identifier.getColumnDefinition())) {
columnBuilder.addStringAttribute("columnDefinition",
identifier.getColumnDefinition());
}
// Add length attribute for String field
if (identifier != null && identifier.getColumnSize() > 0
&& identifier.getColumnSize() < 4000
&& identifierType.equals(JavaType.STRING)) {
columnBuilder.addIntegerAttribute("length",
identifier.getColumnSize());
}
// Add precision and scale attributes for numeric field
if (identifier != null
&& identifier.getScale() > 0
&& (identifierType.equals(JavaType.DOUBLE_OBJECT)
|| identifierType.equals(JavaType.DOUBLE_PRIMITIVE) || identifierType
.equals(BIG_DECIMAL))) {
columnBuilder.addIntegerAttribute("precision",
identifier.getColumnSize());
columnBuilder.addIntegerAttribute("scale",
identifier.getScale());
}
annotations.add(columnBuilder);
}