Search in sources :

Example 46 with FieldMetadata

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

the class AdminBasicOperationsController method getTypeaheadResults.

@RequestMapping(value = "/{owningClass:.*}/{collectionField:.*}/typeahead", method = RequestMethod.GET)
@ResponseBody
public List<Map<String, String>> getTypeaheadResults(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "owningClass") String owningClass, @PathVariable(value = "collectionField") String collectionField, @RequestParam(required = false) String query, @RequestParam(required = false) String requestingEntityId, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, null, null);
    String validatedClass = getClassNameForSection(owningClass);
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(validatedClass, requestParams, sectionCrumbs, pathVars);
    ClassMetadata mainMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    Property collectionProperty = mainMetadata.getPMap().get(collectionField);
    FieldMetadata md = collectionProperty.getMetadata();
    ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
    ppr.addFilterAndSortCriteria(getCriteria(requestParams));
    ppr.setStartIndex(getStartIndex(requestParams));
    ppr.setMaxIndex(getMaxIndex(requestParams));
    ppr.removeFilterAndSortCriteria("query");
    ppr.removeFilterAndSortCriteria("requestingEntityId");
    ppr.addCustomCriteria("requestingEntityId=" + requestingEntityId);
    // This list of datums will populate the typeahead suggestions.
    List<Map<String, String>> responses = new ArrayList<Map<String, String>>();
    if (md instanceof BasicFieldMetadata) {
        String searchField = searchFieldResolver.resolveField(((BasicFieldMetadata) md).getForeignKeyClass());
        ppr.addFilterAndSortCriteria(new FilterAndSortCriteria(searchField, query));
        DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
        ClassMetadata lookupMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        for (Entity e : drs.getRecords()) {
            Map<String, String> responseMap = new HashMap<String, String>();
            String idProperty = service.getIdProperty(lookupMetadata);
            responseMap.put("id", e.findProperty(idProperty).getValue());
            String displayKey = e.findProperty(searchField).getDisplayValue();
            if (StringUtils.isBlank(displayKey)) {
                displayKey = e.findProperty(searchField).getValue();
            }
            responseMap.put("displayKey", displayKey);
            responses.add(responseMap);
        }
    }
    return responses;
}
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) ArrayList(java.util.ArrayList) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) Property(org.broadleafcommerce.openadmin.dto.Property) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 47 with FieldMetadata

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

the class AdminAbstractController method getDynamicFieldTemplateForm.

/**
 * Convenience method for obtaining a dynamic field template form for a particular entity. This method differs from
 * {@link #getBlankDynamicFieldTemplateForm(DynamicEntityFormInfo)} in that it will fill out the current values for
 * the fields in this dynamic form from the database. This method is invoked when the initial view of a page containing
 * a dynamic form is triggered.
 *
 * Optionally, you can pass in a pre-existing dynamic form to this method that already has updated values. Example usage
 * would be for after validation has failed and you do not want to lookup old values from the database again.
 *
 * @param info
 * @param entityId
 * @param dynamicFormOverride optional dynamic form that already has values to fill out
 * @return the entity form
 * @throws ServiceException
 */
protected EntityForm getDynamicFieldTemplateForm(DynamicEntityFormInfo info, String entityId, EntityForm dynamicFormOverride) throws ServiceException {
    // We need to inspect with the second custom criteria set to the id of
    // the desired structured content type
    PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(info.getCeilingClassName()).withSecurityCeilingEntityClassname(info.getSecurityCeilingClassName()).withCustomCriteria(new String[] { info.getCriteriaName(), entityId, info.getPropertyName(), info.getPropertyValue() });
    ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    // However, when we fetch, the second custom criteria needs to be the id
    // of this particular structured content entity
    ppr.setCustomCriteria(new String[] { info.getCriteriaName(), entityId });
    Entity entity = service.getRecord(ppr, info.getPropertyValue(), cmd, true).getDynamicResultSet().getRecords()[0];
    List<Field> fieldsToMove = new ArrayList<>();
    // override the results of the entity with the dynamic form passed in
    if (dynamicFormOverride != null) {
        dynamicFormOverride.clearFieldsMap();
        Map<String, Field> fieldOverrides = dynamicFormOverride.getFields();
        for (Entry<String, Field> override : fieldOverrides.entrySet()) {
            if (entity.getPMap().containsKey(override.getKey())) {
                entity.getPMap().get(override.getKey()).setValue(override.getValue().getValue());
            } else {
                fieldsToMove.add(override.getValue());
            }
        }
    }
    // Assemble the dynamic form for structured content type
    EntityForm dynamicForm = formService.createEntityForm(cmd, entity, null, null);
    for (Field field : fieldsToMove) {
        FieldMetadata fmd = cmd.getPMap().get(field.getName()).getMetadata();
        if (fmd instanceof BasicFieldMetadata) {
            BasicFieldMetadata bfmd = (BasicFieldMetadata) fmd;
            field.setFieldType(bfmd.getFieldType().toString());
            field.setFriendlyName(bfmd.getFriendlyName());
            field.setRequired(bfmd.getRequired());
        }
        dynamicForm.addField(cmd, field);
    }
    setSpecializedNameForFields(info, dynamicForm);
    extensionManager.getProxy().modifyDynamicForm(dynamicForm, entityId);
    return dynamicForm;
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)

Example 48 with FieldMetadata

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

the class CategoryCustomPersistenceHandler method inspect.

@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
    Map<String, FieldMetadata> md = getMetadata(persistencePackage, helper);
    if (!isDefaultCategoryLegacyMode()) {
        md.remove("allParentCategoryXrefs");
        BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) md.get(DEFAULT_PARENT_CATEGORY));
        defaultCategory.setFriendlyName("CategoryImpl_ParentCategory");
    }
    return getResultSet(persistencePackage, helper, md);
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)

Example 49 with FieldMetadata

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

the class CategoryCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        entity = validateParentCategory(entity, true);
        if (entity.isValidationFailure()) {
            return entity;
        } else {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Category adminInstance = (Category) Class.forName(entity.getType()[0]).newInstance();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Category.class.getName(), persistencePerspective);
            adminInstance = (Category) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
            adminInstance = dynamicEntityDao.merge(adminInstance);
            boolean handled = false;
            if (extensionManager != null) {
                ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
                handled = ExtensionResultStatusType.NOT_HANDLED != result;
            }
            if (!handled) {
                setupXref(adminInstance);
            }
            adminInstance = dynamicEntityDao.merge(adminInstance);
            return helper.getRecord(adminProperties, adminInstance, null, null);
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Category(org.broadleafcommerce.core.catalog.domain.Category) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 50 with FieldMetadata

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

the class CustomerCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Customer adminInstance = (Customer) Class.forName(entity.getType()[0]).newInstance();
        adminInstance.setId(customerService.findNextCustomerId());
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
        adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        if (useEmailForLogin) {
            adminInstance.setUsername(adminInstance.getEmailAddress());
        }
        if (!entity.isPreAdd()) {
            Entity errorEntity = validateUniqueUsername(entity, adminInstance);
            if (errorEntity != null) {
                return errorEntity;
            }
        }
        adminInstance = dynamicEntityDao.merge(adminInstance);
        customerService.createRegisteredCustomerRoles(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Aggregations

FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)114 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)80 Entity (org.broadleafcommerce.openadmin.dto.Entity)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)46 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)44 HashMap (java.util.HashMap)39 Property (org.broadleafcommerce.openadmin.dto.Property)38 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)22 Field (java.lang.reflect.Field)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)19 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)19 Serializable (java.io.Serializable)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)18 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)17 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)15 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13