Search in sources :

Example 1 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class EntityDeserializerMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EntityDeserializerAnnotationValues values = new EntityDeserializerAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType deserializerType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    // Register JpaEntityMetadata dependency
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(entityId, metadataIdentificationString);
    // Register JavaBeanMetadata dependency
    final String javaBeanId = JavaBeanMetadata.createIdentifier(entityDetails);
    final JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanId);
    if (javaBeanMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(javaBeanId, metadataIdentificationString);
    // Register ServiceMetadata dependency
    ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
    String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
    ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
    if (serviceMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    return new EntityDeserializerMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, serviceMetadata);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) 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)

Example 2 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JSONMixinMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JSONMixinAnnotationValues values = new JSONMixinAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType mixinType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready for this metadata yet
        return null;
    }
    // register metadata dependency
    registerDependency(entityId, metadataIdentificationString);
    // Register dependency with DomainModelModule
    Set<ClassOrInterfaceTypeDetails> domainModelModuleDetails = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DOMAIN_MODEL_MODULE);
    if (!domainModelModuleDetails.isEmpty()) {
        String domainModelModuleMetadataId = DomainModelModuleMetadata.createIdentifier(domainModelModuleDetails.iterator().next());
        registerDependency(metadataIdentificationString, domainModelModuleMetadataId);
    }
    Map<FieldMetadata, JavaType> jsonDeserializerByEntity = new TreeMap<FieldMetadata, JavaType>(FieldMetadata.COMPARATOR_BY_NAME);
    for (FieldMetadata field : entityMetadata.getRelationsAsChild().values()) {
        if (isAnyToOneRelation(field)) {
            JavaType parentEntity = field.getFieldType().withoutParameters();
            JavaType entityDeserializer = getEntityDeserializerFor(parentEntity);
            Validate.notNull(entityDeserializer, "Can't locate class with @%s.entity=%s required for %s entity Json Mixin (%s)", RooJavaType.ROO_DESERIALIZER, parentEntity, entity.getFullyQualifiedTypeName(), mixinType.getFullyQualifiedTypeName());
            jsonDeserializerByEntity.put(field, entityDeserializer);
        }
    }
    return new JSONMixinMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, jsonDeserializerByEntity);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) TreeMap(java.util.TreeMap)

Example 3 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JpaOperationsImpl method getFieldChildPartOfCompositionRelation.

