use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createLinkFactoryClass.
/**
* Creates a new class which supports its associated controller building
* URL's for its methods
*
* @param controller
* the JavaType of the associated controller
*/
@Override
public void createLinkFactoryClass(JavaType controller) {
// Create name
String name = controller.getSimpleTypeName().concat("LinkFactory");
if (name.contains("Controller")) {
name = name.replace("Controller", "");
}
// Create type
final JavaType linkFactoryJavaType = new JavaType(controller.getPackage().getFullyQualifiedPackageName().concat(".").concat(name), controller.getModule());
// Create identifier
final String linkFactoryPathIdentifier = getPathResolver().getCanonicalPath(linkFactoryJavaType.getModule(), Path.SRC_MAIN_JAVA, linkFactoryJavaType);
final String mid = PhysicalTypeIdentifier.createIdentifier(linkFactoryJavaType, getPathResolver().getPath(linkFactoryPathIdentifier));
// Create builder
final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, linkFactoryJavaType, PhysicalTypeCategory.CLASS);
// Add @RooLinkFactory annotation
AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_LINK_FACTORY);
annotationBuilder.addAttribute(new ClassAttributeValue(new JavaSymbolName("controller"), controller));
typeBuilder.addAnnotation(annotationBuilder);
// Write changes to disk
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ThymeleafMVCViewResponseService method annotate.
/**
* This operation annotates a controller with the THYMELEAF annotation
*
* @param controller JavaType with the controller to be annotated.
*/
@Override
public void annotate(JavaType controller) {
Validate.notNull(controller, "ERROR: You must provide a valid controller");
ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
// Check if provided controller exists on current project
Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
// Check if provided controller has been annotated with @RooController
Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
// Add Thymeleaf annotation
ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
typeBuilder.addAnnotation(new AnnotationMetadataBuilder(getAnnotation()));
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ThymeleafMVCViewResponseService method addWebMVCThymeleafUIConfiguration.
/**
* This method adds new WebMVCThymeleafUIConfiguration.java class inside .config
* package of generated project
*
* @param module
*/
private void addWebMVCThymeleafUIConfiguration(Pom module) {
// Obtain the class annotated with @RooWebMvcConfiguration
Set<ClassOrInterfaceTypeDetails> webMvcConfigurationSet = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
if (webMvcConfigurationSet == null || webMvcConfigurationSet.isEmpty()) {
throw new RuntimeException(String.format("ERROR: Can't found configuration class annotated with @%s.", RooJavaType.ROO_WEB_MVC_CONFIGURATION));
}
ClassOrInterfaceTypeDetails webMvcConfiguration = webMvcConfigurationSet.iterator().next();
// Prevent to include the @RooWebMvcThymeleafUIConfiguration more than once
if (webMvcConfiguration.getAnnotation(RooJavaType.ROO_WEB_MVC_THYMELEAF_UI_CONFIGURATION) == null) {
AnnotationMetadataBuilder thymeleaftConfigurationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_WEB_MVC_THYMELEAF_UI_CONFIGURATION);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(webMvcConfiguration);
;
cidBuilder.addAnnotation(thymeleaftConfigurationAnnotation);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class I18nOperationsImpl method installLanguage.
@Override
public void installLanguage(final I18n language, final boolean useAsDefault, final Pom module) {
// Check if provided module match with application modules features
Validate.isTrue(getTypeLocationService().hasModuleFeature(module, ModuleFeatureName.APPLICATION), "ERROR: Provided module doesn't match with application modules features. " + "Execute this operation again and provide a valid application module.");
Validate.notNull(language, "ERROR: You should provide a valid language code.");
if (language.getLocale() == null) {
LOGGER.warning("ERROR: Provided language is not valid.");
return;
}
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, module.getModuleName());
final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
// Getting message.properties file
String messageBundle = "";
if (language.getLocale().equals(Locale.ENGLISH)) {
messageBundle = targetDirectory + "messages.properties";
} else {
messageBundle = targetDirectory.concat("messages_").concat(language.getLocale().getLanguage().concat(".properties"));
}
if (!getFileManager().exists(messageBundle)) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = language.getMessageBundle();
outputStream = getFileManager().createFile(messageBundle).getOutputStream();
IOUtils.copy(inputStream, outputStream);
} catch (final Exception e) {
throw new IllegalStateException("Encountered an error during copying of message bundle MVC JSP addon.", e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
// Install flag
final String flagGraphic = targetDirectory.concat("static/public/img/").concat(language.getLocale().getLanguage()).concat(".png");
if (!getFileManager().exists(flagGraphic)) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = language.getFlagGraphic();
outputStream = getFileManager().createFile(flagGraphic).getOutputStream();
IOUtils.copy(inputStream, outputStream);
} catch (final Exception e) {
throw new IllegalStateException("Encountered an error during copying of flag graphic for MVC JSP addon.", e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
// attribute
if (useAsDefault) {
// Obtain all existing configuration classes annotated with
// @RooWebMvcConfiguration
Set<ClassOrInterfaceTypeDetails> configurationClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
for (ClassOrInterfaceTypeDetails configurationClass : configurationClasses) {
// If configuration class is located in the provided module
if (configurationClass.getType().getModule().equals(module.getModuleName())) {
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(configurationClass);
AnnotationMetadataBuilder annotation = cidBuilder.getDeclaredTypeAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
annotation.addStringAttribute("defaultLanguage", language.getLocale().getLanguage());
// Update configuration class
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
LOGGER.log(Level.INFO, String.format("INFO: Default language of your project has been changed to %s.", language.getLanguage()));
}
// Get all controllers and update its message bundles
Set<ClassOrInterfaceTypeDetails> controllers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_CONTROLLER);
for (ClassOrInterfaceTypeDetails controller : controllers) {
getMetadataService().evictAndGet(ControllerMetadata.createIdentifier(controller));
}
// Add application property
getApplicationConfigService().addProperty(module.getModuleName(), "spring.messages.fallback-to-system-locale", "false", "", true);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class RepositoryJpaOperationsImpl method addRepositoryCustom.
/**
* Method that generates RepositoryCustom interface and its implementation
* for an specific entity
*
* @param domainType
* @param repositoryType
* @param repositoryPackage
* @param defaultReturnType
*
* @return JavaType with new RepositoryCustom interface.
*/
private JavaType addRepositoryCustom(JavaType domainType, JavaType repositoryType, JavaPackage repositoryPackage) {
// Getting RepositoryCustom interface JavaTYpe
JavaType interfaceType = new JavaType(repositoryPackage.getFullyQualifiedPackageName().concat(".").concat(repositoryType.getSimpleTypeName()).concat("Custom"), repositoryType.getModule());
// Check if new interface exists yet
final String interfaceIdentifier = getPathResolver().getCanonicalPath(interfaceType.getModule(), Path.SRC_MAIN_JAVA, interfaceType);
if (getFileManager().exists(interfaceIdentifier)) {
// Type already exists - return
return interfaceType;
}
final String interfaceMdId = PhysicalTypeIdentifier.createIdentifier(interfaceType, getPathResolver().getPath(interfaceIdentifier));
final ClassOrInterfaceTypeDetailsBuilder interfaceBuilder = new ClassOrInterfaceTypeDetailsBuilder(interfaceMdId, Modifier.PUBLIC, interfaceType, PhysicalTypeCategory.INTERFACE);
// Generates @RooJpaRepositoryCustom annotation with referenced entity value
final AnnotationMetadataBuilder repositoryCustomAnnotationMetadata = new AnnotationMetadataBuilder(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM);
repositoryCustomAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("entity"), domainType));
interfaceBuilder.addAnnotation(repositoryCustomAnnotationMetadata);
// Save RepositoryCustom interface and its implementation on disk
getTypeManagementService().createOrUpdateTypeOnDisk(interfaceBuilder.build());
generateRepositoryCustomImpl(interfaceType, repositoryType, domainType);
return interfaceType;
}
Aggregations