Search in sources :

Example 71 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method addCollaboratingFieldsToBuilder.

private void addCollaboratingFieldsToBuilder(final Set<ClassOrInterfaceTypeDetails> entityFactoryClasses) {
    final Set<JavaSymbolName> fields = new LinkedHashSet<JavaSymbolName>();
    for (final JavaType entityNeedingCollaborator : this.requiredDataOnDemandCollaborators) {
        final JavaType collaboratorType = getCollaboratingType(entityNeedingCollaborator, entityFactoryClasses);
        if (collaboratorType != null) {
            final String collaboratingFieldName = getCollaboratingFieldName(entityNeedingCollaborator, entityFactoryClasses).getSymbolName();
            final JavaSymbolName fieldSymbolName = new JavaSymbolName(collaboratingFieldName);
            final FieldMetadata candidate = governorTypeDetails.getField(fieldSymbolName);
            if (candidate != null) {
                // We really expect the field to be correct if we're going to
                // rely on it
                Validate.isTrue(candidate.getFieldType().equals(collaboratorType), "Field '%s' on '%s' must be of type '%s'", collaboratingFieldName, destination.getFullyQualifiedTypeName(), collaboratorType.getFullyQualifiedTypeName());
                Validate.isTrue(Modifier.isPrivate(candidate.getModifier()), "Field '%s' on '%s' must be private", collaboratingFieldName, destination.getFullyQualifiedTypeName());
                Validate.notNull(MemberFindingUtils.getAnnotationOfType(candidate.getAnnotations(), AUTOWIRED), "Field '%s' on '%s' must be @Autowired", collaboratingFieldName, destination.getFullyQualifiedTypeName());
                // It's ok, so we can move onto the new field
                continue;
            }
            // Create field and add it to the ITD, if it hasn't already been
            if (!fields.contains(fieldSymbolName)) {
                // Must make the field
                final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
                annotations.add(new AnnotationMetadataBuilder(AUTOWIRED));
                builder.addField(new FieldMetadataBuilder(getId(), 0, annotations, fieldSymbolName, collaboratorType));
                fields.add(fieldSymbolName);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 72 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String entityFactoryMetadata, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Get related entity
    final JavaType entity = (JavaType) governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue();
    if (entity == null) {
        return null;
    }
    final MemberDetails memberDetails = getMemberDetails(entity);
    if (memberDetails == null) {
        return null;
    }
    final MemberHoldingTypeDetails persistenceMemberHoldingTypeDetails = MemberFindingUtils.getMostConcreteMemberHoldingTypeDetailsWithTag(memberDetails, PERSISTENT_TYPE);
    if (persistenceMemberHoldingTypeDetails == null) {
        return null;
    }
    // We need to be informed if our dependent metadata changes
    getMetadataDependencyRegistry().registerDependency(persistenceMemberHoldingTypeDetails.getDeclaredByMetadataId(), entityFactoryMetadata);
    final JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    if (identifierType == null) {
        return null;
    }
    // Identify all the fields we care about on the entity
    final Map<FieldMetadata, JpaEntityFactoryMetadata> locatedFields = getLocatedFields(memberDetails, entityFactoryMetadata);
    // Get the list of embedded metadata holders - may be an empty list if
    // no embedded identifier exists
    final List<EmbeddedHolder> embeddedHolders = getEmbeddedHolders(memberDetails, entityFactoryMetadata);
    // Get the embedded identifier metadata holder - may be null if no
    // embedded identifier exists
    final EmbeddedIdHolder embeddedIdHolder = getEmbeddedIdHolder(memberDetails, entityFactoryMetadata);
    Set<ClassOrInterfaceTypeDetails> entityFactoryClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
    return new JpaEntityFactoryMetadata(entityFactoryMetadata, aspectName, governorPhysicalTypeMetadata, entity, locatedFields, embeddedHolders, entityFactoryClasses, embeddedIdHolder);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EmbeddedIdHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedIdHolder) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) EmbeddedHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedHolder)

Example 73 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadataProviderImpl method getLocatedFields.

private Map<FieldMetadata, JpaEntityFactoryMetadata> getLocatedFields(final MemberDetails memberDetails, final String dodMetadataId) {
    final Map<FieldMetadata, JpaEntityFactoryMetadata> locatedFields = new LinkedHashMap<FieldMetadata, JpaEntityFactoryMetadata>();
    final Iterable<ClassOrInterfaceTypeDetails> entityFactoryTypes = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_JPA_ENTITY_FACTORY);
    final List<MethodMetadata> mutatorMethods = memberDetails.getMethods();
    // To avoid unnecessary rewriting of the DoD ITD we sort the mutators by
    // method name to provide a consistent ordering
    Collections.sort(mutatorMethods, new NaturalOrderComparator<MethodMetadata>() {

        @Override
        protected String stringify(final MethodMetadata object) {
            return object.getMethodName().getSymbolName();
        }
    });
    for (final MethodMetadata method : mutatorMethods) {
        if (!BeanInfoUtils.isMutatorMethod(method)) {
            continue;
        }
        final FieldMetadata field = BeanInfoUtils.getFieldForJavaBeanMethod(memberDetails, method);
        if (field == null) {
            continue;
        }
        // Track any changes to the mutator method (eg it goes away)
        getMetadataDependencyRegistry().registerDependency(method.getDeclaredByMetadataId(), dodMetadataId);
        final Set<Object> fieldCustomDataKeys = field.getCustomData().keySet();
        // types
        if (fieldCustomDataKeys.contains(IDENTIFIER_FIELD) || fieldCustomDataKeys.contains(EMBEDDED_ID_FIELD) || fieldCustomDataKeys.contains(EMBEDDED_FIELD) || fieldCustomDataKeys.contains(VERSION_FIELD)) {
            continue;
        }
        // Never include persistence transient fields
        if (fieldCustomDataKeys.contains(TRANSIENT_FIELD)) {
            continue;
        }
        // entities by hand
        if (field.getFieldType().isCommonCollectionType() || fieldCustomDataKeys.contains(ONE_TO_MANY_FIELD) || fieldCustomDataKeys.contains(MANY_TO_MANY_FIELD)) {
            continue;
        }
        // Look up collaborating metadata
        final JpaEntityFactoryMetadata collaboratingMetadata = locateCollaboratingMetadata(dodMetadataId, field, entityFactoryTypes);
        locatedFields.put(field, collaboratingMetadata);
    }
    return locatedFields;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) LinkedHashMap(java.util.LinkedHashMap) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 74 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadataProviderImpl method getEmbeddedHolders.

