use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DbreMetadataProviderImpl method getIdentifierHolder.
private IdentifierHolder getIdentifierHolder(final JavaType javaType) {
final List<FieldMetadata> identifierFields = getPersistenceMemberLocator().getIdentifierFields(javaType);
if (identifierFields.isEmpty()) {
return null;
}
final FieldMetadata identifierField = identifierFields.get(0);
final boolean embeddedIdField = identifierField.getCustomData().get(CustomDataKeys.EMBEDDED_ID_FIELD) != null;
final List<FieldMetadata> embeddedIdFields = getPersistenceMemberLocator().getEmbeddedIdentifierFields(javaType);
return new IdentifierHolder(identifierField, embeddedIdField, embeddedIdFields);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DbreMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// We need to parse the annotation, which we expect to be present
final DbManagedAnnotationValues annotationValues = new DbManagedAnnotationValues(governorPhysicalTypeMetadata);
if (!annotationValues.isAnnotationFound()) {
return null;
}
// Abort if the database couldn't be deserialized. This can occur if the
// DBRE XML file has been deleted or is empty.
final Database database = getDbreModelService().getDatabase(false);
if (database == null) {
return null;
}
// We know governor type details are non-null and can be safely cast
final JavaType javaType = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getName();
final IdentifierHolder identifierHolder = getIdentifierHolder(javaType);
if (identifierHolder == null) {
return null;
}
final FieldMetadata versionField = getVersionField(javaType, metadataIdentificationString);
// Search for database-managed entities
final Iterable<ClassOrInterfaceTypeDetails> managedEntities = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_DB_MANAGED);
boolean found = false;
for (final ClassOrInterfaceTypeDetails managedEntity : managedEntities) {
if (managedEntity.getName().equals(javaType)) {
found = true;
break;
}
}
if (!found) {
final String mid = getTypeLocationService().getPhysicalTypeIdentifier(javaType);
getMetadataDependencyRegistry().registerDependency(mid, metadataIdentificationString);
return null;
}
final DbreMetadata dbreMetadata = new DbreMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, identifierHolder, versionField, managedEntities, database);
final ClassOrInterfaceTypeDetails updatedGovernor = dbreMetadata.getUpdatedGovernor();
if (updatedGovernor != null) {
getTypeManagementService().createOrUpdateTypeOnDisk(updatedGovernor);
}
return dbreMetadata;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DtoCommands method returnFieldValues.
/**
* Attempts to obtain entity specified in 'entity' option and returns an
* auto-complete list with the entity fields, separated by comma's.
*
* @param shellContext
* @return a List<String> with the possible values.
*/
@CliOptionAutocompleteIndicator(command = "entity projection", param = "fields", help = "Option fields must have a comma-separated list of valid fields. Please, assign it a " + "correct value. Transient, static and entity collection fields are not valid for " + "projections.", includeSpaceOnFinish = false)
public List<String> returnFieldValues(ShellContext shellContext) {
List<String> fieldValuesToReturn = new ArrayList<String>();
// Get entity JavaType
JavaType entity = getTypeFromEntityParam(shellContext);
// Get current fields in --field value
String currentFieldValue = shellContext.getParameters().get("fields");
String[] fields = StringUtils.split(currentFieldValue, ",");
// Check for bad written separators and return no options
if (currentFieldValue.contains(",.") || currentFieldValue.contains(".,")) {
return fieldValuesToReturn;
}
// Check if it is first field
if (currentFieldValue.equals("")) {
for (FieldMetadata field : getEntityFieldList(entity)) {
fieldValuesToReturn.add(field.getFieldName().getSymbolName());
}
return fieldValuesToReturn;
}
// VALIDATION OF CURRENT SPECIFIED VALUES UNTIL LAST MEMBER
JavaType lastRelatedEntity = null;
String completedValue = "";
List<FieldMetadata> entityFields = null;
boolean fieldFound = false;
boolean lastFieldIsEntity = false;
boolean isMainEntityField = true;
for (int i = 0; i < fields.length; i++) {
JavaType lastFieldType = entity;
// Split field by ".", in case it was a relation field
String[] splittedByPeriod = StringUtils.split(fields[i], ".");
// Build auto-complete values
for (int t = 0; t < splittedByPeriod.length; t++) {
fieldFound = false;
// Find the field in entity fields
if (typeLocationService.getTypeDetails(lastFieldType) != null && typeLocationService.getTypeDetails(lastFieldType).getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
entityFields = getEntityFieldList(lastFieldType);
}
for (FieldMetadata entityField : entityFields) {
lastFieldIsEntity = false;
if (splittedByPeriod[t].equals(entityField.getFieldName().getSymbolName())) {
// Add auto-complete value
if (completedValue.equals("")) {
completedValue = entityField.getFieldName().getSymbolName();
} else {
if (splittedByPeriod.length > 1 && t > 0) {
// Field is from a relation
completedValue = completedValue.concat(".").concat(entityField.getFieldName().getSymbolName());
} else {
// Field is a simple field
completedValue = completedValue.concat(",").concat(entityField.getFieldName().getSymbolName());
}
}
// Record last field JavaType for auto-completing last value
lastFieldType = entityField.getFieldType();
// Check if field is an entity different from original entity
if (typeLocationService.getTypeDetails(lastFieldType) != null && typeLocationService.getTypeDetails(lastFieldType).getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null && !entityField.getFieldType().equals(entity)) {
lastFieldIsEntity = true;
lastRelatedEntity = lastFieldType;
}
fieldFound = true;
break;
}
}
// Checks if field to autocomplete is from main entity
if (currentFieldValue.endsWith(".") || (splittedByPeriod.length > 1 && !fieldFound)) {
isMainEntityField = false;
}
}
}
// Add field separator if needed
if (fieldFound) {
// Always add current value for validation only
fieldValuesToReturn.add(completedValue);
// If field is entity, append , and . and return values
if (lastFieldIsEntity) {
fieldValuesToReturn.add(completedValue.concat(","));
fieldValuesToReturn.add(completedValue.concat("."));
if (!currentFieldValue.endsWith(",") && !currentFieldValue.endsWith(".")) {
return fieldValuesToReturn;
}
}
}
// Build auto-complete values for last member
String autocompleteValue = "";
if (isMainEntityField) {
// Complete simple fields. Add entity fields as auto-complete values
List<FieldMetadata> mainEntityFields = getEntityFieldList(entity);
for (FieldMetadata mainEntityField : mainEntityFields) {
if (completedValue.equals("")) {
// Is first field to complete
fieldValuesToReturn.add(mainEntityField.getFieldName().getSymbolName());
} else if (!completedValue.equals("")) {
// Check if field is specified and add it if not
boolean alreadySpecified = false;
// boolean relationField = false;
for (int i = 0; i < fields.length; i++) {
if (mainEntityField.getFieldName().getSymbolName().equals(fields[i])) {
alreadySpecified = true;
}
}
if (!alreadySpecified) {
// Add completion
autocompleteValue = completedValue.concat(",").concat(mainEntityField.getFieldName().getSymbolName());
} else if (alreadySpecified && typeIsEntity(mainEntityField.getFieldType())) {
// Add completion as relation field
autocompleteValue = completedValue.concat(",").concat(mainEntityField.getFieldName().getSymbolName()).concat(".");
}
// Add completion
fieldValuesToReturn.add(autocompleteValue);
}
}
} else if (lastRelatedEntity != null) {
// Complete with fields of current relation field
List<FieldMetadata> relatedEntityFields = getEntityFieldList(lastRelatedEntity);
for (FieldMetadata relatedEntityField : relatedEntityFields) {
autocompleteValue = completedValue.concat(".").concat(relatedEntityField.getFieldName().getSymbolName());
// Check if value already exists
String additionalValueToAdd = StringUtils.substringAfterLast(autocompleteValue, ",");
if (!fieldValuesToReturn.contains(autocompleteValue) && !currentFieldValue.contains(additionalValueToAdd)) {
fieldValuesToReturn.add(autocompleteValue);
} else if (!fieldValuesToReturn.contains(autocompleteValue) && typeIsEntity(relatedEntityField.getFieldType())) {
fieldValuesToReturn.add(autocompleteValue);
} else if (additionalValueToAdd.equals("")) {
fieldValuesToReturn.add(autocompleteValue);
}
}
}
return fieldValuesToReturn;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DtoCommands method getEntityFieldList.
/**
* Gets a list of fields from an entity. Static and transient fields are excluded
* as well as collection fields which type is an entity.
*
* @param entity the JavaType from which to obtain the field list.
* @return a List<FieldMetadata> with info of the entity fields.
*/
private List<FieldMetadata> getEntityFieldList(JavaType entity) {
List<FieldMetadata> fieldList = new ArrayList<FieldMetadata>();
ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(entity);
Validate.notNull(typeDetails, String.format("Cannot find details for class %s. Please be sure that the class exists.", entity.getFullyQualifiedTypeName()));
MemberDetails entityMemberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), typeDetails);
Validate.notNull(entityMemberDetails.getAnnotation(JpaJavaType.ENTITY), String.format("%s must be an entity to obtain it's fields info.", entity.getFullyQualifiedTypeName()));
// Get fields and check for other fields from relations
List<FieldMetadata> entityFields = entityMemberDetails.getFields();
for (FieldMetadata field : entityFields) {
// Exclude static fields
if (Modifier.isStatic(field.getModifier())) {
continue;
}
// Exclude transient fields
if (field.getAnnotation(JpaJavaType.TRANSIENT) != null) {
continue;
}
// Exclude entity collection fields
JavaType fieldType = field.getFieldType();
if (fieldType.isCommonCollectionType()) {
boolean isEntityCollectionField = false;
List<JavaType> parameters = fieldType.getParameters();
for (JavaType parameter : parameters) {
if (typeIsEntity(parameter)) {
isEntityCollectionField = true;
break;
}
}
if (isEntityCollectionField) {
continue;
}
}
fieldList.add(field);
}
return fieldList;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class EqualsMetadata method generateDefaultEqualsMethodBody.
/**
* Returns the default `equals` method body. Used for not-entity classes.
*
* @return {@link InvocableMemberBodyBuilder}
*/
private static InvocableMemberBodyBuilder generateDefaultEqualsMethodBody(JavaType target, boolean appendSuper, List<FieldMetadata> fields, ImportRegistrationResolver importRegistrationResolver) {
String typeName = target.getSimpleTypeName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("if (!(" + OBJECT_NAME + " instanceof " + typeName + ")) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return false;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("if (this == " + OBJECT_NAME + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return true;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine(typeName + " rhs = (" + typeName + ") " + OBJECT_NAME + ";");
final StringBuilder builder = new StringBuilder(String.format("return new %s()", EQUALS_BUILDER.getNameIncludingTypeParameters(false, importRegistrationResolver)));
if (appendSuper) {
builder.append(".appendSuper(super.equals(" + OBJECT_NAME + "))");
}
for (final FieldMetadata field : fields) {
builder.append(".append(" + field.getFieldName() + ", rhs." + field.getFieldName() + ")");
}
builder.append(".isEquals();");
bodyBuilder.appendFormalLine(builder.toString());
return bodyBuilder;
}
Aggregations