use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method update.
@Override
public Entity update(PersistencePackage persistencePackage) throws ServiceException {
String[] customCriteria = persistencePackage.getCustomCriteria();
if (customCriteria != null && customCriteria.length > 0) {
LOG.warn("custom persistence handlers and custom criteria not supported for update types other than BASIC");
}
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Entity entity = persistencePackage.getEntity();
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
if (!adornedTargetList.getMutable()) {
throw new SecurityServiceException("Field is not mutable");
}
try {
AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, entity, adornedTargetList).invokeForUpdate();
List<Serializable> records = adornedTargetRetrieval.getRecords();
Assert.isTrue(!CollectionUtils.isEmpty(records), "Entity not found");
int index = adornedTargetRetrieval.getIndex();
Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
FieldManager fieldManager = getFieldManager();
Serializable myRecord;
if (adornedTargetList.getSortField() != null && entity.findProperty(adornedTargetList.getSortField()).getValue() != null) {
myRecord = records.get(index);
BigDecimal requestedSequence = new BigDecimal(entity.findProperty(adornedTargetList.getSortField()).getValue());
BigDecimal previousSequence = new BigDecimal(String.valueOf(getFieldManager().getFieldValue(myRecord, adornedTargetList.getSortField())));
if (!previousSequence.equals(requestedSequence)) {
// Sequence has changed. Rebalance the list
myRecord = records.remove(index);
myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
if (CollectionUtils.isEmpty(records)) {
records.add(myRecord);
} else {
records.add(requestedSequence.intValue() - 1, myRecord);
}
index = 1;
Class<?> type = fieldManager.getField(myRecord.getClass(), adornedTargetList.getSortField()).getType();
boolean isBigDecimal = BigDecimal.class.isAssignableFrom(type);
for (Serializable record : records) {
fieldManager.setFieldValue(record, adornedTargetList.getSortField(), isBigDecimal ? new BigDecimal(index) : Long.valueOf(index));
index++;
}
}
} else {
myRecord = records.get(index);
}
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
myRecord = persistenceManager.getDynamicEntityDao().merge(myRecord);
List<Serializable> myList = new ArrayList<Serializable>();
myList.add(myRecord);
Entity[] payload = getRecords(mergedPropertiesTarget, myList, mergedProperties, adornedTargetList.getTargetObjectPath(), null);
entity = payload[0];
return entity;
} catch (Exception e) {
throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method fetch.
@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
Entity[] payload;
int totalRecords;
try {
Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, adornedTargetList, cto).invokeForFetch();
List<Serializable> records = adornedTargetRetrieval.getRecords();
Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
payload = getRecords(mergedPropertiesTarget, records, mergedProperties, adornedTargetList.getTargetObjectPath(), persistencePackage.getCustomCriteria());
totalRecords = getTotalRecords(adornedTargetList.getAdornedTargetEntityClassname(), adornedTargetRetrieval.getFilterMappings());
} catch (Exception e) {
throw new ServiceException("Unable to fetch results for " + adornedTargetList.getAdornedTargetEntityClassname(), e);
}
DynamicResultSet results = new DynamicResultSet(null, payload, totalRecords);
return results;
}
use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.
the class ChildCategoriesCustomPersistenceHandler method getChildAndParentCategories.
protected Tuple<Category, Category> getChildAndParentCategories(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao) {
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePackage.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
final String targetPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
final String linkedPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
Long parentId = Long.parseLong(persistencePackage.getEntity().findProperty(linkedPath).getValue());
Long childId = Long.parseLong(persistencePackage.getEntity().findProperty(targetPath).getValue());
Category parent = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, parentId);
Category child = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, childId);
return new Tuple<>(parent, child);
}
use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method updateCollectionItemSequence.
/**
* Updates the given collection item's sequence. This should only be triggered for adorned target collections
* where a sort field is specified -- any other invocation is incorrect and will result in an exception.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @param collectionField
* @param collectionItemId
* @return an object explaining the state of the operation
* @throws Exception
*/
@RequestMapping(value = "/{id}/{collectionField:.*}/{collectionItemId}/{alternateId}/sequence", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> updateCollectionItemSequence(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "collectionItemId") String collectionItemId, @RequestParam(value = "newSequence") String newSequence, @PathVariable(value = "alternateId") String alternateId) throws Exception {
String sectionKey = getSectionKey(pathVars);
String mainClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
FieldMetadata md = collectionProperty.getMetadata();
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
ppr.addCustomCriteria("reorderParentEntityFetch");
Entity parentEntity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
if (md instanceof AdornedTargetCollectionMetadata) {
AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
AdornedTargetList atl = ppr.getAdornedList();
// Get an entity form for the entity
EntityForm entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, sectionCrumbs, false);
Entity entity = service.getAdvancedCollectionRecord(mainMetadata, parentEntity, collectionProperty, collectionItemId, sectionCrumbs, alternateId, new String[] { "reorderChildEntityFetch" }).getDynamicResultSet().getRecords()[0];
formService.populateEntityFormFields(entityForm, entity);
formService.populateAdornedEntityFormFields(entityForm, entity, ppr.getAdornedList());
// Set the new sequence (note that it will come in 0-indexed but the persistence module expects 1-indexed)
int sequenceValue = Integer.parseInt(newSequence) + 1;
Field field = entityForm.findField(atl.getSortField());
field.setValue(String.valueOf(sequenceValue));
Map<String, Object> responseMap = new HashMap<>();
PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, alternateId, sectionCrumbs);
Property displayOrder = persistenceResponse.getEntity().findProperty(atl.getSortField());
responseMap.put("status", "ok");
responseMap.put("field", collectionField);
responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
return responseMap;
} else if (md instanceof BasicCollectionMetadata) {
BasicCollectionMetadata cd = (BasicCollectionMetadata) md;
Map<String, Object> responseMap = new HashMap<>();
Entity entity = service.getRecord(ppr, collectionItemId, mainMetadata, false).getDynamicResultSet().getRecords()[0];
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
EntityForm entityForm = formService.createEntityForm(collectionMetadata, sectionCrumbs);
if (!StringUtils.isEmpty(cd.getSortProperty())) {
Field f = new Field().withName(cd.getSortProperty()).withFieldType(SupportedFieldType.HIDDEN.toString());
entityForm.addHiddenField(mainMetadata, f);
}
formService.populateEntityFormFields(entityForm, entity);
if (!StringUtils.isEmpty(cd.getSortProperty())) {
int sequenceValue = Integer.parseInt(newSequence) + 1;
Field field = entityForm.findField(cd.getSortProperty());
field.setValue(String.valueOf(sequenceValue));
}
PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, sectionCrumbs);
Property displayOrder = persistenceResponse.getEntity().findProperty(cd.getSortProperty());
responseMap.put("status", "ok");
responseMap.put("field", collectionField);
responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
return responseMap;
} else {
throw new UnsupportedOperationException("Cannot handle sequencing for non adorned target collection fields.");
}
}
Aggregations