Search in sources :

Example 26 with SectionCrumb

use of org.broadleafcommerce.openadmin.dto.SectionCrumb 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 27 with SectionCrumb

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

the class ListGrid method getSectionCrumbRepresentation.

public String getSectionCrumbRepresentation() {
    StringBuilder sb = new StringBuilder();
    if (!sectionCrumbs.isEmpty()) {
        sb.append("?sectionCrumbs=");
    }
    int index = 0;
    for (SectionCrumb section : sectionCrumbs) {
        sb.append(section.getSectionIdentifier());
        sb.append("--");
        sb.append(section.getSectionId());
        if (index < sectionCrumbs.size() - 1) {
            sb.append(",");
        }
        index++;
    }
    return sb.toString();
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb)

Example 28 with SectionCrumb

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

the class AdminSecurityServiceRemote method securityCheck.

@Override
public void securityCheck(PersistencePackage persistencePackage, EntityOperationType operationType) throws ServiceException {
    Set<String> ceilingNames = new HashSet<String>();
    ceilingNames.add(persistencePackage.getSecurityCeilingEntityFullyQualifiedClassname());
    if (!ArrayUtils.isEmpty(persistencePackage.getSectionCrumbs())) {
        ceilingNames.addAll(CollectionUtils.transform(Arrays.asList(persistencePackage.getSectionCrumbs()), new Transformer() {

            @Override
            public Object transform(Object o) {
                return ((SectionCrumb) o).getSectionIdentifier();
            }
        }));
    }
    Entity entity = persistencePackage.getEntity();
    if (persistencePackage.getPersistencePerspectiveItems().containsKey(PersistencePerspectiveItemType.ADORNEDTARGETLIST)) {
        if (persistencePackage.getEntity() != null) {
            for (Property property : persistencePackage.getProperties()) {
                if (property.getName() != null && property.getName().endsWith(".id") && property.getValue() == null) {
                    entity.addGlobalValidationError("adornedTargetRequired");
                    throw new ValidationException(entity);
                }
            }
        }
    }
    GlobalValidationResult globalValidationResult = null;
    if (operationType.equals(EntityOperationType.UPDATE)) {
        globalValidationResult = rowLevelSecurityService.validateUpdateRequest(getPersistentAdminUser(), entity, persistencePackage);
    } else if (operationType.equals(EntityOperationType.REMOVE)) {
        globalValidationResult = rowLevelSecurityService.validateRemoveRequest(getPersistentAdminUser(), entity, persistencePackage);
    } else if (operationType.equals(EntityOperationType.ADD)) {
        globalValidationResult = rowLevelSecurityService.validateAddRequest(getPersistentAdminUser(), entity, persistencePackage);
    }
    if (globalValidationResult != null) {
        if (!globalValidationResult.isValid()) {
            if (StringUtils.isEmpty(globalValidationResult.getErrorMessage())) {
                entity.addGlobalValidationError("rowLevelSecurityFailed");
            } else {
                entity.addGlobalValidationErrors(globalValidationResult.getErrorMessages());
            }
            throw new ValidationException(entity, "Row level security check failed for " + operationType);
        }
    }
    securityCheck(ceilingNames.toArray(new String[ceilingNames.size()]), operationType);
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) Entity(org.broadleafcommerce.openadmin.dto.Entity) Transformer(org.springframework.cglib.core.Transformer) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) GlobalValidationResult(org.broadleafcommerce.openadmin.server.service.persistence.validation.GlobalValidationResult) Property(org.broadleafcommerce.openadmin.dto.Property) HashSet(java.util.HashSet)

Example 29 with SectionCrumb

use of org.broadleafcommerce.openadmin.dto.SectionCrumb 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)

Example 30 with SectionCrumb

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

the class AdminBasicEntityController method addEmptyCollectionItem.

@RequestMapping(value = "/{id}/{collectionField:.*}/addEmpty", method = RequestMethod.POST)
@ResponseBody
public String addEmptyCollectionItem(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 {
    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);
    Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
    entity.setIsPreAdd(true);
    // First, we must save the collection entity
    PersistenceResponse persistenceResponse = service.addSubCollectionEntity(entityForm, mainMetadata, collectionProperty, entity, sectionCrumbs);
    Entity savedEntity = persistenceResponse.getEntity();
    return new JsonResponse(response).with("status", "complete").with("id", savedEntity.findProperty(entityForm.getIdProperty()).getValue()).done();
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)42 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)32 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)28 Entity (org.broadleafcommerce.openadmin.dto.Entity)19 Property (org.broadleafcommerce.openadmin.dto.Property)19 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)15 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)13 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)12 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)9 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)8 EntityForm (org.broadleafcommerce.openadmin.web.form.entity.EntityForm)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)6 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)5 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)5 Map (java.util.Map)4 Translation (org.broadleafcommerce.common.i18n.domain.Translation)4