Search in sources :

Example 16 with Entity

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

the class PageTemplateCustomPersistenceHandler method fetchDynamicEntity.

@Override
public Entity fetchDynamicEntity(Serializable root, List<String> dirtyFields, boolean includeId) throws Exception {
    Page page = (Page) root;
    Map<String, PageField> pageFieldMap = page.getPageFields();
    Entity entity = new Entity();
    entity.setType(new String[] { PageTemplateImpl.class.getName() });
    List<Property> propertiesList = new ArrayList<Property>();
    List<FieldGroup> fieldGroups = getFieldGroups(page, null);
    processFieldGroups(dirtyFields, pageFieldMap, propertiesList, fieldGroups);
    processIncludeId(includeId, page, propertiesList);
    entity.setProperties(propertiesList.toArray(new Property[] {}));
    return entity;
}
Also used : PageField(org.broadleafcommerce.cms.page.domain.PageField) Entity(org.broadleafcommerce.openadmin.dto.Entity) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) ArrayList(java.util.ArrayList) PageTemplateImpl(org.broadleafcommerce.cms.page.domain.PageTemplateImpl) Page(org.broadleafcommerce.cms.page.domain.Page) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 17 with Entity

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

the class StructuredContentTypeCustomPersistenceHandler method fetchDynamicEntity.

@Override
public Entity fetchDynamicEntity(Serializable root, List<String> dirtyFields, boolean includeId) throws Exception {
    StructuredContent structuredContent = (StructuredContent) root;
    Map<String, StructuredContentFieldXref> structuredContentFieldMap = structuredContent.getStructuredContentFieldXrefs();
    Entity entity = new Entity();
    entity.setType(new String[] { StructuredContentType.class.getName() });
    List<Property> propertiesList = new ArrayList<Property>();
    for (FieldGroup fieldGroup : structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups()) {
        for (FieldDefinition def : fieldGroup.getFieldDefinitions()) {
            Property property = new Property();
            property.setName(def.getName());
            String value = null;
            if (!MapUtils.isEmpty(structuredContentFieldMap)) {
                StructuredContentFieldXref structuredContentFieldXref = structuredContentFieldMap.get(def.getName());
                if (structuredContentFieldXref != null) {
                    StructuredContentField structuredContentField = structuredContentFieldXref.getStructuredContentField();
                    if (structuredContentField != null) {
                        value = structuredContentField.getValue();
                    }
                }
            }
            property.setValue(value);
            if (!CollectionUtils.isEmpty(dirtyFields) && dirtyFields.contains(property.getName())) {
                property.setIsDirty(true);
            }
            propertiesList.add(property);
        }
    }
    if (includeId) {
        Property property = new Property();
        propertiesList.add(property);
        property.setName("id");
        property.setValue(String.valueOf(structuredContent.getId()));
    }
    entity.setProperties(propertiesList.toArray(new Property[] {}));
    return entity;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref) Property(org.broadleafcommerce.openadmin.dto.Property) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent)

Example 18 with Entity

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

the class StructuredContentTypeCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        String structuredContentId = persistencePackage.getCustomCriteria()[1];
        Entity entity = fetchEntityBasedOnId(structuredContentId, null);
        DynamicResultSet results = new DynamicResultSet(new Entity[] { entity }, 1);
        return results;
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) ServiceException(org.broadleafcommerce.common.exception.ServiceException) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException)

Example 19 with Entity

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

the class StructuredContentTypeCustomPersistenceHandler method addOrUpdate.

