Search in sources :

Example 21 with JavaPackage

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);
}
Also used : ControllerMetadata(org.springframework.roo.addon.web.mvc.controller.addon.ControllerMetadata) SearchAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.finder.SearchAnnotationValues) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) JavaPackage(org.springframework.roo.model.JavaPackage) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Example 22 with JavaPackage

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;
}
Also used : JavaPackage(org.springframework.roo.model.JavaPackage) Pom(org.springframework.roo.project.maven.Pom)

Example 23 with JavaPackage

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;
}
Also used : JavaType(org.springframework.roo.model.JavaType) JavaPackage(org.springframework.roo.model.JavaPackage)

Example 24 with JavaPackage

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);
}
Also used : JavaType(org.springframework.roo.model.JavaType) CustomData(org.springframework.roo.model.CustomData) TreeMap(java.util.TreeMap) JavaPackage(org.springframework.roo.model.JavaPackage) Test(org.junit.Test)

Aggregations

JavaPackage (org.springframework.roo.model.JavaPackage)24 JavaType (org.springframework.roo.model.JavaType)11 ArrayList (java.util.ArrayList)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)6 QualifiedNameExpr (com.github.antlrjavaparser.api.expr.QualifiedNameExpr)5 Pom (org.springframework.roo.project.maven.Pom)5 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)4 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)4 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)4 JdkJavaType (org.springframework.roo.model.JdkJavaType)4 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)3 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)3 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)3 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)3 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)3 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)2 ClassOrInterfaceDeclaration (com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration)2 EnumConstantDeclaration (com.github.antlrjavaparser.api.body.EnumConstantDeclaration)2 EnumDeclaration (com.github.antlrjavaparser.api.body.EnumDeclaration)2 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2