Search in sources :

Example 41 with DynamicResultSet

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

the class AdminBasicEntityController method viewEntityForm.

/**
 * Renders the main entity form for the specified entity
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String viewEntityForm(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable("id") String id) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> crumbs = getSectionCrumbs(request, sectionKey, id);
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, crumbs, pathVars);
    ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    Entity entity = service.getRecord(ppr, id, cmd, false).getDynamicResultSet().getRecords()[0];
    Map<String, DynamicResultSet> subRecordsMap = getViewSubRecords(request, pathVars, cmd, entity, crumbs);
    EntityForm entityForm = formService.createEntityForm(cmd, entity, subRecordsMap, crumbs);
    if (isAddRequest(entity)) {
        modifyAddEntityForm(entityForm, pathVars);
    } else {
        modifyEntityForm(entityForm, pathVars);
    }
    if (request.getParameter("headerFlash") != null) {
        model.addAttribute("headerFlash", request.getParameter("headerFlash"));
    }
    // Set the sectionKey again incase this is a typed entity
    entityForm.setSectionKey(sectionKey);
    // Build the current url in the cast that this is a typed entity
    String originatingUri = new UrlPathHelper().getOriginatingRequestUri(request);
    int startIndex = request.getContextPath().length();
    // Remove the context path from servlet path
    String currentUrl = originatingUri.substring(startIndex);
    model.addAttribute("entity", entity);
    model.addAttribute("entityForm", entityForm);
    model.addAttribute("currentUrl", currentUrl);
    setModelAttributes(model, sectionKey);
    // We want to replace author ids with their names
    addAuditableDisplayFields(entityForm);
    if (isAjaxRequest(request)) {
        entityForm.setReadOnly();
        model.addAttribute("viewType", "modal/entityView");
        model.addAttribute("modalHeaderType", ModalHeaderType.VIEW_ENTITY.getType());
        return "modules/modalContainer";
    } else {
        model.addAttribute("useAjaxUpdate", true);
        model.addAttribute("viewType", "entityEdit");
        return "modules/defaultContainer";
    }
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 42 with DynamicResultSet

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

the class AdminBasicEntityController method saveEntity.

/**
 * Attempts to save the given entity. If validation is unsuccessful, it will re-render the entity form with
 * error fields highlighted. On a successful save, it will refresh the entity page.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param entityForm
 * @param result
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String saveEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, sectionCrumbs, pathVars);
    ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    extractDynamicFormFields(cmd, entityForm);
    String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(sectionClassName, getSectionCustomCriteria());
    Entity entity = service.updateEntity(entityForm, sectionCriteria, sectionCrumbs).getEntity();
    entityFormValidator.validate(entityForm, entity, result);
    if (result.hasErrors()) {
        model.addAttribute("headerFlash", "save.unsuccessful");
        model.addAttribute("headerFlashAlert", true);
        Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForAllSubCollections(ppr, entity, sectionCrumbs);
        entityForm.clearFieldsMap();
        formService.populateEntityForm(cmd, entity, subRecordsMap, entityForm, sectionCrumbs);
        if (isAddRequest(entity)) {
            modifyAddEntityForm(entityForm, pathVars);
        } else {
            modifyEntityForm(entityForm, pathVars);
        }
        model.addAttribute("entity", entity);
        model.addAttribute("currentUrl", request.getRequestURL().toString());
        setModelAttributes(model, sectionKey);
        if (isAjaxRequest(request)) {
            entityForm.setReadOnly();
            model.addAttribute("viewType", "modal/entityView");
            model.addAttribute("modalHeaderType", ModalHeaderType.VIEW_ENTITY.getType());
            return "modules/modalContainer";
        } else {
            model.addAttribute("useAjaxUpdate", true);
            model.addAttribute("viewType", "entityEdit");
            return "modules/defaultContainer";
        }
    }
    ra.addFlashAttribute("headerFlash", "save.successful");
    return "redirect:/" + sectionKey + "/" + id;
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with DynamicResultSet

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

the class AdminBasicEntityController method showViewUpdateCollection.

/**
 * Shows the view and populates the model for updating a collection item. You can also pass in an entityform and entity
 * which are optional. If they are not passed in then they are automatically looked up
 *
 * @param request
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param collectionItemId
 * @param modalHeaderType
 * @param entityForm
 * @param entity
 * @return
 * @throws ServiceException
 */