protected Entity addOrUpdate(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        String structuredContentId = persistencePackage.getCustomCriteria()[1];
        StructuredContent structuredContent = structuredContentService.findStructuredContentById(Long.valueOf(structuredContentId));
        Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups(), StructuredContentType.class);
        Map<String, FieldMetadata> md = new HashMap<String, FieldMetadata>();
        for (Property property : properties) {
            md.put(property.getName(), property.getMetadata());
        }
        boolean validated = helper.validate(persistencePackage.getEntity(), null, md);
        if (!validated) {
            throw new ValidationException(persistencePackage.getEntity(), "Structured Content dynamic fields failed validation");
        }
        List<String> templateFieldNames = new ArrayList<String>(20);
        for (FieldGroup group : structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups()) {
            for (FieldDefinition def : group.getFieldDefinitions()) {
                templateFieldNames.add(def.getName());
            }
        }
        Map<String, String> dirtyFieldsOrigVals = new HashMap<String, String>();
        List<String> dirtyFields = new ArrayList<String>();
        Map<String, StructuredContentFieldXref> structuredContentFieldMap = structuredContent.getStructuredContentFieldXrefs();
        for (Property property : persistencePackage.getEntity().getProperties()) {
            if (property.getEnabled() && templateFieldNames.contains(property.getName())) {
                StructuredContentFieldXref scXref = structuredContentFieldMap.get(property.getName());
                if (scXref != null && scXref.getStructuredContentField() != null) {
                    StructuredContentField structuredContentField = scXref.getStructuredContentField();
                    boolean isDirty = (structuredContentField.getValue() == null && property.getValue() != null) || (structuredContentField.getValue() != null && property.getValue() == null);
                    if (isDirty || (structuredContentField.getValue() != null && property.getValue() != null && !structuredContentField.getValue().trim().equals(property.getValue().trim()))) {
                        dirtyFields.add(property.getName());
                        dirtyFieldsOrigVals.put(property.getName(), structuredContentField.getValue());
                        structuredContentField.setValue(property.getValue());
                        scXref = dynamicEntityDao.merge(scXref);
                    }
                } else {
                    StructuredContentField structuredContentField = new StructuredContentFieldImpl();
                    structuredContentField.setFieldKey(property.getName());
                    structuredContentField.setValue(property.getValue());
                    StructuredContentFieldXref scfx = new StructuredContentFieldXrefImpl();
                    scfx.setStructuredContent(structuredContent);
                    scfx.setKey(property.getName());
                    scfx.setStrucuturedContentField(structuredContentField);
                    scfx = dynamicEntityDao.persist(scfx);
                    dirtyFields.add(property.getName());
                }
            }
        }
        List<String> removeItems = new ArrayList<String>();
        for (String key : structuredContentFieldMap.keySet()) {
            if (persistencePackage.getEntity().findProperty(key) == null) {
                removeItems.add(key);
            }
        }
        if (removeItems.size() > 0) {
            for (String removeKey : removeItems) {
                structuredContentFieldMap.remove(removeKey);
            }
        }
        Collections.sort(dirtyFields);
        Entity entity = fetchEntityBasedOnId(structuredContentId, dirtyFields);
        for (Entry<String, String> entry : dirtyFieldsOrigVals.entrySet()) {
            entity.getPMap().get(entry.getKey()).setOriginalValue(entry.getValue());
            entity.getPMap().get(entry.getKey()).setOriginalDisplayValue(entry.getValue());
        }
        return entity;
    } catch (ValidationException e) {
        throw e;
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) StructuredContentFieldXrefImpl(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXrefImpl) HashMap(java.util.HashMap) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent) ServiceException(org.broadleafcommerce.common.exception.ServiceException) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref) Property(org.broadleafcommerce.openadmin.dto.Property) StructuredContentFieldImpl(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldImpl)

Example 20 with Entity

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

the class MVELToDataWrapperTranslatorTest method testCreateRuleData.

/**
 * Tests the creation of a DataWrapper given an mvel/quantity property
 * @throws MVELTranslationException
 */
public void testCreateRuleData() throws MVELTranslationException {
    MVELToDataWrapperTranslator translator = new MVELToDataWrapperTranslator();
    Property[] properties = new Property[3];
    Property mvelProperty = new Property();
    mvelProperty.setName("orderItemMatchRule");
    mvelProperty.setValue("MVEL.eval(\"toUpperCase()\",discreteOrderItem.?category.?name)==MVEL.eval(\"toUpperCase()\",\"merchandise\")");
    Property quantityProperty = new Property();
    quantityProperty.setName("quantity");
    quantityProperty.setValue("1");
    Property idProperty = new Property();
    idProperty.setName("id");
    idProperty.setValue("100");
    properties[0] = mvelProperty;
    properties[1] = quantityProperty;
    properties[2] = idProperty;
    Entity[] entities = new Entity[1];
    Entity entity = new Entity();
    entity.setProperties(properties);
    entities[0] = entity;
    DataWrapper dataWrapper = translator.createRuleData(entities, "orderItemMatchRule", "quantity", "id", orderItemFieldService);
    assert (dataWrapper.getData().size() == 1);
    assert (dataWrapper.getData().get(0).getQuantity() == 1);
    assert (dataWrapper.getData().get(0).getRules().size() == 1);
    assert (dataWrapper.getData().get(0).getRules().get(0) instanceof ExpressionDTO);
    ExpressionDTO exp = (ExpressionDTO) dataWrapper.getData().get(0).getRules().get(0);
    assert (exp.getId().equals("category.name"));
    assert (exp.getOperator().equals(BLCOperator.IEQUALS.name()));
    assert (exp.getValue().equals("merchandise"));
}
Also used : DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) Entity(org.broadleafcommerce.openadmin.dto.Entity) MVELToDataWrapperTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.MVELToDataWrapperTranslator) Property(org.broadleafcommerce.openadmin.dto.Property) ExpressionDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)

Aggregations

Entity (org.broadleafcommerce.openadmin.dto.Entity)112 Property (org.broadleafcommerce.openadmin.dto.Property)62 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)48 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)36 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)34 ArrayList (java.util.ArrayList)25 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)24 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)24 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)20 Serializable (java.io.Serializable)19 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)19 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)17 Map (java.util.Map)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)16 HashMap (java.util.HashMap)15 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14