Search in sources :

Example 16 with ArrayAttributeValue

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

the class DbreMetadata method excludeFieldsInToStringAnnotation.

// XXX DiSiD: Invoke this method when add non other fields to builder
// http://projects.disid.com/issues/7455
private void excludeFieldsInToStringAnnotation(final String fieldName) {
    if (toStringAnnotation == null) {
        return;
    }
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    final List<StringAttributeValue> ignoreFields = new ArrayList<StringAttributeValue>();
    // Copy the existing attributes, excluding the "ignoreFields" attribute
    boolean alreadyAdded = false;
    AnnotationAttributeValue<?> value = toStringAnnotation.getAttribute(new JavaSymbolName("excludeFields"));
    if (value == null) {
        value = new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), new ArrayList<StringAttributeValue>());
    }
    // Ensure we have an array of strings
    final String errMsg = "@RooToString attribute 'excludeFields' must be an array of strings";
    Validate.isInstanceOf(ArrayAttributeValue.class, value, errMsg);
    final ArrayAttributeValue<?> arrayVal = (ArrayAttributeValue<?>) value;
    for (final Object obj : arrayVal.getValue()) {
        Validate.isInstanceOf(StringAttributeValue.class, obj, errMsg);
        final StringAttributeValue sv = (StringAttributeValue) obj;
        if (sv.getValue().equals(fieldName)) {
            alreadyAdded = true;
        }
        ignoreFields.add(sv);
    }
    // Add the desired field to ignore to the end
    if (!alreadyAdded) {
        ignoreFields.add(new StringAttributeValue(new JavaSymbolName("ignored"), fieldName));
    }
    attributes.add(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), ignoreFields));
    final AnnotationMetadataBuilder toStringAnnotationBuilder = new AnnotationMetadataBuilder(ROO_TO_STRING, attributes);
    updatedGovernorBuilder = new ClassOrInterfaceTypeDetailsBuilder(governorTypeDetails);
    toStringAnnotation = toStringAnnotationBuilder.build();
    updatedGovernorBuilder.updateTypeAnnotation(toStringAnnotation, new HashSet<JavaSymbolName>());
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayList(java.util.ArrayList) 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)

Example 17 with ArrayAttributeValue

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

the class DbreMetadata method addCascadeType.

private void addCascadeType(final AnnotationMetadataBuilder annotationBuilder, final CascadeAction onUpdate, final CascadeAction onDelete) {
    final String attributeName = "cascade";
    boolean hasCascadeType = true;
    if (onUpdate == CascadeAction.CASCADE && onDelete == CascadeAction.CASCADE) {
        annotationBuilder.addEnumAttribute(attributeName, CASCADE_TYPE, "ALL");
    } else if (onUpdate == CascadeAction.CASCADE && onDelete != CascadeAction.CASCADE) {
        final List<EnumAttributeValue> arrayValues = new ArrayList<EnumAttributeValue>();
        arrayValues.add(new EnumAttributeValue(new JavaSymbolName(attributeName), new EnumDetails(CASCADE_TYPE, new JavaSymbolName("PERSIST"))));
        arrayValues.add(new EnumAttributeValue(new JavaSymbolName(attributeName), new EnumDetails(CASCADE_TYPE, new JavaSymbolName("MERGE"))));
        annotationBuilder.addAttribute(new ArrayAttributeValue<EnumAttributeValue>(new JavaSymbolName(attributeName), arrayValues));
    } else if (onUpdate != CascadeAction.CASCADE && onDelete == CascadeAction.CASCADE) {
        annotationBuilder.addEnumAttribute(attributeName, CASCADE_TYPE.getSimpleTypeName(), "REMOVE");
    } else {
        hasCascadeType = false;
    }
    if (hasCascadeType) {
        builder.getImportRegistrationResolver().addImport(CASCADE_TYPE);
    }
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) List(java.util.List) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails)

Example 18 with ArrayAttributeValue

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

the class FinderOperationsImpl method installFinder.

