use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JpaAuditOperationsImpl method addJpaAuditToEntity.
@Override
public void addJpaAuditToEntity(JavaType entity, String createdDateColumn, String modifiedDateColumn, String createdByColumn, String modifiedByColumn) {
// Getting entity details
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(entityDetails);
// Add audit fields
cidBuilder.addField(getCreatedDateField(entityDetails, createdDateColumn));
cidBuilder.addField(getModifiedDateField(entityDetails, modifiedDateColumn));
cidBuilder.addField(getCreatedByField(entityDetails, createdByColumn));
cidBuilder.addField(getModifiedByField(entityDetails, modifiedByColumn));
// Add @RooJpaAudit annotation if needed
if (entityDetails.getAnnotation(RooJavaType.ROO_JPA_AUDIT) == null) {
cidBuilder.addAnnotation(new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_AUDIT).build());
}
// Write changes on disk
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandCreator method createEntityFactory.
@Override
public JavaType createEntityFactory(JavaType currentEntity) {
Validate.notNull(currentEntity, "Entity to produce a data on demand provider for is required");
// Verify the requested entity actually exists as a class and is not
// abstract
final ClassOrInterfaceTypeDetails cid = getEntityDetails(currentEntity);
Validate.isTrue(cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS, "Type %s is not a class", currentEntity.getFullyQualifiedTypeName());
Validate.isTrue(!Modifier.isAbstract(cid.getModifier()), "Type %s is abstract", currentEntity.getFullyQualifiedTypeName());
// Check if the requested entity is a JPA @Entity
final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(JpaDataOnDemandCreator.class.getName(), cid);
final AnnotationMetadata entityAnnotation = memberDetails.getAnnotation(ENTITY);
Validate.isTrue(entityAnnotation != null, "Type %s must be a JPA entity type", currentEntity.getFullyQualifiedTypeName());
// Get related entities
List<JavaType> entities = getEntityAndRelatedEntitiesList(currentEntity);
// Get test Path for module
final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, currentEntity.getModule());
JavaType currentEntityFactory = null;
for (JavaType entity : entities) {
// Create the JavaType for the configuration class
JavaType factoryClass = new JavaType(String.format("%s.dod.%sFactory", entity.getPackage().getFullyQualifiedPackageName(), entity.getSimpleTypeName()), entity.getModule());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(factoryClass, path);
if (metadataService.get(declaredByMetadataId) != null) {
// The file already exists
continue;
}
// Create the CID builder
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, factoryClass, PhysicalTypeCategory.CLASS);
// Add @RooEntityFactory annotation
AnnotationMetadataBuilder entityFactoryAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_ENTITY_FACTORY);
entityFactoryAnnotation.addClassAttribute("entity", entity);
cidBuilder.addAnnotation(entityFactoryAnnotation);
// Write changes to disk
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
// First entity is current entity
if (currentEntityFactory == null) {
currentEntityFactory = cidBuilder.getName();
}
}
return currentEntityFactory;
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JpaOperationsImpl method newEntity.
@Override
public void newEntity(final JavaType name, final boolean createAbstract, final JavaType superclass, final JavaType implementsType, final String identifierField, final JavaType identifierType, final String identifierColumn, final String sequenceName, final IdentifierStrategy identifierStrategy, final String versionField, final JavaType versionType, final String versionColumn, final InheritanceType inheritanceType, final List<AnnotationMetadataBuilder> annotations) {
Validate.notNull(name, "Entity name required");
Validate.isTrue(!JdkJavaType.isPartOfJavaLang(name.getSimpleTypeName()), "Entity name '%s' must not be part of java.lang", name.getSimpleTypeName());
getProjectOperations().setModule(getProjectOperations().getPomFromModuleName(name.getModule()));
// Add springlets-context dependency
getProjectOperations().addDependency(name.getModule(), SPRINGLETS_CONTEXT_DEPENDENCY);
getProjectOperations().addProperty("", SPRINGLETS_VERSION_PROPERTY);
int modifier = Modifier.PUBLIC;
if (createAbstract) {
modifier |= Modifier.ABSTRACT;
}
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, getPathResolver().getFocusedPath(Path.SRC_MAIN_JAVA));
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, modifier, name, PhysicalTypeCategory.CLASS);
if (!superclass.equals(OBJECT)) {
final ClassOrInterfaceTypeDetails superclassClassOrInterfaceTypeDetails = getTypeLocationService().getTypeDetails(superclass);
if (superclassClassOrInterfaceTypeDetails != null) {
cidBuilder.setSuperclass(new ClassOrInterfaceTypeDetailsBuilder(superclassClassOrInterfaceTypeDetails));
// Add dependency with superclass module
getProjectOperations().addModuleDependency(superclass.getModule());
}
}
cidBuilder.setExtendsTypes(Arrays.asList(superclass));
if (implementsType != null) {
final Set<JavaType> implementsTypes = new LinkedHashSet<JavaType>();
final ClassOrInterfaceTypeDetails typeDetails = getTypeLocationService().getTypeDetails(declaredByMetadataId);
if (typeDetails != null) {
implementsTypes.addAll(typeDetails.getImplementsTypes());
}
implementsTypes.add(implementsType);
cidBuilder.setImplementsTypes(implementsTypes);
// Add dependency with implementsType modules
getProjectOperations().addModuleDependency(implementsType.getModule());
}
// Set annotations to new entity
cidBuilder.setAnnotations(annotations);
// Write entity on disk
ClassOrInterfaceTypeDetails entityDetails = cidBuilder.build();
getTypeManagementService().createOrUpdateTypeOnDisk(entityDetails);
// Adding identifier and version fields
if (superclass.equals(OBJECT)) {
getTypeManagementService().addField(getIdentifierField(name, identifierField, identifierType, identifierColumn, sequenceName, identifierStrategy, inheritanceType), true);
getTypeManagementService().addField(getVersionField(name, versionField, versionType, versionColumn), true);
}
// Don't need to add them if spring-boot-starter-data-jpa is present, often in single module project
if (!getProjectOperations().getFocusedModule().hasDependencyExcludingVersion(new Dependency("org.springframework.boot", "spring-boot-starter-data-jpa", null))) {
List<Dependency> dependencies = new ArrayList<Dependency>();
dependencies.add(new Dependency("org.springframework", "spring-aspects", null));
dependencies.add(new Dependency("org.springframework", "spring-context", null));
dependencies.add(new Dependency("org.springframework.data", "spring-data-jpa", null));
dependencies.add(new Dependency("org.springframework.data", "spring-data-commons", null));
dependencies.add(new Dependency("org.eclipse.persistence", "javax.persistence", null));
getProjectOperations().addDependencies(getProjectOperations().getFocusedModuleName(), dependencies);
}
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JmsOperationsImpl method addJmsReceiver.
@Override
public void addJmsReceiver(String destinationName, JavaType endpointService, String jndiConnectionFactory, String profile, boolean force) {
boolean isApplicationModule = false;
// Check that the module of the service is type application
Collection<Pom> modules = getTypeLocationService().getModules(ModuleFeatureName.APPLICATION);
for (Pom pom : modules) {
if (pom.getModuleName().equals(endpointService.getModule())) {
isApplicationModule = true;
}
}
if (!isApplicationModule) {
LOGGER.log(Level.SEVERE, String.format("The module selected where JMS service will be configured must be of type application"));
return;
}
// Check if Service already exists and --force is included
final String serviceFilePathIdentifier = getPathResolver().getCanonicalPath(endpointService.getModule(), Path.SRC_MAIN_JAVA, endpointService);
if (getFileManager().exists(serviceFilePathIdentifier) && force) {
getFileManager().delete(serviceFilePathIdentifier);
} else if (getFileManager().exists(serviceFilePathIdentifier)) {
throw new IllegalArgumentException(String.format("Endpoint '%s' already exists and cannot be created. Try to use a " + "different name on --endpoint parameter or use this command with '--force' " + "to overwrite the current service.", endpointService));
}
// Set destionation property name
StringBuffer destinationNamePropertyName = new StringBuffer(JMS_PROPERTY_DESTINATION_NAME_PREFIX);
destinationNamePropertyName.append(destinationName.replaceAll("/", "."));
destinationNamePropertyName.append(JMS_PROPERTY_DESTINATION_NAME_SUFIX);
// Set properties
setProperties(destinationName, destinationNamePropertyName.toString(), jndiConnectionFactory, endpointService.getModule(), profile, force);
// Create service
createReceiverJmsService(endpointService, destinationNamePropertyName.toString());
// Add jms dependecy in module
getProjectOperations().addDependency(endpointService.getModule(), DEPENDENCY_JMS);
// Add annotation @EnableJms to application class of the module
Set<ClassOrInterfaceTypeDetails> applicationClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(SpringJavaType.SPRING_BOOT_APPLICATION);
for (ClassOrInterfaceTypeDetails applicationClass : applicationClasses) {
if (applicationClass.getType().getModule().equals(endpointService.getModule())) {
// Check if annotation exists
boolean annotationNotExists = true;
for (AnnotationMetadata annotation : applicationClass.getAnnotations()) {
if (annotation.getAnnotationType().equals(SpringJavaType.ENABLE_JMS)) {
annotationNotExists = false;
break;
}
}
if (annotationNotExists) {
ClassOrInterfaceTypeDetailsBuilder builder = new ClassOrInterfaceTypeDetailsBuilder(applicationClass);
builder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.ENABLE_JMS));
getTypeManagementService().createOrUpdateTypeOnDisk(builder.build());
}
break;
}
}
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JmsOperationsImpl method createReceiverJmsService.
private void createReceiverJmsService(JavaType service, String destinationProperty) {
// Create new service class
final String serviceClassIdentifier = getPathResolver().getCanonicalPath(service.getModule(), Path.SRC_MAIN_JAVA, service);
final String mid = PhysicalTypeIdentifier.createIdentifier(service, getPathResolver().getPath(serviceClassIdentifier));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, Modifier.PUBLIC, service, PhysicalTypeCategory.CLASS);
// Create new @Service annotation
AnnotationMetadataBuilder serviceAnnotation = new AnnotationMetadataBuilder(SpringJavaType.SERVICE);
cidBuilder.addAnnotation(serviceAnnotation);
// Add method receiveJmsMessage
// @JmsListener(destination =
// "${application.jms.queue.plaintext.jndi-name}")
// public void receiveJmsMessage(String msg) {
//
// }
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("receiveJmsMessage");
// Define parameters
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(JavaType.STRING));
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("msg"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @JmsListener annotation
AnnotationMetadataBuilder jmsListenerAnnotation = new AnnotationMetadataBuilder(SpringJavaType.JMS_LISTENER);
jmsListenerAnnotation.addStringAttribute("destination", "${".concat(destinationProperty).concat("}"));
annotations.add(jmsListenerAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.newLine();
bodyBuilder.appendFormalLine(" // To be implemented");
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(mid, Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
cidBuilder.addMethod(methodBuilder);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
Aggregations