protected String showViewUpdateCollection(HttpServletRequest request, Model model, Map<String, String> pathVars, String id, String collectionField, String collectionItemId, String alternateId, String modalHeaderType, EntityForm entityForm, Entity entity) throws ServiceException {
    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);
    FieldMetadata md = collectionProperty.getMetadata();
    SectionCrumb nextCrumb = new SectionCrumb();
    if (md instanceof MapMetadata) {
        nextCrumb.setSectionIdentifier(((MapMetadata) md).getValueClassName());
    } else {
        nextCrumb.setSectionIdentifier(((CollectionMetadata) md).getCollectionCeilingEntity());
    }
    nextCrumb.setSectionId(collectionItemId);
    if (!sectionCrumbs.contains(nextCrumb)) {
        sectionCrumbs.add(nextCrumb);
    }
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
    Entity parentEntity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
    ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
    if (md instanceof BasicCollectionMetadata && (((BasicCollectionMetadata) md).getAddMethodType().equals(AddMethodType.PERSIST) || ((BasicCollectionMetadata) md).getAddMethodType().equals(AddMethodType.PERSIST_EMPTY))) {
        BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
        ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        if (entity == null) {
            entity = service.getRecord(ppr, collectionItemId, collectionMetadata, true).getDynamicResultSet().getRecords()[0];
        }
        String currentTabName = getCurrentTabName(pathVars, collectionMetadata);
        Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForSelectedTab(collectionMetadata, entity, sectionCrumbs, currentTabName);
        if (entityForm == null) {
            entityForm = formService.createEntityForm(collectionMetadata, entity, subRecordsMap, sectionCrumbs);
        } else {
            entityForm.clearFieldsMap();
            formService.populateEntityForm(collectionMetadata, entity, subRecordsMap, entityForm, sectionCrumbs);
            // remove all the actions since we're not trying to redisplay them on the form
            entityForm.removeAllActions();
        }
        entityForm.removeAction(DefaultEntityFormActions.DELETE);
        addAuditableDisplayFields(entityForm);
        model.addAttribute("entityForm", entityForm);
        model.addAttribute("viewType", "modal/simpleEditEntity");
    } else if (md instanceof AdornedTargetCollectionMetadata) {
        AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
        if (entity == null) {
            entity = service.getAdvancedCollectionRecord(mainMetadata, parentEntity, collectionProperty, collectionItemId, sectionCrumbs, alternateId).getDynamicResultSet().getRecords()[0];
        }
        boolean populateTypeAndId = true;
        boolean isViewCollectionItem = ModalHeaderType.VIEW_COLLECTION_ITEM.getType().equals(modalHeaderType);
        if (entityForm == null) {
            entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, isViewCollectionItem, sectionCrumbs, false);
            entityForm.removeAction(DefaultAdornedEntityFormActions.Add);
            entityForm.addAction(DefaultAdornedEntityFormActions.Save);
        } else {
            entityForm.clearFieldsMap();
            String entityType = entityForm.getEntityType();
            formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, isViewCollectionItem, entityForm, sectionCrumbs, false);
            entityForm.setEntityType(entityType);
            populateTypeAndId = false;
        }
        Map<String, Object> responseMap = new HashMap<>();
        adornedTargetAutoPopulateExtensionManager.getProxy().autoSetAdornedTargetManagedFields(md, mainClassName, id, collectionField, collectionItemId, responseMap);
        ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        for (String field : fmd.getMaintainedAdornedTargetFields()) {
            if (responseMap.containsKey(field) && responseMap.containsKey("autoSubmit")) {
                continue;
            }
            Property p = cmd.getPMap().get(field);
            if (p != null && p.getMetadata() instanceof AdornedTargetCollectionMetadata) {
                // Because we're dealing with a nested adorned target collection, this particular request must act
                // directly on the first adorned target collection. Because of this, we need the actual id property
                // from the entity that models the adorned target relationship, and not the id of the target object.
                Property alternateIdProperty = entity.getPMap().get(BasicPersistenceModule.ALTERNATE_ID_PROPERTY);
                DynamicResultSet drs = service.getRecordsForCollection(cmd, entity, p, null, null, null, alternateIdProperty.getValue(), sectionCrumbs).getDynamicResultSet();
                ListGrid listGrid = formService.buildCollectionListGrid(alternateIdProperty.getValue(), drs, p, ppr.getAdornedList().getAdornedTargetEntityClassname(), sectionCrumbs);
                listGrid.getToolbarActions().add(DefaultListGridActions.ADD);
                if (drs.getUnselectedTabMetadata().get(EntityForm.DEFAULT_TAB_NAME) != null) {
                    entityForm.addListGrid(cmd, listGrid, EntityForm.DEFAULT_TAB_NAME, EntityForm.DEFAULT_TAB_ORDER, fmd.getGroup(), true);
                } else {
                    entityForm.addListGrid(cmd, listGrid, EntityForm.DEFAULT_TAB_NAME, EntityForm.DEFAULT_TAB_ORDER, fmd.getGroup(), false);
                }
            } else if (p != null && p.getMetadata() instanceof MapMetadata) {
                // See above comment for AdornedTargetCollectionMetadata
                MapMetadata mmd = (MapMetadata) p.getMetadata();
                Property alternateIdProperty = entity.getPMap().get(BasicPersistenceModule.ALTERNATE_ID_PROPERTY);
                DynamicResultSet drs = service.getRecordsForCollection(cmd, entity, p, null, null, null, alternateIdProperty.getValue(), sectionCrumbs).getDynamicResultSet();
                ListGrid listGrid = formService.buildCollectionListGrid(alternateIdProperty.getValue(), drs, p, mmd.getTargetClass(), sectionCrumbs);
                listGrid.getToolbarActions().add(DefaultListGridActions.ADD);
                if (drs.getUnselectedTabMetadata().get(EntityForm.DEFAULT_TAB_NAME) != null) {
                    entityForm.addListGrid(cmd, listGrid, EntityForm.DEFAULT_TAB_NAME, EntityForm.DEFAULT_TAB_ORDER, fmd.getGroup(), true);
                } else {
                    entityForm.addListGrid(cmd, listGrid, EntityForm.DEFAULT_TAB_NAME, EntityForm.DEFAULT_TAB_ORDER, fmd.getGroup(), false);
                }
            }
        }
        formService.populateEntityFormFields(entityForm, entity, populateTypeAndId, populateTypeAndId);
        formService.populateAdornedEntityFormFields(entityForm, entity, ppr.getAdornedList());
        boolean atLeastOneBasicField = false;
        for (Entry<String, Field> entry : entityForm.getFields().entrySet()) {
            if (entry.getValue().getIsVisible() && !responseMap.containsKey(entry.getValue().getName()) && !responseMap.containsKey("autoSubmit")) {
                atLeastOneBasicField = true;
                break;
            }
        }
        if (!atLeastOneBasicField) {
            entityForm.removeAction(DefaultEntityFormActions.SAVE);
        }
        addAuditableDisplayFields(entityForm);
        model.addAttribute("entityForm", entityForm);
        model.addAttribute("viewType", "modal/adornedEditEntity");
    } else if (md instanceof MapMetadata) {
        MapMetadata fmd = (MapMetadata) md;
        ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        if (entity == null) {
            entity = service.getAdvancedCollectionRecord(mainMetadata, parentEntity, collectionProperty, collectionItemId, sectionCrumbs, null).getEntity();
        }
        boolean populateTypeAndId = true;
        if (entityForm == null) {
            entityForm = formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id);
        } else {
            // save off the prior key before clearing out the fields map as it will not appear
            // back on the saved entity
            String priorKey = entityForm.findField("priorKey").getValue();
            entityForm.clearFieldsMap();
            formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id, entityForm);
            entityForm.findField("priorKey").setValue(priorKey);
            populateTypeAndId = false;
        }
        formService.populateEntityFormFields(entityForm, entity, populateTypeAndId, populateTypeAndId);
        formService.populateMapEntityFormFields(entityForm, entity);
        addAuditableDisplayFields(entityForm);
        model.addAttribute("entityForm", entityForm);
        model.addAttribute("viewType", "modal/mapEditEntity");
    }
    model.addAttribute("currentUrl", request.getRequestURL().toString());
    model.addAttribute("modalHeaderType", modalHeaderType);
    model.addAttribute("collectionProperty", collectionProperty);
    setModelAttributes(model, sectionKey);
    return "modules/modalContainer";
}
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) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) Entry(java.util.Map.Entry) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) Map(java.util.Map) HashMap(java.util.HashMap) FlashMap(org.springframework.web.servlet.FlashMap) MultiValueMap(org.springframework.util.MultiValueMap) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 44 with DynamicResultSet

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

