use of org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy in project spring-roo by spring-projects.
the class JpaOperationsImpl method getIdentifierField.
/**
* This method generates the identifier field using the provided values.
*
* @param entity
* @param identifierField
* @param identifierType
* @param identifierColumn
* @param sequenceName
* @param identifierStrategy
* @param inheritanceType
* @return
*/
private FieldMetadata getIdentifierField(final JavaType entity, String identifierField, final JavaType identifierType, final String identifierColumn, final String sequenceName, IdentifierStrategy identifierStrategy, InheritanceType inheritanceType) {
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final boolean hasIdClass = !(identifierType.isCoreType());
final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
annotations.add(new AnnotationMetadataBuilder(annotationType));
if (StringUtils.isEmpty(identifierField)) {
identifierField = "id";
}
// Compute the column name, as required
if (!hasIdClass) {
if (!"".equals(sequenceName)) {
// ROO-3719: Add SEQUENCE as @GeneratedValue strategy
// Check if provided identifierStrategy is valid
boolean isValidIdentifierStrategy = false;
if (identifierStrategy != null) {
for (IdentifierStrategy identifierStrategyType : IdentifierStrategy.values()) {
if (identifierStrategyType.name().equals(identifierStrategy.name())) {
isValidIdentifierStrategy = true;
break;
}
}
}
if (!isValidIdentifierStrategy) {
identifierStrategy = IdentifierStrategy.AUTO;
}
// InheritanceType.TABLE_PER_CLASS)
if (IdentifierStrategy.AUTO.name().equals(identifierStrategy.name())) {
if (inheritanceType != null) {
if ("TABLE_PER_CLASS".equals(inheritanceType.name())) {
identifierStrategy = IdentifierStrategy.TABLE;
}
}
}
final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(GENERATED_VALUE);
generatedValueBuilder.addEnumAttribute("strategy", new EnumDetails(GENERATION_TYPE, new JavaSymbolName(identifierStrategy.name())));
if (StringUtils.isNotBlank(sequenceName)) {
final String sequenceKey = StringUtils.uncapitalize(entity.getSimpleTypeName()) + "Gen";
generatedValueBuilder.addStringAttribute("generator", sequenceKey);
final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(SEQUENCE_GENERATOR);
sequenceGeneratorBuilder.addStringAttribute("name", sequenceKey);
sequenceGeneratorBuilder.addStringAttribute("sequenceName", sequenceName);
annotations.add(sequenceGeneratorBuilder);
}
annotations.add(generatedValueBuilder);
}
// User has specified alternative columnName
if (StringUtils.isNotBlank(identifierColumn)) {
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
columnBuilder.addStringAttribute("name", identifierColumn);
annotations.add(columnBuilder);
}
}
FieldDetails identifierFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), identifierType, new JavaSymbolName(identifierField));
identifierFieldDetails.setModifiers(Modifier.PRIVATE);
identifierFieldDetails.addAnnotations(annotations);
return new FieldMetadataBuilder(identifierFieldDetails).build();
}
Aggregations