Search in sources :

Example 1 with BasicCollectionMetadata

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

the class CollectionFieldMetadataProvider method buildAdminPresentationCollectionOverride.

protected void buildAdminPresentationCollectionOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationCollectionOverride> presentationCollectionOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
    AdminPresentationCollectionOverride override = presentationCollectionOverrides.get(propertyName);
    if (override != null) {
        AdminPresentationCollection annot = override.value();
        if (annot != null) {
            String testKey = prefix + key;
            if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && annot.excluded()) {
                FieldMetadata metadata = mergedProperties.get(key);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("buildAdminPresentationCollectionOverride:Excluding " + key + "because an override annotation declared " + testKey + "to be excluded");
                }
                metadata.setExcluded(true);
                return;
            }
            if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && !annot.excluded()) {
                FieldMetadata metadata = mergedProperties.get(key);
                if (!isParentExcluded) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("buildAdminPresentationCollectionOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
                    }
                    metadata.setExcluded(false);
                }
            }
            if (!(mergedProperties.get(key) instanceof BasicCollectionMetadata)) {
                return;
            }
            BasicCollectionMetadata serverMetadata = (BasicCollectionMetadata) mergedProperties.get(key);
            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 = dynamicEntityDao.getFieldManager().getField(targetClass, fieldName);
                    FieldMetadataOverride localMetadata = constructBasicCollectionMetadataOverride(annot);
                    // do not include the previous metadata - we want to construct a fresh metadata from the override annotation
                    Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                    FieldInfo info = buildFieldInfo(field);
                    buildCollectionMetadata(parentClass, targetClass, temp, info, localMetadata, prefix);
                    BasicCollectionMetadata result = (BasicCollectionMetadata) temp.get(field.getName());
                    result.setInheritedFromType(serverMetadata.getInheritedFromType());
                    result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
                    mergedProperties.put(key, result);
                    if (isParentExcluded) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("buildAdminPresentationCollectionOverride:Excluding " + key + "because the parent was excluded");
                        }
                        serverMetadata.setExcluded(true);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride) HashMap(java.util.HashMap) AdminPresentationCollection(org.broadleafcommerce.common.presentation.AdminPresentationCollection) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Field(java.lang.reflect.Field) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Example 2 with BasicCollectionMetadata

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

the class CollectionFieldMetadataProvider method overrideViaXml.

