use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder 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.annotations.AnnotationMetadataBuilder 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;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getDodField.
/**
* Builds and returns a `private` data-on-demand field.
*
* @return {@link FieldMetadataBuilder}
*/
private FieldMetadataBuilder getDodField() {
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, DATA_ON_DEMAND_FIELD_NAME, this.annotationValues.getDodClass(), null);
// Add @Autowired
fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
return fieldBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getExpectedExceptionField.
/**
* Builds and returns a <code>public</code> ExpectedException field.
*
* @return {@link FieldMetadataBuilder}
*/
private FieldMetadataBuilder getExpectedExceptionField() {
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PUBLIC, EXPECTED_EXCEPTION_FIELD_NAME, EXPECTED_EXCEPTION, String.format("%s.none()", getNameOfJavaType(EXPECTED_EXCEPTION)));
// Add @Rule
fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(RULE));
return fieldBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getFindAllCustomNotFilteredPagedTestMethod.
/**
* Builds a method to test the find all custom method not filtered and paged.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getFindAllCustomNotFilteredPagedTestMethod() {
JavaSymbolName methodName = new JavaSymbolName(String.format("findAllCustomNotFilteredPagedShouldReturnA%sPage", this.entityPlural));
// Check if method exists on governor
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
// Build method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
// Page<Pet> all = repository.findAll((GlobalSearch) null, new PageRequest(0, 3));
bodyBuilder.appendFormalLine("%s<%s> all = %s().findAll((%s) null, new %s(0, 3));", getNameOfJavaType(SpringJavaType.PAGE), getNameOfJavaType(this.defaultReturnType), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH), getNameOfJavaType(SpringJavaType.PAGE_REQUEST));
// Verify
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Verify");
// assertThat(all.getNumberOfElements())
bodyBuilder.appendFormalLine("%s(all.getNumberOfElements())", getNameOfJavaType(ASSERT_THAT));
// .as("Check result number is not greater than the page size").isLessThanOrEqualTo(3);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".as(\"Check result number is not greater than the page size\").isLessThanOrEqualTo(3);");
bodyBuilder.indentRemove();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
return methodBuilder.build();
}
Aggregations