use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getRndField.
private FieldMetadataBuilder getRndField() {
int index = -1;
while (true) {
// Compute the required field name
index++;
final JavaSymbolName fieldName = new JavaSymbolName("rnd" + StringUtils.repeat("_", index));
this.rndFieldName = fieldName;
final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
if (candidate != null) {
// Verify if candidate is suitable
if (!Modifier.isPrivate(candidate.getModifier())) {
// next possible name)
continue;
}
if (!candidate.getFieldType().equals(RANDOM)) {
// Candidate isn't a java.util.Random, so it isn't suitable
continue;
}
// we assume the user knows what they're doing and have made one
return new FieldMetadataBuilder(candidate);
}
// Candidate not found, so let's create one
builder.getImportRegistrationResolver().addImports(RANDOM, SECURE_RANDOM);
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId());
fieldBuilder.setModifier(Modifier.PRIVATE);
fieldBuilder.setFieldName(fieldName);
fieldBuilder.setFieldType(RANDOM);
fieldBuilder.setFieldInitializer("new SecureRandom()");
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("Random generator for the entities index."), CommentLocation.BEGINNING);
fieldBuilder.setCommentStructure(comment);
return fieldBuilder;
}
}
use of org.springframework.roo.classpath.details.FieldMetadata 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.FieldMetadata in project spring-roo by spring-projects.
the class JpaEntityMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalType, final String itdFilename) {
if (projectOperations == null) {
projectOperations = getProjectOperations();
}
Validate.notNull(projectOperations, "ProjectOperations is required");
// Find out the entity-level JPA details from the trigger annotation
final JpaEntityAnnotationValues jpaEntityAnnotationValues = getJpaEntityAnnotationValues(governorPhysicalType);
/*
* Walk the inheritance hierarchy for any existing JpaEntityMetadata. We
* don't need to monitor any such parent, as any changes to its Java
* type will trickle down to the governing java type.
*/
final JpaEntityMetadata parent = getParentMetadata(governorPhysicalType.getMemberHoldingTypeDetails());
// Get the governor's members
final MemberDetails governorMemberDetails = getMemberDetails(governorPhysicalType);
final String moduleName = PhysicalTypeIdentifierNamingUtils.getPath(metadataIdentificationString).getModule();
if (projectOperations.isProjectAvailable(moduleName)) {
// If the project itself changes, we want a chance to refresh this
// item
getMetadataDependencyRegistry().registerDependency(ProjectMetadata.getProjectIdentifier(moduleName), metadataIdentificationString);
}
// Getting entity details
JavaType entity = JpaEntityMetadata.getJavaType(metadataIdentificationString);
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
// Getting JavaBeanMetadata
String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
JavaBeanMetadata entityJavaBeanMetadata = getMetadataService().get(javaBeanMetadataKey);
// This metadata is not available yet
if (entityJavaBeanMetadata == null) {
return null;
}
// Locate relation fields to process
List<FieldMetadata> fieldsParent = new ArrayList<FieldMetadata>();
Map<String, FieldMetadata> relationsAsChild = new HashMap<String, FieldMetadata>();
for (FieldMetadata field : entityDetails.getDeclaredFields()) {
if (field.getAnnotation(RooJavaType.ROO_JPA_RELATION) != null) {
fieldsParent.add(field);
} else if (field.getAnnotation(JpaJavaType.ONE_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_ONE) != null || field.getAnnotation(JpaJavaType.MANY_TO_MANY) != null) {
relationsAsChild.put(field.getFieldName().getSymbolName(), field);
}
}
// Check if it's a child part of a composition
FieldMetadata compositionRelationField;
try {
compositionRelationField = getCompositionRelationField(entity, entityDetails, relationsAsChild);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Problems found when trying to identify composition relationship", e);
}
// Getting identifier field and version field and its accessors
FieldMetadata identifierField = null;
MethodMetadata identifierAccessor = null;
FieldMetadata versionField = null;
MethodMetadata versionAccessor = null;
if (parent == null) {
// Obtain identifier field from entity details
List<FieldMetadata> identifierFields = entityDetails.getFieldsWithAnnotation(ID);
List<FieldMetadata> embeddedIdentifierFields = entityDetails.getFieldsWithAnnotation(EMBEDDED_ID);
Validate.isTrue(!(identifierFields.isEmpty() && embeddedIdentifierFields.isEmpty()), String.format("ERROR: The annotated entity '%s' doesn't contain any identifier field.", entityDetails.getType().getFullyQualifiedTypeName()));
if (!identifierFields.isEmpty()) {
identifierField = identifierFields.get(0);
} else if (!embeddedIdentifierFields.isEmpty()) {
identifierField = embeddedIdentifierFields.get(0);
}
identifierAccessor = entityJavaBeanMetadata.getAccesorMethod(identifierField);
// Obtain version field from entity details
List<FieldMetadata> versionFields = entityDetails.getFieldsWithAnnotation(VERSION);
// Check and add version field
if (!versionFields.isEmpty()) {
versionField = versionFields.get(0);
versionAccessor = entityJavaBeanMetadata.getAccesorMethod(versionField);
}
} else {
identifierField = parent.getCurrentIndentifierField();
versionField = parent.getCurrentVersionField();
}
return new JpaEntityMetadata(metadataIdentificationString, aspectName, governorPhysicalType, parent, governorMemberDetails, identifierField, identifierAccessor, versionField, versionAccessor, jpaEntityAnnotationValues, entityDetails, fieldsParent, relationsAsChild, compositionRelationField);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaOperationsImpl method getFieldChildPartOfRelation.
@Override
public List<Pair<FieldMetadata, RelationInfo>> getFieldChildPartOfRelation(ClassOrInterfaceTypeDetails entityCdi) {
JavaType domainType = entityCdi.getType();
JpaEntityMetadata entityMetadata = getJpaEntityMetadata(entityCdi);
Validate.notNull(entityMetadata, "%s should be a Jpa Entity", domainType);
Map<String, FieldMetadata> relations = entityMetadata.getRelationsAsChild();
List<Pair<FieldMetadata, RelationInfo>> childRelations = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
JpaEntityMetadata parent;
JavaType parentType;
RelationInfo info;
for (Entry<String, FieldMetadata> fieldEntry : relations.entrySet()) {
parentType = fieldEntry.getValue().getFieldType().getBaseType();
parent = getJpaEntityMetadata(parentType);
Validate.notNull(parent, "Can't get information about Entity %s which is declared as parent in %s.%s field", parentType, domainType, fieldEntry.getKey());
info = parent.getRelationInfosByMappedBy(domainType, fieldEntry.getKey());
if (info != null) {
childRelations.add(Pair.of(fieldEntry.getValue(), info));
}
}
return childRelations;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method addEmbeddedClassFieldMutatorMethodsToBuilder.
private void addEmbeddedClassFieldMutatorMethodsToBuilder(final EmbeddedHolder embeddedHolder, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
final JavaType[] parameterTypes = { embeddedFieldType, JavaType.INT_PRIMITIVE };
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
for (final FieldMetadata field : embeddedHolder.getFields()) {
final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
final JavaSymbolName fieldMutatorMethodName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.append(getFieldValidationBody(field, initializer, fieldMutatorMethodName, false));
final JavaSymbolName embeddedClassMethodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName());
if (governorHasMethod(embeddedClassMethodName, parameterTypes)) {
// Method found in governor so do not create method in ITD
continue;
}
builder.addMethod(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, embeddedClassMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
}
}
Aggregations