Search in sources :

Example 11 with DetailEntityItem

use of org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem in project spring-roo by spring-projects.

the class AbstractViewGenerationService method addShowView.

@Override
public void addShowView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entityDetails, List<T> detailsControllers, ViewContext<T> ctx) {
    // Getting entity fields that should be included on view
    List<FieldMetadata> entityFields = new ArrayList<FieldMetadata>();
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    Map<String, List<FieldItem>> compositeRelationFields = manageChildcompositionFields(entityMetadata, entityDetails, ctx);
    // Remove one-to-one fields from composite relations and create EntityItems
    // for each referenced entity field
    Set<String> compositeRelationFieldNames = compositeRelationFields.keySet();
    for (FieldMetadata field : getPersistentFields(entityDetails.getFields())) {
        if (!compositeRelationFieldNames.contains(field.getFieldName().getSymbolName())) {
            entityFields.add(field);
        }
    }
    List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), false, ctx, FIELD_SUFFIX);
    ctx.addExtraParameter("fields", fields);
    ctx.addExtraParameter("entity", entityItem);
    ctx.addExtraParameter("compositeRelationFields", compositeRelationFields);
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat("/show").concat(getViewsExtension());
    // Including details if needed
    List<List<DetailEntityItem>> detailsLevels = new ArrayList<List<DetailEntityItem>>();
    if (detailsControllers != null && !detailsControllers.isEmpty()) {
        List<DetailEntityItem> details = new ArrayList<DetailEntityItem>();
        for (T detailController : detailsControllers) {
            DetailEntityItem detailItem = createDetailEntityItem(detailController, entityDetails, entityMetadata, ctx.getEntityName(), ctx, DETAIL_SUFFIX, entityItem);
            details.add(detailItem);
        }
        // Sort details by path
        Collections.sort(details, new Comparator<DetailEntityItem>() {

            @Override
            public int compare(DetailEntityItem o1, DetailEntityItem o2) {
                return o1.getPathString().compareTo(o2.getPathString());
            }
        });
        // levels
        for (DetailEntityItem detail : details) {
            // Create group until item level
            while (detailsLevels.size() < detail.getLevel()) {
                detailsLevels.add(new ArrayList<DetailEntityItem>());
            }
            // Include detail in its group
            detailsLevels.get(detail.getLevel() - 1).add(detail);
            if (detail.getLevel() < 1) {
                // Nothing more to do with detail
                continue;
            }
            // look for parent
            for (DetailEntityItem parent : details) {
                if (detail.isTheParentEntity(parent)) {
                    // set parent
                    detail.setParentEntity(parent);
                    break;
                }
            }
        }
    }
    ctx.addExtraParameter("detailsLevels", detailsLevels);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = merge("show", existingDoc, ctx, fields);
        }
    } else {
        newDoc = process("show", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem) List(java.util.List) ArrayList(java.util.ArrayList) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Aggregations

DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)11 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)8 ArrayList (java.util.ArrayList)5 Document (org.jsoup.nodes.Document)5 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)5 List (java.util.List)4 FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)4 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)4 JavaType (org.springframework.roo.model.JavaType)4 RooJavaType (org.springframework.roo.model.RooJavaType)4 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)2 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 JdkJavaType (org.springframework.roo.model.JdkJavaType)2 JpaJavaType (org.springframework.roo.model.JpaJavaType)2 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)2 SpringJavaType (org.springframework.roo.model.SpringJavaType)2 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)2 Element (org.jsoup.nodes.Element)1 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)1 ControllerMetadata (org.springframework.roo.addon.web.mvc.controller.addon.ControllerMetadata)1