Search in sources :

Example 16 with EnumDetails

use of org.springframework.roo.model.EnumDetails in project spring-roo by spring-projects.

the class JpaEntityMetadata method getInheritanceAnnotation.

/**
 * Returns the JPA @Inheritance annotation to be applied to the entity, if
 * applicable
 *
 * @param annotationValues the values of the {@link RooJpaEntity} annotation
 *            (required)
 * @return <code>null</code> if it's already present or not required
 */
private AnnotationMetadata getInheritanceAnnotation() {
    if (governorTypeDetails.getAnnotation(INHERITANCE) != null) {
        return null;
    }
    if (StringUtils.isNotBlank(annotationValues.getInheritanceType())) {
        final AnnotationMetadataBuilder inheritanceBuilder = new AnnotationMetadataBuilder(INHERITANCE);
        inheritanceBuilder.addEnumAttribute("strategy", new EnumDetails(INHERITANCE_TYPE, new JavaSymbolName(annotationValues.getInheritanceType())));
        return inheritanceBuilder.build();
    }
    return null;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 17 with EnumDetails

use of org.springframework.roo.model.EnumDetails in project spring-roo by spring-projects.

the class JpaEntityMetadataProviderImpl method getCompositionRelationField.

/**
 * Gets {@link FieldMetadata} of entity field which declares a composition relationship
 *
 * @param entity
 * @param entityDetails
 * @param relationsAsChild
 * @return
 * @throws ClassNotFoundException
 */
private FieldMetadata getCompositionRelationField(JavaType entity, ClassOrInterfaceTypeDetails entityDetails, Map<String, FieldMetadata> relationsAsChild) throws ClassNotFoundException {
    // Try to identify if it's is a child part of a composition.
    // It uses details and annotation values instead metadata to
    // avoid problems of circular dependencies
    ClassOrInterfaceTypeDetails parentDatils;
    FieldMetadata compositionRelationField = null;
    AnnotationMetadata parentFieldRelationAnnotation;
    JpaRelationType type;
    String parentMappedBy;
    for (FieldMetadata field : relationsAsChild.values()) {
        parentDatils = getTypeLocationService().getTypeDetails(field.getFieldType().getBaseType());
        for (FieldMetadata parentField : parentDatils.getFieldsWithAnnotation(RooJavaType.ROO_JPA_RELATION)) {
            parentFieldRelationAnnotation = parentField.getAnnotation(RooJavaType.ROO_JPA_RELATION);
            if (parentFieldRelationAnnotation != null && entity.equals(parentField.getFieldType().getBaseType())) {
                parentMappedBy = getFieldMappedByAnnotationValue(parentField);
                if (field.getFieldName().getSymbolName().equals(parentMappedBy)) {
                    // Found parent relation field
                    // Check composition
                    EnumDetails value = ((EnumAttributeValue) parentFieldRelationAnnotation.getAttribute(new JavaSymbolName("type"))).getValue();
                    if (JpaRelationType.COMPOSITION.name().equals(value.getField().getSymbolName())) {
                        // Found composition
                        if (compositionRelationField != null) {
                            throw new IllegalArgumentException(String.format("Found to relations which '%s' is child part of composition relation field: '%s' and '%s'", entity.getFullyQualifiedTypeName(), compositionRelationField.getFieldName(), field.getFieldName()));
                        }
                        compositionRelationField = field;
                    }
                }
            }
        }
    }
    return compositionRelationField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EnumDetails(org.springframework.roo.model.EnumDetails) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaRelationType(org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 18 with EnumDetails

use of org.springframework.roo.model.EnumDetails 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)

Example 19 with EnumDetails

use of org.springframework.roo.model.EnumDetails in project spring-roo by spring-projects.

the class JavaParserAnnotationMetadataBuilder method convert.

@SuppressWarnings("unchecked")
private static MemberValuePair convert(final AnnotationAttributeValue<?> value, CompilationUnitServices compilationUnitServices) {
    if (value instanceof NestedAnnotationAttributeValue) {
        final NestedAnnotationAttributeValue castValue = (NestedAnnotationAttributeValue) value;
        AnnotationExpr annotationExpr;
        final AnnotationMetadata nestedAnnotation = castValue.getValue();
        if (castValue.getValue().getAttributeNames().size() == 0) {
            annotationExpr = new MarkerAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()));
        } else if (castValue.getValue().getAttributeNames().size() == 1 && (castValue.getValue().getAttributeNames().get(0) == null || "value".equals(castValue.getValue().getAttributeNames().get(0).getSymbolName()))) {
            annotationExpr = new SingleMemberAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), convert(nestedAnnotation.getAttribute(nestedAnnotation.getAttributeNames().get(0)), compilationUnitServices).getValue());
        } else {
            final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
            for (final JavaSymbolName attributeName : nestedAnnotation.getAttributeNames()) {
                memberValuePairs.add(convert(nestedAnnotation.getAttribute(attributeName), compilationUnitServices));
            }
            annotationExpr = new NormalAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), memberValuePairs);
        }
        // Add imports for nested annotation types
        JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), nestedAnnotation.getAnnotationType());
        // Rely on the nested instance to know its member value pairs
        return new MemberValuePair(value.getName().getSymbolName(), annotationExpr);
    }
    if (value instanceof BooleanAttributeValue) {
        final boolean castValue = ((BooleanAttributeValue) value).getValue();
        final BooleanLiteralExpr convertedValue = new BooleanLiteralExpr(castValue);
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof CharAttributeValue) {
        final char castValue = ((CharAttributeValue) value).getValue();
        final CharLiteralExpr convertedValue = new CharLiteralExpr(new String(new char[] { castValue }));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof LongAttributeValue) {
        final Long castValue = ((LongAttributeValue) value).getValue();
        final LongLiteralExpr convertedValue = new LongLiteralExpr(castValue.toString() + "L");
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof IntegerAttributeValue) {
        final Integer castValue = ((IntegerAttributeValue) value).getValue();
        final IntegerLiteralExpr convertedValue = new IntegerLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof DoubleAttributeValue) {
        final DoubleAttributeValue doubleAttributeValue = (DoubleAttributeValue) value;
        final Double castValue = doubleAttributeValue.getValue();
        DoubleLiteralExpr convertedValue;
        if (doubleAttributeValue.isFloatingPrecisionOnly()) {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "F");
        } else {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "D");
        }
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof StringAttributeValue) {
        final String castValue = ((StringAttributeValue) value).getValue();
        final StringLiteralExpr convertedValue = new StringLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof EnumAttributeValue) {
        final EnumDetails castValue = ((EnumAttributeValue) value).getValue();
        // This isn't as elegant as it could be (ie loss of type
        // parameters), but it will do for now
        final FieldAccessExpr convertedValue = new FieldAccessExpr(JavaParserUtils.getNameExpr(castValue.getType().getFullyQualifiedTypeName()), castValue.getField().getSymbolName());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ClassAttributeValue) {
        final JavaType castValue = ((ClassAttributeValue) value).getValue();
        // This doesn't preserve type parameters
        final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), castValue);
        final ClassExpr convertedValue = new ClassExpr(JavaParserUtils.getReferenceType(nameExpr));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ArrayAttributeValue) {
        final ArrayAttributeValue<AnnotationAttributeValue<?>> castValue = (ArrayAttributeValue<AnnotationAttributeValue<?>>) value;
        final List<Expression> arrayElements = new ArrayList<Expression>();
        for (final AnnotationAttributeValue<?> v : castValue.getValue()) {
            final MemberValuePair converted = convert(v, compilationUnitServices);
            if (converted != null) {
                arrayElements.add(converted.getValue());
            }
        }
        return new MemberValuePair(value.getName().getSymbolName(), new ArrayInitializerExpr(arrayElements));
    }
    throw new UnsupportedOperationException("Unsupported attribute value '" + value.getName() + "' of type '" + value.getClass().getName() + "'");
}
Also used : IntegerLiteralExpr(com.github.antlrjavaparser.api.expr.IntegerLiteralExpr) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) StringLiteralExpr(com.github.antlrjavaparser.api.expr.StringLiteralExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) ArrayList(java.util.ArrayList) CharAttributeValue(org.springframework.roo.classpath.details.annotations.CharAttributeValue) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MemberValuePair(com.github.antlrjavaparser.api.expr.MemberValuePair) ArrayInitializerExpr(com.github.antlrjavaparser.api.expr.ArrayInitializerExpr) BooleanLiteralExpr(com.github.antlrjavaparser.api.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.antlrjavaparser.api.expr.LongLiteralExpr) ArrayList(java.util.ArrayList) List(java.util.List) FieldAccessExpr(com.github.antlrjavaparser.api.expr.FieldAccessExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) CharLiteralExpr(com.github.antlrjavaparser.api.expr.CharLiteralExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) DoubleLiteralExpr(com.github.antlrjavaparser.api.expr.DoubleLiteralExpr) Expression(com.github.antlrjavaparser.api.expr.Expression) ClassExpr(com.github.antlrjavaparser.api.expr.ClassExpr)

