Search in sources :

Example 1 with IdentifierStrategy

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();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) EnumDetails(org.springframework.roo.model.EnumDetails) IdentifierStrategy(org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

ArrayList (java.util.ArrayList)1 IdentifierStrategy (org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy)1 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)1 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)1 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)1 EnumDetails (org.springframework.roo.model.EnumDetails)1 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)1 JavaType (org.springframework.roo.model.JavaType)1 JdkJavaType (org.springframework.roo.model.JdkJavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1