Search in sources :

Example 41 with ClassMetadata

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

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

the class AdminBasicEntityController method addCollectionItem.

@RequestMapping(value = "/{id}/{collectionField:.*}/add/{collectionItemId}/verify", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> addCollectionItem(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) 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();
    Map<String, Object> responseMap = new HashMap<>();
    if (md instanceof AdornedTargetCollectionMetadata) {
        adornedTargetAutoPopulateExtensionManager.getProxy().autoSetAdornedTargetManagedFields(md, mainClassName, id, collectionField, collectionItemId, responseMap);
    }
    return responseMap;
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) Property(org.broadleafcommerce.openadmin.dto.Property) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 43 with ClassMetadata

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

the class AdminBasicEntityController method getSelectizeCollectionOptions.

/**
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param requestParams
 * @return Json collection data
 * @throws Exception
 */
@RequestMapping(value = "/{id}/{collectionField:.*}/selectize", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getSelectizeCollectionOptions(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)).withCustomCriteria(buildSelectizeCustomCriteria());
    if (md instanceof AdornedTargetCollectionMetadata) {
        ppr.setOperationTypesOverride(null);
        ppr.setType(PersistencePackageRequest.Type.STANDARD);
        ppr.setSectionEntityField(collectionField);
    }
    DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
    return formService.buildSelectizeCollectionInfo(id, drs, collectionProperty, sectionKey, sectionCrumbs);
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 44 with ClassMetadata

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

Example 45 with ClassMetadata

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

the class AdminBasicEntityController method addCollectionItem.

/**
 * Adds the requested collection item
 *
 * @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:.*}/add", method = RequestMethod.POST)
public String addCollectionItem(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);
    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()) {
        FieldMetadata md = collectionProperty.getMetadata();
        ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
        return buildAddCollectionItemModel(request, response, model, id, collectionField, sectionKey, collectionProperty, md, ppr, entityForm, savedEntity);
    }
    // Next, we must get the new list grid that represents this collection
    ListGrid listGrid = getCollectionListGrid(mainMetadata, entity, collectionProperty, null, sectionKey, persistenceResponse, sectionCrumbs);
    model.addAttribute("listGrid", listGrid);
    // We return the new list grid so that it can replace the currently visible one
    model.addAttribute("actualEntityId", id);
    setModelAttributes(model, sectionKey);
    return "views/standaloneListGrid";
}
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) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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