use of org.broadleafcommerce.openadmin.dto.MapMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method removeSubCollectionEntity.
@Override
public PersistenceResponse removeSubCollectionEntity(ClassMetadata mainMetadata, Property field, Entity parentEntity, String itemId, String alternateId, String priorKey, List<SectionCrumb> sectionCrumbs) throws ServiceException {
List<Property> properties = new ArrayList<Property>();
Property p;
String parentId = getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName());
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(field.getMetadata(), sectionCrumbs).withEntity(new Entity());
if (field.getMetadata() instanceof BasicCollectionMetadata) {
BasicCollectionMetadata fmd = (BasicCollectionMetadata) field.getMetadata();
p = new Property();
p.setName("id");
p.setValue(itemId);
properties.add(p);
p = new Property();
p.setName(ppr.getForeignKey().getManyToField());
p.setValue(parentId);
properties.add(p);
ppr.getEntity().setType(new String[] { fmd.getCollectionCeilingEntity() });
} else if (field.getMetadata() instanceof AdornedTargetCollectionMetadata) {
AdornedTargetList adornedList = ppr.getAdornedList();
p = new Property();
p.setName(adornedList.getLinkedObjectPath() + "." + adornedList.getLinkedIdProperty());
p.setValue(parentId);
properties.add(p);
p = new Property();
p.setName(adornedList.getTargetObjectPath() + "." + adornedList.getTargetIdProperty());
p.setValue(itemId);
properties.add(p);
if (!StringUtils.isEmpty(alternateId)) {
p = new Property();
p.setName(BasicPersistenceModule.ALTERNATE_ID_PROPERTY);
p.setValue(alternateId);
properties.add(p);
}
ppr.getEntity().setType(new String[] { adornedList.getAdornedTargetEntityClassname() });
} else if (field.getMetadata() instanceof MapMetadata) {
MapMetadata fmd = (MapMetadata) field.getMetadata();
p = new Property();
p.setName("id");
p.setValue(itemId);
properties.add(p);
p = new Property();
p.setName("symbolicId");
p.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
properties.add(p);
p = new Property();
p.setName("priorKey");
p.setValue(priorKey);
properties.add(p);
MapStructure mapStructure = ppr.getMapStructure();
p = new Property();
p.setName(mapStructure.getKeyPropertyName());
p.setValue(itemId);
properties.add(p);
ppr.getEntity().setType(new String[] { fmd.getTargetClass() });
}
ppr.setCeilingEntityClassname(ppr.getEntity().getType()[0]);
String sectionField = field.getName();
if (sectionField.contains(".")) {
sectionField = sectionField.substring(0, sectionField.lastIndexOf("."));
}
ppr.setSectionEntityField(sectionField);
Property parentNameProp = parentEntity.getPMap().get(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
if (parentNameProp != null) {
ppr.setRequestingEntityName(parentNameProp.getValue());
}
Property[] propArr = new Property[properties.size()];
properties.toArray(propArr);
ppr.getEntity().setProperties(propArr);
return remove(ppr);
}
use of org.broadleafcommerce.openadmin.dto.MapMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method addSubCollectionEntity.
@Override
public PersistenceResponse addSubCollectionEntity(EntityForm entityForm, ClassMetadata mainMetadata, Property field, Entity parentEntity, List<SectionCrumb> sectionCrumbs) throws ServiceException, ClassNotFoundException {
// Assemble the properties from the entity form
List<Property> properties = getPropertiesFromEntityForm(entityForm);
FieldMetadata md = field.getMetadata();
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withEntity(new Entity());
ppr.getEntity().setIsPreAdd(parentEntity.isPreAdd());
if (md instanceof BasicCollectionMetadata) {
BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
ppr.getEntity().setType(new String[] { entityForm.getEntityType() });
// that we're not changing the target entity at all and only creating the association to the id
if (fmd.getAddMethodType().equals(AddMethodType.LOOKUP) || fmd.getAddMethodType().equals(AddMethodType.LOOKUP_FOR_UPDATE)) {
List<String> fieldsToRemove = new ArrayList<String>();
String idProp = getIdProperty(mainMetadata);
for (String key : entityForm.getFields().keySet()) {
if (!idProp.equals(key)) {
fieldsToRemove.add(key);
}
}
for (String key : fieldsToRemove) {
ListIterator<Property> li = properties.listIterator();
while (li.hasNext()) {
if (li.next().getName().equals(key)) {
li.remove();
}
}
}
ppr.setValidateUnsubmittedProperties(false);
}
if (fmd.getAddMethodType().equals(AddMethodType.LOOKUP_FOR_UPDATE)) {
ppr.setUpdateLookupType(true);
}
Property fp = new Property();
fp.setName(ppr.getForeignKey().getManyToField());
fp.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
properties.add(fp);
} else if (md instanceof AdornedTargetCollectionMetadata) {
ppr.getEntity().setType(new String[] { ppr.getAdornedList().getAdornedTargetEntityClassname() });
String[] maintainedFields = ((AdornedTargetCollectionMetadata) md).getMaintainedAdornedTargetFields();
if (maintainedFields == null || maintainedFields.length == 0) {
ppr.setValidateUnsubmittedProperties(false);
}
} else if (md instanceof MapMetadata) {
ppr.getEntity().setType(new String[] { entityForm.getEntityType() });
Property p = new Property();
p.setName("symbolicId");
p.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
properties.add(p);
} else {
throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was" + " not a collection field.", field.getName(), mainMetadata.getCeilingType()));
}
ppr.setCeilingEntityClassname(ppr.getEntity().getType()[0]);
String sectionField = field.getName();
if (sectionField.contains(".")) {
sectionField = sectionField.substring(0, sectionField.lastIndexOf("."));
}
ppr.setSectionEntityField(sectionField);
Property parentNameProp = parentEntity.getPMap().get(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
if (parentNameProp != null) {
ppr.setRequestingEntityName(parentNameProp.getValue());
}
Property[] propArr = new Property[properties.size()];
properties.toArray(propArr);
ppr.getEntity().setProperties(propArr);
return add(ppr);
}
use of org.broadleafcommerce.openadmin.dto.MapMetadata in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method buildCollectionListGrid.
@Override
public ListGrid buildCollectionListGrid(String containingEntityId, DynamicResultSet drs, Property field, String sectionKey, List<SectionCrumb> sectionCrumbs) throws ServiceException {
FieldMetadata fmd = field.getMetadata();
// Get the class metadata for this particular field
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(fmd, sectionCrumbs);
if (field != null) {
ppr.setSectionEntityField(field.getName());
}
ClassMetadata cmd = adminEntityService.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
List<Field> headerFields = new ArrayList<>();
ListGrid.Type type = null;
boolean editable = false;
boolean sortable = false;
boolean readOnly = false;
boolean hideIdColumn = false;
boolean canFilterAndSort = true;
boolean modalSingleSelectable = false;
boolean modalMultiSelectable = false;
boolean selectize = false;
boolean isMedia = false;
boolean isLookup = false;
String sortProperty = null;
FieldWrapper wrapper = new FieldWrapper();
ArrayList<FieldDTO> defaultWrapperFields = new ArrayList<>();
String idProperty = "id";
for (Property property : cmd.getProperties()) {
if (property.getMetadata() instanceof BasicFieldMetadata && SupportedFieldType.ID == ((BasicFieldMetadata) property.getMetadata()).getFieldType() && // make sure it's a property for this entity - not an association
!property.getName().contains(".")) {
idProperty = property.getName();
break;
}
}
// kind of field this is.
if (fmd instanceof BasicFieldMetadata) {
readOnly = ((BasicFieldMetadata) fmd).getReadOnly();
modalSingleSelectable = true;
for (Property p : cmd.getProperties()) {
if (p.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
if (SupportedFieldType.ID.equals(md.getFieldType())) {
idProperty = md.getName();
}
if (md.isProminent() != null && md.isProminent() && !ArrayUtils.contains(getGridHiddenVisibilities(), md.getVisibility())) {
Field hf = createHeaderField(p, md);
headerFields.add(hf);
defaultWrapperFields.add(constructFieldDTOFromFieldData(hf, md));
}
if (md.getIsFilter() != null && md.getIsFilter()) {
Field f = createHeaderField(p, md);
wrapper.getFields().add(constructFieldDTOFromFieldData(f, md));
}
}
}
type = ListGrid.Type.TO_ONE;
} else if (fmd instanceof BasicCollectionMetadata) {
BasicCollectionMetadata bcm = (BasicCollectionMetadata) fmd;
readOnly = !bcm.isMutable();
if (AddMethodType.LOOKUP.equals(bcm.getAddMethodType())) {
isLookup = true;
}
if (AddMethodType.SELECTIZE_LOOKUP.equals(bcm.getAddMethodType())) {
Property p = cmd.getPMap().get(bcm.getSelectizeVisibleField());
if (p != null) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
Field hf = createHeaderField(p, md);
headerFields.add(hf);
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, md));
}
} else {
for (Property p : cmd.getProperties()) {
if (p.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
if (md.isProminent() != null && md.isProminent() && !ArrayUtils.contains(getGridHiddenVisibilities(), md.getVisibility())) {
Field hf = createHeaderField(p, md);
headerFields.add(hf);
defaultWrapperFields.add(constructFieldDTOFromFieldData(hf, md));
}
if (md.getIsFilter() != null && md.getIsFilter()) {
Field f = createHeaderField(p, md);
wrapper.getFields().add(constructFieldDTOFromFieldData(f, md));
}
}
}
}
type = ListGrid.Type.BASIC;
if (AddMethodType.PERSIST.equals(bcm.getAddMethodType()) || AddMethodType.PERSIST_EMPTY.equals(bcm.getAddMethodType())) {
editable = true;
} else if (AddMethodType.SELECTIZE_LOOKUP.equals(bcm.getAddMethodType())) {
selectize = true;
modalSingleSelectable = true;
} else {
modalSingleSelectable = true;
}
sortable = StringUtils.isNotBlank(bcm.getSortProperty());
if (sortable) {
sortProperty = bcm.getSortProperty();
}
} else if (fmd instanceof AdornedTargetCollectionMetadata) {
modalSingleSelectable = true;
readOnly = !((AdornedTargetCollectionMetadata) fmd).isMutable();
AdornedTargetCollectionMetadata atcmd = (AdornedTargetCollectionMetadata) fmd;
if (AdornedTargetAddMethodType.LOOKUP.equals(atcmd.getAdornedTargetAddMethodType())) {
isLookup = true;
}
if (AdornedTargetAddMethodType.SELECTIZE_LOOKUP.equals(atcmd.getAdornedTargetAddMethodType())) {
selectize = true;
Property p = cmd.getPMap().get(atcmd.getSelectizeVisibleField());
if (p != null) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
Field hf = createHeaderField(p, md);
headerFields.add(hf);
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, md));
}
} else {
for (String fieldName : atcmd.getGridVisibleFields()) {
Property p = cmd.getPMap().get(fieldName);
if (p != null) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
Field hf = createHeaderField(p, md);
headerFields.add(hf);
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, md));
}
}
}
type = ListGrid.Type.ADORNED;
if (atcmd.getMaintainedAdornedTargetFields().length > 0) {
editable = true;
}
AdornedTargetList adornedList = (AdornedTargetList) atcmd.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
sortable = StringUtils.isNotBlank(adornedList.getSortField());
if (sortable) {
sortProperty = adornedList.getSortField();
}
} else if (fmd instanceof MapMetadata) {
readOnly = !((MapMetadata) fmd).isMutable();
MapMetadata mmd = (MapMetadata) fmd;
Property p2 = cmd.getPMap().get("key");
BasicFieldMetadata keyMd = (BasicFieldMetadata) p2.getMetadata();
keyMd.setFriendlyName(getMapKeyFriendlyName(p2));
Field hf = createHeaderField(p2, keyMd);
headerFields.add(hf);
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, keyMd));
if (mmd.isSimpleValue()) {
Property valueProperty = cmd.getPMap().get("value");
BasicFieldMetadata valueMd = (BasicFieldMetadata) valueProperty.getMetadata();
valueMd.setFriendlyName("Value");
hf = createHeaderField(valueProperty, valueMd);
headerFields.add(hf);
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, valueMd));
idProperty = "key";
hideIdColumn = true;
} else {
for (Property p : cmd.getProperties()) {
if (p.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata md = (BasicFieldMetadata) p.getMetadata();
String valueClassName = mmd.getValueClassName();
if (!StringUtils.isEmpty(mmd.getToOneTargetProperty())) {
Class<?> clazz;
try {
clazz = Class.forName(mmd.getValueClassName());
} catch (ClassNotFoundException e) {
throw ExceptionHelper.refineException(e);
}
java.lang.reflect.Field nestedField = FieldManager.getSingleField(clazz, mmd.getToOneTargetProperty());
ManyToOne manyToOne = nestedField.getAnnotation(ManyToOne.class);
if (manyToOne != null && !manyToOne.targetEntity().getName().equals(void.class.getName())) {
valueClassName = manyToOne.targetEntity().getName();
} else {
OneToOne oneToOne = nestedField.getAnnotation(OneToOne.class);
if (oneToOne != null && !oneToOne.targetEntity().getName().equals(void.class.getName())) {
valueClassName = oneToOne.targetEntity().getName();
}
}
}
if (md.getTargetClass().equals(valueClassName)) {
if (md.isProminent() != null && md.isProminent() && !ArrayUtils.contains(getGridHiddenVisibilities(), md.getVisibility())) {
hf = createHeaderField(p, md);
headerFields.add(hf);
defaultWrapperFields.add(constructFieldDTOFromFieldData(hf, md));
// Is this a media listgrid
if (hf.getFieldType().equals("ASSET_LOOKUP")) {
isMedia = true;
}
}
if (md.getIsFilter() != null && md.getIsFilter()) {
wrapper.getFields().add(constructFieldDTOFromFieldData(hf, md));
}
}
}
}
}
type = ListGrid.Type.MAP;
editable = true;
canFilterAndSort = false;
}
String ceilingType = "";
if (fmd instanceof BasicFieldMetadata) {
ceilingType = cmd.getCeilingType();
} else if (fmd instanceof CollectionMetadata) {
ceilingType = ((CollectionMetadata) fmd).getCollectionCeilingEntity();
}
if (CollectionUtils.isEmpty(headerFields)) {
String message = "There are no listgrid header fields configured for the class " + ceilingType + " and property '" + StringUtil.sanitize(field.getName()) + "'.";
if (selectize && (type == ListGrid.Type.ADORNED || type == ListGrid.Type.ADORNED_WITH_FORM)) {
message += " Please configure 'selectizeVisibleField' in your @AdminPresentationAdornedTargetCollection configuration";
} else if (type == ListGrid.Type.ADORNED || type == ListGrid.Type.ADORNED_WITH_FORM) {
message += " Please configure 'gridVisibleFields' in your @AdminPresentationAdornedTargetCollection configuration";
} else if (selectize && type == ListGrid.Type.BASIC) {
message += " Please configure 'selectizeVisibleField' in your @AdminPresentationCollection configuration";
} else {
message += " Please mark some @AdminPresentation fields with 'prominent = true'";
}
LOG.error(message);
}
ListGrid listGrid = createListGrid(ceilingType, headerFields, type, drs, sectionKey, fmd.getOrder(), idProperty, sectionCrumbs, sortProperty);
listGrid.setSubCollectionFieldName(field.getName());
listGrid.setFriendlyName(field.getMetadata().getFriendlyName());
if (StringUtils.isEmpty(listGrid.getFriendlyName())) {
listGrid.setFriendlyName(field.getName());
}
listGrid.setContainingEntityId(containingEntityId);
listGrid.setIsReadOnly(readOnly);
listGrid.setHideIdColumn(hideIdColumn);
listGrid.setCanFilterAndSort(canFilterAndSort);
// Set up the filter builder params
Date c = new Date();
String friendlyName = field.getMetadata().getFriendlyName();
String jsonFriendlyName = friendlyName.replaceAll(" ", "_");
listGrid.setJsonFieldName(jsonFriendlyName + c.getTime() + "Json");
listGrid.setFriendlyName(friendlyName);
listGrid.setFieldBuilder("RULE_SIMPLE");
if (CollectionUtils.isEmpty(wrapper.getFields())) {
wrapper.setFields(defaultWrapperFields);
}
listGrid.setFieldWrapper(wrapper);
String blankJsonString = "{\"data\":[]}";
listGrid.setJson(blankJsonString);
DataWrapper dw = convertJsonToDataWrapper(blankJsonString);
if (dw != null) {
listGrid.setDataWrapper(dw);
}
if (editable) {
listGrid.getRowActions().add(DefaultListGridActions.UPDATE);
}
if (readOnly) {
listGrid.getRowActions().add(DefaultListGridActions.VIEW);
}
if (sortable) {
listGrid.setCanFilterAndSort(false);
listGrid.setIsSortable(true);
}
if (modalSingleSelectable) {
if (readOnly) {
listGrid.addModalRowAction(DefaultListGridActions.SINGLE_SELECT.clone().withForListGridReadOnly(true));
} else {
listGrid.addModalRowAction(DefaultListGridActions.SINGLE_SELECT);
}
}
listGrid.setSelectType(ListGrid.SelectType.SINGLE_SELECT);
if (selectize) {
listGrid.setSelectizeUrl(buildSelectizeUrl(listGrid));
listGrid.setSelectType(ListGrid.SelectType.SELECTIZE);
}
if (modalMultiSelectable) {
listGrid.addModalRowAction(DefaultListGridActions.MULTI_SELECT);
listGrid.setSelectType(ListGrid.SelectType.MULTI_SELECT);
}
listGrid.getRowActions().add(DefaultListGridActions.REMOVE);
if (fmd.getManualFetch()) {
listGrid.setManualFetch(true);
listGrid.getToolbarActions().add(DefaultListGridActions.MANUAL_FETCH);
}
if (isMedia) {
listGrid.setListGridType(ListGrid.Type.ASSET_GRID);
}
extensionManager.getProxy().modifyListGrid(listGrid.getClassName(), listGrid);
// If someone has replaced RowLevelSecurityService, check here to make sure the replacement implements the expected interface
if (rowLevelSecurityService instanceof ExceptionAwareRowLevelSecurityProvider) {
EntityFormModifierConfiguration entityFormModifierConfiguration = ((ExceptionAwareRowLevelSecurityProvider) rowLevelSecurityService).getUpdateDenialExceptions();
for (EntityFormModifierData<EntityFormModifierDataPoint> data : entityFormModifierConfiguration.getData()) {
for (EntityFormModifier modifier : entityFormModifierConfiguration.getModifier()) {
if (modifier.isQualified(data.getModifierType())) {
modifier.modifyListGrid(new EntityFormModifierRequest().withListGrid(listGrid).withConfiguration(data).withCurrentUser(adminRemoteSecurityService.getPersistentAdminUser()).withRowLevelSecurityService(rowLevelSecurityService));
}
}
}
}
return listGrid;
}
use of org.broadleafcommerce.openadmin.dto.MapMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method buildAddCollectionItemModel.
/**
* Builds out all of the model information needed for showing the add modal for collection items on both the initial GET
* as well as after a POST with validation errors
*
* @param request
* @param model
* @param id
* @param collectionField
* @param sectionKey
* @param collectionProperty
* @param md
* @param ppr
* @return the appropriate view to display for the modal
* @see {@link #addCollectionItem(HttpServletRequest, HttpServletResponse, Model, Map, String, String, EntityForm, BindingResult)}
* @see {@link #showAddCollectionItem(HttpServletRequest, HttpServletResponse, Model, Map, String, String, MultiValueMap)}
* @throws ServiceException
*/
protected String buildAddCollectionItemModel(HttpServletRequest request, HttpServletResponse response, Model model, String id, String collectionField, String sectionKey, Property collectionProperty, FieldMetadata md, PersistencePackageRequest ppr, EntityForm entityForm, Entity entity) throws ServiceException {
// For requests to add a new collection item include the main class that the subsequent request comes from.
// For instance, with basic collections we know the main associated class for a fetch through the ForeignKey
// persistence item but map and adorned target lookups make a standard persistence request. This solution
// fixes all cases.
String mainClassName = getClassNameForSection(sectionKey);
ppr.addCustomCriteria("owningClass=" + mainClassName);
ppr.setAddOperationInspect(true);
if (entityForm != null) {
entityForm.clearFieldsMap();
}
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
if (md instanceof BasicCollectionMetadata) {
BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
// and sometimes show a list grid to allow the user to associate an existing record.
if (fmd.getAddMethodType().equals(AddMethodType.PERSIST)) {
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
if (entityForm == null) {
entityForm = formService.createEntityForm(collectionMetadata, sectionCrumbs);
entityForm.setCeilingEntityClassname(ppr.getCeilingEntityClassname());
entityForm.setEntityType(ppr.getCeilingEntityClassname());
} else {
formService.populateEntityForm(collectionMetadata, entityForm, sectionCrumbs);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
formService.removeNonApplicableFields(collectionMetadata, entityForm, ppr.getCeilingEntityClassname());
entityForm.getTabs().iterator().next().getIsVisible();
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/simpleAddEntity");
} else {
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ListGrid listGrid = formService.buildCollectionListGrid(id, drs, collectionProperty, sectionKey, sectionCrumbs);
listGrid.setPathOverride(request.getRequestURL().toString());
if (AddMethodType.LOOKUP.equals(fmd.getAddMethodType()) || AddMethodType.SELECTIZE_LOOKUP.equals(fmd.getAddMethodType())) {
listGrid.removeAllRowActions();
}
model.addAttribute("listGrid", listGrid);
model.addAttribute("viewType", "modal/simpleSelectEntity");
}
} else if (md instanceof AdornedTargetCollectionMetadata) {
AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
// Even though this field represents an adorned target collection, the list we want to show in the modal
// is the standard list grid for the target entity of this field
ppr.setOperationTypesOverride(null);
ppr.setType(PersistencePackageRequest.Type.STANDARD);
ppr.setSectionEntityField(collectionField);
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ListGrid listGrid = formService.buildCollectionListGrid(id, drs, collectionProperty, sectionKey, sectionCrumbs);
listGrid.setSubCollectionFieldName(collectionField);
listGrid.setPathOverride(request.getRequestURL().toString());
listGrid.setFriendlyName(collectionMetadata.getPolymorphicEntities().getFriendlyName());
if (entityForm == null) {
entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, sectionCrumbs, true);
entityForm.setCeilingEntityClassname(ppr.getAdornedList().getAdornedTargetEntityClassname());
} else {
formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, entityForm, sectionCrumbs, true);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
listGrid.setListGridType(ListGrid.Type.ADORNED);
for (Entry<String, Field> entry : entityForm.getFields().entrySet()) {
if (entry.getValue().getIsVisible()) {
listGrid.setListGridType(ListGrid.Type.ADORNED_WITH_FORM);
break;
}
}
// This is part of an add, so we want to be able to filter/sort the listgrid
listGrid.setIsSortable(false);
listGrid.setCanFilterAndSort(true);
listGrid.removeAllRowActions();
model.addAttribute("listGrid", listGrid);
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/adornedSelectEntity");
} else if (md instanceof MapMetadata) {
MapMetadata fmd = (MapMetadata) md;
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
if (entityForm == null) {
entityForm = formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id);
} else {
formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id, entityForm);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/mapAddEntity");
}
// Set the parent id on the entity form
if (entityForm != null) {
entityForm.setParentId(id);
}
model.addAttribute("currentUrl", request.getRequestURL().toString());
model.addAttribute("modalHeaderType", ModalHeaderType.ADD_COLLECTION_ITEM.getType());
model.addAttribute("collectionProperty", collectionProperty);
setModelAttributes(model, sectionKey);
return "modules/modalContainer";
}
use of org.broadleafcommerce.openadmin.dto.MapMetadata in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldMetadataProvider method buildMapMetadata.
protected void buildMapMetadata(Class<?> parentClass, Class<?> targetClass, Map<String, FieldMetadata> attributes, FieldInfo field, FieldMetadataOverride map, DynamicEntityDao dynamicEntityDao, String prefix) {
MapMetadata serverMetadata = (MapMetadata) attributes.get(field.getName());
Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
MapMetadata metadata;
if (serverMetadata != null) {
metadata = serverMetadata;
} else {
metadata = new MapMetadata();
}
if (map.getReadOnly() != null) {
metadata.setMutable(!map.getReadOnly());
}
if (map.getShowIfProperty() != null) {
metadata.setShowIfProperty(map.getShowIfProperty());
}
if (map.getShowIfFieldEquals() != null) {
metadata.setShowIfFieldEquals(map.getShowIfFieldEquals());
}
metadata.setPrefix(prefix);
metadata.setTargetClass(targetClass.getName());
metadata.setFieldName(field.getName());
org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.broadleafcommerce.openadmin.dto.OperationTypes(OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP);
if (map.getAddType() != null) {
dtoOperationTypes.setAddType(map.getAddType());
}
if (map.getRemoveType() != null) {
dtoOperationTypes.setRemoveType(map.getRemoveType());
}
if (map.getFetchType() != null) {
dtoOperationTypes.setFetchType(map.getFetchType());
}
if (map.getInspectType() != null) {
dtoOperationTypes.setInspectType(map.getInspectType());
}
if (map.getUpdateType() != null) {
dtoOperationTypes.setUpdateType(map.getUpdateType());
}
// don't allow additional non-persistent properties or additional foreign keys for an advanced collection datasource - they don't make sense in this context
PersistencePerspective persistencePerspective;
if (serverMetadata != null) {
persistencePerspective = metadata.getPersistencePerspective();
persistencePerspective.setOperationTypes(dtoOperationTypes);
} else {
persistencePerspective = new PersistencePerspective(dtoOperationTypes, new String[] {}, new ForeignKey[] {});
metadata.setPersistencePerspective(persistencePerspective);
}
String parentObjectClass = resolvedClass.getName();
Map idMetadata;
if (parentClass != null) {
idMetadata = dynamicEntityDao.getIdMetadata(parentClass);
} else {
idMetadata = dynamicEntityDao.getIdMetadata(targetClass);
}
String parentObjectIdField = (String) idMetadata.get("name");
String keyClassName = null;
if (serverMetadata != null) {
keyClassName = ((MapStructure) metadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyClassName();
}
if (map.getKeyClass() != null && !void.class.getName().equals(map.getKeyClass())) {
keyClassName = map.getKeyClass();
}
if (keyClassName == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[0];
if (!ArrayUtils.isEmpty(dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz))) {
throw new IllegalArgumentException("Key class for AdminPresentationMap was determined to be a JPA managed type. Only primitive types for the key type are currently supported.");
}
keyClassName = clazz.getName();
}
}
if (keyClassName == null) {
keyClassName = String.class.getName();
}
String keyPropertyName = "key";
String mapKeyValueProperty = "";
if (StringUtils.isNotBlank(field.getMapKey())) {
mapKeyValueProperty = field.getMapKey();
}
if (StringUtils.isNotBlank(map.getMapKeyValueProperty())) {
mapKeyValueProperty = map.getMapKeyValueProperty();
}
String keyPropertyFriendlyName = null;
if (serverMetadata != null) {
keyPropertyFriendlyName = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyPropertyFriendlyName();
}
if (map.getKeyPropertyFriendlyName() != null) {
keyPropertyFriendlyName = map.getKeyPropertyFriendlyName();
}
Boolean deleteEntityUponRemove = null;
if (serverMetadata != null) {
deleteEntityUponRemove = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getDeleteValueEntity();
}
if (map.isDeleteEntityUponRemove() != null) {
deleteEntityUponRemove = map.isDeleteEntityUponRemove();
}
String valuePropertyName = "value";
String valuePropertyFriendlyName = null;
if (serverMetadata != null) {
MapStructure structure = (MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
if (structure instanceof SimpleValueMapStructure) {
valuePropertyFriendlyName = ((SimpleValueMapStructure) structure).getValuePropertyFriendlyName();
} else {
valuePropertyFriendlyName = "";
}
}
if (map.getValuePropertyFriendlyName() != null) {
valuePropertyFriendlyName = map.getValuePropertyFriendlyName();
}
if (map.getMediaField() != null) {
metadata.setMediaField(map.getMediaField());
}
if (map.getToOneTargetProperty() != null) {
metadata.setToOneTargetProperty(map.getToOneTargetProperty());
}
if (map.getToOneParentProperty() != null) {
metadata.setToOneParentProperty((map.getToOneParentProperty()));
}
if (map.getValueClass() != null && !void.class.getName().equals(map.getValueClass())) {
metadata.setValueClassName(map.getValueClass());
}
if (metadata.getValueClassName() == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
if (!ArrayUtils.isEmpty(entities)) {
metadata.setValueClassName(entities[entities.length - 1].getName());
}
}
}
if (metadata.getValueClassName() == null) {
if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
metadata.setValueClassName(field.getManyToManyTargetEntity());
}
}
if (metadata.getValueClassName() == null) {
metadata.setValueClassName(String.class.getName());
}
Boolean simpleValue = null;
if (map.getSimpleValue() != null && map.getSimpleValue() != UnspecifiedBooleanType.UNSPECIFIED) {
simpleValue = map.getSimpleValue() == UnspecifiedBooleanType.TRUE;
}
if (simpleValue == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
simpleValue = ArrayUtils.isEmpty(entities);
}
}
if (simpleValue == null) {
// ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
simpleValue = false;
}
}
if (simpleValue == null) {
throw new IllegalArgumentException("Unable to infer if the value for the map is of a complex or simple type based on any parameterized type or ManyToMany annotation. Please explicitly set the isSimpleValue property.");
}
metadata.setSimpleValue(simpleValue);
if (map.getKeys() != null) {
metadata.setKeys(map.getKeys());
}
metadata.setMapKeyValueProperty(mapKeyValueProperty);
if (map.getMapKeyOptionEntityClass() != null) {
if (!void.class.getName().equals(map.getMapKeyOptionEntityClass())) {
metadata.setMapKeyOptionEntityClass(map.getMapKeyOptionEntityClass());
} else {
metadata.setMapKeyOptionEntityClass("");
}
}
if (map.getMapKeyOptionEntityDisplayField() != null) {
metadata.setMapKeyOptionEntityDisplayField(map.getMapKeyOptionEntityDisplayField());
}
if (map.getMapKeyOptionEntityValueField() != null) {
metadata.setMapKeyOptionEntityValueField(map.getMapKeyOptionEntityValueField());
}
if (map.getForceFreeFormKeys() != null) {
if (!map.getForceFreeFormKeys() && ArrayUtils.isEmpty(metadata.getKeys()) && (StringUtils.isEmpty(metadata.getMapKeyOptionEntityClass()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityValueField()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityDisplayField()))) {
throw new IllegalArgumentException("Could not ascertain method for generating key options for the annotated map (" + field.getName() + "). Must specify either an array of AdminPresentationMapKey values for the keys property, or utilize the mapOptionKeyClass, mapOptionKeyDisplayField and mapOptionKeyValueField properties. If you wish to allow free form entry for key values, then set forceFreeFormKeys on AdminPresentationMap.");
}
}
MapStructure mapStructure;
if (serverMetadata != null) {
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
foreignKey.setManyToField(parentObjectIdField);
foreignKey.setForeignKeyClass(parentObjectClass);
if (metadata.isSimpleValue()) {
mapStructure = (SimpleValueMapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
mapStructure.setKeyClassName(keyClassName);
mapStructure.setKeyPropertyName(keyPropertyName);
mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
mapStructure.setValueClassName(metadata.getValueClassName());
((SimpleValueMapStructure) mapStructure).setValuePropertyName(valuePropertyName);
((SimpleValueMapStructure) mapStructure).setValuePropertyFriendlyName(valuePropertyFriendlyName);
mapStructure.setMapProperty(prefix + field.getName());
mapStructure.setMutable(metadata.isMutable());
} else {
mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
mapStructure.setKeyClassName(keyClassName);
mapStructure.setKeyPropertyName(keyPropertyName);
mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
mapStructure.setValueClassName(metadata.getValueClassName());
mapStructure.setMapProperty(prefix + field.getName());
mapStructure.setDeleteValueEntity(deleteEntityUponRemove);
mapStructure.setMutable(metadata.isMutable());
}
} else {
ForeignKey foreignKey = new ForeignKey(parentObjectIdField, parentObjectClass);
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, foreignKey);
if (metadata.isSimpleValue()) {
mapStructure = new SimpleValueMapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), valuePropertyName, valuePropertyFriendlyName, prefix + field.getName(), mapKeyValueProperty);
mapStructure.setMutable(metadata.isMutable());
} else {
mapStructure = new MapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), prefix + field.getName(), deleteEntityUponRemove, mapKeyValueProperty);
mapStructure.setMutable(metadata.isMutable());
}
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.MAPSTRUCTURE, mapStructure);
}
if (!StringUtils.isEmpty(map.getManyToField())) {
mapStructure.setManyToField(map.getManyToField());
}
if (mapStructure.getManyToField() == null) {
// try to infer the value
if (field.getManyToManyMappedBy() != null) {
mapStructure.setManyToField(field.getManyToManyMappedBy());
}
}
if (mapStructure.getManyToField() == null) {
// try to infer the value
if (field.getOneToManyMappedBy() != null) {
mapStructure.setManyToField(field.getOneToManyMappedBy());
}
}
if (map.getExcluded() != null) {
if (LOG.isDebugEnabled()) {
if (map.getExcluded()) {
LOG.debug("buildMapMetadata:Excluding " + field.getName() + " because it was explicitly declared in config");
} else {
LOG.debug("buildMapMetadata:Showing " + field.getName() + " because it was explicitly declared in config");
}
}
metadata.setExcluded(map.getExcluded());
}
if (map.getLazyFetch() != null) {
metadata.setLazyFetch(map.getLazyFetch());
}
if (map.getManualFetch() != null) {
metadata.setManualFetch(map.getManualFetch());
}
if (map.getFriendlyName() != null) {
metadata.setFriendlyName(map.getFriendlyName());
}
if (map.getSecurityLevel() != null) {
metadata.setSecurityLevel(map.getSecurityLevel());
}
if (map.getOrder() != null) {
metadata.setOrder(map.getOrder());
}
if (map.getTab() != null) {
metadata.setTab(map.getTab());
}
if (map.getTabOrder() != null) {
metadata.setTabOrder(map.getTabOrder());
}
if (map.getGroup() != null) {
metadata.setGroup(map.getGroup());
}
if (map.getCustomCriteria() != null) {
metadata.setCustomCriteria(map.getCustomCriteria());
}
if (map.getUseServerSideInspectionCache() != null) {
persistencePerspective.setUseServerSideInspectionCache(map.getUseServerSideInspectionCache());
}
if (map.getCurrencyCodeField() != null) {
metadata.setCurrencyCodeField(map.getCurrencyCodeField());
}
if (map.getForceFreeFormKeys() != null) {
metadata.setForceFreeFormKeys(map.getForceFreeFormKeys());
}
attributes.put(field.getName(), metadata);
}
Aggregations