@Override
public Pair<FieldMetadata, RelationInfo> getFieldChildPartOfCompositionRelation(ClassOrInterfaceTypeDetails entityCdi) {
    JavaType domainType = entityCdi.getType();
    List<Pair<FieldMetadata, RelationInfo>> relations = getFieldChildPartOfRelation(entityCdi);
    if (relations.isEmpty()) {
        return null;
    }
    JpaEntityMetadata parent;
    JavaType parentType;
    RelationInfo info;
    List<Pair<FieldMetadata, RelationInfo>> compositionRelation = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
    for (Pair<FieldMetadata, RelationInfo> field : relations) {
        if (field.getRight().type == JpaRelationType.COMPOSITION) {
            compositionRelation.add(field);
        }
    }
    Validate.isTrue(compositionRelation.size() <= 1, "Entity %s has more than one relations of composition as child part: ", domainType, StringUtils.join(getFieldNamesOfRelationList(compositionRelation), ","));
    if (compositionRelation.isEmpty()) {
        return null;
    }
    return compositionRelation.get(0);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ArrayList(java.util.ArrayList) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class JaxbEntityMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting the annotated entity type
    JavaType annotatedEntity = governorPhysicalTypeMetadata.getType();
    // Getting the entity details
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(annotatedEntity);
    // Getting the plural
    String entityPlural = getPluralService().getPlural(entityDetails);
    // Getting JavaBean Metadata
    String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanMetadataKey);
    // Getting JpaEntity Metadata
    String jpaEntityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata jpaEntityMetadata = getMetadataService().get(jpaEntityMetadataKey);
    if (javaBeanMetadata == null) {
        // Not ready for this metadata
        return null;
    }
    // Getting the @OneToMany and @ManyToOne getters
    Map<String, String> entityNames = new HashMap<String, String>();
    List<MethodMetadata> oneToManyGetters = new ArrayList<MethodMetadata>();
    List<MethodMetadata> manyToOneGetters = new ArrayList<MethodMetadata>();
    for (FieldMetadata field : entityDetails.getDeclaredFields()) {
        // Getting getter for the oneToMany field
        MethodMetadata getter = javaBeanMetadata.getAccesorMethod(field);
        if (getter != null && (field.getAnnotation(JpaJavaType.ONE_TO_MANY) != null || field.getAnnotation(JpaJavaType.MANY_TO_MANY) != null)) {
            String getterTypeName = getter.getReturnType().getBaseType().getSimpleTypeName().toLowerCase();
            oneToManyGetters.add(getter);
            entityNames.put(getterTypeName, getPluralService().getPlural(getterTypeName));
        } else if (getter != null && field.getAnnotation(JpaJavaType.MANY_TO_ONE) != null) {
            String getterTypeName = getter.getReturnType().getSimpleTypeName().toLowerCase();
            manyToOneGetters.add(getter);
            entityNames.put(getterTypeName, getPluralService().getPlural(getterTypeName));
        }
    }
    // Getting the identifier accessor only if the annotated class doesn't have
    // parent
    MethodMetadata identifierAccessor = null;
    if (jpaEntityMetadata != null && jpaEntityMetadata.getParent() == null) {
        identifierAccessor = jpaEntityMetadata.getCurrentIdentifierAccessor();
    }
    return new JaxbEntityMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, getProjectOperations().getTopLevelPackage(""), annotatedEntity, entityPlural, identifierAccessor, oneToManyGetters, manyToOneGetters, entityNames);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 5 with JpaEntityMetadata

use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.

the class AbstractViewGenerationService method addMenu.

