use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class SkuCustomPersistenceHandler method associateProductOptionValuesToSku.
/**
* This initially removes all of the product option values that are currently related to the Sku and then re-associates
* the {@link ProductOptionValue}s
* @param entity
* @param adminInstance
*/
protected void associateProductOptionValuesToSku(Entity entity, Sku adminInstance, DynamicEntityDao dynamicEntityDao) {
// Get the list of product option value ids that were selected from the form
List<Long> productOptionValueIds = new ArrayList<>();
for (Property property : getProductOptionProperties(entity)) {
Long propId = Long.parseLong(property.getValue());
productOptionValueIds.add(propId);
property.setIsDirty(true);
}
// Only process associations if product option value changes came in via the form
if (CollectionUtils.isNotEmpty(productOptionValueIds)) {
// remove the current list of product option values from the Sku
if (adminInstance.getProductOptionValueXrefs().size() > 0) {
Iterator<SkuProductOptionValueXref> iterator = adminInstance.getProductOptionValueXrefs().iterator();
while (iterator.hasNext()) {
dynamicEntityDao.remove(iterator.next());
}
dynamicEntityDao.merge(adminInstance);
}
// Associate the product option values from the form with the Sku
for (Long id : productOptionValueIds) {
// Simply find the changed ProductOptionValues directly - seems to work better with sandboxing code
ProductOptionValue pov = (ProductOptionValue) dynamicEntityDao.find(ProductOptionValueImpl.class, id);
SkuProductOptionValueXref xref = new SkuProductOptionValueXrefImpl(adminInstance, pov);
xref = dynamicEntityDao.merge(xref);
adminInstance.getProductOptionValueXrefs().add(xref);
}
}
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class PageTemplateCustomPersistenceHandler method inspect.
@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
try {
List<FieldGroup> fieldGroups = getFieldGroups(persistencePackage, dynamicEntityDao);
ClassMetadata metadata = new ClassMetadata();
metadata.setCeilingType(PageTemplate.class.getName());
ClassTree entities = new ClassTree(PageTemplateImpl.class.getName());
metadata.setPolymorphicEntities(entities);
Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(fieldGroups, PageTemplate.class);
metadata.setProperties(properties);
DynamicResultSet results = new DynamicResultSet(metadata);
return results;
} catch (Exception e) {
throw new ServiceException("Unable to perform inspect for entity: " + ceilingEntityFullyQualifiedClassname, e);
}
}
use of org.broadleafcommerce.openadmin.dto.Property 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.Property 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.Property 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);
}
}
Aggregations