use of org.springframework.roo.project.Dependency in project spring-roo by spring-projects.
the class ServiceOperationsImpl method createServiceImplementation.
/**
* Method that creates the service implementation
*
* @param interfaceType
* @param implType
* @param domainType
*/
private void createServiceImplementation(final JavaType interfaceType, JavaType implType, ClassOrInterfaceTypeDetails repository, JavaType domainType) {
Validate.notNull(interfaceType, "ERROR: Interface should be provided to be able to generate its implementation");
Validate.notNull(interfaceType.getModule(), "ERROR: Interface module is required");
Validate.notNull(domainType, "ERROR: Domain type required to generate service");
// Generating implementation JavaType if needed
if (implType == null) {
implType = new JavaType(String.format("%sImpl", interfaceType.getFullyQualifiedTypeName()), interfaceType.getModule());
}
Validate.notNull(implType.getModule(), "ERROR: Implementation module is required");
// Checks if new service interface already exists.
final String implIdentifier = pathResolver.getCanonicalPath(implType.getModule(), Path.SRC_MAIN_JAVA, implType);
if (fileManager.exists(implIdentifier)) {
// Type already exists - nothing to do
return;
}
// Generating @RooServiceImpl annotation
final AnnotationMetadataBuilder implAnnotationMetadata = new AnnotationMetadataBuilder(ROO_SERVICE_IMPL);
implAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("service"), interfaceType));
// Creating class builder
final String implMid = PhysicalTypeIdentifier.createIdentifier(implType, pathResolver.getPath(implIdentifier));
final ClassOrInterfaceTypeDetailsBuilder implTypeBuilder = new ClassOrInterfaceTypeDetailsBuilder(implMid, PUBLIC, implType, PhysicalTypeCategory.CLASS);
// Adding @RooService annotation to current interface
implTypeBuilder.addAnnotation(implAnnotationMetadata.build());
// Adding implements
implTypeBuilder.addImplementsType(interfaceType);
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(implTypeBuilder.build().getType(), pathResolver.getPath(implType.getModule(), Path.SRC_MAIN_JAVA));
// Write service implementation on disk
typeManagementService.createOrUpdateTypeOnDisk(implTypeBuilder.build());
// Add dependencies between modules
projectOperations.addModuleDependency(implType.getModule(), interfaceType.getModule());
projectOperations.addModuleDependency(implType.getModule(), repository.getName().getModule());
projectOperations.addModuleDependency(implType.getModule(), domainType.getModule());
// ROO-3799 Included dependency spring-tx if it's a multimodule project
if (projectOperations.isMultimoduleProject()) {
projectOperations.addDependency(implType.getModule(), new Dependency("org.springframework", "spring-tx", "", DependencyType.JAR, DependencyScope.COMPILE));
}
}
use of org.springframework.roo.project.Dependency in project spring-roo by spring-projects.
the class CacheOperationsImpl method addSpringCacheDependency.
/**
* Add Spring Cache starter to provided module.
*
* @param module the Pom where the starter should be installed.
*/
private void addSpringCacheDependency(Pom module) {
// Parse the configuration.xml file
final Element configuration = XmlUtils.getConfiguration(getClass());
final List<Dependency> dependencies = new ArrayList<Dependency>();
final List<Element> auditDependencies = XmlUtils.findElements("/configuration/cache/dependencies/dependency", configuration);
for (final Element dependencyElement : auditDependencies) {
dependencies.add(new Dependency(dependencyElement));
}
getProjectOperations().addDependencies(module.getModuleName(), dependencies);
}
use of org.springframework.roo.project.Dependency in project spring-roo by spring-projects.
the class JpaAuditOperationsImpl method isInstalledInModule.
@Override
public boolean isInstalledInModule(String moduleName) {
boolean isInstalledInModule = false;
Pom module = getProjectOperations().getPomFromModuleName(moduleName);
Set<Dependency> starter = module.getDependenciesExcludingVersion(SPRINGLETS_DATA_JPA_STARTER);
if (!starter.isEmpty()) {
isInstalledInModule = true;
}
return isInstalledInModule;
}
use of org.springframework.roo.project.Dependency 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.project.Dependency in project spring-roo by spring-projects.
the class JpaOperationsImpl method updateDependencies.
/**
* Updates the POM with the dependencies required for the given database and
* ORM provider, removing any other persistence-related dependencies
*
* @param configuration
* @param ormProvider
* @param jdbcDatabase
* @param startersXPath
* @param profile
*/
private void updateDependencies(final Pom module, final Element configuration, final OrmProvider ormProvider, final JdbcDatabase jdbcDatabase, final String startersXPath, final String providersXPath, final String databaseXPath, String profile) {
final List<Dependency> requiredDependencies = new ArrayList<Dependency>();
final List<Element> starterDependencies = XmlUtils.findElements("/configuration/starter/provider[@id = '" + ormProvider.name() + "']/dependencies/dependency", configuration);
for (final Element dependencyElement : starterDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
// Add database dependencies
final List<Element> databaseDependencies = XmlUtils.findElements(jdbcDatabase.getConfigPrefix() + "/dependencies/dependency", configuration);
for (final Element dependencyElement : databaseDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
if (jdbcDatabase.toString().equals(JdbcDatabase.ORACLE.toString())) {
LOGGER.warning("Oracle drivers aren't in Maven public repositories!! You should include them manually in your local Maven repository.");
}
final List<Element> ormDependencies = XmlUtils.findElements(ormProvider.getConfigPrefix() + "/dependencies/dependency", configuration);
for (final Element dependencyElement : ormDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
// Hard coded to JPA & Hibernate Validator for now
final List<Element> jpaDependencies = XmlUtils.findElements("/configuration/persistence/provider[@id = 'JPA']/dependencies/dependency", configuration);
for (final Element dependencyElement : jpaDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
final List<Element> springDependencies = XmlUtils.findElements("/configuration/spring/dependencies/dependency", configuration);
for (final Element dependencyElement : springDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
final List<Element> commonDependencies = XmlUtils.findElements("/configuration/common/dependencies/dependency", configuration);
for (final Element dependencyElement : commonDependencies) {
requiredDependencies.add(new Dependency(dependencyElement));
}
// Add properties
List<Element> properties = XmlUtils.findElements("/configuration/properties/*", configuration);
for (Element property : properties) {
getProjectOperations().addProperty("", new Property(property));
}
// Add dependencies used by other profiles, excluding the current profile
List<String> profiles = getApplicationConfigService().getApplicationProfiles(module.getModuleName());
profiles.remove(profile);
for (String applicationProfile : profiles) {
// Extract database
final String driver = getApplicationConfigService().getProperty(module.getModuleName(), DATASOURCE_PREFIX, DATABASE_DRIVER, applicationProfile);
for (JdbcDatabase database : JdbcDatabase.values()) {
if (database.getDriverClassName().equals(driver)) {
for (final Element dependencyElement : XmlUtils.findElements(database.getConfigPrefix() + "/dependencies/dependency", configuration)) {
requiredDependencies.add(new Dependency(dependencyElement));
}
break;
}
}
}
// Remove redundant dependencies
final List<Dependency> redundantDependencies = new ArrayList<Dependency>();
redundantDependencies.addAll(getDependencies(databaseXPath, configuration));
redundantDependencies.addAll(getDependencies(startersXPath, configuration));
redundantDependencies.addAll(getDependencies(providersXPath, configuration));
// Don't remove any we actually need
redundantDependencies.removeAll(requiredDependencies);
// Update the POM
getProjectOperations().removeDependencies(module.getModuleName(), redundantDependencies);
getProjectOperations().addDependencies(module.getModuleName(), requiredDependencies);
// and some repository has been already added
if (getProjectOperations().isMultimoduleProject()) {
Set<JavaType> repositoryTypes = getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_REPOSITORY_JPA);
if (!repositoryTypes.isEmpty()) {
Iterator<JavaType> repositoryIterator = repositoryTypes.iterator();
while (repositoryIterator.hasNext()) {
JavaType repositoryType = repositoryIterator.next();
String moduleName = repositoryType.getModule();
// Remove redundant dependencies from modules with repository classes
getProjectOperations().removeDependencies(moduleName, redundantDependencies);
// Add new database dependencies
addDatabaseDependencyWithTestScope(moduleName, profile, jdbcDatabase.getConfigPrefix());
}
}
}
// Include Springlets Starter project dependencies and properties
getProjectOperations().addProperty("", SPRINGLETS_VERSION_PROPERTY);
// If current project is a multimodule project, include dependencies
// first
// on dependencyManagement and then on current module
getProjectOperations().addDependency(module.getModuleName(), SPRINGLETS_DATA_JPA_STARTER);
getProjectOperations().addDependency(module.getModuleName(), SPRINGLETS_DATA_COMMONS_STARTER);
}
Aggregations