use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JpaEntityMetadata method getEntityAnnotation.
/**
* Generates the JPA @Entity annotation to be applied to the entity
*
* @return
*/
private AnnotationMetadata getEntityAnnotation() {
AnnotationMetadata entityAnnotation = getTypeAnnotation(ENTITY);
if (entityAnnotation == null) {
return null;
}
if (StringUtils.isNotBlank(annotationValues.getEntityName())) {
final AnnotationMetadataBuilder entityBuilder = new AnnotationMetadataBuilder(entityAnnotation);
entityBuilder.addStringAttribute("name", annotationValues.getEntityName());
entityAnnotation = entityBuilder.build();
}
return entityAnnotation;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JpaEntityMetadataProviderImpl method getCompositionRelationField.
/**
* Gets {@link FieldMetadata} of entity field which declares a composition relationship
*
* @param entity
* @param entityDetails
* @param relationsAsChild
* @return
* @throws ClassNotFoundException
*/
private FieldMetadata getCompositionRelationField(JavaType entity, ClassOrInterfaceTypeDetails entityDetails, Map<String, FieldMetadata> relationsAsChild) throws ClassNotFoundException {
// Try to identify if it's is a child part of a composition.
// It uses details and annotation values instead metadata to
// avoid problems of circular dependencies
ClassOrInterfaceTypeDetails parentDatils;
FieldMetadata compositionRelationField = null;
AnnotationMetadata parentFieldRelationAnnotation;
JpaRelationType type;
String parentMappedBy;
for (FieldMetadata field : relationsAsChild.values()) {
parentDatils = getTypeLocationService().getTypeDetails(field.getFieldType().getBaseType());
for (FieldMetadata parentField : parentDatils.getFieldsWithAnnotation(RooJavaType.ROO_JPA_RELATION)) {
parentFieldRelationAnnotation = parentField.getAnnotation(RooJavaType.ROO_JPA_RELATION);
if (parentFieldRelationAnnotation != null && entity.equals(parentField.getFieldType().getBaseType())) {
parentMappedBy = getFieldMappedByAnnotationValue(parentField);
if (field.getFieldName().getSymbolName().equals(parentMappedBy)) {
// Found parent relation field
// Check composition
EnumDetails value = ((EnumAttributeValue) parentFieldRelationAnnotation.getAttribute(new JavaSymbolName("type"))).getValue();
if (JpaRelationType.COMPOSITION.name().equals(value.getField().getSymbolName())) {
// Found composition
if (compositionRelationField != null) {
throw new IllegalArgumentException(String.format("Found to relations which '%s' is child part of composition relation field: '%s' and '%s'", entity.getFullyQualifiedTypeName(), compositionRelationField.getFieldName(), field.getFieldName()));
}
compositionRelationField = field;
}
}
}
}
}
return compositionRelationField;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getMinAndMaxBody.
private String getMinAndMaxBody(final FieldMetadata field, final String suffix) {
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final AnnotationMetadata minAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MIN);
final AnnotationMetadata maxAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MAX);
if (minAnnotation != null && maxAnnotation == null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE).getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\")) == -1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + minValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
} else if (minAnnotation == null && maxAnnotation != null) {
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE).getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
} else if (minAnnotation != null && maxAnnotation != null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE).getValue();
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE).getValue();
Validate.isTrue(maxValue.longValue() >= minValue.longValue(), "The value of @Max must be greater or equal to the value of @Min for field %s", fieldName);
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\")) == -1 || " + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + " || " + fieldName + " > " + maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
return bodyBuilder.getOutput();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getFieldInitializer.
private String getFieldInitializer(final FieldMetadata field, final JpaEntityFactoryMetadata collaboratingMetadata, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
final JavaType fieldType = field.getFieldType();
final String fieldName = field.getFieldName().getSymbolName();
String initializer = "null";
final String fieldInitializer = field.getFieldInitializer();
final Set<Object> fieldCustomDataKeys = field.getCustomData().keySet();
// Date fields included for DataNucleus (
if (fieldType.equals(DATE)) {
if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST) != null) {
builder.getImportRegistrationResolver().addImport(DATE);
initializer = "new Date(new Date().getTime() - 10000000L)";
} else if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE) != null) {
builder.getImportRegistrationResolver().addImport(DATE);
initializer = "new Date(new Date().getTime() + 10000000L)";
} else {
builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR);
initializer = "new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.HOUR_OF_DAY), \n\t\t\tCalendar.getInstance().get(Calendar.MINUTE), \n\t\t\tCalendar.getInstance().get(Calendar.SECOND) + \n\t\t\tnew Double(Math.random() * 1000).intValue()).getTime()";
}
} else if (fieldType.equals(CALENDAR)) {
builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR);
final String calendarString = "new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH)";
if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST) != null) {
initializer = calendarString + " - 1)";
} else if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE) != null) {
initializer = calendarString + " + 1)";
} else {
initializer = "Calendar.getInstance()";
}
} else if (fieldType.equals(TIMESTAMP)) {
builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR, TIMESTAMP);
initializer = "new Timestamp(new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.HOUR_OF_DAY), \n\t\t\tCalendar.getInstance().get(Calendar.MINUTE), \n\t\t\tCalendar.getInstance().get(Calendar.SECOND) + \n\t\t\tnew Double(Math.random() * 1000).intValue()).getTime().getTime())";
} else if (fieldType.equals(STRING)) {
if (fieldInitializer != null && fieldInitializer.contains("\"")) {
final int offset = fieldInitializer.indexOf("\"");
initializer = fieldInitializer.substring(offset + 1, fieldInitializer.lastIndexOf("\""));
} else {
initializer = fieldName;
}
if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), VALIDATOR_CONSTRAINTS_EMAIL) != null || fieldName.toLowerCase().contains("email")) {
initializer = "\"foo\" + " + INDEX_VAR + " + \"@bar.com\"";
} else {
int maxLength = Integer.MAX_VALUE;
// Check for @Size
final AnnotationMetadata sizeAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE);
if (sizeAnnotation != null) {
final AnnotationAttributeValue<?> maxValue = sizeAnnotation.getAttribute(MAX_SYMBOL);
if (maxValue != null) {
validateNumericAnnotationAttribute(fieldName, "@Size", "max", maxValue.getValue());
maxLength = ((Integer) maxValue.getValue()).intValue();
}
final AnnotationAttributeValue<?> minValue = sizeAnnotation.getAttribute(MIN_SYMBOL);
if (minValue != null) {
validateNumericAnnotationAttribute(fieldName, "@Size", "min", minValue.getValue());
final int minLength = ((Integer) minValue.getValue()).intValue();
Validate.isTrue(maxLength >= minLength, "@Size attribute 'max' must be greater than 'min' for field '%s' in %s", fieldName, entity.getFullyQualifiedTypeName());
if (initializer.length() + 2 < minLength) {
initializer = String.format("%1$-" + (minLength - 2) + "s", initializer).replace(' ', 'x');
}
}
} else {
if (field.getCustomData().keySet().contains(CustomDataKeys.COLUMN_FIELD)) {
@SuppressWarnings("unchecked") final Map<String, Object> columnValues = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
if (columnValues.keySet().contains("length")) {
final Object lengthValue = columnValues.get("length");
validateNumericAnnotationAttribute(fieldName, "@Column", "length", lengthValue);
maxLength = ((Integer) lengthValue).intValue();
}
}
}
switch(maxLength) {
case 0:
initializer = "\"\"";
break;
case 1:
initializer = "String.valueOf(" + INDEX_VAR + ")";
break;
case 2:
initializer = "\"" + initializer.charAt(0) + "\" + " + INDEX_VAR;
break;
default:
if (initializer.length() + 2 > maxLength) {
initializer = "\"" + initializer.substring(0, maxLength - 2) + "_\" + " + INDEX_VAR;
} else {
initializer = "\"" + initializer + "_\" + " + INDEX_VAR;
}
}
}
} else if (fieldType.equals(new JavaType(STRING.getFullyQualifiedTypeName(), 1, DataType.TYPE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ \"Y\", \"N\" }");
} else if (fieldType.equals(JavaType.BOOLEAN_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "Boolean.TRUE");
} else if (fieldType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "true");
} else if (fieldType.equals(JavaType.INT_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ")");
} else if (fieldType.equals(JavaType.INT_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, INDEX_VAR);
} else if (fieldType.equals(new JavaType(JavaType.INT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ " + INDEX_VAR + ", " + INDEX_VAR + " }");
} else if (fieldType.equals(JavaType.DOUBLE_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
").doubleValue()");
} else if (fieldType.equals(JavaType.DOUBLE_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").doubleValue()");
} else if (fieldType.equals(new JavaType(JavaType.DOUBLE_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").doubleValue(), new Integer(" + INDEX_VAR + ").doubleValue() }");
} else if (fieldType.equals(JavaType.FLOAT_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
").floatValue()");
} else if (fieldType.equals(JavaType.FLOAT_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").floatValue()");
} else if (fieldType.equals(new JavaType(JavaType.FLOAT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").floatValue(), new Integer(" + INDEX_VAR + ").floatValue() }");
} else if (fieldType.equals(JavaType.LONG_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, // Auto-boxed
"new Integer(" + INDEX_VAR + ").longValue()");
} else if (fieldType.equals(JavaType.LONG_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").longValue()");
} else if (fieldType.equals(new JavaType(JavaType.LONG_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").longValue(), new Integer(" + INDEX_VAR + ").longValue() }");
} else if (fieldType.equals(JavaType.SHORT_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
").shortValue()");
} else if (fieldType.equals(JavaType.SHORT_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").shortValue()");
} else if (fieldType.equals(new JavaType(JavaType.SHORT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").shortValue(), new Integer(" + INDEX_VAR + ").shortValue() }");
} else if (fieldType.equals(JavaType.CHAR_OBJECT)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Character('N')");
} else if (fieldType.equals(JavaType.CHAR_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "'N'");
} else if (fieldType.equals(new JavaType(JavaType.CHAR_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ 'Y', 'N' }");
} else if (fieldType.equals(BIG_DECIMAL)) {
builder.getImportRegistrationResolver().addImport(BIG_DECIMAL);
initializer = BIG_DECIMAL.getSimpleTypeName() + ".valueOf(" + INDEX_VAR + ")";
} else if (fieldType.equals(BIG_INTEGER)) {
builder.getImportRegistrationResolver().addImport(BIG_INTEGER);
initializer = BIG_INTEGER.getSimpleTypeName() + ".valueOf(" + INDEX_VAR + ")";
} else if (fieldType.equals(JavaType.BYTE_OBJECT)) {
initializer = "new Byte(" + StringUtils.defaultIfEmpty(fieldInitializer, "\"1\"") + ")";
} else if (fieldType.equals(JavaType.BYTE_PRIMITIVE)) {
initializer = "new Byte(" + StringUtils.defaultIfEmpty(fieldInitializer, "\"1\"") + ").byteValue()";
} else if (fieldType.equals(JavaType.BYTE_ARRAY_PRIMITIVE)) {
initializer = StringUtils.defaultIfEmpty(fieldInitializer, "String.valueOf(" + INDEX_VAR + ").getBytes()");
} else if (fieldType.equals(entity)) {
// Avoid circular references (ROO-562)
initializer = OBJ_VAR;
} else if (fieldCustomDataKeys.contains(CustomDataKeys.ENUMERATED_FIELD)) {
builder.getImportRegistrationResolver().addImport(fieldType);
initializer = fieldType.getSimpleTypeName() + ".class.getEnumConstants()[0]";
} else if (collaboratingMetadata != null && collaboratingMetadata.getEntityType() != null) {
requiredDataOnDemandCollaborators.add(fieldType);
initializer = getFieldInitializerForRelatedEntity(field, collaboratingMetadata, fieldCustomDataKeys, dataOnDemandClasses);
}
return initializer;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method isNullableJoinColumn.
private boolean isNullableJoinColumn(final FieldMetadata field) {
final AnnotationMetadata joinColumnAnnotation = field.getAnnotation(JOIN_COLUMN);
if (joinColumnAnnotation == null) {
return true;
}
final AnnotationAttributeValue<?> nullableAttr = joinColumnAnnotation.getAttribute(new JavaSymbolName("nullable"));
return nullableAttr == null || (Boolean) nullableAttr.getValue();
}
Aggregations