use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ServiceImplMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final ServiceImplAnnotationValues annotationValues = new ServiceImplAnnotationValues(governorPhysicalTypeMetadata);
// Getting service interface
JavaType serviceInterface = annotationValues.getService();
ClassOrInterfaceTypeDetails serviceInterfaceDetails = getTypeLocationService().getTypeDetails(serviceInterface);
final ServiceMetadata serviceMetadata = getServiceMetadata(metadataIdentificationString, serviceInterfaceDetails);
AnnotationMetadata serviceAnnotation = serviceInterfaceDetails.getAnnotation(RooJavaType.ROO_SERVICE);
Validate.notNull(serviceAnnotation, "ERROR: Provided service should be annotated with @RooService");
JavaType entity = (JavaType) serviceAnnotation.getAttribute("entity").getValue();
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails.getType(), PhysicalTypeIdentifier.getPath(entityDetails.getDeclaredByMetadataId()));
final JpaEntityMetadata entityMetadata = (JpaEntityMetadata) getMetadataService().get(entityMetadataId);
// Add dependencies between modules
if (serviceMetadata != null) {
for (MethodMetadata method : serviceMetadata.getAllMethods()) {
List<JavaType> types = new ArrayList<JavaType>();
types.add(method.getReturnType());
types.addAll(method.getReturnType().getParameters());
for (AnnotatedJavaType parameter : method.getParameterTypes()) {
types.add(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter));
types.addAll(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter).getParameters());
}
for (JavaType parameter : types) {
getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
}
}
}
// Getting associated repository
ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
final String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails.getType(), PhysicalTypeIdentifier.getPath(repositoryDetails.getDeclaredByMetadataId()));
final RepositoryJpaMetadata repositoryMetadata = (RepositoryJpaMetadata) getMetadataService().get(repositoryMetadataId);
// Locate related services API types required by relations
Map<JavaType, ServiceMetadata> requiredServicesByEntity = new HashMap<JavaType, ServiceMetadata>();
ClassOrInterfaceTypeDetails relatedService;
for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
if (info.cardinality != Cardinality.ONE_TO_ONE && !requiredServicesByEntity.containsKey(info.childType)) {
relatedService = getServiceLocator().getFirstService(info.childType);
if (relatedService != null) {
requiredServicesByEntity.put(info.childType, getServiceMetadata(metadataIdentificationString, relatedService));
}
}
}
// Get child relations info
List<Pair<FieldMetadata, RelationInfo>> childRelationsInfo = getJpaOperations().getFieldChildPartOfRelation(entityDetails);
return new ServiceImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, serviceInterface, repositoryDetails.getType(), repositoryMetadata, entity, entityMetadata, serviceMetadata, requiredServicesByEntity, childRelationsInfo);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class RepositoryJpaMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final RepositoryJpaAnnotationValues annotationValues = new RepositoryJpaAnnotationValues(governorPhysicalTypeMetadata);
final JavaType domainType = annotationValues.getEntity();
// XXX clear entitiesDetails by security
entitiesDetails.clear();
// Remember that this entity JavaType matches up with this metadata
// identification string
// Start by clearing any previous association
final JavaType oldEntity = repositoryMidToDomainTypeMap.get(metadataIdentificationString);
if (oldEntity != null) {
domainTypeToRepositoryMidMap.remove(oldEntity);
}
domainTypeToRepositoryMidMap.put(domainType, metadataIdentificationString);
repositoryMidToDomainTypeMap.put(metadataIdentificationString, domainType);
// Getting associated entity
JavaType entity = annotationValues.getEntity();
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
MemberDetails entityMemberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), entityDetails);
final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
final JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataId);
if (entityMetadata == null) {
return null;
}
// Getting java bean metadata
final String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
// Register dependency between repositoryMetadata and jpaEntityMetadata
registerDependency(entityMetadataId, metadataIdentificationString);
// Create dependency between repository and java bean annotation
registerDependency(javaBeanMetadataKey, metadataIdentificationString);
// Check if related entity is setted as readOnly
JavaType readOnlyRepository = null;
if (entityMetadata.isReadOnly()) {
// Getting ReadOnlyRepository interface annotated with
// @RooReadOnlyRepository
Set<ClassOrInterfaceTypeDetails> readOnlyRepositories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_READ_ONLY_REPOSITORY);
Validate.notEmpty(readOnlyRepositories, "ERROR: You should define a ReadOnlyRepository interface annotated with @RooReadOnlyRepository to be able to generate repositories of readOnly entities.");
Iterator<ClassOrInterfaceTypeDetails> it = readOnlyRepositories.iterator();
while (it.hasNext()) {
ClassOrInterfaceTypeDetails readOnlyRepositoryDetails = it.next();
readOnlyRepository = readOnlyRepositoryDetails.getType();
break;
}
}
List<JavaType> repositoryCustomList = new ArrayList<JavaType>();
// Getting RepositoryCustom interface annotated with
// @RooJpaRepositoryCustom
Set<ClassOrInterfaceTypeDetails> customRepositories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_REPOSITORY_JPA_CUSTOM);
Iterator<ClassOrInterfaceTypeDetails> customRepositoriesIt = customRepositories.iterator();
while (customRepositoriesIt.hasNext()) {
ClassOrInterfaceTypeDetails customRepository = customRepositoriesIt.next();
AnnotationMetadata annotation = customRepository.getAnnotation(ROO_REPOSITORY_JPA_CUSTOM);
if (annotation.getAttribute("entity").getValue().equals(entity)) {
repositoryCustomList.add(customRepository.getType());
}
}
Validate.notEmpty(repositoryCustomList, "Can't find any interface annotated with @%s(entity=%s)", ROO_REPOSITORY_JPA_CUSTOM.getSimpleTypeName(), entity.getSimpleTypeName());
Validate.isTrue(repositoryCustomList.size() == 1, "More than one interface annotated with @%s(entity=%s): %s", ROO_REPOSITORY_JPA_CUSTOM.getSimpleTypeName(), entity.getSimpleTypeName(), StringUtils.join(repositoryCustomList, ","));
JavaType defaultReturnType;
final JavaType annotationDefaultReturnType = annotationValues.getDefaultReturnType();
// Get and check defaultReturnType
if (annotationDefaultReturnType == null || annotationDefaultReturnType.equals(JavaType.CLASS) || annotationDefaultReturnType.equals(entity)) {
defaultReturnType = entity;
} else {
// Validate that defaultReturnValue is projection of current entity
ClassOrInterfaceTypeDetails returnTypeCid = getTypeLocationService().getTypeDetails(annotationDefaultReturnType);
AnnotationMetadata projectionAnnotation = returnTypeCid.getAnnotation(RooJavaType.ROO_ENTITY_PROJECTION);
Validate.notNull(projectionAnnotation, "ERROR: %s defined on %s.@%s.defaultReturnType must be annotated with @%s annotation", annotationDefaultReturnType, governorPhysicalTypeMetadata.getType(), RooJavaType.ROO_REPOSITORY_JPA, RooJavaType.ROO_ENTITY_PROJECTION);
Validate.isTrue(entity.equals(projectionAnnotation.getAttribute("entity").getValue()), "ERROR: %s defined on %s.@%s.defaultReturnType must be annotated with @%s annotation and match the 'entity' attribute value", annotationDefaultReturnType, governorPhysicalTypeMetadata.getType(), RooJavaType.ROO_REPOSITORY_JPA, RooJavaType.ROO_ENTITY_PROJECTION);
defaultReturnType = annotationDefaultReturnType;
}
// Get field which entity is field part
List<Pair<FieldMetadata, RelationInfo>> relationsAsChild = getJpaOperations().getFieldChildPartOfRelation(entityDetails);
// Get Annotation
ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
AnnotationMetadata repositoryAnnotation = cid.getAnnotation(ROO_REPOSITORY_JPA);
// Create list of finder to add
List<FinderMethod> findersToAdd = new ArrayList<FinderMethod>();
// Create list of finder to add in RespositoryCustom
List<Pair<FinderMethod, PartTree>> findersToAddInCustom = new ArrayList<Pair<FinderMethod, PartTree>>();
Map<JavaType, ClassOrInterfaceTypeDetails> detailsCache = new HashMap<JavaType, ClassOrInterfaceTypeDetails>();
List<String> declaredFinderNames = new ArrayList<String>();
RooFinder[] findersAnnValue = annotationValues.getFinders();
// Get finders attributes
AnnotationAttributeValue<?> currentFinders = repositoryAnnotation.getAttribute("finders");
if (currentFinders != null) {
List<?> values = (List<?>) currentFinders.getValue();
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue finderAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
if (finderAnnotation.getValue() != null && finderAnnotation.getValue().getAttribute("value") != null) {
// Get finder name
String finderName = null;
if (finderAnnotation.getValue().getAttribute("value").getValue() instanceof String) {
finderName = (String) finderAnnotation.getValue().getAttribute("value").getValue();
}
Validate.notNull(finderName, "'finder' attribute in @RooFinder must be a String");
declaredFinderNames.add(finderName);
// Get finder return type
JavaType returnType = getNestedAttributeValue(finderAnnotation, "returnType");
JavaType finderReturnType;
if (returnType == null || JavaType.CLASS.equals(returnType)) {
finderReturnType = defaultReturnType;
returnType = null;
} else {
finderReturnType = returnType;
}
// Get finder return type
JavaType formBean = getNestedAttributeValue(finderAnnotation, "formBean");
boolean isDeclaredFormBean = false;
if (JavaType.CLASS.equals(formBean)) {
formBean = null;
}
// Create FinderMethods
PartTree finder = new PartTree(finderName, entityMemberDetails, this, finderReturnType);
Validate.notNull(finder, String.format("ERROR: '%s' is not a valid finder. Use autocomplete feature (TAB or CTRL + Space) to include finder that follows Spring Data nomenclature.", finderName));
FinderMethod finderMethod = new FinderMethod(finder);
// Add dependencies between modules
List<JavaType> types = new ArrayList<JavaType>();
types.add(finder.getReturnType());
types.addAll(finder.getReturnType().getParameters());
for (FinderParameter parameter : finder.getParameters()) {
types.add(parameter.getType());
types.addAll(parameter.getType().getParameters());
}
for (JavaType parameter : types) {
getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
}
if (formBean == null && (returnType == null || entity.equals(returnType))) {
// Add to finder methods list
findersToAdd.add(finderMethod);
} else {
// RepositoryCustom classes.
if (returnType != null && !returnType.equals(entity)) {
ClassOrInterfaceTypeDetails returnTypeDetails = getDetailsFor(returnType, detailsCache);
Validate.isTrue(returnTypeDetails != null && returnTypeDetails.getAnnotation(RooJavaType.ROO_ENTITY_PROJECTION) != null, "ERROR: finder '%s' declared 'returnType' (%s) is not annotated with @%s annotation", finderName, returnType.getSimpleTypeName(), RooJavaType.ROO_ENTITY_PROJECTION.getSimpleTypeName());
}
Validate.notNull(formBean, "ERROR: finder '%s' requires 'formBean' value if 'defaultReturnType' is defined", finderName);
ClassOrInterfaceTypeDetails formBeanDetails = getTypeLocationService().getTypeDetails(formBean);
Validate.isTrue(formBeanDetails != null && formBeanDetails.getAnnotation(RooJavaType.ROO_DTO) != null, "ERROR: finder '%s' declared 'formBean' (%s) is not annotated with @%s annotation", finderName, formBean.getSimpleTypeName(), RooJavaType.ROO_ENTITY_PROJECTION.getSimpleTypeName());
checkDtoFieldsForFinder(formBeanDetails, finder, governorPhysicalTypeMetadata.getType());
if (returnType == null) {
returnType = entity;
}
finderMethod = new FinderMethod(returnType, new JavaSymbolName(finderName), Arrays.asList(new FinderParameter(formBean, new JavaSymbolName(StringUtils.uncapitalize(formBean.getSimpleTypeName())))));
findersToAddInCustom.add(Pair.of(finderMethod, finder));
}
}
}
}
return new RepositoryJpaMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, entityMetadata, readOnlyRepository, repositoryCustomList.get(0), defaultReturnType, relationsAsChild, findersToAdd, findersToAddInCustom, declaredFinderNames);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class RepositoryJpaMetadataProviderImpl method getNestedAttributeValue.
/**
* Gets attributo of a nested annotation attribute value
*
* @param newstedAnnotationAttr
* @param attributeName
* @return
*/
private <T> T getNestedAttributeValue(NestedAnnotationAttributeValue newstedAnnotationAttr, String attributeName) {
AnnotationMetadata annotationValue = newstedAnnotationAttr.getValue();
if (annotationValue == null) {
return null;
}
AnnotationAttributeValue<?> attribute = annotationValue.getAttribute(attributeName);
if (attribute == null) {
return null;
}
return (T) attribute.getValue();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class FinderCommands method isFormBeanMandatory.
@CliOptionMandatoryIndicator(command = "finder add", params = { "formBean" })
public boolean isFormBeanMandatory(ShellContext shellContext) {
// Get current value of 'entity'
JavaType entity = getTypeFromEntityParam(shellContext);
if (entity == null) {
return false;
}
// Get current value of returnType
String returnType = shellContext.getParameters().get("returnType");
// This parameter is not mandatory if returnType has not been specified
if (StringUtils.isBlank(returnType)) {
return false;
}
// If returnType has been specified, but it's an entity, this parameter is not
// mandatory
JavaTypeConverter converter = (JavaTypeConverter) getJavaTypeConverter().get(0);
JavaType type = converter.convertFromText(returnType, JavaType.class, PROJECT);
if (type == null) {
return false;
}
ClassOrInterfaceTypeDetails details = getTypeLocationService().getTypeDetails(type);
if (details == null) {
return false;
}
AnnotationMetadata entityAnnotation = details.getAnnotation(RooJavaType.ROO_JPA_ENTITY);
if (entityAnnotation != null) {
return false;
}
return true;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class SecurityAuthorizationsMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting the annotated entity type
JavaType annotatedService = governorPhysicalTypeMetadata.getType();
// Getting the service details
MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
Map<MethodMetadata, String> preAuthorizationMethods = new LinkedHashMap<MethodMetadata, String>();
// Get methods defined in each annotation @RooSecurityAuthorization
AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
List<?> values = (List<?>) attributeAuthorizations.getValue();
if (values != null && !values.isEmpty()) {
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
// Get attributes from the annotation @RooSecurityAuthorization
String methodName = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
List<?> methodParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
for (MethodMetadata method : methods) {
// check the parameters to get corresponding method
if (checkParameters(method, methodParameters)) {
String roles = null;
if (authorizationAnnotation.getValue().getAttribute("roles") != null) {
roles = (String) authorizationAnnotation.getValue().getAttribute("roles").getValue();
}
String usernames = null;
if (authorizationAnnotation.getValue().getAttribute("usernames") != null) {
usernames = (String) authorizationAnnotation.getValue().getAttribute("usernames").getValue();
}
// Create the content based on defined roles and
// usernames to include it in the security annotation
String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
preAuthorizationMethods.put(method, secAnnotationValue);
break;
}
}
}
}
return new SecurityAuthorizationsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, preAuthorizationMethods);
}
Aggregations