use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JSONMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JSONAnnotationValues annotationValues = new JSONAnnotationValues(governorPhysicalTypeMetadata);
ClassOrInterfaceTypeDetails controllerDetail = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
// Getting controller metadata
final String controllerMetadataKey = ControllerMetadata.createIdentifier(controllerDetail);
final ControllerMetadata controllerMetadata = getMetadataService().get(controllerMetadataKey);
// This metadata is not available yet.
if (controllerMetadata == null) {
return null;
}
// Getting entity and check if is a readOnly entity or not
final JavaType entity = controllerMetadata.getEntity();
JavaType itemController = null;
if (controllerMetadata.getType() != ControllerType.ITEM) {
// Locate ItemController
Collection<ClassOrInterfaceTypeDetails> itemControllers = getControllerLocator().getControllers(entity, ControllerType.ITEM, RooJavaType.ROO_JSON);
if (itemControllers.isEmpty()) {
// We can't create metadata "Jet"
return null;
} else {
// Get controller with the same package
JavaPackage controllerPackage = controllerDetail.getType().getPackage();
for (ClassOrInterfaceTypeDetails controller : itemControllers) {
if (controllerPackage.equals(controller.getType().getPackage())) {
itemController = controller.getType();
break;
}
}
Validate.notNull(itemController, "ERROR: Can't find ITEM-type controller related to controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
}
}
Validate.notNull(entity, "ERROR: You should provide a valid entity for controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
Validate.notNull(entityDetails, "ERROR: Can't load details of %s", entity.getFullyQualifiedTypeName());
final JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(entityDetails));
Validate.notNull(entityMetadata, "ERROR: Can't get Jpa Entity metada of %s", entity.getFullyQualifiedTypeName());
// Get entity plural
final String entityPlural = getPluralService().getPlural(entity);
final String entityIdentifierPlural = getPluralService().getPlural(entityMetadata.getCurrentIndentifierField().getFieldName());
// Getting service and its metadata
final JavaType service = controllerMetadata.getService();
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(service);
final String serviceMetadataKey = ServiceMetadata.createIdentifier(serviceDetails);
registerDependency(serviceMetadataKey, metadataIdentificationString);
final ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataKey);
// Prepare information about ONE-TO-ONE relations
final List<Pair<RelationInfo, JpaEntityMetadata>> compositionRelationOneToOne = new ArrayList<Pair<RelationInfo, JpaEntityMetadata>>();
ClassOrInterfaceTypeDetails childEntityDetails;
JpaEntityMetadata childEntityMetadata;
for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
if (info.cardinality == Cardinality.ONE_TO_ONE && info.type == JpaRelationType.COMPOSITION) {
childEntityDetails = getTypeLocationService().getTypeDetails(info.childType);
childEntityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(childEntityDetails));
compositionRelationOneToOne.add(Pair.of(info, childEntityMetadata));
}
}
Map<String, MethodMetadata> findersToAdd = new HashMap<String, MethodMetadata>();
// Getting annotated finders
final SearchAnnotationValues searchAnnotationValues = new SearchAnnotationValues(governorPhysicalTypeMetadata);
// Add finders only if controller is of search type
if (controllerMetadata.getType() == ControllerType.SEARCH && searchAnnotationValues != null && searchAnnotationValues.getFinders() != null) {
List<String> finders = new ArrayList<String>(Arrays.asList(searchAnnotationValues.getFinders()));
// Search indicated finders in its related service
for (MethodMetadata serviceFinder : serviceMetadata.getFinders()) {
String finderName = serviceFinder.getMethodName().getSymbolName();
if (finders.contains(finderName)) {
findersToAdd.put(finderName, serviceFinder);
// Add dependencies between modules
List<JavaType> types = new ArrayList<JavaType>();
types.add(serviceFinder.getReturnType());
types.addAll(serviceFinder.getReturnType().getParameters());
for (AnnotatedJavaType parameter : serviceFinder.getParameterTypes()) {
types.add(parameter.getJavaType());
types.addAll(parameter.getJavaType().getParameters());
}
for (JavaType parameter : types) {
getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
}
finders.remove(finderName);
}
}
// Check all finders have its service method
if (!finders.isEmpty()) {
throw new IllegalArgumentException(String.format("ERROR: Service %s does not have these finder methods: %s ", service.getFullyQualifiedTypeName(), StringUtils.join(finders, ", ")));
}
}
return new JSONMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, controllerMetadata, serviceMetadata, entityMetadata, entityPlural, entityIdentifierPlural, compositionRelationOneToOne, itemController, findersToAdd);
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JavaPackageConverter method convertFromText.
public JavaPackage convertFromText(String value, final Class<?> requiredType, final String optionContext) {
if (StringUtils.isBlank(value)) {
return null;
}
Pom module = projectOperations.getFocusedModule();
if (value.contains(MODULE_PATH_SEPARATOR)) {
final String moduleName = value.substring(0, value.indexOf(MODULE_PATH_SEPARATOR));
module = projectOperations.getPomFromModuleName(moduleName);
value = value.substring(value.indexOf(MODULE_PATH_SEPARATOR) + 1, value.length()).trim();
if (StringUtils.contains(optionContext, UPDATE)) {
projectOperations.setModule(module);
}
}
String moduleName = module == null ? null : module.getModuleName();
JavaPackage result = new JavaPackage(convertToFullyQualifiedPackageName(module, value), moduleName);
if (optionContext != null && (optionContext.contains(UPDATE) || optionContext.contains(UPDATELAST))) {
lastUsed.setPackage(result, module);
}
return result;
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JavaTypeConverter method locateExisting.
private String locateExisting(final String value, String topLevelPath) {
String newValue = value;
if (value.startsWith(TOP_LEVEL_PACKAGE_SYMBOL)) {
boolean found = false;
while (!found) {
if (value.length() > 1) {
newValue = (value.charAt(1) == '.' ? topLevelPath : topLevelPath + ".") + value.substring(1);
} else {
newValue = topLevelPath + ".";
}
final String physicalTypeIdentifier = typeLocationService.getPhysicalTypeIdentifier(new JavaType(newValue));
if (physicalTypeIdentifier != null) {
topLevelPath = typeLocationService.getTopLevelPackageForModule(projectOperations.getPomFromModuleName(PhysicalTypeIdentifier.getPath(physicalTypeIdentifier).getModule()));
found = true;
} else {
final int index = topLevelPath.lastIndexOf('.');
if (index == -1) {
break;
}
topLevelPath = topLevelPath.substring(0, topLevelPath.lastIndexOf('.'));
}
}
if (!found) {
return null;
}
}
lastUsed.setTopLevelPackage(new JavaPackage(topLevelPath));
return newValue;
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class DefaultItdTypeDetailsTest method testMinimalInstance.
@Test
public void testMinimalInstance() {
// Set up
final boolean privilegedAspect = false;
final int modifier = 42;
final ClassOrInterfaceTypeDetails mockGovernor = mock(ClassOrInterfaceTypeDetails.class);
when(mockGovernor.getPhysicalTypeCategory()).thenReturn(PhysicalTypeCategory.CLASS);
final JavaPackage mockPackage = mock(JavaPackage.class);
when(mockPackage.getFullyQualifiedPackageName()).thenReturn("com.foo.bar");
final JavaType mockGovernorType = mock(JavaType.class);
when(mockGovernorType.getPackage()).thenReturn(mockPackage);
when(mockGovernor.getType()).thenReturn(mockGovernorType);
when(mockGovernorType.getSimpleTypeName()).thenReturn("Person");
when(mockGovernorType.getFullyQualifiedTypeName()).thenReturn("com.foo.bar.Person");
when(mockGovernorType.getDataType()).thenReturn(DataType.TYPE);
final CustomData mockCustomData = mock(CustomData.class);
final JavaType mockAspectType = mock(JavaType.class);
when(mockAspectType.getPackage()).thenReturn(mockPackage);
when(mockAspectType.isDefaultPackage()).thenReturn(false);
when(mockAspectType.getSimpleTypeName()).thenReturn("Person_Roo_Extra");
SortedMap<JavaType, Boolean> imports = new TreeMap<JavaType, Boolean>();
final JavaType mockImportType = mock(JavaType.class);
when(mockImportType.getSimpleTypeName()).thenReturn("Person");
when(mockImportType.getFullyQualifiedTypeName()).thenReturn("com.foo.bar.Person");
when(mockImportType.getDataType()).thenReturn(DataType.TYPE);
imports.put(mockImportType, false);
final String declaredByMetadataId = "MID:foo#bar";
// Invoke
final DefaultItdTypeDetails itd = new DefaultItdTypeDetails(mockCustomData, declaredByMetadataId, modifier, mockGovernor, mockAspectType, privilegedAspect, imports, null, null, null, null, null, null, null, null, null, null);
// Check
assertEquals(0, itd.getAnnotations().size());
assertEquals(0, itd.getDeclaredConstructors().size());
assertEquals(0, itd.getDeclaredFields().size());
assertEquals(0, itd.getDeclaredInitializers().size());
assertEquals(0, itd.getDeclaredInnerTypes().size());
assertEquals(0, itd.getDeclaredMethods().size());
assertEquals(0, itd.getExtendsTypes().size());
assertEquals(0, itd.getFieldAnnotations().size());
assertEquals(0, itd.getImplementsTypes().size());
assertEquals(0, itd.getInnerTypes().size());
assertEquals(0, itd.getDeclarePrecedence().size());
assertEquals(0, itd.getMethodAnnotations().size());
assertEquals(1, itd.getRegisteredImports().size());
assertEquals(mockAspectType, itd.getAspect());
assertEquals(mockCustomData, itd.getCustomData());
assertEquals(declaredByMetadataId, itd.getDeclaredByMetadataId());
assertEquals(modifier, itd.getModifier());
assertEquals(mockGovernorType, itd.getType());
assertEquals(DefaultItdTypeDetails.PHYSICAL_TYPE_CATEGORY, itd.getPhysicalTypeCategory());
assertEquals(privilegedAspect, itd.isPrivilegedAspect());
assertEquals(mockGovernor, itd.getGovernor());
assertFalse(itd.extendsType(mock(JavaType.class)));
assertOutput(MINIMAL_ITD, itd);
}
Aggregations