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;
}
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;
}
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);
}
}
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);
}
}
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"));
}
Aggregations