@Override
public MetadataProviderResponse overrideViaXml(OverrideViaXmlRequest overrideViaXmlRequest, Map<String, FieldMetadata> metadata) {
    Map<String, MetadataOverride> overrides = getTargetedOverride(overrideViaXmlRequest.getDynamicEntityDao(), overrideViaXmlRequest.getRequestedConfigKey(), overrideViaXmlRequest.getRequestedCeilingEntity());
    if (overrides != null) {
        for (String propertyName : overrides.keySet()) {
            MetadataOverride localMetadata = overrides.get(propertyName);
            if (localMetadata instanceof FieldMetadataOverride) {
                FieldMetadataOverride localFieldMetadata = (FieldMetadataOverride) localMetadata;
                for (String key : metadata.keySet()) {
                    if (key.equals(propertyName)) {
                        try {
                            if (metadata.get(key) instanceof BasicCollectionMetadata) {
                                BasicCollectionMetadata serverMetadata = (BasicCollectionMetadata) metadata.get(key);
                                if (serverMetadata.getTargetClass() != null) {
                                    Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                                    Class<?> parentClass = null;
                                    if (serverMetadata.getOwningClass() != null) {
                                        parentClass = Class.forName(serverMetadata.getOwningClass());
                                    }
                                    String fieldName = serverMetadata.getFieldName();
                                    Field field = overrideViaXmlRequest.getDynamicEntityDao().getFieldManager().getField(targetClass, fieldName);
                                    Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                                    temp.put(field.getName(), serverMetadata);
                                    FieldInfo info = buildFieldInfo(field);
                                    buildCollectionMetadata(parentClass, targetClass, temp, info, localFieldMetadata, overrideViaXmlRequest.getPrefix());
                                    serverMetadata = (BasicCollectionMetadata) temp.get(field.getName());
                                    metadata.put(key, serverMetadata);
                                    if (overrideViaXmlRequest.getParentExcluded()) {
                                        if (LOG.isDebugEnabled()) {
                                            LOG.debug("applyCollectionMetadataOverrides:Excluding " + key + "because parent is marked as excluded.");
                                        }
                                        serverMetadata.setExcluded(true);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Field(java.lang.reflect.Field) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) 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 3 with BasicCollectionMetadata

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

the class AdminEntityServiceImpl method updateSubCollectionEntity.

@Override
public PersistenceResponse updateSubCollectionEntity(EntityForm entityForm, ClassMetadata mainMetadata, Property field, Entity parentEntity, String collectionItemId, String alternateId, List<SectionCrumb> sectionCrumbs) throws ServiceException, ClassNotFoundException {
    List<Property> properties = getPropertiesFromEntityForm(entityForm);
    FieldMetadata md = field.getMetadata();
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withEntity(new Entity());
    if (md instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
        ppr.setCeilingEntityClassname(fmd.getCollectionCeilingEntity());
        ppr.getEntity().setType(new String[] { fmd.getCollectionCeilingEntity() });
        Property fp = new Property();
        fp.setName(ppr.getForeignKey().getManyToField());
        fp.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
        if (!properties.contains(fp)) {
            properties.add(fp);
        }
    } else if (md instanceof AdornedTargetCollectionMetadata) {
        String adornedTargetEntityClassname = ppr.getAdornedList().getAdornedTargetEntityClassname();
        ppr.setCeilingEntityClassname(adornedTargetEntityClassname);
        ppr.getEntity().setType(new String[] { adornedTargetEntityClassname });
        for (Property property : properties) {
            if (property.getName().equals(ppr.getAdornedList().getLinkedObjectPath() + "." + ppr.getAdornedList().getLinkedIdProperty())) {
                break;
            }
        }
        if (!StringUtils.isEmpty(alternateId)) {
            Property p = new Property();
            p.setName(BasicPersistenceModule.ALTERNATE_ID_PROPERTY);
            p.setValue(alternateId);
            if (!properties.contains(p)) {
                properties.add(p);
            }
        }
    } else if (md instanceof MapMetadata) {
        ppr.getEntity().setType(new String[] { entityForm.getEntityType() });
        Property p = new Property();
        p.setName("symbolicId");
        p.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
        properties.add(p);
    } else {
        throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was" + " not a collection field.", field.getName(), mainMetadata.getCeilingType()));
    }
    String sectionField = field.getName();
    if (sectionField.contains(".")) {
        sectionField = sectionField.substring(0, sectionField.lastIndexOf("."));
    }
    ppr.setSectionEntityField(sectionField);
    Property parentNameProp = parentEntity.getPMap().get(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
    if (parentNameProp != null) {
        ppr.setRequestingEntityName(parentNameProp.getValue());
    }
    Property p = new Property();
    p.setName(entityForm.getIdProperty());
    p.setValue(collectionItemId);
    if (!properties.contains(p)) {
        properties.add(p);
    }
    Property[] propArr = new Property[properties.size()];
    properties.toArray(propArr);
    ppr.getEntity().setProperties(propArr);
    return update(ppr);
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) 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) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) BLCSystemProperty(org.broadleafcommerce.common.util.BLCSystemProperty) Property(org.broadleafcommerce.openadmin.dto.Property) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 4 with BasicCollectionMetadata

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

the class AdminEntityServiceImpl method removeSubCollectionEntity.

@Override
public PersistenceResponse removeSubCollectionEntity(ClassMetadata mainMetadata, Property field, Entity parentEntity, String itemId, String alternateId, String priorKey, List<SectionCrumb> sectionCrumbs) throws ServiceException {
    List<Property> properties = new ArrayList<Property>();
    Property p;
    String parentId = getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName());
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(field.getMetadata(), sectionCrumbs).withEntity(new Entity());
    if (field.getMetadata() instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata fmd = (BasicCollectionMetadata) field.getMetadata();
        p = new Property();
        p.setName("id");
        p.setValue(itemId);
        properties.add(p);
        p = new Property();
        p.setName(ppr.getForeignKey().getManyToField());
        p.setValue(parentId);
        properties.add(p);
        ppr.getEntity().setType(new String[] { fmd.getCollectionCeilingEntity() });
    } else if (field.getMetadata() instanceof AdornedTargetCollectionMetadata) {
        AdornedTargetList adornedList = ppr.getAdornedList();
        p = new Property();
        p.setName(adornedList.getLinkedObjectPath() + "." + adornedList.getLinkedIdProperty());
        p.setValue(parentId);
        properties.add(p);
        p = new Property();
        p.setName(adornedList.getTargetObjectPath() + "." + adornedList.getTargetIdProperty());
        p.setValue(itemId);
        properties.add(p);
        if (!StringUtils.isEmpty(alternateId)) {
            p = new Property();
            p.setName(BasicPersistenceModule.ALTERNATE_ID_PROPERTY);
            p.setValue(alternateId);
            properties.add(p);
        }
        ppr.getEntity().setType(new String[] { adornedList.getAdornedTargetEntityClassname() });
    } else if (field.getMetadata() instanceof MapMetadata) {
        MapMetadata fmd = (MapMetadata) field.getMetadata();
        p = new Property();
        p.setName("id");
        p.setValue(itemId);
        properties.add(p);
        p = new Property();
        p.setName("symbolicId");
        p.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
        properties.add(p);
        p = new Property();
        p.setName("priorKey");
        p.setValue(priorKey);
        properties.add(p);
        MapStructure mapStructure = ppr.getMapStructure();
        p = new Property();
        p.setName(mapStructure.getKeyPropertyName());
        p.setValue(itemId);
        properties.add(p);
        ppr.getEntity().setType(new String[] { fmd.getTargetClass() });
    }
    ppr.setCeilingEntityClassname(ppr.getEntity().getType()[0]);
    String sectionField = field.getName();
    if (sectionField.contains(".")) {
        sectionField = sectionField.substring(0, sectionField.lastIndexOf("."));
    }
    ppr.setSectionEntityField(sectionField);
    Property parentNameProp = parentEntity.getPMap().get(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
    if (parentNameProp != null) {
        ppr.setRequestingEntityName(parentNameProp.getValue());
    }
    Property[] propArr = new Property[properties.size()];
    properties.toArray(propArr);
    ppr.getEntity().setProperties(propArr);
    return remove(ppr);
}
Also used : MapStructure(org.broadleafcommerce.openadmin.dto.MapStructure) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ArrayList(java.util.ArrayList) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) BLCSystemProperty(org.broadleafcommerce.common.util.BLCSystemProperty) Property(org.broadleafcommerce.openadmin.dto.Property) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 5 with BasicCollectionMetadata

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

the class AdminEntityServiceImpl method addSubCollectionEntity.

@Override
public PersistenceResponse addSubCollectionEntity(EntityForm entityForm, ClassMetadata mainMetadata, Property field, Entity parentEntity, List<SectionCrumb> sectionCrumbs) throws ServiceException, ClassNotFoundException {
    // Assemble the properties from the entity form
    List<Property> properties = getPropertiesFromEntityForm(entityForm);
    FieldMetadata md = field.getMetadata();
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withEntity(new Entity());
    ppr.getEntity().setIsPreAdd(parentEntity.isPreAdd());
    if (md instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
        ppr.getEntity().setType(new String[] { entityForm.getEntityType() });
        // that we're not changing the target entity at all and only creating the association to the id
        if (fmd.getAddMethodType().equals(AddMethodType.LOOKUP) || fmd.getAddMethodType().equals(AddMethodType.LOOKUP_FOR_UPDATE)) {
            List<String> fieldsToRemove = new ArrayList<String>();
            String idProp = getIdProperty(mainMetadata);
            for (String key : entityForm.getFields().keySet()) {
                if (!idProp.equals(key)) {
                    fieldsToRemove.add(key);
                }
            }
            for (String key : fieldsToRemove) {
                ListIterator<Property> li = properties.listIterator();
                while (li.hasNext()) {
                    if (li.next().getName().equals(key)) {
                        li.remove();
                    }
                }
            }
            ppr.setValidateUnsubmittedProperties(false);
        }
        if (fmd.getAddMethodType().equals(AddMethodType.LOOKUP_FOR_UPDATE)) {
            ppr.setUpdateLookupType(true);
        }
        Property fp = new Property();
        fp.setName(ppr.getForeignKey().getManyToField());
        fp.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
        properties.add(fp);
    } else if (md instanceof AdornedTargetCollectionMetadata) {
        ppr.getEntity().setType(new String[] { ppr.getAdornedList().getAdornedTargetEntityClassname() });
        String[] maintainedFields = ((AdornedTargetCollectionMetadata) md).getMaintainedAdornedTargetFields();
        if (maintainedFields == null || maintainedFields.length == 0) {
            ppr.setValidateUnsubmittedProperties(false);
        }
    } else if (md instanceof MapMetadata) {
        ppr.getEntity().setType(new String[] { entityForm.getEntityType() });
        Property p = new Property();
        p.setName("symbolicId");
        p.setValue(getContextSpecificRelationshipId(mainMetadata, parentEntity, field.getName()));
        properties.add(p);
    } else {
        throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was" + " not a collection field.", field.getName(), mainMetadata.getCeilingType()));
    }
    ppr.setCeilingEntityClassname(ppr.getEntity().getType()[0]);
    String sectionField = field.getName();
    if (sectionField.contains(".")) {
        sectionField = sectionField.substring(0, sectionField.lastIndexOf("."));
    }
    ppr.setSectionEntityField(sectionField);
    Property parentNameProp = parentEntity.getPMap().get(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
    if (parentNameProp != null) {
        ppr.setRequestingEntityName(parentNameProp.getValue());
    }
    Property[] propArr = new Property[properties.size()];
    properties.toArray(propArr);
    ppr.getEntity().setProperties(propArr);
    return add(ppr);
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) BLCSystemProperty(org.broadleafcommerce.common.util.BLCSystemProperty) Property(org.broadleafcommerce.openadmin.dto.Property) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

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