use of org.broadleafcommerce.cms.page.domain.PageFieldImpl in project BroadleafCommerce by BroadleafCommerce.
the class PageDaoImpl method readPageFieldsByPageId.
@Override
public List<PageField> readPageFieldsByPageId(Long pageId) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<PageField> criteria = builder.createQuery(PageField.class);
Root<PageFieldImpl> pageField = criteria.from(PageFieldImpl.class);
criteria.select(pageField);
Path<Object> path = pageField.get("page").get("id");
criteria.where(builder.equal(pageField.get("page").get("id"), pageId));
TypedQuery<PageField> query = em.createQuery(criteria);
query.setHint(QueryHints.HINT_CACHEABLE, true);
return query.getResultList();
}
use of org.broadleafcommerce.cms.page.domain.PageFieldImpl 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);
}
}
Aggregations