Search in sources :

Example 56 with FieldMetadata

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

the class JpaEntityFactoryMetadataProviderImpl method getEmbeddedIdHolder.

private EmbeddedIdHolder getEmbeddedIdHolder(final MemberDetails memberDetails, final String metadataIdentificationString) {
    final List<FieldMetadata> idFields = new ArrayList<FieldMetadata>();
    final List<FieldMetadata> fields = MemberFindingUtils.getFieldsWithTag(memberDetails, EMBEDDED_ID_FIELD);
    if (fields.isEmpty()) {
        return null;
    }
    final FieldMetadata embeddedIdField = fields.get(0);
    final MemberDetails identifierMemberDetails = getMemberDetails(embeddedIdField.getFieldType());
    if (identifierMemberDetails == null) {
        return null;
    }
    for (final FieldMetadata field : identifierMemberDetails.getFields()) {
        if (!(Modifier.isStatic(field.getModifier()) || Modifier.isFinal(field.getModifier()) || Modifier.isTransient(field.getModifier()))) {
            getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
            idFields.add(field);
        }
    }
    return new EmbeddedIdHolder(embeddedIdField, idFields);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EmbeddedIdHolder(org.springframework.roo.addon.jpa.addon.dod.EmbeddedIdHolder) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 57 with FieldMetadata

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

the class PartTreeUnitTest method setUp.

@Before
public void setUp() throws IllegalArgumentException, IllegalAccessException {
    List<FieldMetadata> declaredFields = new ArrayList<FieldMetadata>();
    declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "text", 0, null, new JavaSymbolName("text"), new JavaType(String.class), null));
    declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "number", 0, null, new JavaSymbolName("number"), new JavaType(Integer.class), null));
    declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "date", 0, null, new JavaSymbolName("date"), new JavaType(Date.class), null));
    declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "enumer", 0, null, new JavaSymbolName("enumer"), new JavaType(Enum.class), null));
    declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "primitiveInt", 0, null, new JavaSymbolName("primitiveInt"), JavaType.INT_PRIMITIVE, null));
    final List<MemberHoldingTypeDetails> memberHoldingTypeDetails = new ArrayList<MemberHoldingTypeDetails>();
    memberHoldingTypeDetails.add(new DefaultClassOrInterfaceTypeDetails(new CustomDataImpl(new HashMap<Object, Object>()), "Example", 0, null, new JavaType("Example"), PhysicalTypeCategory.CLASS, null, declaredFields, null, null, null, null, null, null, null, null));
    memberDetails = new MemberDetailsImpl(memberHoldingTypeDetails);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DefaultFieldMetadata(org.springframework.roo.classpath.details.DefaultFieldMetadata) ArrayList(java.util.ArrayList) DefaultClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.DefaultClassOrInterfaceTypeDetails) MemberDetailsImpl(org.springframework.roo.classpath.scanner.MemberDetailsImpl) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavaType(org.springframework.roo.model.JavaType) CustomDataImpl(org.springframework.roo.model.CustomDataImpl) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) DefaultFieldMetadata(org.springframework.roo.classpath.details.DefaultFieldMetadata) Before(org.junit.Before)

Example 58 with FieldMetadata

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

the class RepositoryJpaCustomMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final RepositoryJpaCustomAnnotationValues annotationValues = new RepositoryJpaCustomAnnotationValues(governorPhysicalTypeMetadata);
    // Getting repository custom
    JavaType entity = annotationValues.getEntity();
    Validate.notNull(entity, "ERROR: Repository custom interface should be contain an entity on @RooJpaRepositoryCustom annotation");
    final ClassOrInterfaceTypeDetails repositoryClass = getRepositoryJpaLocator().getRepository(entity);
    final String repositoryMedatadataId = RepositoryJpaMetadata.createIdentifier(repositoryClass);
    registerDependency(repositoryMedatadataId, metadataIdentificationString);
    RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMedatadataId);
    // This metadata is not available yet.
    if (repositoryMetadata == null) {
        return null;
    }
    // Add dependency between modules
    ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    String module = cid.getName().getModule();
    getTypeLocationService().addModuleDependency(module, entity);
    getTypeLocationService().addModuleDependency(module, repositoryMetadata.getDefaultReturnType());
    // Get field which entity is field part
    List<Pair<FieldMetadata, RelationInfo>> relationsAsChild = getJpaOperations().getFieldChildPartOfRelation(entity);
    for (Pair<FieldMetadata, RelationInfo> fieldInfo : relationsAsChild) {
        // Add dependency between modules
        getTypeLocationService().addModuleDependency(module, fieldInfo.getLeft().getFieldType());
    }
    // Register dependency between JavaBeanMetadata and this one
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    final String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    registerDependency(javaBeanMetadataKey, metadataIdentificationString);
    String entityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataKey);
    // Entity metadata is not available
    if (entityMetadata == null) {
        return null;
    }
    return new RepositoryJpaCustomMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, entityMetadata.getCurrentIndentifierField().getFieldType(), entity, repositoryMetadata, relationsAsChild);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Example 59 with FieldMetadata

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

