Search in sources :

Example 11 with MapMetadata

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

the class AdminEntityServiceImpl method getPagedRecordsForCollection.

@Override
public PersistenceResponse getPagedRecordsForCollection(ClassMetadata containingClassMetadata, Entity containingEntity, Property collectionProperty, FilterAndSortCriteria[] fascs, FetchPageRequest fetchPageRequest, String idValueOverride, List<SectionCrumb> sectionCrumbs) throws ServiceException {
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(collectionProperty.getMetadata(), sectionCrumbs).withFilterAndSortCriteria(fascs).withStartIndex(fetchPageRequest.getStartIndex()).withMaxIndex(fetchPageRequest.getMaxIndex()).withFirstId(fetchPageRequest.getFirstId()).withLastId(fetchPageRequest.getLastId()).withLowerCount(fetchPageRequest.getLowerCount()).withUpperCount(fetchPageRequest.getUpperCount()).withPageSize(fetchPageRequest.getPageSize()).withPresentationFetch(true);
    FilterAndSortCriteria fasc;
    FieldMetadata md = collectionProperty.getMetadata();
    String collectionCeilingClass = null;
    if (md instanceof BasicCollectionMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
        collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
    } else if (md instanceof AdornedTargetCollectionMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getAdornedList().getCollectionFieldName());
        collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
    } else if (md instanceof MapMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
    } else {
        throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was not a " + "collection field.", collectionProperty.getName(), containingClassMetadata.getCeilingType()));
    }
    String id;
    if (idValueOverride == null) {
        id = getContextSpecificRelationshipId(containingClassMetadata, containingEntity, collectionProperty.getName());
    } else {
        id = idValueOverride;
    }
    fasc.setFilterValue(id);
    ppr.addFilterAndSortCriteria(fasc);
    if (collectionCeilingClass != null) {
        ppr.setCeilingEntityClassname(collectionCeilingClass);
    }
    ppr.setSectionEntityField(collectionProperty.getName());
    return fetch(ppr);
}
Also used : BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) CollectionMetadata(org.broadleafcommerce.openadmin.dto.CollectionMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 12 with MapMetadata

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

the class AdminBasicEntityController method showAddCollectionItem.

/**
 * Shows the modal dialog that is used to add an item to a given collection. There are several possible outcomes
 * of this call depending on the type of the specified collection field.
 *
 * <ul>
 *  <li>
 *    <b>Basic Collection (Persist)</b> - Renders a blank form for the specified target entity so that the user may
 *    enter information and associate the record with this collection. Used by fields such as ProductAttribute.
 *  </li>
 *  <li>
 *    <b>Basic Collection (Lookup)</b> - Renders a list grid that allows the user to click on an entity and select it.
 *    Used by fields such as "allParentCategories".
 *  </li>
 *  <li>
 *    <b>Adorned Collection (without form)</b> - Renders a list grid that allows the user to click on an entity and
 *    select it. The view rendered by this is identical to basic collection (lookup), but will perform the operation
 *    on an adorned field, which may carry extra meta-information about the created relationship, such as order.
 *  </li>
 *  <li>
 *    <b>Adorned Collection (with form)</b> - Renders a list grid that allows the user to click on an entity and
 *    select it. Once the user selects the entity, he will be presented with an empty form based on the specified
 *    "maintainedAdornedTargetFields" for this field. Used by fields such as "crossSellProducts", which in addition
 *    to linking an entity, provide extra fields, such as a promotional message.
 *  </li>
 *  <li>
 *    <b>Map Collection</b> - Renders a form for the target entity that has an additional key field. This field is
 *    populated either from the configured map keys, or as a result of a lookup in the case of a key based on another
 *    entity. Used by fields such as the mediaMap on a Sku.
 *  </li>
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param requestParams
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}/{collectionField:.*}/add", method = RequestMethod.GET)
public String showAddCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @RequestParam MultiValueMap<String, String> requestParams) 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);
    FieldMetadata md = collectionProperty.getMetadata();
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withFilterAndSortCriteria(getCriteria(requestParams)).withStartIndex(getStartIndex(requestParams)).withMaxIndex(getMaxIndex(requestParams)).withLastId(getLastId(requestParams)).withFirstId(getFirstId(requestParams)).withUpperCount(getUpperCount(requestParams)).withLowerCount(getLowerCount(requestParams)).withPageSize(getPageSize(requestParams)).withPresentationFetch(true);
    if (md instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
        if (fmd.getAddMethodType().equals(AddMethodType.PERSIST)) {
            ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
            // If the entity type isn't specified, we need to determine if there are various polymorphic types
            // for this entity.
            String entityType = null;
            if (requestParams.containsKey("entityType")) {
                entityType = requestParams.get("entityType").get(0);
            }
            if (StringUtils.isBlank(entityType)) {
                if (cmd.getPolymorphicEntities().getChildren().length == 0) {
                    entityType = cmd.getPolymorphicEntities().getFullyQualifiedClassname();
                } else {
                    entityType = getDefaultEntityType();
                }
            } else {
                entityType = URLDecoder.decode(entityType, "UTF-8");
            }
            if (StringUtils.isBlank(entityType)) {
                List<ClassTree> entityTypes = getAddEntityTypes(cmd.getPolymorphicEntities());
                model.addAttribute("entityTypes", entityTypes);
                model.addAttribute("viewType", "modal/entityTypeSelection");
                model.addAttribute("entityFriendlyName", cmd.getPolymorphicEntities().getFriendlyName());
                String requestUri = request.getRequestURI();
                if (!request.getContextPath().equals("/") && requestUri.startsWith(request.getContextPath())) {
                    requestUri = requestUri.substring(request.getContextPath().length() + 1, requestUri.length());
                }
                model.addAttribute("currentUri", requestUri);
                model.addAttribute("modalHeaderType", ModalHeaderType.ADD_ENTITY.getType());
                setModelAttributes(model, sectionKey);
                return "modules/modalContainer";
            } else {
                ppr = ppr.withCeilingEntityClassname(entityType);
            }
        }
    } else if (md instanceof MapMetadata) {
        ExtensionResultStatusType result = extensionManager.getProxy().modifyModelForAddCollectionType(request, response, model, sectionKey, id, requestParams, (MapMetadata) md);
        if (result.equals(ExtensionResultStatusType.HANDLED)) {
            model.addAttribute("entityId", id);
            model.addAttribute("sectionKey", sectionKey);
            model.addAttribute("collectionField", collectionField);
            return "modules/modalContainer";
        }
    }
    // service.getContextSpecificRelationshipId(mainMetadata, entity, prefix);
    model.addAttribute("currentParams", new ObjectMapper().writeValueAsString(requestParams));
    return buildAddCollectionItemModel(request, response, model, id, collectionField, sectionKey, collectionProperty, md, ppr, null, null);
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ClassTree(org.broadleafcommerce.openadmin.dto.ClassTree) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with MapMetadata

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

Aggregations

MapMetadata (org.broadleafcommerce.openadmin.dto.MapMetadata)13 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)10 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)8 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)8 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)8 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)7 Property (org.broadleafcommerce.openadmin.dto.Property)7 HashMap (java.util.HashMap)5 Entity (org.broadleafcommerce.openadmin.dto.Entity)5 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)4 BLCSystemProperty (org.broadleafcommerce.common.util.BLCSystemProperty)4 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)4 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 AdminPresentationMap (org.broadleafcommerce.common.presentation.AdminPresentationMap)3 AdminPresentationMapOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride)3 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)3 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)3 FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)3