use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class JspOperationsImpl method createManualController.
/**
* Creates a new Spring MVC controller.
* <p>
* Request mappings assigned by this method will always commence with "/"
* and end with "/**". You may present this prefix and/or this suffix if you
* wish, although it will automatically be added should it not be provided.
*
* @param controller the controller class to create (required)
* @param preferredMapping the mapping this controller should adopt
* (optional; if unspecified it will be based on the controller
* name)
*/
public void createManualController(final JavaType controller, final String preferredMapping, final LogicalPath webappPath) {
Validate.notNull(controller, "Controller Java Type required");
// Create annotation @RequestMapping("/myobject/**")
final ImmutablePair<String, String> folderAndMapping = getFolderAndMapping(preferredMapping, controller);
final String folderName = folderAndMapping.getKey();
final String resourceIdentifier = getPathResolver().getFocusedCanonicalPath(Path.SRC_MAIN_JAVA, controller);
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(controller, getProjectOperations().getPathResolver().getPath(resourceIdentifier));
final List<MethodMetadataBuilder> methods = new ArrayList<MethodMetadataBuilder>();
// Add HTTP post method
methods.add(getHttpPostMethod(declaredByMetadataId));
// Add index method
methods.add(getIndexMethod(folderName, declaredByMetadataId));
// Create Type definition
final List<AnnotationMetadataBuilder> typeAnnotations = new ArrayList<AnnotationMetadataBuilder>();
final List<AnnotationAttributeValue<?>> requestMappingAttributes = new ArrayList<AnnotationAttributeValue<?>>();
requestMappingAttributes.add(new StringAttributeValue(new JavaSymbolName("value"), folderAndMapping.getValue()));
final AnnotationMetadataBuilder requestMapping = new AnnotationMetadataBuilder(REQUEST_MAPPING, requestMappingAttributes);
typeAnnotations.add(requestMapping);
// Create annotation @Controller
final List<AnnotationAttributeValue<?>> controllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
final AnnotationMetadataBuilder controllerAnnotation = new AnnotationMetadataBuilder(CONTROLLER, controllerAttributes);
typeAnnotations.add(controllerAnnotation);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, controller, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(typeAnnotations);
cidBuilder.setDeclaredMethods(methods);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
installView(folderName, "/index", new JavaSymbolName(controller.getSimpleTypeName()).getReadableSymbolName() + " View", "Controller", null, false, webappPath);
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue 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.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class ControllerOperationsImpl method getRooDetailAnnotation.
/**
* Method that returns @RooDetail annotation
*
* @param relationField
* Field that set the relationship
* @param viewsList
* Separated comma list that defines the parent views where the
* new detail will be displayed.
* @return
*/
private AnnotationMetadataBuilder getRooDetailAnnotation(final String relationField, final String viewsList) {
AnnotationMetadataBuilder annotationDetail = new AnnotationMetadataBuilder(RooJavaType.ROO_DETAIL);
annotationDetail.addStringAttribute("relationField", relationField);
// Including views attribute if needed
if (StringUtils.isNotEmpty(viewsList)) {
String[] views = viewsList.split(",");
List<StringAttributeValue> viewsValues = new ArrayList<StringAttributeValue>();
for (String view : views) {
viewsValues.add(new StringAttributeValue(new JavaSymbolName("value"), view));
}
ArrayAttributeValue<StringAttributeValue> viewsAttr = new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("views"), viewsValues);
annotationDetail.addAttribute(viewsAttr);
}
return annotationDetail;
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class ControllerOperationsImpl method getRooControllerAnnotation.
/**
* Method that returns @RooController annotation
*
* @param entity
* Entity over which create the controller
* @param pathPrefix
* Prefix to use in RequestMapping
* @param controllerType
* Indicates the controller type
* @return
*/
private AnnotationMetadataBuilder getRooControllerAnnotation(final JavaType entity, final String pathPrefix, final ControllerType controllerType) {
final List<AnnotationAttributeValue<?>> rooControllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
rooControllerAttributes.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
if (StringUtils.isNotEmpty(pathPrefix)) {
rooControllerAttributes.add(new StringAttributeValue(new JavaSymbolName("pathPrefix"), pathPrefix));
}
rooControllerAttributes.add(new EnumAttributeValue(new JavaSymbolName("type"), new EnumDetails(RooJavaType.ROO_ENUM_CONTROLLER_TYPE, new JavaSymbolName(controllerType.name()))));
return new AnnotationMetadataBuilder(RooJavaType.ROO_CONTROLLER, rooControllerAttributes);
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class ControllerOperationsImpl method exportOperation.
/**
* Generate the operations selected in the controller indicated
*
* @param controller
* Controller where the operations will be created
* @param operations
* Service operations names that will be created
*/
public void exportOperation(JavaType controller, List<String> operations) {
ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
// Check if provided controller exists on current project
Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
// Check if provided controller has been annotated with @RooController
Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
// Check parameter operations
Validate.notEmpty(operations, "INFO: Don't exist operations to publish");
ClassOrInterfaceTypeDetailsBuilder controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
AnnotationMetadata operationsAnnotation = controllerDetails.getAnnotation(RooJavaType.ROO_OPERATIONS);
// Create an array with new attributes array
List<StringAttributeValue> operationsToAdd = new ArrayList<StringAttributeValue>();
if (operationsAnnotation == null) {
// Add Operations annotation
AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_OPERATIONS);
controllerBuilder.addAnnotation(opAnnotation);
// set operations from command
for (String operation : operations) {
operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
}
opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
} else {
List<String> operationsNames = new ArrayList<String>();
boolean operationsAdded = false;
AnnotationAttributeValue<Object> attributeOperations = operationsAnnotation.getAttribute("operations");
if (attributeOperations != null) {
List<StringAttributeValue> existingOperations = (List<StringAttributeValue>) attributeOperations.getValue();
Iterator<StringAttributeValue> it = existingOperations.iterator();
// new ones
while (it.hasNext()) {
StringAttributeValue attributeValue = (StringAttributeValue) it.next();
operationsToAdd.add(attributeValue);
operationsNames.add(attributeValue.getValue());
}
// Add new finders to new attributes array
for (String operation : operations) {
if (!operationsNames.contains(operation)) {
operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
operationsAdded = true;
}
}
if (operationsAdded) {
AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(operationsAnnotation);
opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
controllerBuilder.updateTypeAnnotation(opAnnotation);
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
}
}
}
}
Aggregations