use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadataProviderImpl method getEmbeddedIdHolder.
private EmbeddedIdHolder getEmbeddedIdHolder(final MemberDetails memberDetails, final String metadataIdentificationString) {
final List<FieldMetadata> idFields = new ArrayList<FieldMetadata>();
final List<FieldMetadata> fields = MemberFindingUtils.getFieldsWithTag(memberDetails, EMBEDDED_ID_FIELD);
if (fields.isEmpty()) {
return null;
}
final FieldMetadata embeddedIdField = fields.get(0);
final MemberDetails identifierMemberDetails = getMemberDetails(embeddedIdField.getFieldType());
if (identifierMemberDetails == null) {
return null;
}
for (final FieldMetadata field : identifierMemberDetails.getFields()) {
if (!(Modifier.isStatic(field.getModifier()) || Modifier.isFinal(field.getModifier()) || Modifier.isTransient(field.getModifier()))) {
getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
idFields.add(field);
}
}
return new EmbeddedIdHolder(embeddedIdField, idFields);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class PartTreeUnitTest method setUp.
@Before
public void setUp() throws IllegalArgumentException, IllegalAccessException {
List<FieldMetadata> declaredFields = new ArrayList<FieldMetadata>();
declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "text", 0, null, new JavaSymbolName("text"), new JavaType(String.class), null));
declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "number", 0, null, new JavaSymbolName("number"), new JavaType(Integer.class), null));
declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "date", 0, null, new JavaSymbolName("date"), new JavaType(Date.class), null));
declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "enumer", 0, null, new JavaSymbolName("enumer"), new JavaType(Enum.class), null));
declaredFields.add(new DefaultFieldMetadata(new CustomDataImpl(new HashMap<Object, Object>()), "primitiveInt", 0, null, new JavaSymbolName("primitiveInt"), JavaType.INT_PRIMITIVE, null));
final List<MemberHoldingTypeDetails> memberHoldingTypeDetails = new ArrayList<MemberHoldingTypeDetails>();
memberHoldingTypeDetails.add(new DefaultClassOrInterfaceTypeDetails(new CustomDataImpl(new HashMap<Object, Object>()), "Example", 0, null, new JavaType("Example"), PhysicalTypeCategory.CLASS, null, declaredFields, null, null, null, null, null, null, null, null));
memberDetails = new MemberDetailsImpl(memberHoldingTypeDetails);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final RepositoryJpaCustomAnnotationValues annotationValues = new RepositoryJpaCustomAnnotationValues(governorPhysicalTypeMetadata);
// Getting repository custom
JavaType entity = annotationValues.getEntity();
Validate.notNull(entity, "ERROR: Repository custom interface should be contain an entity on @RooJpaRepositoryCustom annotation");
final ClassOrInterfaceTypeDetails repositoryClass = getRepositoryJpaLocator().getRepository(entity);
final String repositoryMedatadataId = RepositoryJpaMetadata.createIdentifier(repositoryClass);
registerDependency(repositoryMedatadataId, metadataIdentificationString);
RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMedatadataId);
// This metadata is not available yet.
if (repositoryMetadata == null) {
return null;
}
// Add dependency between modules
ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
String module = cid.getName().getModule();
getTypeLocationService().addModuleDependency(module, entity);
getTypeLocationService().addModuleDependency(module, repositoryMetadata.getDefaultReturnType());
// Get field which entity is field part
List<Pair<FieldMetadata, RelationInfo>> relationsAsChild = getJpaOperations().getFieldChildPartOfRelation(entity);
for (Pair<FieldMetadata, RelationInfo> fieldInfo : relationsAsChild) {
// Add dependency between modules
getTypeLocationService().addModuleDependency(module, fieldInfo.getLeft().getFieldType());
}
// Register dependency between JavaBeanMetadata and this one
final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
final String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
registerDependency(javaBeanMetadataKey, metadataIdentificationString);
String entityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataKey);
// Entity metadata is not available
if (entityMetadata == null) {
return null;
}
return new RepositoryJpaCustomMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, entityMetadata.getCurrentIndentifierField().getFieldType(), entity, repositoryMetadata, relationsAsChild);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class FieldCommands method checkFieldExists.
/**
* Checks if entity has already a field with the same name and throws an exception
* in that case.
*
* @param fieldName
* @param shellContext
* @param javaTypeDetails
*
* @deprecated this should be done by operation class (see JpaFieldCreatorProvider.checkFieldExists)
*/
private void checkFieldExists(final JavaSymbolName fieldName, ShellContext shellContext, final ClassOrInterfaceTypeDetails javaTypeDetails) {
MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), javaTypeDetails);
List<FieldMetadata> fields = memberDetails.getFields();
for (FieldMetadata field : fields) {
if (field.getFieldName().equals(fieldName) && !shellContext.isForce()) {
throw new IllegalArgumentException(String.format("Field '%s' already exists and cannot be created. Try to use a " + "different field name on --fieldName parameter or use --force parameter to overwrite it.", fieldName));
}
}
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DtoOperationsImpl method createProjection.
@Override
public void createProjection(JavaType entity, JavaType name, String fields, String suffix, String formatExpression, String formatMessage) {
Validate.notNull(name, "Use --class to select the name of the Projection.");
// TODO: Validate fields for excluding entity collection, transient and
// static fields from operations (already doing when comming from commands).
// Set focus on projection module
projectOperations.setModule(projectOperations.getPomFromModuleName(name.getModule()));
// Add springlets-context dependency
projectOperations.addDependency(name.getModule(), SPRINGLETS_CONTEXT_DEPENDENCY);
projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
Map<String, FieldMetadata> fieldsToAdd = new LinkedHashMap<String, FieldMetadata>();
boolean onlyMainEntityFields = true;
if (fields != null) {
onlyMainEntityFields = false;
// Check that id field has been included. If not, include it.
fields = checkAndAddIdField(entity, fields);
fieldsToAdd = buildFieldsFromString(fields, entity);
} else {
List<FieldMetadata> allFields = memberDetailsScanner.getMemberDetails(this.getClass().getName(), typeLocationService.getTypeDetails(entity)).getFields();
for (FieldMetadata field : allFields) {
// Add only valid fields
if (isFieldValidForProjection(field)) {
fieldsToAdd.put(field.getFieldName().getSymbolName(), field);
}
}
}
// Create projection
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, name.getModule()));
final ClassOrInterfaceTypeDetailsBuilder projectionBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
// Add fields to projection
addFieldsToProjection(projectionBuilder, fieldsToAdd);
// @RooJavaBean, @RooToString and @RooEquals
AnnotationMetadataBuilder rooJavaBeanAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAVA_BEAN);
rooJavaBeanAnnotation.addBooleanAttribute("settersByDefault", false);
projectionBuilder.addAnnotation(rooJavaBeanAnnotation);
AnnotationMetadataBuilder rooToStringAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_TO_STRING);
AnnotationMetadataBuilder rooEqualsAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_EQUALS);
projectionBuilder.addAnnotation(rooToStringAnnotation);
projectionBuilder.addAnnotation(rooEqualsAnnotation);
// Add @RooEntityProjection
AnnotationMetadataBuilder projectionAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_ENTITY_PROJECTION);
projectionAnnotation.addClassAttribute("entity", entity);
List<StringAttributeValue> fieldNames = new ArrayList<StringAttributeValue>();
if (onlyMainEntityFields) {
// Should add all entity fields
for (FieldMetadata field : fieldsToAdd.values()) {
fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), field.getFieldName().getSymbolName()));
}
} else {
// --fields option has been completed and validated, so build annotation 'fields'
// param from selected fields
String[] fieldsFromCommand = StringUtils.split(fields, ",");
for (int i = 0; i < fieldsFromCommand.length; i++) {
fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), fieldsFromCommand[i]));
}
}
projectionAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("fields"), fieldNames));
// Check for each attribute individually
if (StringUtils.isNotBlank(formatExpression)) {
projectionAnnotation.addStringAttribute("formatExpression", formatExpression);
}
if (StringUtils.isNotBlank(formatMessage)) {
projectionAnnotation.addStringAttribute("formatMessage", formatMessage);
}
projectionBuilder.addAnnotation(projectionAnnotation);
// Build and save changes to disk
typeManagementService.createOrUpdateTypeOnDisk(projectionBuilder.build());
}
Aggregations