use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadataProviderImpl method getLocalMidToRequest.
@Override
protected String getLocalMidToRequest(final ItdTypeDetails itdTypeDetails) {
// Determine the governor for this ITD, and whether any DOD metadata is
// even hoping to hear about changes to that JavaType and its ITDs
final JavaType governor = itdTypeDetails.getName();
for (final JavaType type : itdTypeDetails.getGovernor().getLayerEntities()) {
final String localMidType = entityToDodMidMap.get(type);
if (localMidType != null) {
return localMidType;
}
}
final String localMid = entityToDodMidMap.get(governor);
if (localMid == null) {
// No DOD is interested in this JavaType, so let's move on
return null;
}
// match our requirements
for (final MethodMetadata method : itdTypeDetails.getDeclaredMethods()) {
if (BeanInfoUtils.isMutatorMethod(method)) {
// a direct dependency on it for the future.
return localMid;
}
}
return null;
}
use of org.springframework.roo.classpath.details.MethodMetadata 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.MethodMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadataProviderImpl method getLocatedFields.
private Map<FieldMetadata, JpaEntityFactoryMetadata> getLocatedFields(final MemberDetails memberDetails, final String dodMetadataId) {
final Map<FieldMetadata, JpaEntityFactoryMetadata> locatedFields = new LinkedHashMap<FieldMetadata, JpaEntityFactoryMetadata>();
final Iterable<ClassOrInterfaceTypeDetails> entityFactoryTypes = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_JPA_ENTITY_FACTORY);
final List<MethodMetadata> mutatorMethods = memberDetails.getMethods();
// To avoid unnecessary rewriting of the DoD ITD we sort the mutators by
// method name to provide a consistent ordering
Collections.sort(mutatorMethods, new NaturalOrderComparator<MethodMetadata>() {
@Override
protected String stringify(final MethodMetadata object) {
return object.getMethodName().getSymbolName();
}
});
for (final MethodMetadata method : mutatorMethods) {
if (!BeanInfoUtils.isMutatorMethod(method)) {
continue;
}
final FieldMetadata field = BeanInfoUtils.getFieldForJavaBeanMethod(memberDetails, method);
if (field == null) {
continue;
}
// Track any changes to the mutator method (eg it goes away)
getMetadataDependencyRegistry().registerDependency(method.getDeclaredByMetadataId(), dodMetadataId);
final Set<Object> fieldCustomDataKeys = field.getCustomData().keySet();
// types
if (fieldCustomDataKeys.contains(IDENTIFIER_FIELD) || fieldCustomDataKeys.contains(EMBEDDED_ID_FIELD) || fieldCustomDataKeys.contains(EMBEDDED_FIELD) || fieldCustomDataKeys.contains(VERSION_FIELD)) {
continue;
}
// Never include persistence transient fields
if (fieldCustomDataKeys.contains(TRANSIENT_FIELD)) {
continue;
}
// entities by hand
if (field.getFieldType().isCommonCollectionType() || fieldCustomDataKeys.contains(ONE_TO_MANY_FIELD) || fieldCustomDataKeys.contains(MANY_TO_MANY_FIELD)) {
continue;
}
// Look up collaborating metadata
final JpaEntityFactoryMetadata collaboratingMetadata = locateCollaboratingMetadata(dodMetadataId, field, entityFactoryTypes);
locatedFields.put(field, collaboratingMetadata);
}
return locatedFields;
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class IdentifierMetadata method getAccessors.
/**
* Locates the accessor methods.
* <p>
* If {@link #getFieldBuilders()} returns fields created by this ITD, public
* accessors will automatically be produced in the declaring class.
*
* @param fields
* @return the accessors (never returns null)
*/
private List<MethodMetadataBuilder> getAccessors(final List<FieldMetadataBuilder> fields) {
final List<MethodMetadataBuilder> accessors = new ArrayList<MethodMetadataBuilder>();
// Compute the names of the accessors that will be produced
for (final FieldMetadataBuilder field : fields) {
final JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(field.getFieldName(), field.getFieldType());
final MethodMetadata accessor = getGovernorMethod(requiredAccessorName);
if (accessor == null) {
accessors.add(getAccessorMethod(field.getFieldName(), field.getFieldType()));
} else {
Validate.isTrue(Modifier.isPublic(accessor.getModifier()), "User provided field but failed to provide a public '%s()' method in '%s'", requiredAccessorName.getSymbolName(), destination.getFullyQualifiedTypeName());
accessors.add(new MethodMetadataBuilder(accessor));
}
}
return accessors;
}
use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.
the class JspViewManager method getUpdateDocument.
public Document getUpdateDocument() {
final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
final Document document = builder.newDocument();
// Add document namespaces
final Element div = (Element) document.appendChild(new XmlElementBuilder("div", document).addAttribute("xmlns:form", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:field", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("version", "2.0").addChild(new XmlElementBuilder("jsp:directive.page", document).addAttribute("contentType", "text/html;charset=UTF-8").build()).addChild(new XmlElementBuilder("jsp:output", document).addAttribute("omit-xml-declaration", "yes").build()).build());
// Add form update element
final Element formUpdate = new XmlElementBuilder("form:update", document).addAttribute("id", XmlUtils.convertId("fu:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).build();
if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
formUpdate.setAttribute("path", controllerPath);
}
if (!"id".equals(formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName())) {
formUpdate.setAttribute("idField", formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName());
}
final MethodMetadata versionAccessorMethod = formBackingTypePersistenceMetadata.getVersionAccessorMethod();
if (versionAccessorMethod == null) {
formUpdate.setAttribute("versionField", "none");
} else {
final String methodName = versionAccessorMethod.getMethodName().getSymbolName();
formUpdate.setAttribute("versionField", methodName.substring("get".length()));
}
// Filter out embedded ID fields as they represent the composite PK
// which is not to be updated.
final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
for (int i = 0; i < fieldCopy.size(); i++) {
// Make sure form fields are not presented twice.
if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
fieldCopy.remove(i);
}
}
}
createFieldsForCreateAndUpdate(fieldCopy, document, formUpdate, false);
formUpdate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formUpdate));
div.appendChild(formUpdate);
return document;
}
Aggregations