public void installFinder(final JavaType entity, final JavaSymbolName finderName, JavaType formBean, JavaType returnType) {
    Validate.notNull(entity, "ERROR: Entity type required to generate finder.");
    Validate.notNull(finderName, "ERROR: Finder name required to generate finder.");
    final String id = getTypeLocationService().getPhysicalTypeIdentifier(entity);
    if (id == null) {
        LOGGER.warning("Cannot locate source for '" + entity.getFullyQualifiedTypeName() + "'");
        return;
    }
    // Check if provided entity is annotated with @RooJpaEntity
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    AnnotationMetadata entityAnnotation = entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY);
    Validate.notNull(entityAnnotation, "ERROR: Provided entity must be annotated with @RooJpaEntity");
    // Getting repository that manages current entity
    ClassOrInterfaceTypeDetails repository = getRepositoryJpaLocator().getRepository(entity);
    // Entity must have a repository that manages it, if not, shows an error
    Validate.notNull(repository, "ERROR: You must generate a repository to the provided entity before to add new finder. You could use 'repository jpa' commands.");
    // Check if provided formBean contains the field names and types indicated as finder params
    if (formBean != null) {
        MemberDetails entityMemberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(entity));
        PartTree finderPartTree = new PartTree(finderName.getSymbolName(), entityMemberDetails);
        checkDtoFieldsForFinder(getTypeLocationService().getTypeDetails(formBean), finderPartTree, repository.getType());
    }
    // Get repository annotation
    AnnotationMetadata repositoryAnnotation = repository.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA);
    AnnotationMetadataBuilder repositoryAnnotationBuilder = new AnnotationMetadataBuilder(repositoryAnnotation);
    // Create list that will include finders to add
    List<AnnotationAttributeValue<?>> finders = new ArrayList<AnnotationAttributeValue<?>>();
    // Check if new finder name to be included already exists in @RooRepositoryJpa annotation
    AnnotationAttributeValue<?> currentFinders = repositoryAnnotation.getAttribute("finders");
    if (currentFinders != null) {
        List<?> values = (List<?>) currentFinders.getValue();
        Iterator<?> it = values.iterator();
        while (it.hasNext()) {
            NestedAnnotationAttributeValue finder = (NestedAnnotationAttributeValue) it.next();
            if (finder.getValue() != null && finder.getValue().getAttribute("value") != null) {
                if (finder.getValue().getAttribute("value").getValue().equals(finderName.getSymbolName())) {
                    LOGGER.log(Level.WARNING, String.format("ERROR: Finder '%s' already exists on entity '%s'", finderName.getSymbolName(), entity.getSimpleTypeName()));
                    return;
                }
                finders.add(finder);
            }
        }
    }
    // Create @RooFinder
    AnnotationMetadataBuilder singleFinderAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_FINDER);
    // Add finder attribute
    singleFinderAnnotation.addStringAttribute("value", finderName.getSymbolName());
    // Add returnType attribute
    singleFinderAnnotation.addClassAttribute("returnType", returnType);
    // Prevent errors validating if the return type contains a valid module
    if (returnType.getModule() != null) {
        getProjectOperations().addModuleDependency(repository.getName().getModule(), returnType.getModule());
    }
    // Add formBean attribute
    if (formBean != null) {
        singleFinderAnnotation.addClassAttribute("formBean", formBean);
        getProjectOperations().addModuleDependency(repository.getName().getModule(), formBean.getModule());
    }
    NestedAnnotationAttributeValue newFinder = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), singleFinderAnnotation.build());
    // If not exists current finder, include it
    finders.add(newFinder);
    // Add finder list to currentFinders
    ArrayAttributeValue<AnnotationAttributeValue<?>> newFinders = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), finders);
    // Include finder name to finders attribute
    repositoryAnnotationBuilder.addAttribute(newFinders);
    // Update @RooRepositoryJpa annotation
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(repository);
    // Update annotation
    cidBuilder.updateTypeAnnotation(repositoryAnnotationBuilder);
    // Save changes on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 19 with ArrayAttributeValue

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

the class WebFinderOperationsImpl method updateExistingSearchController.

/**
 * Update existing controller with new query methods
 * @param queryMethods the finder names to add
 * @param controllerToUpdateOrCreate the existing controller
 * @param controllerBuilder the builder to apply the changes
 * @return
 */
private boolean updateExistingSearchController(List<String> queryMethods, ClassOrInterfaceTypeDetails controllerToUpdateOrCreate, ClassOrInterfaceTypeDetailsBuilder controllerBuilder) {
    // Get @RooSearch and build necessary variables
    AnnotationMetadata searchAnnotation = controllerToUpdateOrCreate.getAnnotation(RooJavaType.ROO_SEARCH);
    List<AnnotationAttributeValue<?>> findersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    List<String> finderNames = new ArrayList<String>();
    boolean findersAdded = false;
    // Get existent finder values
    if (searchAnnotation != null && searchAnnotation.getAttribute("finders") != null) {
        List<?> existingFinders = (List<?>) searchAnnotation.getAttribute("finders").getValue();
        Iterator<?> it = existingFinders.iterator();
        // Add existent finders to new attributes array to merge with new ones
        while (it.hasNext()) {
            StringAttributeValue attributeValue = (StringAttributeValue) it.next();
            findersToAdd.add(attributeValue);
            finderNames.add(attributeValue.getValue());
        }
        // Add new finders to new attributes array
        for (String finder : queryMethods) {
            if (!finderNames.contains(finder)) {
                findersToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), finder));
                findersAdded = true;
            }
        }
        // Add attributes array to @RooSearch
        AnnotationMetadataBuilder searchAnnotationBuilder = new AnnotationMetadataBuilder(searchAnnotation);
        ArrayAttributeValue<AnnotationAttributeValue<?>> allFinders = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), findersToAdd);
        searchAnnotationBuilder.addAttribute(allFinders);
        controllerBuilder.updateTypeAnnotation(searchAnnotationBuilder);
    }
    return findersAdded;
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) List(java.util.List) ArrayList(java.util.ArrayList) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)19 ArrayList (java.util.ArrayList)18 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)16 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)13 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)13 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)11 List (java.util.List)10 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)10 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)9 JavaType (org.springframework.roo.model.JavaType)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)5 EnumDetails (org.springframework.roo.model.EnumDetails)5 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)4 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)4 CharAttributeValue (org.springframework.roo.classpath.details.annotations.CharAttributeValue)4 DoubleAttributeValue (org.springframework.roo.classpath.details.annotations.DoubleAttributeValue)4 IntegerAttributeValue (org.springframework.roo.classpath.details.annotations.IntegerAttributeValue)3 LongAttributeValue (org.springframework.roo.classpath.details.annotations.LongAttributeValue)3