Search in sources :

Example 86 with ClassOrInterfaceTypeDetails

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

the class RepositoryJpaOperationsImpl method generateRepositoryCustomImpl.

/**
 * Method that generates RepositoryCustom implementation on current package.
 * If this RepositoryCustom implementation already exists in this or other
 * package, will not be generated.
 *
 * @param interfaceType
 * @param repository
 * @param entity
 * @return JavaType with existing or new RepositoryCustom implementation
 */
private JavaType generateRepositoryCustomImpl(JavaType interfaceType, JavaType repository, JavaType entity) {
    // Getting RepositoryCustomImpl JavaType
    JavaType implType = new JavaType(repository.getFullyQualifiedTypeName().concat("Impl"), repository.getModule());
    // Check if new class exists yet
    final String implIdentifier = getPathResolver().getCanonicalPath(implType.getModule(), Path.SRC_MAIN_JAVA, implType);
    if (getFileManager().exists(implIdentifier)) {
        // Type already exists - return
        return implType;
    }
    // Check if already exists some class annotated with
    // @RooJpaRepositoryCustomImpl
    // that implements the same repositoryCustom interface.
    Set<JavaType> repositoriesCustomImpl = getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM_IMPL);
    if (!repositoriesCustomImpl.isEmpty()) {
        Iterator<JavaType> it = repositoriesCustomImpl.iterator();
        while (it.hasNext()) {
            JavaType repositoryCustom = it.next();
            ClassOrInterfaceTypeDetails repositoryDetails = getTypeLocationService().getTypeDetails(repositoryCustom);
            AnnotationMetadata annotation = repositoryDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM_IMPL);
            AnnotationAttributeValue<JavaType> repositoryType = annotation.getAttribute("repository");
            if (repositoryType.getValue().equals(interfaceType)) {
                return repositoryType.getValue();
            }
        }
    }
    // If not, continue creating new RepositoryCustomImpl
    InputStream inputStream = null;
    try {
        // Use defined template
        inputStream = FileUtils.getInputStream(getClass(), "RepositoryCustomImpl-template._java");
        String input = IOUtils.toString(inputStream);
        // Replacing package
        input = input.replace("__PACKAGE__", implType.getPackage().getFullyQualifiedPackageName());
        // Replacing entity import
        input = input.replace("__ENTITY_IMPORT__", entity.getFullyQualifiedTypeName());
        // Replacing interface .class
        input = input.replace("__REPOSITORY_CUSTOM_INTERFACE__", interfaceType.getSimpleTypeName());
        // Replacing class name
        input = input.replaceAll("__REPOSITORY_CUSTOM_IMPL__", implType.getSimpleTypeName());
        // Replacing entity name
        input = input.replace("__ENTITY_NAME__", entity.getSimpleTypeName());
        // Creating RepositoryCustomImpl class
        getFileManager().createOrUpdateTextFileIfRequired(implIdentifier, input, false);
    } catch (final IOException e) {
        throw new IllegalStateException(String.format("Unable to create '%s'", implIdentifier), e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return implType;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) InputStream(java.io.InputStream) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) IOException(java.io.IOException) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 87 with ClassOrInterfaceTypeDetails

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

the class RepositoryJpaIntegrationTestMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final RepositoryJpaIntegrationTestAnnotationValues annotationValues = new RepositoryJpaIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
    // Find SpringJpaDataDetachableConfiguration class
    Set<JavaType> jpaConfigurationClasses = getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_JPA_REPOSITORY_CONFIGURATION);
    Validate.isTrue(!jpaConfigurationClasses.isEmpty(), "Couldn't find the 'SpringDataJpaDetachableRepositoryConfiguration' on the project for '%s'", this.getClass().getName());
    JavaType jpaDetachableRepositoryClass = jpaConfigurationClasses.iterator().next();
    // Get repository metadata
    JavaType repositoryInterface = annotationValues.getTargetClass();
    ClassOrInterfaceTypeDetails repositoryDetails = getTypeLocationService().getTypeDetails(repositoryInterface);
    Validate.notNull(repositoryDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA), "Couldn't find @RooJpaRepository in '%s'", repositoryInterface.getSimpleTypeName());
    String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails);
    RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMetadataId);
    if (repositoryMetadata == null) {
        return null;
    }
    // Get entity identifier info
    JavaType entity = repositoryMetadata.getEntity();
    JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    JavaSymbolName identifierAccessorMethodName = getPersistenceMemberLocator().getIdentifierAccessor(entity).getMethodName();
    // Get entity metadata
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    String jpaEntityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata jpaEntityMetadata = getMetadataService().get(jpaEntityMetadataId);
    if (jpaEntityMetadata == null) {
        return null;
    }
    // Get entity read only
    boolean isReadOnly = jpaEntityMetadata.isReadOnly();
    // Get entity plural
    String entityPlural = getPluralService().getPlural(entity);
    // Get repository default return type
    JavaType defaultReturnType = repositoryMetadata.getDefaultReturnType();
    return new RepositoryJpaIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jpaDetachableRepositoryClass, identifierType, identifierAccessorMethodName, entityPlural, entity, defaultReturnType, isReadOnly);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 88 with ClassOrInterfaceTypeDetails

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

the class ServiceCommands method getEntityPossibleResults.

@CliOptionAutocompleteIndicator(command = "service", param = "entity", help = "--entity option should be an entity.")
public List<String> getEntityPossibleResults(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("entity");
    List<String> allPossibleValues = new ArrayList<String>();
    // Getting all existing entities
    Set<ClassOrInterfaceTypeDetails> entitiesInProject = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY);
    for (ClassOrInterfaceTypeDetails entity : entitiesInProject) {
        String name = replaceTopLevelPackageString(entity, currentText);
        if (!allPossibleValues.contains(name)) {
            allPossibleValues.add(name);
        }
    }
    return allPossibleValues;
}
Also used : ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 89 with ClassOrInterfaceTypeDetails

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

the class ServiceCommands method returnRepositories.

@CliOptionAutocompleteIndicator(command = "service", param = "repository", help = "--repository parameter must  be the repository associated to the entity specified in --entity parameter. Please, write a valid value using autocomplete feature (TAB or CTRL + Space)")
public List<String> returnRepositories(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("repository");
    List<String> allPossibleValues = new ArrayList<String>();
    // Get all defined parameters
    Map<String, String> contextParameters = shellContext.getParameters();
    // Getting entity
    String entity = contextParameters.get("entity");
    if (StringUtils.isNotBlank(entity)) {
        JavaType domainEntity = getJavaTypeConverter().convertFromText(entity, JavaType.class, PROJECT);
        // Check if current entity has valid repository
        Collection<ClassOrInterfaceTypeDetails> repositories = repositoryJpaLocator.getRepositories(domainEntity);
        for (ClassOrInterfaceTypeDetails repository : repositories) {
            String replacedValue = replaceTopLevelPackageString(repository, currentText);
            allPossibleValues.add(replacedValue);
        }
        if (allPossibleValues.isEmpty()) {
            LOGGER.log(Level.INFO, String.format("ERROR: Entity '%s' does not have any repository generated. Use 'repository' commands to generate a valid repository and then try again.", entity));
            allPossibleValues.add("");
        }
    }
    return allPossibleValues;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 90 with ClassOrInterfaceTypeDetails

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

the class JpaEntityFactoryMetadataProviderImpl method getEntityFactoryMetadataId.

private String getEntityFactoryMetadataId(final JavaType javaType, final Iterable<ClassOrInterfaceTypeDetails> entityFactoryTypes) {
    for (final ClassOrInterfaceTypeDetails cid : entityFactoryTypes) {
        final AnnotationMetadata entityFactoryAnnotation = cid.getAnnotation(ROO_JPA_ENTITY_FACTORY);
        final AnnotationAttributeValue<JavaType> entityAttribute = entityFactoryAnnotation.getAttribute("entity");
        if (entityAttribute != null && entityAttribute.getValue().equals(javaType)) {
            // Found the DoD type for the given field's type
            return JpaDataOnDemandMetadata.createIdentifier(cid.getName(), PhysicalTypeIdentifier.getPath(cid.getDeclaredByMetadataId()));
        }
    }
    return null;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17