the class AdminBasicEntityController method removeEntity.

/**
 * Attempts to remove the given entity.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
public String removeEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
    String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(sectionClassName, getSectionCustomCriteria());
    Entity entity = service.removeEntity(entityForm, sectionCriteria, sectionCrumbs).getEntity();
    // Removal does not normally return an Entity unless there is some validation error
    if (entity != null) {
        entityFormValidator.validate(entityForm, entity, result);
        if (result.hasErrors()) {
            // Create a flash attribute for the unsuccessful delete
            FlashMap fm = new FlashMap();
            fm.put("headerFlash", "delete.unsuccessful");
            fm.put("headerFlashAlert", true);
            request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, fm);
            // Re-look back up the entity so that we can return something populated
            PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, sectionCrumbs, pathVars);
            ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
            entity = service.getRecord(ppr, id, cmd, false).getDynamicResultSet().getRecords()[0];
            Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForAllSubCollections(ppr, entity, sectionCrumbs);
            entityForm.clearFieldsMap();
            formService.populateEntityForm(cmd, entity, subRecordsMap, entityForm, sectionCrumbs);
            modifyEntityForm(entityForm, pathVars);
            return populateJsonValidationErrors(entityForm, result, new JsonResponse(response)).done();
        }
    }
    ra.addFlashAttribute("headerFlash", "delete.successful");
    ra.addFlashAttribute("headerFlashAlert", true);
    if (isAjaxRequest(request)) {
        // redirect attributes won't work here since ajaxredirect actually makes a new request
        return "ajaxredirect:" + getContextPath(request) + sectionKey + "?headerFlash=delete.successful";
    } else {
        return "redirect:/" + sectionKey;
    }
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FlashMap(org.springframework.web.servlet.FlashMap) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)44 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)26 Entity (org.broadleafcommerce.openadmin.dto.Entity)20 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)18 Property (org.broadleafcommerce.openadmin.dto.Property)16 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)16 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)15 ServiceException (org.broadleafcommerce.common.exception.ServiceException)14 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)14 HashMap (java.util.HashMap)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Map (java.util.Map)10 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)9 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)7 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)7 MergedPropertyType (org.broadleafcommerce.openadmin.dto.MergedPropertyType)7 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)7 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)6 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)6 Serializable (java.io.Serializable)5