Search in sources :

Example 11 with BasicCollectionMetadata

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

the class CollectionFieldMetadataProvider method overrideViaAnnotation.

@Override
public MetadataProviderResponse overrideViaAnnotation(OverrideViaAnnotationRequest overrideViaAnnotationRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleAnnotationOverride(overrideViaAnnotationRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Map<String, AdminPresentationCollectionOverride> presentationCollectionOverrides = new HashMap<String, AdminPresentationCollectionOverride>();
    AdminPresentationOverrides myOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationOverrides.class);
    if (myOverrides != null) {
        for (AdminPresentationCollectionOverride myOverride : myOverrides.collections()) {
            presentationCollectionOverrides.put(myOverride.name(), myOverride);
        }
    }
    for (String propertyName : presentationCollectionOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (key.startsWith(propertyName)) {
                buildAdminPresentationCollectionOverride(overrideViaAnnotationRequest.getPrefix(), overrideViaAnnotationRequest.getParentExcluded(), metadata, presentationCollectionOverrides, propertyName, key, overrideViaAnnotationRequest.getDynamicEntityDao());
            }
        }
    }
    AdminPresentationMergeOverrides myMergeOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationMergeOverrides.class);
    if (myMergeOverrides != null) {
        for (AdminPresentationMergeOverride override : myMergeOverrides.value()) {
            String propertyName = override.name();
            Map<String, FieldMetadata> loopMap = new HashMap<String, FieldMetadata>();
            loopMap.putAll(metadata);
            for (Map.Entry<String, FieldMetadata> entry : loopMap.entrySet()) {
                if (entry.getKey().startsWith(propertyName) || StringUtils.isEmpty(propertyName)) {
                    FieldMetadata targetMetadata = entry.getValue();
                    if (targetMetadata instanceof BasicCollectionMetadata) {
                        BasicCollectionMetadata serverMetadata = (BasicCollectionMetadata) targetMetadata;
                        if (serverMetadata.getTargetClass() != null) {
                            try {
                                Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                                Class<?> parentClass = null;
                                if (serverMetadata.getOwningClass() != null) {
                                    parentClass = Class.forName(serverMetadata.getOwningClass());
                                }
                                String fieldName = serverMetadata.getFieldName();
                                Field field = overrideViaAnnotationRequest.getDynamicEntityDao().getFieldManager().getField(targetClass, fieldName);
                                Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                                temp.put(field.getName(), serverMetadata);
                                FieldInfo info = buildFieldInfo(field);
                                FieldMetadataOverride fieldMetadataOverride = overrideCollectionMergeMetadata(override);
                                if (serverMetadata.getExcluded() != null && serverMetadata.getExcluded() && (fieldMetadataOverride.getExcluded() == null || fieldMetadataOverride.getExcluded())) {
                                    continue;
                                }
                                buildCollectionMetadata(parentClass, targetClass, temp, info, fieldMetadataOverride, overrideViaAnnotationRequest.getPrefix());
                                serverMetadata = (BasicCollectionMetadata) temp.get(field.getName());
                                metadata.put(entry.getKey(), serverMetadata);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : AdminPresentationOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) Field(java.lang.reflect.Field) AdminPresentationMergeOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) HashMap(java.util.HashMap) Map(java.util.Map) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride)

Example 12 with BasicCollectionMetadata

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

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

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

Example 15 with BasicCollectionMetadata

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

the class AdminBasicEntityController method updateCollectionItemSequence.

/**
 * Updates the given collection item's sequence. This should only be triggered for adorned target collections
 * where a sort field is specified -- any other invocation is incorrect and will result in an exception.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param collectionItemId
 * @return an object explaining the state of the operation
 * @throws Exception
 */
@RequestMapping(value = "/{id}/{collectionField:.*}/{collectionItemId}/{alternateId}/sequence", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> updateCollectionItemSequence(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "collectionItemId") String collectionItemId, @RequestParam(value = "newSequence") String newSequence, @PathVariable(value = "alternateId") String alternateId) 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 = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
    ppr.addCustomCriteria("reorderParentEntityFetch");
    Entity parentEntity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
    ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
    if (md instanceof AdornedTargetCollectionMetadata) {
        AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
        AdornedTargetList atl = ppr.getAdornedList();
        // Get an entity form for the entity
        EntityForm entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, sectionCrumbs, false);
        Entity entity = service.getAdvancedCollectionRecord(mainMetadata, parentEntity, collectionProperty, collectionItemId, sectionCrumbs, alternateId, new String[] { "reorderChildEntityFetch" }).getDynamicResultSet().getRecords()[0];
        formService.populateEntityFormFields(entityForm, entity);
        formService.populateAdornedEntityFormFields(entityForm, entity, ppr.getAdornedList());
        // Set the new sequence (note that it will come in 0-indexed but the persistence module expects 1-indexed)
        int sequenceValue = Integer.parseInt(newSequence) + 1;
        Field field = entityForm.findField(atl.getSortField());
        field.setValue(String.valueOf(sequenceValue));
        Map<String, Object> responseMap = new HashMap<>();
        PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, alternateId, sectionCrumbs);
        Property displayOrder = persistenceResponse.getEntity().findProperty(atl.getSortField());
        responseMap.put("status", "ok");
        responseMap.put("field", collectionField);
        responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
        return responseMap;
    } else if (md instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata cd = (BasicCollectionMetadata) md;
        Map<String, Object> responseMap = new HashMap<>();
        Entity entity = service.getRecord(ppr, collectionItemId, mainMetadata, false).getDynamicResultSet().getRecords()[0];
        ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        EntityForm entityForm = formService.createEntityForm(collectionMetadata, sectionCrumbs);
        if (!StringUtils.isEmpty(cd.getSortProperty())) {
            Field f = new Field().withName(cd.getSortProperty()).withFieldType(SupportedFieldType.HIDDEN.toString());
            entityForm.addHiddenField(mainMetadata, f);
        }
        formService.populateEntityFormFields(entityForm, entity);
        if (!StringUtils.isEmpty(cd.getSortProperty())) {
            int sequenceValue = Integer.parseInt(newSequence) + 1;
            Field field = entityForm.findField(cd.getSortProperty());
            field.setValue(String.valueOf(sequenceValue));
        }
        PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, sectionCrumbs);
        Property displayOrder = persistenceResponse.getEntity().findProperty(cd.getSortProperty());
        responseMap.put("status", "ok");
        responseMap.put("field", collectionField);
        responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
        return responseMap;
    } else {
        throw new UnsupportedOperationException("Cannot handle sequencing for non adorned target collection fields.");
    }
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) 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) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)18 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)14 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)13 Property (org.broadleafcommerce.openadmin.dto.Property)12 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)11 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)10 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)9 Entity (org.broadleafcommerce.openadmin.dto.Entity)8 MapMetadata (org.broadleafcommerce.openadmin.dto.MapMetadata)8 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)8 HashMap (java.util.HashMap)6 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)4 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)4 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 AdminPresentationCollectionOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride)3