Search in sources :

Example 36 with ClassMetadata

use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method buildAdornedListForm.

@Override
public EntityForm buildAdornedListForm(AdornedTargetCollectionMetadata adornedMd, AdornedTargetList adornedList, String parentId, boolean isViewCollectionItem, EntityForm ef, List<SectionCrumb> sectionCrumbs, boolean isAdd) throws ServiceException {
    ef.setEntityType(adornedList.getAdornedTargetEntityClassname());
    // Get the metadata for this adorned field
    PersistencePackageRequest request = PersistencePackageRequest.adorned().withCeilingEntityClassname(adornedMd.getCollectionCeilingEntity()).withAdornedList(adornedList).withSectionCrumbs(sectionCrumbs);
    request.setAddOperationInspect(isAdd);
    ClassMetadata collectionMetadata = adminEntityService.getClassMetadata(request).getDynamicResultSet().getClassMetaData();
    List<Property> entityFormProperties = new ArrayList<>();
    if (isViewCollectionItem) {
        Collections.addAll(entityFormProperties, collectionMetadata.getProperties());
    } else {
        // We want our entity form to only render the maintained adorned target fields
        for (String targetFieldName : adornedMd.getMaintainedAdornedTargetFields()) {
            Property p = collectionMetadata.getPMap().get(targetFieldName);
            if (p.getMetadata() instanceof BasicFieldMetadata && BooleanUtils.isNotTrue(p.getMetadata().getExcluded())) {
                ((BasicFieldMetadata) p.getMetadata()).setVisibility(VisibilityEnum.VISIBLE_ALL);
                entityFormProperties.add(p);
            }
        }
    }
    // Set the maintained fields on the form
    setEntityFormFields(collectionMetadata, ef, entityFormProperties);
    // Add these two additional hidden fields that are required for persistence
    Field f = new Field().withName(adornedList.getLinkedObjectPath() + "." + adornedList.getLinkedIdProperty()).withFieldType(SupportedFieldType.HIDDEN.toString()).withValue(parentId);
    ef.addHiddenField(collectionMetadata, f);
    f = new Field().withName(adornedList.getTargetObjectPath() + "." + adornedList.getTargetIdProperty()).withFieldType(SupportedFieldType.HIDDEN.toString()).withIdOverride("adornedTargetIdProperty");
    ef.addHiddenField(collectionMetadata, f);
    if (StringUtils.isNotBlank(adornedList.getSortField())) {
        f = new Field().withName(adornedList.getSortField()).withFieldType(SupportedFieldType.HIDDEN.toString());
        ef.addHiddenField(collectionMetadata, f);
    }
    ef.setParentId(parentId);
    extensionManager.getProxy().addAdditionalAdornedFormActions(ef);
    return ef;
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 37 with ClassMetadata

use of org.broadleafcommerce.openadmin.dto.ClassMetadata 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";
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entry(java.util.Map.Entry) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 38 with ClassMetadata

use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.

the class AdminEntityServiceImpl method getClassMetadata.

@Override
public PersistenceResponse getClassMetadata(PersistencePackageRequest request) throws ServiceException {
    PersistenceResponse response = inspect(request);
    ClassMetadata cmd = response.getDynamicResultSet().getClassMetaData();
    cmd.setCeilingType(request.getCeilingEntityClassname());
    cmd.setSecurityCeilingType(request.getSecurityCeilingEntityClassname());
    return response;
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)

Example 39 with ClassMetadata

use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentTypeCustomPersistenceHandler method inspect.

@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        String structuredContentTypeId = persistencePackage.getCustomCriteria()[3];
        StructuredContentType structuredContentType = structuredContentService.findStructuredContentTypeById(Long.valueOf(structuredContentTypeId));
        ClassMetadata metadata = new ClassMetadata();
        metadata.setCeilingType(StructuredContentType.class.getName());
        ClassTree entities = new ClassTree(StructuredContentTypeImpl.class.getName());
        metadata.setPolymorphicEntities(entities);
        Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(structuredContentType.getStructuredContentFieldTemplate().getFieldGroups(), StructuredContentTypeImpl.class);
        metadata.setProperties(properties);
        DynamicResultSet results = new DynamicResultSet(metadata);
        return results;
    } catch (Exception e) {
        throw new ServiceException("Unable to perform inspect for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) StructuredContentTypeImpl(org.broadleafcommerce.cms.structure.domain.StructuredContentTypeImpl) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ClassTree(org.broadleafcommerce.openadmin.dto.ClassTree) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException)

Example 40 with ClassMetadata

use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.

the class AdminBasicEntityController method addSelectizeCollectionItem.

/**
 * Adds the requested collection item via Selectize
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param entityForm
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}/{collectionField:.*}/selectize-add", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> addSelectizeCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result) throws Exception {
    Map<String, Object> returnVal = new HashMap<>();
    String sectionKey = getSectionKey(pathVars);
    String mainClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
    ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
    Property collectionProperty = mainMetadata.getPMap().get(collectionField);
    if (StringUtils.isBlank(entityForm.getEntityType())) {
        FieldMetadata fmd = collectionProperty.getMetadata();
        if (fmd instanceof BasicCollectionMetadata) {
            entityForm.setEntityType(((BasicCollectionMetadata) fmd).getCollectionCeilingEntity());
        }
    }
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
    ppr.addCustomCriteria(buildSelectizeCustomCriteria());
    declareShouldIgnoreAdditionStatusFilter();
    Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
    // First, we must save the collection entity
    PersistenceResponse persistenceResponse = service.addSubCollectionEntity(entityForm, mainMetadata, collectionProperty, entity, sectionCrumbs);
    Entity savedEntity = persistenceResponse.getEntity();
    entityFormValidator.validate(entityForm, savedEntity, result);
    if (result.hasErrors()) {
        returnVal.put("error", result.getFieldError());
        return returnVal;
    }
    if (savedEntity.findProperty(ALTERNATE_ID_PROPERTY) != null) {
        returnVal.put("alternateId", savedEntity.findProperty(ALTERNATE_ID_PROPERTY).getValue());
    }
    return returnVal;
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)52 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)36 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)32 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)26 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)26 Property (org.broadleafcommerce.openadmin.dto.Property)25 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)22 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)21 Entity (org.broadleafcommerce.openadmin.dto.Entity)19 HashMap (java.util.HashMap)16 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)11 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)11 Map (java.util.Map)10 EntityForm (org.broadleafcommerce.openadmin.web.form.entity.EntityForm)10 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)9 ArrayList (java.util.ArrayList)8 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)8 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)8 MergedPropertyType (org.broadleafcommerce.openadmin.dto.MergedPropertyType)7 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)7