use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class WsClientsMetadata method getLoggerField.
/**
* This method obtains the LOGGER field
*
* @return FieldMetadataBuilder that contians information about
* the LOGGER field
*/
public FieldMetadataBuilder getLoggerField() {
if (loggerField != null) {
return loggerField;
}
// Create the field
FieldMetadataBuilder loggerField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("LOGGER"), new JavaType("org.slf4j.Logger"), String.format("%s.getLogger(%s.class);", getNameOfJavaType(new JavaType("org.slf4j.LoggerFactory")), getNameOfJavaType(this.governor)));
this.loggerField = loggerField;
return loggerField;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class WsEndpointsMetadata method getServiceField.
/**
* This method obtains the service field that will be used in some different
* methods.
*
* @param service JavaType with the information about the service related with the new Service field
*
* @return FieldMetadata with the necessary information about the service
*/
private FieldMetadata getServiceField(JavaType service) {
// Check if already exists
if (serviceFields.get(service) != null) {
return serviceFields.get(service);
}
// Create the field
FieldMetadataBuilder serviceField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new JavaSymbolName(StringUtils.uncapitalize(service.getSimpleTypeName())), service, null);
serviceField.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
serviceFields.put(service, serviceField.build());
return serviceField.build();
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class MailOperationsImpl method installReceiveEmailSupport.
@Override
public void installReceiveEmailSupport(String host, String port, String protocol, String username, String password, Boolean starttls, String jndiName, String profile, Pom module, JavaType service, boolean force) {
// Include springlets-boot-starter-mail in module
getProjectOperations().addDependency(module.getModuleName(), DEPENDENCY_SPRINGLETS_STARTER_MAIL);
// Include property version
getProjectOperations().addProperty("", PROPERTY_SPRINGLETS_VERSION);
// check if exists any property
if (getApplicationConfigService().existsSpringConfigFile(module.getModuleName(), profile) && areDefinedReceiveEmailProperties(module.getModuleName(), profile) && !force) {
// Send error message to user. He needs to set --force parameter
String profileStr = "profile " + profile;
if (profile == null) {
profileStr = "default profile";
}
String moduleStr = " and module " + module.getModuleName();
if (StringUtils.isEmpty(module.getModuleName())) {
moduleStr = "";
}
LOGGER.log(Level.INFO, String.format("There are defined the mail properties to %s" + "%s. Using this command with '--force' " + "will overwrite the current values.", profileStr, moduleStr));
} else {
Map<String, String> propertiesFormattedToInsert = getReceiveEmailPropertiesFormattedToInsert(host, port, protocol, username, password, starttls, jndiName);
// Set properties in file
getApplicationConfigService().addProperties(module.getModuleName(), propertiesFormattedToInsert, profile, force);
}
if (service != null) {
// Add dependency springlets-mail to the module of the selected Service
if (!service.getModule().equals(StringUtils.EMPTY)) {
getProjectOperations().addDependency(service.getModule(), DEPENDENCY_SPRINGLETS_MAIL);
}
// Add MailReceiverService to service
final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(service);
Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", service.getFullyQualifiedTypeName());
final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
// Create the field
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("mailReceiver"), SpringletsJavaType.SPRINGLETS_MAIL_RECEIVER_SERVICE));
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class MailOperationsImpl method installSendEmailSupport.
@Override
public void installSendEmailSupport(String host, String port, String protocol, String username, String password, Boolean starttls, String jndiName, String profile, Pom module, JavaType service, boolean force) {
// Include spring-boot-starter-mail in module
getProjectOperations().addDependency(module.getModuleName(), DEPENDENCY_SPRING_BOOT_STARTER_MAIL);
// check if exists any property
if (getApplicationConfigService().existsSpringConfigFile(module.getModuleName(), profile) && areDefinedSendEmailProperties(module.getModuleName(), profile) && !force) {
// Send error message to user. He needs to set --force parameter
String profileStr = "profile " + profile;
if (profile == null) {
profileStr = "default profile";
}
String moduleStr = " and module " + module.getModuleName();
if (StringUtils.isEmpty(module.getModuleName())) {
moduleStr = "";
}
LOGGER.log(Level.INFO, String.format("There are defined the mail properties to %s" + "%s. Using this command with '--force' " + "will overwrite the current values.", profileStr, moduleStr));
} else {
Map<String, String> propertiesFormattedToInsert = getSendEmailPropertiesFormattedToInsert(host, port, protocol, username, password, starttls, jndiName);
// Set properties in file
getApplicationConfigService().addProperties(module.getModuleName(), propertiesFormattedToInsert, profile, force);
}
if (service != null) {
// Add dependency spring-context-support to the module of the selected Service
getProjectOperations().addDependency(service.getModule(), DEPENDENCY_SPRING_CONTEXT_SUPPORT);
// Add JavaMailSender to service
final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(service);
Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", service.getFullyQualifiedTypeName());
final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
// Create the field
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("mailSender"), SpringJavaType.JAVA_MAIL_SENDER));
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class DtoOperationsImpl method addFieldsToProjection.
/**
* Removes persistence annotations of provided fields and adds them to a
* ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
* Also adds final modifier to fields if required.
*
* @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
* Projection class.
* @param fieldsToAdd the List<FieldMetadata> to add.
*/
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, FieldMetadata> entry = iterator.next();
FieldMetadata field = entry.getValue();
// List and Set types require special management
FieldMetadataBuilder fieldBuilder = null;
FieldDetails fieldDetails = null;
if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else {
// Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
}
// Add dependency between modules
typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
// Set new fieldName
fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
// If it is a CollectionField it needs an initializer
String initializer = null;
if (fieldDetails instanceof CollectionField) {
final CollectionField collectionField = (CollectionField) fieldDetails;
initializer = "new " + collectionField.getInitializer() + "()";
} else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
initializer = "new Date()";
}
fieldBuilder.setFieldInitializer(initializer);
// Remove persistence annotations
List<AnnotationMetadata> annotations = field.getAnnotations();
for (AnnotationMetadata annotation : annotations) {
if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
} else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
// Add validation dependency
projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
} else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
}
}
fieldBuilder.setModifier(Modifier.PRIVATE);
// Build field
FieldMetadata projectionField = fieldBuilder.build();
// Add field to DTO
projectionBuilder.addField(projectionField);
}
}
Aggregations