Search in sources :

Example 51 with ClassMetadata

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

Example 52 with ClassMetadata

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

the class AdminBasicEntityController method viewCollectionItemDetails.

/**
 * Shows the modal popup for the current selected "to-one" field. For instance, if you are viewing a list of products
 * then this method is invoked when a user clicks on the name of the default category field.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param collectionField
 * @param id
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/{collectionField:.*}/{id}/view", method = RequestMethod.GET)
public String viewCollectionItemDetails(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "id") String id) 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);
    BasicFieldMetadata md = (BasicFieldMetadata) collectionProperty.getMetadata();
    AdminSection section = adminNavigationService.findAdminSectionByClassAndSectionId(md.getForeignKeyClass(), sectionKey);
    String sectionUrlKey = (section.getUrl().startsWith("/")) ? section.getUrl().substring(1) : section.getUrl();
    Map<String, String> varsForField = new HashMap<>();
    varsForField.put("sectionKey", sectionUrlKey);
    return viewEntityForm(request, response, model, varsForField, id);
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) HashMap(java.util.HashMap) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) Property(org.broadleafcommerce.openadmin.dto.Property) 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