use of org.springframework.roo.addon.web.mvc.views.components.FieldItem in project spring-roo by spring-projects.
the class AbstractViewGenerationService method getRelationFieldItems.
/**
* This method obtains all necessary information about fields from each
* one-to-one referenced entity.
*
* @param fieldList the List with main entity fields.
* @param checkMaxFields whether field number is restricted.
* @param ctx the context with all the necessary information for view
* generation.
* @param suffixId the suffix for items.
* @param compositeReferencedFields the entity fields with one-to-one and
* composite relation
* @param parentEntity the parent entity type
* @return the Map where the key is the current entity field and the value is
* the referenced entity editable field list.
*/
private Map<String, List<FieldItem>> getRelationFieldItems(List<FieldMetadata> fieldList, boolean checkMaxFields, ViewContext<T> ctx, String suffixId, List<String> compositeReferencedFields, JpaEntityMetadata parentEntity) {
// Create the Map to return
Map<String, List<FieldItem>> relationFieldsMap = new HashMap<String, List<FieldItem>>();
for (FieldMetadata entityField : fieldList) {
// of parent field
if (compositeReferencedFields.contains(entityField.getFieldName().getSymbolName())) {
// Create the list for related entity fields
List<FieldItem> referencedEntityFieldItems = new ArrayList<FieldItem>();
// Get referenced entity fields
List<FieldMetadata> referencedEntityFields = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(entityField.getFieldType())).getFields();
List<FieldMetadata> referencedEntityEditableFields = getEditableFields(referencedEntityFields);
// Add referenced entity field view items
for (FieldMetadata referencedEntityField : referencedEntityEditableFields) {
// If references the parent entity, don't add it
if (referencedEntityField.getFieldType().equals(parentEntity.getAnnotatedEntity())) {
continue;
}
FieldItem fieldItem = createFieldItem(parentEntity, referencedEntityField, entityField.getFieldType().getSimpleTypeName(), suffixId, ctx, entityField.getFieldName().getSymbolName());
if (fieldItem != null) {
referencedEntityFieldItems.add(fieldItem);
}
if (referencedEntityFieldItems.size() >= MAX_FIELDS_TO_ADD && checkMaxFields) {
break;
}
}
// Add entity to referenced entities map
if (!relationFieldsMap.containsKey(entityField.getFieldName().getSymbolName())) {
relationFieldsMap.put(entityField.getFieldName().getSymbolName(), referencedEntityFieldItems);
}
}
}
return relationFieldsMap;
}
use of org.springframework.roo.addon.web.mvc.views.components.FieldItem in project spring-roo by spring-projects.
the class AbstractViewGenerationService method addListDeleteModalView.
@Override
public void addListDeleteModalView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, ViewContext<T> ctx) {
// Get the repository related with the entity to check the default return type
RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(entityMetadata.getAnnotatedEntity());
// All views should have a repository
Validate.notNull(repository, "ERROR: The provided entity should have an associated repository to be able " + "to generate the list view.");
// Obtain the defaultReturnType
JavaType defaultReturnType = repository.getDefaultReturnType();
// The defaultReturnType must not be null. If it's not an entity projection,
// it must be an entity
Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
// Obtain details of the provided defaultReturnType. If not exists as type, show an error
ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
// all the entity fields will be used
if (defaultReturnTypeFields.isEmpty()) {
defaultReturnTypeFields = entity.getFields();
}
// Getting entity fields that should be included on view
List<FieldMetadata> entityFields = getPersistentFields(defaultReturnTypeFields);
List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
// Process elements to generate
DOC newDoc = null;
// Getting new viewName
String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat("/listDeleteModal").concat(getViewsExtension());
EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
// Check if new view to generate exists or not
if (existsFile(viewName)) {
DOC existingDoc = loadExistingDoc(viewName);
if (!isUserManagedDocument(existingDoc)) {
newDoc = mergeListDeleteModalView("listDeleteModal", existingDoc, ctx, entityItem, fields);
}
} else {
ctx.addExtraParameter("entity", entityItem);
ctx.addExtraParameter("fields", fields);
newDoc = process("listDeleteModal", 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 AbstractViewGenerationService method addListDeleteModalDetailView.
@Override
public void addListDeleteModalDetailView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, ControllerMetadata controllerMetadata, ViewContext<T> ctx) {
// Get the repository related with the entity to check the default return type
RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(entityMetadata.getAnnotatedEntity());
// All views should have a repository
Validate.notNull(repository, "ERROR: The provided entity should have an associated repository to be able " + "to generate the list view.");
// Obtain the defaultReturnType
JavaType defaultReturnType = repository.getDefaultReturnType();
// The defaultReturnType must not be null. If it's not an entity projection,
// it must be an entity
Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
// Obtain details of the provided defaultReturnType. If not exists as type, show an error
ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
// all the entity fields will be used
if (defaultReturnTypeFields.isEmpty()) {
defaultReturnTypeFields = entity.getFields();
}
// Getting entity fields that should be included on view
List<FieldMetadata> entityFields = getPersistentFields(defaultReturnTypeFields);
List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
// Process elements to generate
DOC newDoc = null;
// Getting new viewName
String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat(controllerMetadata.getDetailsPathAsString("/")).concat("/listDeleteModal").concat(getViewsExtension());
EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
// Check if new view to generate exists or not
if (existsFile(viewName)) {
DOC existingDoc = loadExistingDoc(viewName);
if (!isUserManagedDocument(existingDoc)) {
newDoc = mergeListDeleteModalDetailView("listDeleteModalDetail", existingDoc, ctx, entityItem, fields);
}
} else {
ctx.addExtraParameter("entity", entityItem);
ctx.addExtraParameter("fields", fields);
newDoc = process("listDeleteModalDetail", 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 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);
}
use of org.springframework.roo.addon.web.mvc.views.components.FieldItem in project spring-roo by spring-projects.
the class AbstractViewGenerationService method addShowInlineView.
@Override
public void addShowInlineView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entityDetails, 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("/showInline").concat(getViewsExtension());
// Check if new view to generate exists or not
if (existsFile(viewName)) {
DOC existingDoc = loadExistingDoc(viewName);
if (!isUserManagedDocument(existingDoc)) {
newDoc = merge("showInline", existingDoc, ctx, fields);
}
} else {
newDoc = process("showInline", ctx);
}
// Write newDoc on disk
writeDoc(newDoc, viewName);
}
Aggregations