use of org.broadleafcommerce.cms.field.domain.FieldGroup in project BroadleafCommerce by BroadleafCommerce.
the class PageTemplateCustomPersistenceHandler method addOrUpdate.
protected Entity addOrUpdate(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
try {
String pageId = persistencePackage.getCustomCriteria()[1];
if (StringUtils.isBlank(pageId)) {
return persistencePackage.getEntity();
}
Page page = pageService.findPageById(Long.valueOf(pageId));
Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(getFieldGroups(page, null), PageTemplate.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(), "Page dynamic fields failed validation");
}
List<String> templateFieldNames = new ArrayList<String>(20);
for (FieldGroup group : getFieldGroups(page, null)) {
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, PageField> pageFieldMap = page.getPageFields();
for (Property property : persistencePackage.getEntity().getProperties()) {
if (property.getEnabled() && templateFieldNames.contains(property.getName())) {
PageField pageField = pageFieldMap.get(property.getName());
if (pageField != null) {
boolean isDirty = (pageField.getValue() == null && property.getValue() != null) || (pageField.getValue() != null && property.getValue() == null);
if (isDirty || (pageField.getValue() != null && property.getValue() != null && !pageField.getValue().trim().equals(property.getValue().trim()))) {
dirtyFields.add(property.getName());
dirtyFieldsOrigVals.put(property.getName(), pageField.getValue());
pageField.setValue(property.getValue());
pageField = dynamicEntityDao.merge(pageField);
}
} else {
pageField = new PageFieldImpl();
pageField.setFieldKey(property.getName());
pageField.setValue(property.getValue());
pageField.setPage(page);
dynamicEntityDao.persist(pageField);
dirtyFields.add(property.getName());
}
}
}
List<String> removeItems = new ArrayList<String>();
for (String key : pageFieldMap.keySet()) {
if (persistencePackage.getEntity().findProperty(key) == null) {
removeItems.add(key);
}
}
if (removeItems.size() > 0) {
for (String removeKey : removeItems) {
pageFieldMap.remove(removeKey);
}
}
Collections.sort(dirtyFields);
Entity entity = fetchEntityBasedOnId(pageId, dirtyFields);
for (Map.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 update for entity: " + ceilingEntityFullyQualifiedClassname, e);
}
}
use of org.broadleafcommerce.cms.field.domain.FieldGroup in project BroadleafCommerce by BroadleafCommerce.
the class PageTemplateCustomPersistenceHandler method getFieldGroups.
protected List<FieldGroup> getFieldGroups(Page page, PageTemplate template) {
List<PageTemplateFieldGroupXref> fieldGroupXrefs = null;
List<FieldGroup> fieldGroups = new ArrayList<FieldGroup>();
if (template != null) {
fieldGroupXrefs = template.getFieldGroupXrefs();
}
if (page.getPageTemplate() != null) {
fieldGroupXrefs = page.getPageTemplate().getFieldGroupXrefs();
}
if (fieldGroupXrefs != null) {
for (PageTemplateFieldGroupXref xref : fieldGroupXrefs) {
fieldGroups.add(xref.getFieldGroup());
}
}
return fieldGroups;
}
use of org.broadleafcommerce.cms.field.domain.FieldGroup in project BroadleafCommerce by BroadleafCommerce.
the class PageTemplateCustomPersistenceHandler method processFieldGroups.
protected void processFieldGroups(List<String> dirtyFields, Map<String, PageField> pageFieldMap, List<Property> propertiesList, List<FieldGroup> fieldGroups) {
for (FieldGroup fieldGroup : fieldGroups) {
for (FieldDefinition def : fieldGroup.getFieldDefinitions()) {
Property property = new Property();
propertiesList.add(property);
property.setName(def.getName());
String value = null;
if (!MapUtils.isEmpty(pageFieldMap)) {
PageField pageField = pageFieldMap.get(def.getName());
if (pageField == null) {
value = "";
} else {
value = pageField.getValue();
}
}
property.setValue(value);
if (!CollectionUtils.isEmpty(dirtyFields) && dirtyFields.contains(property.getName())) {
property.setIsDirty(true);
}
if (StringUtils.isNotBlank(def.getAdditionalForeignKeyClass())) {
property.getMetadata().setOwningClass(def.getAdditionalForeignKeyClass());
}
}
}
}
use of org.broadleafcommerce.cms.field.domain.FieldGroup in project BroadleafCommerce by BroadleafCommerce.
the class DynamicFieldPersistenceHandlerHelper method buildDynamicPropertyList.
/**
* Builds all of the metadata for all of the dynamic properties within a {@link StructuredContentType}, gleaned from
* the {@link FieldGroup}s and {@link FieldDefinition}s.
*
* @param fieldGroups groups that the {@link Property}s are built from
* @param inheritedType the value that each built {@link FieldMetadata} for each property will use to notate where the
* dynamic field actually came from (meaning {@link FieldMetadata#setAvailableToTypes(String[])} and {@link FieldMetadata#setInheritedFromType(String)}
* @return
*/
public Property[] buildDynamicPropertyList(List<FieldGroup> fieldGroups, Class<?> inheritedType) {
List<Property> propertiesList = new ArrayList<Property>();
int groupCount = 1;
int fieldCount = 0;
for (FieldGroup group : fieldGroups) {
List<FieldDefinition> definitions = group.getFieldDefinitions();
for (FieldDefinition def : definitions) {
Property property = buildDynamicProperty(def, inheritedType);
BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) property.getMetadata();
fieldMetadata.setOrder(fieldCount++);
fieldMetadata.setGroup(group.getName());
fieldMetadata.setGroupOrder(groupCount);
fieldMetadata.setGroupCollapsed(group.getInitCollapsedFlag());
propertiesList.add(property);
}
groupCount++;
fieldCount = 0;
}
Property property = new Property();
property.setName("id");
BasicFieldMetadata fieldMetadata = new BasicFieldMetadata();
property.setMetadata(fieldMetadata);
fieldMetadata.setFieldType(SupportedFieldType.ID);
fieldMetadata.setSecondaryType(SupportedFieldType.INTEGER);
fieldMetadata.setMutable(true);
fieldMetadata.setInheritedFromType(inheritedType.getName());
fieldMetadata.setAvailableToTypes(new String[] { inheritedType.getName() });
fieldMetadata.setForeignKeyCollection(false);
fieldMetadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
fieldMetadata.setName("id");
fieldMetadata.setFriendlyName("ID");
fieldMetadata.setSecurityLevel("");
fieldMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL);
fieldMetadata.setExplicitFieldType(SupportedFieldType.UNKNOWN);
fieldMetadata.setLargeEntry(false);
fieldMetadata.setProminent(false);
fieldMetadata.setColumnWidth("*");
fieldMetadata.setBroadleafEnumeration("");
fieldMetadata.setReadOnly(true);
propertiesList.add(property);
Property[] properties = new Property[propertiesList.size()];
properties = propertiesList.toArray(properties);
Arrays.sort(properties, new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
/*
* First, compare properties based on order fields
*/
if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() != null) {
return o1.getMetadata().getOrder().compareTo(o2.getMetadata().getOrder());
} else if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() == null) {
/*
* Always favor fields that have an order identified
*/
return -1;
} else if (o1.getMetadata().getOrder() == null && o2.getMetadata().getOrder() != null) {
/*
* Always favor fields that have an order identified
*/
return 1;
} else if (o1.getMetadata().getFriendlyName() != null && o2.getMetadata().getFriendlyName() != null) {
return o1.getMetadata().getFriendlyName().compareTo(o2.getMetadata().getFriendlyName());
} else {
return o1.getName().compareTo(o2.getName());
}
}
});
return properties;
}
use of org.broadleafcommerce.cms.field.domain.FieldGroup in project BroadleafCommerce by BroadleafCommerce.
the class PageTemplateImpl method setFieldGroups.
@Override
@Deprecated
public void setFieldGroups(List<FieldGroup> fieldGroups) {
this.legacyFieldGroups.clear();
this.fieldGroups.clear();
for (FieldGroup group : fieldGroups) {
this.fieldGroups.add(new PageTemplateFieldGroupXrefImpl(this, group));
}
}
Aggregations