use of org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues in project spring-roo by spring-projects.
the class JsonControllerTestCreator method createIntegrationTest.
@Override
public void createIntegrationTest(JavaType type, Pom module) {
Validate.notNull(type, "Class to produce an integration test class for is required");
// Check if provided JavaType is a JSON Controller
ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
Validate.notNull(cid.getAnnotation(RooJavaType.ROO_CONTROLLER), "Type must be a Roo controller.");
Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JSON), "Type must be a Roo JSON controller.");
// Add springlets-boot-starter-test dependency
projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
projectOperations.addDependency(module.getModuleName(), SPRINGLETS_BOOT_STARTER_TEST_DEPENDENCY);
// Get the controller managed entity
ControllerAnnotationValues controllerAnnotationValues = new ControllerAnnotationValues(cid);
JavaType managedEntity = controllerAnnotationValues.getEntity();
// Workaround to get a JavaType with not null module when recovering it
// from a ClassAttributeValue
managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
// Create Data On Demand artifacts for managed entity
List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for JSON controllers.");
Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JSON controllers. %s can't decide which one to use.", this.getClass().getName());
DataOnDemandCreatorProvider creator = dodCreators.get(0);
creator.createDataOnDemand(managedEntity);
// Add module dependency with test-jar dependency
if (projectOperations.isMultimoduleProject()) {
String managedEntityModuleName = managedEntity.getModule();
Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
}
// Create integration test class
final JavaType name = new JavaType(type + "IT", module.getModuleName());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
if (metadataService.get(declaredByMetadataId) != null) {
// The file already exists
return;
}
// Add @RooJsonControllerIntegrationTest to source file
AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_CONTROLLER_INTEGRATION_TEST);
rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
// Create integration test class
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
// Write changes to disk
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues in project spring-roo by spring-projects.
the class AbstractViewGenerationService method addMenu.
@Override
public void addMenu(String moduleName, ViewContext<T> ctx) {
Map<String, MenuEntry> mapMenuEntries = new TreeMap<String, MenuEntry>();
Set<ClassOrInterfaceTypeDetails> existingControllers = new HashSet<ClassOrInterfaceTypeDetails>();
existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.COLLECTION, getType()));
existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.SEARCH, getType()));
Iterator<ClassOrInterfaceTypeDetails> it = existingControllers.iterator();
while (it.hasNext()) {
// Getting controller and its information
ClassOrInterfaceTypeDetails controller = it.next();
ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(controller);
JavaType entity = controllerValues.getEntity();
// Obtain the entityMetadata
JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(getTypeLocationService().getTypeDetails(entity)));
boolean isReadOnly = false;
if (entityMetadata != null && entityMetadata.isReadOnly()) {
isReadOnly = true;
}
// Get finders for each controller
AnnotationMetadata controllerSearchAnnotation = controller.getAnnotation(RooJavaType.ROO_SEARCH);
Map<String, String> finderNamesAndPaths = new HashMap<String, String>();
if (controllerSearchAnnotation != null && controllerSearchAnnotation.getAttribute("finders") != null) {
List<?> finders = (List<?>) controllerSearchAnnotation.getAttribute("finders").getValue();
Iterator<?> iterator = finders.iterator();
while (iterator.hasNext()) {
StringAttributeValue attributeValue = (StringAttributeValue) iterator.next();
String finderName = attributeValue.getValue();
// Build URL path to get data
String finderPath = "";
if (StringUtils.startsWith(finderName, "count")) {
finderPath = StringUtils.removeStart(finderName, "count");
} else if (StringUtils.startsWith(finderName, "find")) {
finderPath = StringUtils.removeStart(finderName, "find");
} else if (StringUtils.startsWith(finderName, "query")) {
finderPath = StringUtils.removeStart(finderName, "query");
} else if (StringUtils.startsWith(finderName, "read")) {
finderPath = StringUtils.removeStart(finderName, "read");
} else {
finderPath = finderName;
}
finderPath = String.format("search/%s/search-form", StringUtils.uncapitalize(finderPath));
finderNamesAndPaths.put(finderName, finderPath);
}
}
// Getting pathPrefix
String pathPrefix = StringUtils.defaultString(controllerValues.getPathPrefix(), "");
// Generate path
String path = getControllerOperations().getBaseUrlForController(controller);
if (controllerSearchAnnotation != null) {
// Prevent that /search will be included in every menu entry
// The /search is already included in the step before only for
// finder entries
path = path.replace("/search", "");
}
// Create new menuEntry element for controller
String keyThatRepresentsEntry = pathPrefix.concat(entity.getSimpleTypeName());
// Add new menu entry to menuEntries list if doesn't exist
MenuEntry menuEntry = null;
if (controllerValues.getType() == ControllerType.SEARCH) {
// Only add finder entry
menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, false, false);
} else {
// Add default menu entries
menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, true, isReadOnly);
}
if (mapMenuEntries.containsKey(keyThatRepresentsEntry)) {
MenuEntry menuEntryInserted = mapMenuEntries.get(keyThatRepresentsEntry);
if (menuEntryInserted.getFinderNamesAndPaths().isEmpty() && !menuEntry.getFinderNamesAndPaths().isEmpty()) {
menuEntryInserted.setFinderNamesAndPaths(menuEntry.getFinderNamesAndPaths());
}
// Check the 'addDefaultEntries' attribute and add it if needed
if (!menuEntryInserted.isAddDefaultEntries() && menuEntry.isAddDefaultEntries()) {
menuEntryInserted.setAddDefaultEntries(menuEntry.isAddDefaultEntries());
}
} else {
mapMenuEntries.put(keyThatRepresentsEntry, menuEntry);
}
}
// Also, check web flow views in the views folder
String viewsFolder = getViewsFolder(moduleName);
List<String> webFlowViews = getWebFlowViewsFromDir(viewsFolder, null);
// After obtain the webFlow views, add them to the menu
for (String webFlowView : webFlowViews) {
// Creating the menu entry
MenuEntry menuEntry = createMenuEntry(webFlowView, webFlowView, "", FieldItem.buildLabel(webFlowView, ""), FieldItem.buildLabel(webFlowView, "plural"), null, true, false, false);
mapMenuEntries.put(webFlowView, menuEntry);
}
// First of all, generate a list of MenuEntries based on existing
// controllers
List<MenuEntry> menuEntries = new ArrayList<MenuEntry>(mapMenuEntries.values());
// Generate ids to search when merge new and existing doc
List<String> requiredIds = new ArrayList<String>();
for (MenuEntry entry : menuEntries) {
requiredIds.add(entry.getPathPrefix().concat(entry.getEntityName()).concat("Entry"));
}
// Process elements to generate
DOC newDoc = null;
// Getting new viewName
String viewName = getFragmentsFolder(moduleName).concat("/menu").concat(getViewsExtension());
// Check if new view to generate exists or not
if (existsFile(viewName)) {
DOC existingDoc = loadExistingDoc(viewName);
if (!isUserManagedDocument(existingDoc)) {
newDoc = mergeMenu("fragments/menu", existingDoc, ctx, menuEntries);
}
} else {
ctx.addExtraParameter("menuEntries", menuEntries);
newDoc = process("fragments/menu", ctx);
}
// Write newDoc on disk
writeDoc(newDoc, viewName);
}
use of org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues in project spring-roo by spring-projects.
the class AbstractViewGenerationService method getReferenceField.
/**
* This method obtains all necessary configuration to be able to work with
* reference fields. Complete provided FieldItem with extra fields. If some
* extra configuration is not available, returns false to prevent that this
* field will be added. If everything is ok, returns true to add this field to
* generated view.
*
* @param fieldItem
* @param typeDetails
* @param allControllers
* @return
*/
protected boolean getReferenceField(FieldItem fieldItem, ClassOrInterfaceTypeDetails typeDetails, ViewContext<T> viewContext) {
// Set type as REFERENCE
fieldItem.setType(FieldTypes.REFERENCE.toString());
// Add referencedEntity to configuration
fieldItem.addConfigurationElement("referencedEntity", typeDetails.getType().getSimpleTypeName());
// Add the controllerPath related to the referencedEntity to
// configuration
final String controllerPrefix = viewContext.getViewMetadata().getControllerMetadata().getAnnotationValues().getPathPrefix();
Collection<ClassOrInterfaceTypeDetails> allControllers = getControllerLocator().getControllers(typeDetails.getType(), ControllerType.COLLECTION, getType());
Iterator<ClassOrInterfaceTypeDetails> it = allControllers.iterator();
String referencedPath = "";
ClassOrInterfaceTypeDetails referencedController = null;
while (it.hasNext()) {
ClassOrInterfaceTypeDetails controller = it.next();
ControllerAnnotationValues values = new ControllerAnnotationValues(controller);
AnnotationMetadata controllerAnnotation = controller.getAnnotation(RooJavaType.ROO_CONTROLLER);
AnnotationAttributeValue<String> prefixAttr = controllerAnnotation.getAttribute("pathPrefix");
if (StringUtils.equals(values.getPathPrefix(), controllerPrefix)) {
// Get path
referencedPath = getControllerOperations().getBaseUrlForController(controller);
referencedController = controller;
// Get target entity metadata to get identifier field
ClassOrInterfaceTypeDetails relatedEntityCid = getTypeLocationService().getTypeDetails(typeDetails.getType());
JpaEntityMetadata relatedEntityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(relatedEntityCid));
fieldItem.addConfigurationElement("identifierField", relatedEntityMetadata.getCurrentIndentifierField().getFieldName().getSymbolName());
break;
}
}
if (referencedController == null) {
return false;
}
fieldItem.addConfigurationElement("referencedPath", referencedPath);
fieldItem.addConfigurationElement("referencedController", referencedController);
return true;
}
use of org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues in project spring-roo by spring-projects.
the class ThymeleafMetadataProviderImpl method createMetadataInstance.
@Override
protected ThymeleafMetadata createMetadataInstance(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final ControllerMetadata controllerMetadata, final ServiceMetadata serviceMetadata, final JpaEntityMetadata entityMetadata, final String entityPlural, final String entityIdentifierPlural, final List<Pair<RelationInfo, JpaEntityMetadata>> compositionRelationOneToOne, final JavaType itemController, final JavaType collectionController, final List<FieldMetadata> dateTimeFields, final List<FieldMetadata> enumFields, final Map<String, MethodMetadata> findersToAdd, final Map<JavaType, List<FieldMetadata>> formBeansDateTimeFields, final Map<JavaType, List<FieldMetadata>> formBeansEnumFields, final JavaType detailsItemController, final JavaType detailsCollectionController) {
final ThymeleafAnnotationValues annotationValues = new ThymeleafAnnotationValues(governorPhysicalTypeMetadata);
// Get related controller for select2 request
JavaType relatedCollectionController = null;
JavaType relatedItemController = null;
RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
if (controllerMetadata.getType() == ControllerType.DETAIL && info.type == JpaRelationType.AGGREGATION) {
Collection<ClassOrInterfaceTypeDetails> controllers = getControllerLocator().getControllers(info.childType, ControllerType.COLLECTION, RooJavaType.ROO_THYMELEAF);
final String prefix = controllerMetadata.getAnnotationValues().getPathPrefix();
for (ClassOrInterfaceTypeDetails controller : controllers) {
// use annotation values to avoid cyclic dependencies
ControllerAnnotationValues valuesToCheck = new ControllerAnnotationValues(controller);
if (StringUtils.equals(prefix, valuesToCheck.getPathPrefix())) {
relatedCollectionController = controller.getType();
break;
}
}
Validate.notNull(relatedCollectionController, "Can't found Collection-type controller of entity '%s' and pathPrefix '%s' required by '%s' controller", info.childType, prefix, controllerMetadata.getDestination());
controllers = getControllerLocator().getControllers(info.childType, ControllerType.ITEM, RooJavaType.ROO_THYMELEAF);
for (ClassOrInterfaceTypeDetails controller : controllers) {
// use annotation values to avoid cyclic dependencies
ControllerAnnotationValues valuesToCheck = new ControllerAnnotationValues(controller);
if (StringUtils.equals(prefix, valuesToCheck.getPathPrefix())) {
relatedItemController = controller.getType();
break;
}
}
Validate.notNull(relatedItemController, "Can't found Item-type controller of entity '%s' and pathPrefix '%s' required by '%s' controller", info.childType, prefix, controllerMetadata.getDestination());
}
Validate.notNull(getJasperReportsMap(), "Couldn't find class `JasperReportsExporter` in project.");
// Get valid entity fields (those suitable for reports)
JavaType defaultReturnType = serviceMetadata.getCurrentFindAllWithGlobalSearchMethod().getReturnType().getParameters().get(0);
List<FieldMetadata> entityFields = getReturnTypeValidFields(getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(defaultReturnType)).getFields());
// Getting the related linkFactory for this controller
Set<ClassOrInterfaceTypeDetails> linkFactories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_LINK_FACTORY);
JavaType relatedCollectionLinkFactory = null;
JavaType relatedItemLinkFactory = null;
JavaType collectionControllerToCheck = collectionController;
if (collectionControllerToCheck == null) {
collectionControllerToCheck = governorPhysicalTypeMetadata.getType();
}
JavaType itemControllerToCheck = itemController;
if (itemControllerToCheck == null) {
itemControllerToCheck = governorPhysicalTypeMetadata.getType();
}
for (ClassOrInterfaceTypeDetails linkFactory : linkFactories) {
LinkFactoryAnnotationValues linkAnnotationValues = new LinkFactoryAnnotationValues(linkFactory);
// Getting link factory of the collection controller
if (linkAnnotationValues.getController().equals(collectionControllerToCheck)) {
relatedCollectionLinkFactory = linkFactory.getType();
} else if (linkAnnotationValues.getController().equals(itemControllerToCheck)) {
relatedItemLinkFactory = linkFactory.getType();
}
}
final ThymeleafMetadata metadata = new ThymeleafMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, controllerMetadata, serviceMetadata, entityMetadata, entityPlural, entityIdentifierPlural, compositionRelationOneToOne, itemController, collectionController, dateTimeFields, enumFields, findersToAdd, formBeansDateTimeFields, formBeansEnumFields, detailsItemController, detailsCollectionController, relatedCollectionController, relatedItemController, entityFields, getJasperReportsMap(), relatedCollectionLinkFactory, relatedItemLinkFactory);
return metadata;
}
use of org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues in project spring-roo by spring-projects.
the class WebFinderOperationsImpl method createOrUpdateSearchControllerForEntity.
@Override
public void createOrUpdateSearchControllerForEntity(JavaType entity, List<String> queryMethods, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefix) {
Validate.notNull(entity, "Entity type required");
Validate.notNull(responseType, "Response type required");
Validate.notNull(controllerPackage, "Package required");
Validate.notNull(typeLocationService.getTypeDetails(entity).getAnnotation(RooJavaType.ROO_JPA_ENTITY), "The provided JavaType %s must be annotated with @RooJpaEntity", entity.getSimpleTypeName());
// Check if module has an application class
controllerPackage = checkAndUseApplicationModule(controllerPackage);
// Check if entity has any associated repository
final ClassOrInterfaceTypeDetails relatedRepositoryDetails = repositoryJpaLocator.getRepository(entity);
Validate.notNull(relatedRepositoryDetails, "Entity %s doesn't have an associated repository. You " + "need at least an associated repository with finders to publish them to the web layer." + "Please, create one associated repository with 'repository jpa' command.", entity);
// Get repository finder methods
RepositoryJpaMetadata repositoryMetadata = repositoryJpaLocator.getRepositoryMetadata(entity);
// Get finders in @RooRepositoryJpa
// List<String> entityFinders = repositoryMetadata.getDeclaredFinderNames();
List<String> entityFinders = getFindersWhichCanBePublish(repositoryMetadata, responseType);
// Check if specified finder methods exists in associated repository
for (String finder : queryMethods) {
if (!entityFinders.contains(finder)) {
LOGGER.log(Level.INFO, String.format("ERROR: Provided finder '%s' doesn't exists on the repository '%s' " + "related to entity '%s'", finder, relatedRepositoryDetails.getType().getSimpleTypeName(), entity.getSimpleTypeName()));
return;
}
}
// Check if entity has any associated service
JavaType relatedService = null;
ClassOrInterfaceTypeDetails service = serviceLocator.getService(entity);
if (service == null) {
LOGGER.log(Level.INFO, String.format("Entity %s doesn't have associated services, " + "necessary to create controllers. Please, create one associated service with " + "'service' command before publish finders to web layer.", entity.getSimpleTypeName()));
return;
}
relatedService = service.getType();
// Seek for search type controllers related to entity
Collection<ClassOrInterfaceTypeDetails> entitySearchControllers = controllerLocator.getControllers(entity, ControllerType.SEARCH, responseType.getAnnotation());
// Check if any of the search controllers have the same pathPrefix.
// If so, and controllerPackage is as well the same, update the controller.
ClassOrInterfaceTypeDetails controllerToUpdateOrCreate = null;
for (ClassOrInterfaceTypeDetails entitySearchController : entitySearchControllers) {
ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(entitySearchController);
if (StringUtils.equals(pathPrefix, controllerValues.getPathPrefix())) {
if (controllerPackage.equals(entitySearchController.getType().getPackage())) {
// The controller exists, so choose it for updating.
controllerToUpdateOrCreate = entitySearchController;
break;
} else {
// A related controller already exists for the same entity, with the same 'pathPrefix',
// but in a different package.
LOGGER.log(Level.INFO, String.format("ERROR: Already exists a controller associated " + "to entity '%s' with the pathPrefix '%s', in a different package. Specify " + "a different pathPrefix to create a new one, or the same 'package' and " + "'pathPrefix' to update the existing controller.", entity.getSimpleTypeName(), pathPrefix));
return;
}
} else if (entitySearchController.getType().getPackage().equals(controllerPackage)) {
// A related controller already exists for the same entity, in the same package
LOGGER.log(Level.INFO, String.format("ERROR: Already exists a controller associated to entity '%s' in the " + "same package '%s', with different 'pathPrefix'. Specify a different 'pathPrefix' " + "and a different package that the existing one to create a new one, or the same " + "'package' and 'pathPrefix' to update the existing controller.", entity.getSimpleTypeName(), controllerPackage.getFullyQualifiedPackageName()));
return;
}
}
// Update or create the search controller
ClassOrInterfaceTypeDetailsBuilder controllerBuilder = null;
if (controllerToUpdateOrCreate == null) {
// Create controller builder for a new file
controllerBuilder = buildNewSearchController(entity, queryMethods, responseType, controllerPackage, pathPrefix);
} else {
// Controller already exists, so create builder with it
controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerToUpdateOrCreate);
// Update existing controller
boolean findersAdded = updateExistingSearchController(queryMethods, controllerToUpdateOrCreate, controllerBuilder);
// Check if response type is already added
AnnotationMetadata responseTypeAnnotation = controllerToUpdateOrCreate.getAnnotation(responseType.getAnnotation());
if (responseTypeAnnotation != null) {
if (!findersAdded) {
// Controller already had same response type annotation and same finders added
LOGGER.log(Level.WARNING, String.format("Controller %s already has specified finders and specified response type.", controllerToUpdateOrCreate.getType().getFullyQualifiedTypeName()));
return;
}
} else {
// Add annotation for the new response type
controllerBuilder.addAnnotation(new AnnotationMetadataBuilder(responseType.getAnnotation()));
}
}
// Add dependencies between modules if required
addModuleDependencies(entity, relatedRepositoryDetails, relatedService, controllerBuilder.build());
// Write changes to disk
typeManagementService.createOrUpdateTypeOnDisk(controllerBuilder.build());
// Create LinkFactory class for the search controler
controllerOperations.createLinkFactoryClass(controllerBuilder.getName());
}
Aggregations