use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createJsonMixinIfDontExists.
/**
* Create the Json Mixin utility class (annotated with @RooJsonMixin) for
* target Entity if it isn't created yet.
*
* @param entity
* @param entityMetadata
* @param requiresDeserializer
* @param module
* @param controllerPackage
*/
private void createJsonMixinIfDontExists(JavaType entity, JpaEntityMetadata entityMetadata, String module, JavaPackage controllerPackage) {
Set<ClassOrInterfaceTypeDetails> allJsonMixin = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JSON_MIXIN);
JSONMixinAnnotationValues values;
for (ClassOrInterfaceTypeDetails mixin : allJsonMixin) {
values = new JSONMixinAnnotationValues(mixin);
if (entity.equals(values.getEntity())) {
// Found mixing. Nothing to do.
return;
}
}
// Not found. Create class
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder mixinAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_MIXIN);
mixinAnnotation.addClassAttribute("entity", entity);
annotations.add(mixinAnnotation);
JavaType mixinClass = new JavaType(String.format("%s.%sJsonMixin", controllerPackage.getFullyQualifiedPackageName(), entity.getSimpleTypeName()), module);
final LogicalPath mixinPath = getPathResolver().getPath(module, Path.SRC_MAIN_JAVA);
final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(mixinClass, mixinPath);
final String declaredByMetadataIdItem = PhysicalTypeIdentifier.createIdentifier(mixinClass, getPathResolver().getPath(resourceIdentifierItem));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataIdItem, Modifier.PUBLIC + Modifier.ABSTRACT, mixinClass, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(annotations);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createDetailClass.
private boolean createDetailClass(String field, String controllerName, ControllerType type, JavaType entity, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefixController, String viewsList) {
JavaType detailController = new JavaType(String.format("%s.%s", controllerPackage.getFullyQualifiedPackageName(), controllerName), controllerPackage.getModule());
ClassOrInterfaceTypeDetails detailControllerDetails = getTypeLocationService().getTypeDetails(detailController);
if (detailControllerDetails != null) {
LOGGER.log(Level.INFO, String.format("ERROR: Class '%s' already exists inside your generated project.", detailController.getFullyQualifiedTypeName()));
return false;
}
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations.add(getRooControllerAnnotation(entity, pathPrefixController, type));
annotations.add(getRooDetailAnnotation(field, viewsList));
// Add responseType annotation. Don't use responseTypeService
// annotate to
// prevent multiple
// updates of the .java file. Annotate operation will be used during
// controller update.
annotations.add(new AnnotationMetadataBuilder(responseType.getAnnotation()));
final LogicalPath detailControllerPathItem = getPathResolver().getPath(detailController.getModule(), Path.SRC_MAIN_JAVA);
final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(detailController, detailControllerPathItem);
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(detailController, getPathResolver().getPath(resourceIdentifierItem));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, detailController, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(annotations);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Create LinkFactory class
if (responseType.getName().equals("THYMELEAF")) {
createLinkFactoryClass(cidBuilder.getName());
}
if (getProjectOperations().isMultimoduleProject()) {
// TODO
// // Getting related service
// JavaType relatedEntityService = null;
// Set<ClassOrInterfaceTypeDetails> services =
// getTypeLocationService()
// .findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
// Iterator<ClassOrInterfaceTypeDetails> itServices =
// services.iterator();
//
// while (itServices.hasNext()) {
// ClassOrInterfaceTypeDetails existingService = itServices.next();
// AnnotationAttributeValue<Object> entityAttr =
// existingService.getAnnotation(RooJavaType.ROO_SERVICE).getAttribute("entity");
// JavaType entityJavaType = (JavaType) entityAttr.getValue();
// String entityField = relationFieldObject.get(field);
// if (entityJavaType.getSimpleTypeName().equals(entityField)) {
// relatedEntityService = existingService.getType();
// break;
// }
// }
//
// getProjectOperations().addModuleDependency(detailController.getModule(),
// relatedEntityService.getModule());
}
return true;
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class LinkFactoryMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = LinkFactoryMetadata.getJavaType(metadataIdentificationString);
final LogicalPath path = LinkFactoryMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
use of org.springframework.roo.project.LogicalPath 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.project.LogicalPath in project spring-roo by spring-projects.
the class I18nOperationsImpl method getInstalledLanguages.
/**
* Return a list of installed languages in the provided application module.
*
* @param moduleName
* the module name to search for installed languages.
* @return a list with the available languages.
*/
@Override
public List<I18n> getInstalledLanguages(String moduleName) {
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
// Create list for installed languages
List<I18n> installedLanguages = new ArrayList<I18n>();
// Get all available languages
Set<I18n> supportedLanguages = getI18nSupport().getSupportedLanguages();
for (I18n i18n : supportedLanguages) {
String messageBundle = String.format("messages_%s.properties", i18n.getLocale().getLanguage());
String bundlePath = String.format("%s%s%s", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, messageBundle);
if (getFileManager().exists(bundlePath)) {
installedLanguages.add(i18n);
}
}
// Always add English language as default
installedLanguages.add(new EnglishLanguage());
return Collections.unmodifiableList(installedLanguages);
}
Aggregations