use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class WebFinderOperationsImpl method updateExistingSearchController.
/**
* Update existing controller with new query methods
* @param queryMethods the finder names to add
* @param controllerToUpdateOrCreate the existing controller
* @param controllerBuilder the builder to apply the changes
* @return
*/
private boolean updateExistingSearchController(List<String> queryMethods, ClassOrInterfaceTypeDetails controllerToUpdateOrCreate, ClassOrInterfaceTypeDetailsBuilder controllerBuilder) {
// Get @RooSearch and build necessary variables
AnnotationMetadata searchAnnotation = controllerToUpdateOrCreate.getAnnotation(RooJavaType.ROO_SEARCH);
List<AnnotationAttributeValue<?>> findersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
List<String> finderNames = new ArrayList<String>();
boolean findersAdded = false;
// Get existent finder values
if (searchAnnotation != null && searchAnnotation.getAttribute("finders") != null) {
List<?> existingFinders = (List<?>) searchAnnotation.getAttribute("finders").getValue();
Iterator<?> it = existingFinders.iterator();
// Add existent finders to new attributes array to merge with new ones
while (it.hasNext()) {
StringAttributeValue attributeValue = (StringAttributeValue) it.next();
findersToAdd.add(attributeValue);
finderNames.add(attributeValue.getValue());
}
// Add new finders to new attributes array
for (String finder : queryMethods) {
if (!finderNames.contains(finder)) {
findersToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), finder));
findersAdded = true;
}
}
// Add attributes array to @RooSearch
AnnotationMetadataBuilder searchAnnotationBuilder = new AnnotationMetadataBuilder(searchAnnotation);
ArrayAttributeValue<AnnotationAttributeValue<?>> allFinders = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), findersToAdd);
searchAnnotationBuilder.addAttribute(allFinders);
controllerBuilder.updateTypeAnnotation(searchAnnotationBuilder);
}
return findersAdded;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class WebFinderOperationsImpl method buildNewSearchController.
/**
* Build a new search controller for provided entity, with provided response type,
* package, path prefix and query methods.
*
* @param entity
* @param queryMethods
* @param responseType
* @param controllerPackage
* @param pathPrefix
* @return
*/
private ClassOrInterfaceTypeDetailsBuilder buildNewSearchController(JavaType entity, List<String> queryMethods, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefix) {
ClassOrInterfaceTypeDetailsBuilder controllerBuilder;
JavaType searchController = new JavaType(String.format("%s.%sSearch%sController", controllerPackage.getFullyQualifiedPackageName(), pluralService.getPlural(entity), responseType.getControllerNameModifier()), controllerPackage.getModule());
final String physicalPath = PhysicalTypeIdentifier.createIdentifier(searchController, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, controllerPackage.getModule()));
controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(physicalPath, Modifier.PUBLIC, searchController, PhysicalTypeCategory.CLASS);
// Create @RooController
AnnotationMetadataBuilder controllerAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_CONTROLLER);
controllerAnnotation.addClassAttribute("entity", entity);
if (StringUtils.isNotBlank(pathPrefix)) {
controllerAnnotation.addStringAttribute("pathPrefix", pathPrefix);
}
controllerAnnotation.addEnumAttribute("type", new EnumDetails(RooJavaType.ROO_ENUM_CONTROLLER_TYPE, new JavaSymbolName("SEARCH")));
controllerBuilder.addAnnotation(controllerAnnotation.build());
// Create @RooSearch
AnnotationMetadataBuilder searchAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_SEARCH);
List<AnnotationAttributeValue<?>> findersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
for (String finder : queryMethods) {
findersToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), finder));
}
searchAnnotation.addAttribute(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), findersToAdd));
controllerBuilder.addAnnotation(searchAnnotation);
// Add response type annotation
AnnotationMetadataBuilder responseTypeAnnotation = new AnnotationMetadataBuilder(responseType.getAnnotation());
controllerBuilder.addAnnotation(responseTypeAnnotation);
return controllerBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class AbstractViewGenerationService method createFieldItem.
protected FieldItem createFieldItem(JpaEntityMetadata entityMetadata, FieldMetadata entityField, String entityName, String suffixId, ViewContext<T> ctx, String referencedFieldName) {
// Getting current identifier field
FieldMetadata identifierField = entityMetadata.getCurrentIndentifierField();
FieldMetadata versionField = entityMetadata.getCurrentVersionField();
// Exclude id and version fields
if (entityField.getAnnotation(JpaJavaType.ID) != null || entityField.getAnnotation(JpaJavaType.VERSION) != null || identifierField.getFieldName().equals(entityField.getFieldName()) || (versionField != null && versionField.getFieldName().equals(entityField.getFieldName()))) {
return null;
}
// Generating new FieldItem element
FieldItem fieldItem = null;
if (referencedFieldName != null) {
fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), ctx.getEntityName(), referencedFieldName, entityName, suffixId);
} else {
fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), entityName, suffixId);
}
// Calculate fieldType
JavaType type = entityField.getFieldType();
ClassOrInterfaceTypeDetails typeDetails = getTypeLocationService().getTypeDetails(type);
// ROO-3810: Getting @NotNull annotation to include required attr
AnnotationMetadata notNullAnnotation = entityField.getAnnotation(Jsr303JavaType.NOT_NULL);
if (notNullAnnotation != null) {
fieldItem.addConfigurationElement("required", true);
} else {
fieldItem.addConfigurationElement("required", false);
}
// Check if is a referenced field
if (typeDetails != null && typeDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
// If is child part field of a composition relation, specify it if view context
if (entityMetadata.getCompositionRelationField() != null && entityMetadata.getCompositionRelationField().getFieldName().equals(entityField.getFieldName())) {
fieldItem.addConfigurationElement("isCompositionChildField", true);
} else {
fieldItem.addConfigurationElement("isCompositionChildField", false);
}
boolean shouldBeAdded = getReferenceField(fieldItem, typeDetails, ctx);
if (!shouldBeAdded) {
return null;
}
} else if (type.isBoolean()) {
// Check if is a boolean field
fieldItem.setType(FieldTypes.BOOLEAN.toString());
} else if (typeDetails != null && typeDetails.getPhysicalTypeCategory().equals(PhysicalTypeCategory.ENUMERATION)) {
// Saving enum and items to display. Same name as
// populateForm method
fieldItem.setType(FieldTypes.ENUM.toString());
fieldItem.addConfigurationElement("items", fieldItem.getFieldName());
} else if (type.getFullyQualifiedTypeName().equals(Date.class.getName()) || type.getFullyQualifiedTypeName().equals(Calendar.class.getName())) {
// Check if is a date field
fieldItem.setType(FieldTypes.DATE.toString());
// Getting datetime format to use
AnnotationMetadata dateTimeFormatAnnotation = entityField.getAnnotation(SpringJavaType.DATE_TIME_FORMAT);
String format = "d/m/Y";
if (dateTimeFormatAnnotation != null) {
AnnotationAttributeValue<String> styleAttribute = dateTimeFormatAnnotation.getAttribute("style");
if (styleAttribute != null) {
String annotationFormat = styleAttribute.getValue();
if (annotationFormat.equals("M-")) {
format = "d-M-Y";
} else {
format = annotationFormat;
}
}
}
fieldItem.addConfigurationElement("format", format);
} else if (type.getFullyQualifiedTypeName().equals("java.util.Set") || type.getFullyQualifiedTypeName().equals("java.util.List")) {
// getDetailsFieldViewItems method
return null;
} else if (type.isNumber()) {
// ROO-3810: Getting @Min and @Max annotations to add validations if
// necessary
AnnotationMetadata minAnnotation = entityField.getAnnotation(Jsr303JavaType.MIN);
if (minAnnotation != null) {
AnnotationAttributeValue<Object> min = minAnnotation.getAttribute("value");
if (min != null) {
fieldItem.addConfigurationElement("min", min.getValue().toString());
} else {
fieldItem.addConfigurationElement("min", "NULL");
}
} else {
fieldItem.addConfigurationElement("min", "NULL");
}
AnnotationMetadata maxAnnotation = entityField.getAnnotation(Jsr303JavaType.MAX);
if (maxAnnotation != null) {
AnnotationAttributeValue<Object> max = maxAnnotation.getAttribute("value");
if (max != null) {
fieldItem.addConfigurationElement("max", max.getValue().toString());
} else {
fieldItem.addConfigurationElement("max", "NULL");
}
} else {
fieldItem.addConfigurationElement("max", "NULL");
}
// ROO-3872: Support numeric input validation from client and server side
// Add digits constraints
AnnotationMetadata digitsAnnotation = entityField.getAnnotation(Jsr303JavaType.DIGITS);
if (digitsAnnotation != null) {
// Fraction digits
AnnotationAttributeValue<Object> digitsFraction = digitsAnnotation.getAttribute("fraction");
fieldItem.addConfigurationElement("digitsFraction", digitsFraction.getValue().toString());
// Integer digits
AnnotationAttributeValue<Object> digitsInteger = digitsAnnotation.getAttribute("integer");
fieldItem.addConfigurationElement("digitsInteger", digitsInteger.getValue().toString());
} else {
// Add default values for decimals
if (type.equals(JavaType.INT_OBJECT) || type.equals(JavaType.INT_PRIMITIVE) || type.equals(JdkJavaType.BIG_INTEGER)) {
fieldItem.addConfigurationElement("digitsFraction", "0");
} else {
fieldItem.addConfigurationElement("digitsFraction", "2");
}
fieldItem.addConfigurationElement("digitsInteger", "NULL");
}
fieldItem.setType(FieldTypes.NUMBER.toString());
} else {
// ROO-3810: Getting @Size annotation
AnnotationMetadata sizeAnnotation = entityField.getAnnotation(Jsr303JavaType.SIZE);
if (sizeAnnotation != null) {
AnnotationAttributeValue<Object> maxLength = sizeAnnotation.getAttribute("max");
if (maxLength != null) {
fieldItem.addConfigurationElement("maxLength", maxLength.getValue().toString());
} else {
fieldItem.addConfigurationElement("maxLength", "NULL");
}
} else {
fieldItem.addConfigurationElement("maxLength", "NULL");
}
fieldItem.setType(FieldTypes.TEXT.toString());
}
return fieldItem;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class StringOrNumericField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
if (decimalMin != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("value"), decimalMin));
annotations.add(new AnnotationMetadataBuilder(DECIMAL_MIN, attrs));
}
if (decimalMax != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("value"), decimalMax));
annotations.add(new AnnotationMetadataBuilder(DECIMAL_MAX, attrs));
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue in project spring-roo by spring-projects.
the class CollectionField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
if (sizeMin != null || sizeMax != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
if (sizeMin != null) {
attrs.add(new IntegerAttributeValue(new JavaSymbolName("min"), sizeMin));
}
if (sizeMax != null) {
attrs.add(new IntegerAttributeValue(new JavaSymbolName("max"), sizeMax));
}
annotations.add(new AnnotationMetadataBuilder(SIZE, attrs));
}
}
Aggregations