private List<EmbeddedHolder> getEmbeddedHolders(final MemberDetails memberDetails, final String metadataIdentificationString) {
    final List<EmbeddedHolder> embeddedHolders = new ArrayList<EmbeddedHolder>();
    final List<FieldMetadata> embeddedFields = MemberFindingUtils.getFieldsWithTag(memberDetails, EMBEDDED_FIELD);
    if (embeddedFields.isEmpty()) {
        return embeddedHolders;
    }
    for (final FieldMetadata embeddedField : embeddedFields) {
        final MemberDetails embeddedMemberDetails = getMemberDetails(embeddedField.getFieldType());
        if (embeddedMemberDetails == null) {
            continue;
        }
        final List<FieldMetadata> fields = new ArrayList<FieldMetadata>();
        for (final FieldMetadata field : embeddedMemberDetails.getFields()) {
            if (!(Modifier.isStatic(field.getModifier()) || Modifier.isFinal(field.getModifier()) || Modifier.isTransient(field.getModifier()))) {
                getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
                fields.add(field);
            }
        }
        embeddedHolders.add(new EmbeddedHolder(embeddedField, fields));
    }
    return embeddedHolders;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) EmbeddedHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedHolder)

Example 75 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class JspViewManager method createFieldsForCreateAndUpdate.