the class FieldCommands method checkFieldExists.

/**
 * Checks if entity has already a field with the same name and throws an exception
 * in that case.
 *
 * @param fieldName
 * @param shellContext
 * @param javaTypeDetails
 *
 * @deprecated this should be done by operation class (see JpaFieldCreatorProvider.checkFieldExists)
 */
private void checkFieldExists(final JavaSymbolName fieldName, ShellContext shellContext, final ClassOrInterfaceTypeDetails javaTypeDetails) {
    MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), javaTypeDetails);
    List<FieldMetadata> fields = memberDetails.getFields();
    for (FieldMetadata field : fields) {
        if (field.getFieldName().equals(fieldName) && !shellContext.isForce()) {
            throw new IllegalArgumentException(String.format("Field '%s' already exists and cannot be created. Try to use a " + "different field name on --fieldName parameter or use --force parameter to overwrite it.", fieldName));
        }
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 60 with FieldMetadata

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

the class DtoOperationsImpl method createProjection.

@Override
public void createProjection(JavaType entity, JavaType name, String fields, String suffix, String formatExpression, String formatMessage) {
    Validate.notNull(name, "Use --class to select the name of the Projection.");
    // TODO: Validate fields for excluding entity collection, transient and
    // static fields from operations (already doing when comming from commands).
    // Set focus on projection module
    projectOperations.setModule(projectOperations.getPomFromModuleName(name.getModule()));
    // Add springlets-context dependency
    projectOperations.addDependency(name.getModule(), SPRINGLETS_CONTEXT_DEPENDENCY);
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    Map<String, FieldMetadata> fieldsToAdd = new LinkedHashMap<String, FieldMetadata>();
    boolean onlyMainEntityFields = true;
    if (fields != null) {
        onlyMainEntityFields = false;
        // Check that id field has been included. If not, include it.
        fields = checkAndAddIdField(entity, fields);
        fieldsToAdd = buildFieldsFromString(fields, entity);
    } else {
        List<FieldMetadata> allFields = memberDetailsScanner.getMemberDetails(this.getClass().getName(), typeLocationService.getTypeDetails(entity)).getFields();
        for (FieldMetadata field : allFields) {
            // Add only valid fields
            if (isFieldValidForProjection(field)) {
                fieldsToAdd.put(field.getFieldName().getSymbolName(), field);
            }
        }
    }
    // Create projection
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, name.getModule()));
    final ClassOrInterfaceTypeDetailsBuilder projectionBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    // Add fields to projection
    addFieldsToProjection(projectionBuilder, fieldsToAdd);
    // @RooJavaBean, @RooToString and @RooEquals
    AnnotationMetadataBuilder rooJavaBeanAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAVA_BEAN);
    rooJavaBeanAnnotation.addBooleanAttribute("settersByDefault", false);
    projectionBuilder.addAnnotation(rooJavaBeanAnnotation);
    AnnotationMetadataBuilder rooToStringAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_TO_STRING);
    AnnotationMetadataBuilder rooEqualsAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_EQUALS);
    projectionBuilder.addAnnotation(rooToStringAnnotation);
    projectionBuilder.addAnnotation(rooEqualsAnnotation);
    // Add @RooEntityProjection
    AnnotationMetadataBuilder projectionAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_ENTITY_PROJECTION);
    projectionAnnotation.addClassAttribute("entity", entity);
    List<StringAttributeValue> fieldNames = new ArrayList<StringAttributeValue>();
    if (onlyMainEntityFields) {
        // Should add all entity fields
        for (FieldMetadata field : fieldsToAdd.values()) {
            fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), field.getFieldName().getSymbolName()));
        }
    } else {
        // --fields option has been completed and validated, so build annotation 'fields'
        // param from selected fields
        String[] fieldsFromCommand = StringUtils.split(fields, ",");
        for (int i = 0; i < fieldsFromCommand.length; i++) {
            fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), fieldsFromCommand[i]));
        }
    }
    projectionAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("fields"), fieldNames));
    // Check for each attribute individually
    if (StringUtils.isNotBlank(formatExpression)) {
        projectionAnnotation.addStringAttribute("formatExpression", formatExpression);
    }
    if (StringUtils.isNotBlank(formatMessage)) {
        projectionAnnotation.addStringAttribute("formatMessage", formatMessage);
    }
    projectionBuilder.addAnnotation(projectionAnnotation);
    // Build and save changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(projectionBuilder.build());
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

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