Search in sources :

Example 1 with FieldItem

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

the class AbstractFreeMarkerViewGenerationService method process.

protected DOC process(String templateName, ViewContext<T> ctx) {
    String content = "";
    Map<String, Object> input = null;
    try {
        Configuration cfg = new Configuration(new Version(2, 3, 23));
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        // Check if exists some template. If not, use classpath to locate the template
        if (checkTemplates(getTemplatesLocation(), templateName)) {
            // Process using FreeMarker and setDirectoryForTemplateLoading
            cfg.setDirectoryForTemplateLoading(new File(getTemplatesLocation()));
        } else {
            // Process using FreeMarker and setClassForTemplateLoading
            cfg.setClassForTemplateLoading(getResourceLoaderClass(), "templates");
        }
        // Prepare the template input:
        input = new HashMap<String, Object>();
        // Getting project information from ViewContext
        input.put("projectName", ctx.getProjectName());
        input.put("description", ctx.getDescription());
        input.put("version", ctx.getVersion());
        // Getting controller information from ViewContext
        input.put("controllerPath", ctx.getControllerPath());
        input.put("templatePath", StringUtils.removeStart(ctx.getControllerPath(), "/"));
        // Getting entity information from ViewContext
        input.put("entityName", ctx.getEntityName());
        if (ctx.getEntityName() != null) {
            input.put("entityLabel", FieldItem.buildLabel(ctx.getEntityName(), ""));
            input.put("entityLabelPlural", FieldItem.buildLabel(ctx.getEntityName(), "plural"));
            input.put("z", new FieldItem("", ctx.getEntityName()).getZ());
        }
        input.put("identifierField", ctx.getIdentifierField());
        input.put("modelAttribute", String.format("${%s}", ctx.getModelAttribute()));
        input.put("modelAttributeName", ctx.getModelAttributeName());
        // Getting security information from ViewContext
        input.put("isSecurityEnabled", ctx.isSecurityEnabled());
        // implementation wants to include its own information
        for (Entry<String, Object> extraInformation : ctx.getExtraInformation().entrySet()) {
            input.put(extraInformation.getKey(), extraInformation.getValue());
        }
        Template template = cfg.getTemplate(templateName.concat(".ftl"));
        StringBuilderWriter writer = new StringBuilderWriter();
        template.process(input, writer);
        writer.close();
        content = writer.toString();
        if (StringUtils.isBlank(content)) {
            throw new RuntimeException(String.format("ERROR: Error trying to generate final content from provided template '%s.ftl'", templateName));
        }
        return parse(content);
    } catch (Exception e) {
        throw new RuntimeException(String.format("ERROR: Error trying to generate final content from provided template '%s.ftl'. You should provide a valid .ftl file.\nContext:\n%s", templateName, input), e);
    }
}
Also used : StringBuilderWriter(org.apache.commons.io.output.StringBuilderWriter) Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) Version(freemarker.template.Version) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) File(java.io.File)

Example 2 with FieldItem

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

the class ThymeleafViewGeneratorServiceImpl method merge.

@Override
public Document merge(String templateName, Document loadExistingDoc, ViewContext<ThymeleafMetadata> ctx, List<FieldItem> fields) {
    for (FieldItem field : fields) {
        // Get field code if data-z attribute value is equals to
        // user-managed
        Element elementField = loadExistingDoc.getElementById(field.getFieldId());
        if (elementField != null && elementField.hasAttr("data-z") && elementField.attr("data-z").equals("user-managed")) {
            field.setUserManaged(true);
            field.setCodeManaged(elementField.outerHtml());
        }
    }
    ctx.addExtraParameter("userManagedComponents", mergeStructure(loadExistingDoc));
    ctx.addExtraParameter("fields", fields);
    Document newDoc = process(templateName, ctx);
    return newDoc;
}
Also used : Element(org.jsoup.nodes.Element) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) Document(org.jsoup.nodes.Document)

Example 3 with FieldItem

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

the class ThymeleafViewGeneratorServiceImpl method addFinderFormView.

@Override
public void addFinderFormView(String moduleName, JpaEntityMetadata entityMetadata, ThymeleafMetadata viewMetadata, JavaType formBean, String finderName, ViewContext<ThymeleafMetadata> ctx) {
    // Getting formBean details
    MemberDetails formBeanDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(formBean));
    // Getting entity fields that should be included on view
    List<FieldMetadata> formBeanFields = getPersistentFields(formBeanDetails.getFields());
    List<FieldItem> fields = getFieldViewItems(entityMetadata, formBeanFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Process elements to generate
    Document newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat(finderName).concat("Form").concat(getViewsExtension());
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    ctx.addExtraParameter("finderName", finderName.replace("findBy", "by"));
    ctx.addExtraParameter("entity", entityItem);
    ctx.addExtraParameter("fields", fields);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        Document existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeListView("finderForm", existingDoc, ctx, entityItem, fields, new ArrayList<List<DetailEntityItem>>());
        }
    } else {
        newDoc = process("finderForm", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) Document(org.jsoup.nodes.Document) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Example 4 with FieldItem

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

the class ThymeleafViewGeneratorServiceImpl method mergeDetailsCompositionView.

private Document mergeDetailsCompositionView(String templateName, Document loadExistingDoc, ViewContext ctx, EntityItem entityItem, DetailEntityItem detail, List<FieldItem> fields) {
    for (FieldItem field : fields) {
        // Get field code if data-z attribute value is equals to
        // user-managed
        Element elementField = loadExistingDoc.getElementById(field.getFieldId());
        if (elementField != null && elementField.hasAttr("data-z") && elementField.attr("data-z").equals("user-managed")) {
            field.setUserManaged(true);
            field.setCodeManaged(elementField.outerHtml());
        } else {
            field.setUserManaged(false);
            field.setCodeManaged("");
        }
    }
    ctx.addExtraParameter("fields", fields);
    ctx.addExtraParameter("userManagedComponents", mergeStructure(loadExistingDoc));
    return process(templateName, ctx);
}
Also used : Element(org.jsoup.nodes.Element) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem)

Example 5 with FieldItem

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

the class AbstractViewGenerationService method addCreateView.

@Override
public void addCreateView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entityDetails, ViewContext<T> ctx) {
    // Create void list for adding main entity fields
    List<FieldMetadata> entityFields = new ArrayList<FieldMetadata>();
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat("/create").concat(getViewsExtension());
    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 : getEditableFields(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);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = merge("create", existingDoc, ctx, fields);
        }
    } else {
        newDoc = process("create", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) 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

FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)16 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)13 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)11 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)11 ArrayList (java.util.ArrayList)8 List (java.util.List)7 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)7 JavaType (org.springframework.roo.model.JavaType)7 RooJavaType (org.springframework.roo.model.RooJavaType)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)6 JdkJavaType (org.springframework.roo.model.JdkJavaType)6 JpaJavaType (org.springframework.roo.model.JpaJavaType)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 SpringJavaType (org.springframework.roo.model.SpringJavaType)6 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)6 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)5 Document (org.jsoup.nodes.Document)3 Element (org.jsoup.nodes.Element)2 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1