Example 20 with EnumDetails

use of org.springframework.roo.model.EnumDetails in project spring-roo by spring-projects.

the class WsClientsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    RooWsClientsAnnotationValues annotationValues = new RooWsClientsAnnotationValues(governorPhysicalTypeMetadata);
    // Getting @RooWsClients annotation
    ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    AnnotationMetadata wsClientsAnnotation = cid.getAnnotation(ROO_WS_CLIENTS);
    // Getting profile from annotation
    String profile = annotationValues.getProfile();
    // Getting endpoints from annotation
    List<WsClientEndpoint> endpoints = new ArrayList<WsClientEndpoint>();
    AnnotationAttributeValue<?> currentEndpoints = wsClientsAnnotation.getAttribute("endpoints");
    if (currentEndpoints != null) {
        List<?> values = (List<?>) currentEndpoints.getValue();
        Iterator<?> valuesIt = values.iterator();
        while (valuesIt.hasNext()) {
            NestedAnnotationAttributeValue wsClientAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
            if (wsClientAnnotation.getValue() != null && wsClientAnnotation.getValue().getAttribute("endpoint") != null) {
                // Get endpoint name
                String endpointName = null;
                if (wsClientAnnotation.getValue().getAttribute("endpoint").getValue() instanceof String) {
                    endpointName = (String) wsClientAnnotation.getValue().getAttribute("endpoint").getValue();
                }
                Validate.notNull(endpointName, "'endpoint' attribute in @RooWsClient must be a String");
                // Get endpoint nameSpace
                String endpointNameSpace = null;
                if (wsClientAnnotation.getValue().getAttribute("targetNamespace").getValue() instanceof String) {
                    endpointNameSpace = (String) wsClientAnnotation.getValue().getAttribute("targetNamespace").getValue();
                }
                Validate.notNull(endpointName, "'targetNamespace' attribute in @RooWsClient must be a String");
                // Get endpoint binding type
                EnumDetails endpointBindingType = null;
                if (wsClientAnnotation.getValue().getAttribute("binding").getValue() instanceof EnumDetails) {
                    endpointBindingType = (EnumDetails) wsClientAnnotation.getValue().getAttribute("binding").getValue();
                }
                Validate.notNull(endpointBindingType, "'binding' attribute in @RooWsClient must be a SoapBindingType");
                endpoints.add(new WsClientEndpoint(endpointName, endpointNameSpace, endpointBindingType));
            }
        }
    }
    return new WsClientsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, endpoints, profile);
}
Also used : ArrayList(java.util.ArrayList) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

EnumDetails (org.springframework.roo.model.EnumDetails)26 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)25 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)17 ArrayList (java.util.ArrayList)16 JavaType (org.springframework.roo.model.JavaType)12 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)11 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)11 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)9 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)7 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)6 RooJavaType (org.springframework.roo.model.RooJavaType)6 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)5 JdkJavaType (org.springframework.roo.model.JdkJavaType)5 List (java.util.List)4 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)4 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)3 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)3 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)3 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)3 StringField (org.springframework.roo.classpath.operations.jsr303.StringField)3