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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations