Search in sources :

Example 76 with MethodMetadata

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

the class JpaDataOnDemandMetadataProviderImpl method getLocalMidToRequest.

@Override
protected String getLocalMidToRequest(final ItdTypeDetails itdTypeDetails) {
    // Determine the governor for this ITD, and whether any DOD metadata is
    // even hoping to hear about changes to that JavaType and its ITDs
    final JavaType governor = itdTypeDetails.getName();
    for (final JavaType type : itdTypeDetails.getGovernor().getLayerEntities()) {
        final String localMidType = entityToDodMidMap.get(type);
        if (localMidType != null) {
            return localMidType;
        }
    }
    final String localMid = entityToDodMidMap.get(governor);
    if (localMid == null) {
        // No DOD is interested in this JavaType, so let's move on
        return null;
    }
    // match our requirements
    for (final MethodMetadata method : itdTypeDetails.getDeclaredMethods()) {
        if (BeanInfoUtils.isMutatorMethod(method)) {
            // a direct dependency on it for the future.
            return localMid;
        }
    }
    return null;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Example 77 with MethodMetadata

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

the class JpaEntityMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalType, final String itdFilename) {
    if (projectOperations == null) {
        projectOperations = getProjectOperations();
    }
    Validate.notNull(projectOperations, "ProjectOperations is required");
    // Find out the entity-level JPA details from the trigger annotation
    final JpaEntityAnnotationValues jpaEntityAnnotationValues = getJpaEntityAnnotationValues(governorPhysicalType);
    /*
     * Walk the inheritance hierarchy for any existing JpaEntityMetadata. We
     * don't need to monitor any such parent, as any changes to its Java
     * type will trickle down to the governing java type.
     */
    final JpaEntityMetadata parent = getParentMetadata(governorPhysicalType.getMemberHoldingTypeDetails());
    // Get the governor's members
    final MemberDetails governorMemberDetails = getMemberDetails(governorPhysicalType);
    final String moduleName = PhysicalTypeIdentifierNamingUtils.getPath(metadataIdentificationString).getModule();
    if (projectOperations.isProjectAvailable(moduleName)) {
        // If the project itself changes, we want a chance to refresh this
        // item
        getMetadataDependencyRegistry().registerDependency(ProjectMetadata.getProjectIdentifier(moduleName), metadataIdentificationString);
    }
    // Getting entity details
    JavaType entity = JpaEntityMetadata.getJavaType(metadataIdentificationString);
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    // Getting JavaBeanMetadata
    String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    JavaBeanMetadata entityJavaBeanMetadata = getMetadataService().get(javaBeanMetadataKey);
    // This metadata is not available yet
    if (entityJavaBeanMetadata == null) {
        return null;
    }
    // Locate relation fields to process
    List<FieldMetadata> fieldsParent = new ArrayList<FieldMetadata>();
    Map<String, FieldMetadata> relationsAsChild = new HashMap<String, FieldMetadata>();
    for (FieldMetadata field : entityDetails.getDeclaredFields()) {
        if (field.getAnnotation(RooJavaType.ROO_JPA_RELATION) != null) {
            fieldsParent.add(field);
        } else if (field.getAnnotation(JpaJavaType.ONE_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_MANY) != null) {
            relationsAsChild.put(field.getFieldName().getSymbolName(), field);
        }
    }
    // Check if it's a child part of a composition
    FieldMetadata compositionRelationField;
    try {
        compositionRelationField = getCompositionRelationField(entity, entityDetails, relationsAsChild);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Problems found when trying to identify composition relationship", e);
    }
    // Getting identifier field and version field and its accessors
    FieldMetadata identifierField = null;
    MethodMetadata identifierAccessor = null;
    FieldMetadata versionField = null;
    MethodMetadata versionAccessor = null;
    if (parent == null) {
        // Obtain identifier field from entity details
        List<FieldMetadata> identifierFields = entityDetails.getFieldsWithAnnotation(ID);
        List<FieldMetadata> embeddedIdentifierFields = entityDetails.getFieldsWithAnnotation(EMBEDDED_ID);
        Validate.isTrue(!(identifierFields.isEmpty() && embeddedIdentifierFields.isEmpty()), String.format("ERROR: The annotated entity '%s' doesn't contain any identifier field.", entityDetails.getType().getFullyQualifiedTypeName()));
        if (!identifierFields.isEmpty()) {
            identifierField = identifierFields.get(0);
        } else if (!embeddedIdentifierFields.isEmpty()) {
            identifierField = embeddedIdentifierFields.get(0);
        }
        identifierAccessor = entityJavaBeanMetadata.getAccesorMethod(identifierField);
        // Obtain version field from entity details
        List<FieldMetadata> versionFields = entityDetails.getFieldsWithAnnotation(VERSION);
        // Check and add version field
        if (!versionFields.isEmpty()) {
            versionField = versionFields.get(0);
            versionAccessor = entityJavaBeanMetadata.getAccesorMethod(versionField);
        }
    } else {
        identifierField = parent.getCurrentIndentifierField();
        versionField = parent.getCurrentVersionField();
    }
    return new JpaEntityMetadata(metadataIdentificationString, aspectName, governorPhysicalType, parent, governorMemberDetails, identifierField, identifierAccessor, versionField, versionAccessor, jpaEntityAnnotationValues, entityDetails, fieldsParent, relationsAsChild, compositionRelationField);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 78 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 79 with MethodMetadata

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

the class IdentifierMetadata method getAccessors.

/**
 * Locates the accessor methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * accessors will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the accessors (never returns null)
 */
private List<MethodMetadataBuilder> getAccessors(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> accessors = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the accessors that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(field.getFieldName(), field.getFieldType());
        final MethodMetadata accessor = getGovernorMethod(requiredAccessorName);
        if (accessor == null) {
            accessors.add(getAccessorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(accessor.getModifier()), "User provided field but failed to provide a public '%s()' method in '%s'", requiredAccessorName.getSymbolName(), destination.getFullyQualifiedTypeName());
            accessors.add(new MethodMetadataBuilder(accessor));
        }
    }
    return accessors;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 80 with MethodMetadata

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

the class JspViewManager method getUpdateDocument.

public Document getUpdateDocument() {
    final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
    final Document document = builder.newDocument();
    // Add document namespaces
    final Element div = (Element) document.appendChild(new XmlElementBuilder("div", document).addAttribute("xmlns:form", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:field", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("version", "2.0").addChild(new XmlElementBuilder("jsp:directive.page", document).addAttribute("contentType", "text/html;charset=UTF-8").build()).addChild(new XmlElementBuilder("jsp:output", document).addAttribute("omit-xml-declaration", "yes").build()).build());
    // Add form update element
    final Element formUpdate = new XmlElementBuilder("form:update", document).addAttribute("id", XmlUtils.convertId("fu:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).build();
    if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
        formUpdate.setAttribute("path", controllerPath);
    }
    if (!"id".equals(formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName())) {
        formUpdate.setAttribute("idField", formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName());
    }
    final MethodMetadata versionAccessorMethod = formBackingTypePersistenceMetadata.getVersionAccessorMethod();
    if (versionAccessorMethod == null) {
        formUpdate.setAttribute("versionField", "none");
    } else {
        final String methodName = versionAccessorMethod.getMethodName().getSymbolName();
        formUpdate.setAttribute("versionField", methodName.substring("get".length()));
    }
    // Filter out embedded ID fields as they represent the composite PK
    // which is not to be updated.
    final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
    for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
        for (int i = 0; i < fieldCopy.size(); i++) {
            // Make sure form fields are not presented twice.
            if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
                fieldCopy.remove(i);
            }
        }
    }
    createFieldsForCreateAndUpdate(fieldCopy, document, formUpdate, false);
    formUpdate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formUpdate));
    div.appendChild(formUpdate);
    return document;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15