private void createFieldsForCreateAndUpdate(final List<FieldMetadata> formFields, final Document document, final Element root, final boolean isCreate) {
    for (final FieldMetadata field : formFields) {
        final String fieldName = field.getFieldName().getSymbolName();
        JavaType fieldType = field.getFieldType();
        AnnotationMetadata annotationMetadata;
        // Ignoring java.util.Map field types (see ROO-194)
        if (fieldType.equals(new JavaType(Map.class.getName()))) {
            continue;
        }
        // separately to the field list
        if (field.getCustomData().keySet().contains(CustomDataKeys.EMBEDDED_ID_FIELD)) {
            continue;
        }
        fieldType = getJavaTypeForField(field);
        final JavaTypeMetadataDetails typeMetadataHolder = relatedDomainTypes.get(fieldType);
        JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = null;
        if (typeMetadataHolder != null) {
            typePersistenceMetadataHolder = typeMetadataHolder.getPersistenceDetails();
        }
        Element fieldElement = null;
        if (fieldType.getFullyQualifiedTypeName().equals(Boolean.class.getName()) || fieldType.getFullyQualifiedTypeName().equals(boolean.class.getName())) {
            fieldElement = document.createElement("field:checkbox");
        // Handle enum fields
        } else if (typeMetadataHolder != null && typeMetadataHolder.isEnumType()) {
            fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + typeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("path", getPathForType(fieldType)).build();
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_MANY_FIELD)) {
            // accordingly
            if (typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:simple", document).addAttribute("messageCode", "entity_reference_not_managed").addAttribute("messageCodeAttribute", new JavaSymbolName(fieldType.getSimpleTypeName()).getReadableSymbolName()).build();
            } else {
                continue;
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_ONE_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_ONE_FIELD)) {
            final JavaType referenceType = getJavaTypeForField(field);
            final JavaTypeMetadataDetails referenceTypeMetadata = relatedDomainTypes.get(referenceType);
            if (referenceType != null && referenceTypeMetadata != null && referenceTypeMetadata.isApplicationType() && typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + referenceTypeMetadata.getPlural().toLowerCase() + "}").addAttribute("itemValue", typePersistenceMetadataHolder.getIdentifierField().getFieldName().getSymbolName()).addAttribute("path", "/" + getPathForType(getJavaTypeForField(field))).build();
                if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD)) {
                    fieldElement.setAttribute("multiple", "true");
                }
            }
        } else if (fieldType.equals(DATE) || fieldType.equals(CALENDAR)) {
            if (fieldName.equals(CREATED)) {
                continue;
            }
            // Only include the date picker for styles supported by Dojo
            // (SMALL & MEDIUM)
            fieldElement = new XmlElementBuilder("field:datetime", document).addAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}").build();
            if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE)) {
                fieldElement.setAttribute("future", "true");
            } else if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST)) {
                fieldElement.setAttribute("past", "true");
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.LOB_FIELD)) {
            fieldElement = new XmlElementBuilder("field:textarea", document).build();
        }
        if ((annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE)) != null) {
            final AnnotationAttributeValue<?> max = annotationMetadata.getAttribute(new JavaSymbolName("max"));
            if (max != null) {
                final int maxValue = (Integer) max.getValue();
                if (fieldElement == null && maxValue > 30) {
                    fieldElement = new XmlElementBuilder("field:textarea", document).build();
                }
            }
        }
        // Use a default input field if no other criteria apply
        if (fieldElement == null) {
            fieldElement = document.createElement("field:input");
        }
        addCommonAttributes(field, fieldElement);
        fieldElement.setAttribute("field", fieldName);
        fieldElement.setAttribute("id", XmlUtils.convertId("c:" + formBackingType.getFullyQualifiedTypeName() + "." + field.getFieldName().getSymbolName()));
        // If identifier manually assigned, then add 'required=true'
        if (formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().equals(field.getFieldName()) && field.getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
            fieldElement.setAttribute("required", "true");
        }
        fieldElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldElement));
        root.appendChild(fieldElement);
    }
}
Also used : JavaTypePersistenceMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) Element(org.w3c.dom.Element) JavaTypeMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)141 JavaType (org.springframework.roo.model.JavaType)76 ArrayList (java.util.ArrayList)69 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)40 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)40 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)39 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)38 RooJavaType (org.springframework.roo.model.RooJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)33 JpaJavaType (org.springframework.roo.model.JpaJavaType)32 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)30 SpringJavaType (org.springframework.roo.model.SpringJavaType)30 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)29 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)21 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)21 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)18 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)17 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15