@Override
public void addMenu(String moduleName, ViewContext<T> ctx) {
    Map<String, MenuEntry> mapMenuEntries = new TreeMap<String, MenuEntry>();
    Set<ClassOrInterfaceTypeDetails> existingControllers = new HashSet<ClassOrInterfaceTypeDetails>();
    existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.COLLECTION, getType()));
    existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.SEARCH, getType()));
    Iterator<ClassOrInterfaceTypeDetails> it = existingControllers.iterator();
    while (it.hasNext()) {
        // Getting controller and its information
        ClassOrInterfaceTypeDetails controller = it.next();
        ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(controller);
        JavaType entity = controllerValues.getEntity();
        // Obtain the entityMetadata
        JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(getTypeLocationService().getTypeDetails(entity)));
        boolean isReadOnly = false;
        if (entityMetadata != null && entityMetadata.isReadOnly()) {
            isReadOnly = true;
        }
        // Get finders for each controller
        AnnotationMetadata controllerSearchAnnotation = controller.getAnnotation(RooJavaType.ROO_SEARCH);
        Map<String, String> finderNamesAndPaths = new HashMap<String, String>();
        if (controllerSearchAnnotation != null && controllerSearchAnnotation.getAttribute("finders") != null) {
            List<?> finders = (List<?>) controllerSearchAnnotation.getAttribute("finders").getValue();
            Iterator<?> iterator = finders.iterator();
            while (iterator.hasNext()) {
                StringAttributeValue attributeValue = (StringAttributeValue) iterator.next();
                String finderName = attributeValue.getValue();
                // Build URL path to get data
                String finderPath = "";
                if (StringUtils.startsWith(finderName, "count")) {
                    finderPath = StringUtils.removeStart(finderName, "count");
                } else if (StringUtils.startsWith(finderName, "find")) {
                    finderPath = StringUtils.removeStart(finderName, "find");
                } else if (StringUtils.startsWith(finderName, "query")) {
                    finderPath = StringUtils.removeStart(finderName, "query");
                } else if (StringUtils.startsWith(finderName, "read")) {
                    finderPath = StringUtils.removeStart(finderName, "read");
                } else {
                    finderPath = finderName;
                }
                finderPath = String.format("search/%s/search-form", StringUtils.uncapitalize(finderPath));
                finderNamesAndPaths.put(finderName, finderPath);
            }
        }
        // Getting pathPrefix
        String pathPrefix = StringUtils.defaultString(controllerValues.getPathPrefix(), "");
        // Generate path
        String path = getControllerOperations().getBaseUrlForController(controller);
        if (controllerSearchAnnotation != null) {
            // Prevent that /search will be included in every menu entry
            // The /search is already included in the step before only for
            // finder entries
            path = path.replace("/search", "");
        }
        // Create new menuEntry element for controller
        String keyThatRepresentsEntry = pathPrefix.concat(entity.getSimpleTypeName());
        // Add new menu entry to menuEntries list if doesn't exist
        MenuEntry menuEntry = null;
        if (controllerValues.getType() == ControllerType.SEARCH) {
            // Only add finder entry
            menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, false, false);
        } else {
            // Add default menu entries
            menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, true, isReadOnly);
        }
        if (mapMenuEntries.containsKey(keyThatRepresentsEntry)) {
            MenuEntry menuEntryInserted = mapMenuEntries.get(keyThatRepresentsEntry);
            if (menuEntryInserted.getFinderNamesAndPaths().isEmpty() && !menuEntry.getFinderNamesAndPaths().isEmpty()) {
                menuEntryInserted.setFinderNamesAndPaths(menuEntry.getFinderNamesAndPaths());
            }
            // Check the 'addDefaultEntries' attribute and add it if needed
            if (!menuEntryInserted.isAddDefaultEntries() && menuEntry.isAddDefaultEntries()) {
                menuEntryInserted.setAddDefaultEntries(menuEntry.isAddDefaultEntries());
            }
        } else {
            mapMenuEntries.put(keyThatRepresentsEntry, menuEntry);
        }
    }
    // Also, check web flow views in the views folder
    String viewsFolder = getViewsFolder(moduleName);
    List<String> webFlowViews = getWebFlowViewsFromDir(viewsFolder, null);
    // After obtain the webFlow views, add them to the menu
    for (String webFlowView : webFlowViews) {
        // Creating the menu entry
        MenuEntry menuEntry = createMenuEntry(webFlowView, webFlowView, "", FieldItem.buildLabel(webFlowView, ""), FieldItem.buildLabel(webFlowView, "plural"), null, true, false, false);
        mapMenuEntries.put(webFlowView, menuEntry);
    }
    // First of all, generate a list of MenuEntries based on existing
    // controllers
    List<MenuEntry> menuEntries = new ArrayList<MenuEntry>(mapMenuEntries.values());
    // Generate ids to search when merge new and existing doc
    List<String> requiredIds = new ArrayList<String>();
    for (MenuEntry entry : menuEntries) {
        requiredIds.add(entry.getPathPrefix().concat(entry.getEntityName()).concat("Entry"));
    }
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getFragmentsFolder(moduleName).concat("/menu").concat(getViewsExtension());
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeMenu("fragments/menu", existingDoc, ctx, menuEntries);
        }
    } else {
        ctx.addExtraParameter("menuEntries", menuEntries);
        newDoc = process("fragments/menu", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MenuEntry(org.springframework.roo.addon.web.mvc.views.components.MenuEntry) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) HashSet(java.util.HashSet)

Aggregations

JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)35 ArrayList (java.util.ArrayList)26 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)23 JavaType (org.springframework.roo.model.JavaType)22 RooJavaType (org.springframework.roo.model.RooJavaType)20 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)18 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)13 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)13 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)11 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)10 HashMap (java.util.HashMap)9 Pair (org.apache.commons.lang3.tuple.Pair)9 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 LinkedHashMap (java.util.LinkedHashMap)8 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)8 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)7 SpringJavaType (org.springframework.roo.model.SpringJavaType)6 List (java.util.List)5 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)5 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)5