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);
}
}
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;
}
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);
}
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);
}
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);
}
Aggregations